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
,
48 for (uint8_t Buffer
= 0; Buffer
< 3; Buffer
++)
49 BitBuffer_Init(&TrackDataBuffers
[Buffer
]);
53 if (Magstripe_GetStatus() & MAG_CARDPRESENT
)
56 USB_HID_USBTask(&Keyboard_HID_Interface
);
61 void SetupHardware(void)
63 /* Disable watchdog if enabled by bootloader/fuses */
64 MCUSR
&= ~(1 << WDRF
);
67 /* Disable clock division */
68 clock_prescale_set(clock_div_1
);
70 /* Hardware Initialization */
75 void ReadMagstripeData(void)
77 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
82 } TrackInfo
[] = {{MAG_T1_CLOCK
, MAG_T1_DATA
},
83 {MAG_T2_CLOCK
, MAG_T2_DATA
},
84 {MAG_T3_CLOCK
, MAG_T3_DATA
}};
86 uint8_t Magstripe_Prev
= 0;
87 uint8_t Magstripe_LCL
= Magstripe_GetStatus();
89 while (Magstripe_LCL
& MAG_CARDPRESENT
)
91 for (uint8_t Track
= 0; Track
< 3; Track
++)
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);
97 if (ClockPinLevel
&& ClockLevelChanged
)
98 BitBuffer_StoreNextBit(&TrackDataBuffers
[Track
], DataPinLevel
);
101 Magstripe_Prev
= Magstripe_LCL
;
102 Magstripe_LCL
= Magstripe_GetStatus();
106 void EVENT_USB_ConfigurationChanged(void)
108 USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface
);
111 void EVENT_USB_UnhandledControlPacket(void)
113 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface
);
116 void EVENT_USB_StartOfFrame(void)
118 USB_HID_RegisterStartOfFrame(&Keyboard_HID_Interface
);
121 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
)
123 static bool IsKeyReleaseReport
;
124 static bool IsNewlineReport
;
126 BitBuffer_t
* Buffer
= NULL
;
127 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
129 /* Key reports must be interleaved with 0 Key Code reports to release the keys, or repeated keys will be ignored */
130 IsKeyReleaseReport
= !IsKeyReleaseReport
;
132 if (IsKeyReleaseReport
)
134 KeyboardReport
->KeyCode
= 0;
136 else if (IsNewlineReport
)
138 IsNewlineReport
= false;
139 KeyboardReport
->KeyCode
= KEY_ENTER
;
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];
152 KeyboardReport
->KeyCode
= BitBuffer_GetNextBit(Buffer
) ? KEY_1
: KEY_0
;
154 /* If buffer now empty, next report must be a newline to seperate track data */
155 if (!(Buffer
->Elements
))
156 IsNewlineReport
= true;
159 return sizeof(USB_KeyboardReport_Data_t
);
162 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
, uint16_t ReportSize
)
164 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports