Removed new Start of Frame event from the library; performance suffered far too much...
[pub/lufa.git] / Projects / Magstripe / Magstripe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
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.
21
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
29 this software.
30 */
31
32 #include "Magstripe.h"
33
34 BitBuffer_t TrackDataBuffers[3];
35
36 USB_ClassInfo_HID_t Keyboard_HID_Interface =
37 {
38 .InterfaceNumber = 0,
39
40 .ReportINEndpointNumber = KEYBOARD_EPNUM,
41 .ReportINEndpointSize = KEYBOARD_EPSIZE,
42
43 .ReportINBufferSize = sizeof(USB_KeyboardReport_Data_t),
44 };
45
46 int main(void)
47 {
48 SetupHardware();
49
50 for (uint8_t Buffer = 0; Buffer < 3; Buffer++)
51 BitBuffer_Init(&TrackDataBuffers[Buffer]);
52
53 for (;;)
54 {
55 if (Magstripe_GetStatus() & MAG_CARDPRESENT)
56 ReadMagstripeData();
57
58 USB_HID_USBTask(&Keyboard_HID_Interface);
59 USB_USBTask();
60 }
61 }
62
63 void SetupHardware(void)
64 {
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR &= ~(1 << WDRF);
67 wdt_disable();
68
69 /* Disable clock division */
70 clock_prescale_set(clock_div_1);
71
72 /* Hardware Initialization */
73 Magstripe_Init();
74 USB_Init();
75
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);
81 }
82
83 void ReadMagstripeData(void)
84 {
85 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
86 const struct
87 {
88 uint8_t ClockMask;
89 uint8_t DataMask;
90 } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
91 {MAG_T2_CLOCK, MAG_T2_DATA},
92 {MAG_T3_CLOCK, MAG_T3_DATA}};
93
94 uint8_t Magstripe_Prev = 0;
95 uint8_t Magstripe_LCL = Magstripe_GetStatus();
96
97 while (Magstripe_LCL & MAG_CARDPRESENT)
98 {
99 for (uint8_t Track = 0; Track < 3; Track++)
100 {
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);
104
105 if (ClockPinLevel && ClockLevelChanged)
106 BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
107 }
108
109 Magstripe_Prev = Magstripe_LCL;
110 Magstripe_LCL = Magstripe_GetStatus();
111 }
112 }
113
114 void EVENT_USB_ConfigurationChanged(void)
115 {
116 USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface);
117 }
118
119 void EVENT_USB_UnhandledControlPacket(void)
120 {
121 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface);
122 }
123
124 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
125 {
126 if (Keyboard_HID_Interface.IdleMSRemaining)
127 Keyboard_HID_Interface.IdleMSRemaining--;
128 }
129
130 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData)
131 {
132 static bool IsKeyReleaseReport;
133 static bool IsNewlineReport;
134
135 BitBuffer_t* Buffer = NULL;
136 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
137
138 /* Key reports must be interleaved with 0 Key Code reports to release the keys, or repeated keys will be ignored */
139 IsKeyReleaseReport = !IsKeyReleaseReport;
140
141 if (IsKeyReleaseReport)
142 {
143 KeyboardReport->KeyCode = 0;
144 }
145 else if (IsNewlineReport)
146 {
147 IsNewlineReport = false;
148 KeyboardReport->KeyCode = KEY_ENTER;
149 }
150 else
151 {
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];
158 else
159 return 0;
160
161 KeyboardReport->KeyCode = BitBuffer_GetNextBit(Buffer) ? KEY_1 : KEY_0;
162
163 /* If buffer now empty, next report must be a newline to seperate track data */
164 if (!(Buffer->Elements))
165 IsNewlineReport = true;
166 }
167
168 return sizeof(USB_KeyboardReport_Data_t);
169 }
170
171 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize)
172 {
173 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
174 }