Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[pub/USBasp.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
44 int main(void)
45 {
46 SetupHardware();
47
48 for (uint8_t Buffer = 0; Buffer < 3; Buffer++)
49 BitBuffer_Init(&TrackDataBuffers[Buffer]);
50
51 for (;;)
52 {
53 if (Magstripe_GetStatus() & MAG_CARDPRESENT)
54 ReadMagstripeData();
55
56 USB_HID_USBTask(&Keyboard_HID_Interface);
57 USB_USBTask();
58 }
59 }
60
61 void SetupHardware(void)
62 {
63 /* Disable watchdog if enabled by bootloader/fuses */
64 MCUSR &= ~(1 << WDRF);
65 wdt_disable();
66
67 /* Disable clock division */
68 clock_prescale_set(clock_div_1);
69
70 /* Hardware Initialization */
71 Magstripe_Init();
72 USB_Init();
73 }
74
75 void ReadMagstripeData(void)
76 {
77 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
78 const struct
79 {
80 uint8_t ClockMask;
81 uint8_t DataMask;
82 } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
83 {MAG_T2_CLOCK, MAG_T2_DATA},
84 {MAG_T3_CLOCK, MAG_T3_DATA}};
85
86 uint8_t Magstripe_Prev = 0;
87 uint8_t Magstripe_LCL = Magstripe_GetStatus();
88
89 while (Magstripe_LCL & MAG_CARDPRESENT)
90 {
91 for (uint8_t Track = 0; Track < 3; Track++)
92 {
93 bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
94 bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
95 bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
96
97 if (ClockPinLevel && ClockLevelChanged)
98 BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
99 }
100
101 Magstripe_Prev = Magstripe_LCL;
102 Magstripe_LCL = Magstripe_GetStatus();
103 }
104 }
105
106 void EVENT_USB_ConfigurationChanged(void)
107 {
108 USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface);
109 }
110
111 void EVENT_USB_UnhandledControlPacket(void)
112 {
113 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface);
114 }
115
116 void EVENT_USB_StartOfFrame(void)
117 {
118 USB_HID_RegisterStartOfFrame(&Keyboard_HID_Interface);
119 }
120
121 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData)
122 {
123 static bool IsKeyReleaseReport;
124 static bool IsNewlineReport;
125
126 BitBuffer_t* Buffer = NULL;
127 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
128
129 /* Key reports must be interleaved with 0 Key Code reports to release the keys, or repeated keys will be ignored */
130 IsKeyReleaseReport = !IsKeyReleaseReport;
131
132 if (IsKeyReleaseReport)
133 {
134 KeyboardReport->KeyCode = 0;
135 }
136 else if (IsNewlineReport)
137 {
138 IsNewlineReport = false;
139 KeyboardReport->KeyCode = KEY_ENTER;
140 }
141 else
142 {
143 if (TrackDataBuffers[0].Elements)
144 Buffer = &TrackDataBuffers[0];
145 else if (TrackDataBuffers[1].Elements)
146 Buffer = &TrackDataBuffers[1];
147 else if (TrackDataBuffers[2].Elements)
148 Buffer = &TrackDataBuffers[2];
149 else
150 return 0;
151
152 KeyboardReport->KeyCode = BitBuffer_GetNextBit(Buffer) ? KEY_1 : KEY_0;
153
154 /* If buffer now empty, next report must be a newline to seperate track data */
155 if (!(Buffer->Elements))
156 IsNewlineReport = true;
157 }
158
159 return sizeof(USB_KeyboardReport_Data_t);
160 }
161
162 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize)
163 {
164 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
165 }