3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is hereby
15 granted, provided that the above copyright notice appear in all
16 copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
32 #include "Magstripe.h"
34 BitBuffer_t TrackDataBuffers
[3];
36 USB_ClassInfo_HID_t Keyboard_HID_Interface
=
40 .ReportINEndpointNumber
= KEYBOARD_EPNUM
,
41 .ReportINEndpointSize
= KEYBOARD_EPSIZE
,
43 .ReportINBufferSize
= sizeof(USB_KeyboardReport_Data_t
),
50 for (uint8_t Buffer
= 0; Buffer
< 3; Buffer
++)
51 BitBuffer_Init(&TrackDataBuffers
[Buffer
]);
55 if (Magstripe_GetStatus() & MAG_CARDPRESENT
)
58 USB_HID_USBTask(&Keyboard_HID_Interface
);
63 void SetupHardware(void)
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR
&= ~(1 << WDRF
);
69 /* Disable clock division */
70 clock_prescale_set(clock_div_1
);
72 /* Hardware Initialization */
76 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
77 OCR0A
= ((F_CPU
/ 64) / 1000);
78 TCCR0A
= (1 << WGM01
);
79 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
80 TIMSK0
= (1 << OCIE0A
);
83 void ReadMagstripeData(void)
85 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
90 } TrackInfo
[] = {{MAG_T1_CLOCK
, MAG_T1_DATA
},
91 {MAG_T2_CLOCK
, MAG_T2_DATA
},
92 {MAG_T3_CLOCK
, MAG_T3_DATA
}};
94 uint8_t Magstripe_Prev
= 0;
95 uint8_t Magstripe_LCL
= Magstripe_GetStatus();
97 while (Magstripe_LCL
& MAG_CARDPRESENT
)
99 for (uint8_t Track
= 0; Track
< 3; Track
++)
101 bool DataPinLevel
= ((Magstripe_LCL
& TrackInfo
[Track
].DataMask
) != 0);
102 bool ClockPinLevel
= ((Magstripe_LCL
& TrackInfo
[Track
].ClockMask
) != 0);
103 bool ClockLevelChanged
= (((Magstripe_LCL
^ Magstripe_Prev
) & TrackInfo
[Track
].ClockMask
) != 0);
105 if (ClockPinLevel
&& ClockLevelChanged
)
106 BitBuffer_StoreNextBit(&TrackDataBuffers
[Track
], DataPinLevel
);
109 Magstripe_Prev
= Magstripe_LCL
;
110 Magstripe_LCL
= Magstripe_GetStatus();
114 void EVENT_USB_ConfigurationChanged(void)
116 USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface
);
119 void EVENT_USB_UnhandledControlPacket(void)
121 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface
);
124 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
126 if (Keyboard_HID_Interface
.IdleMSRemaining
)
127 Keyboard_HID_Interface
.IdleMSRemaining
--;
130 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
)
132 static bool IsKeyReleaseReport
;
133 static bool IsNewlineReport
;
135 BitBuffer_t
* Buffer
= NULL
;
136 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
138 /* Key reports must be interleaved with 0 Key Code reports to release the keys, or repeated keys will be ignored */
139 IsKeyReleaseReport
= !IsKeyReleaseReport
;
141 if (IsKeyReleaseReport
)
143 KeyboardReport
->KeyCode
= 0;
145 else if (IsNewlineReport
)
147 IsNewlineReport
= false;
148 KeyboardReport
->KeyCode
= KEY_ENTER
;
152 if (TrackDataBuffers
[0].Elements
)
153 Buffer
= &TrackDataBuffers
[0];
154 else if (TrackDataBuffers
[1].Elements
)
155 Buffer
= &TrackDataBuffers
[1];
156 else if (TrackDataBuffers
[2].Elements
)
157 Buffer
= &TrackDataBuffers
[2];
161 KeyboardReport
->KeyCode
= BitBuffer_GetNextBit(Buffer
) ? KEY_1
: KEY_0
;
163 /* If buffer now empty, next report must be a newline to seperate track data */
164 if (!(Buffer
->Elements
))
165 IsNewlineReport
= true;
168 return sizeof(USB_KeyboardReport_Data_t
);
171 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
, uint16_t ReportSize
)
173 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports