123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #include "Magstripe.h"
- static BitBuffer_t TrackDataBuffers[TOTAL_TRACKS];
- static BitBuffer_t* CurrentTrackBuffer = &TrackDataBuffers[TOTAL_TRACKS];
- static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
- USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
- {
- .Config =
- {
- .InterfaceNumber = INTERFACE_ID_Keyboard,
- .ReportINEndpoint =
- {
- .Address = KEYBOARD_EPADDR,
- .Size = KEYBOARD_EPSIZE,
- .Banks = 1,
- },
- .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
- .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
- },
- };
- int main(void)
- {
- SetupHardware();
- for (uint8_t Buffer = 0; Buffer < TOTAL_TRACKS; Buffer++)
- BitBuffer_Init(&TrackDataBuffers[Buffer]);
- GlobalInterruptEnable();
- for (;;)
- {
- if (Magstripe_GetStatus() & MAG_CARDPRESENT)
- ReadMagstripeData();
- HID_Device_USBTask(&Keyboard_HID_Interface);
- USB_USBTask();
- }
- }
- void SetupHardware(void)
- {
- #if (ARCH == ARCH_AVR8)
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
- #endif
-
- Magstripe_Init();
- USB_Init();
- }
- void ReadMagstripeData(void)
- {
-
- const struct
- {
- uint8_t ClockMask;
- uint8_t DataMask;
- } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
- {MAG_T2_CLOCK, MAG_T2_DATA},
- {MAG_T3_CLOCK, MAG_T3_DATA}};
- uint8_t Magstripe_Prev = 0;
- uint8_t Magstripe_LCL = Magstripe_GetStatus();
- while (Magstripe_LCL & MAG_CARDPRESENT)
- {
- for (uint8_t Track = 0; Track < TOTAL_TRACKS; Track++)
- {
- bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
- bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
- bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
-
- if (ClockPinLevel && ClockLevelChanged)
- BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
- }
- Magstripe_Prev = Magstripe_LCL;
- Magstripe_LCL = Magstripe_GetStatus();
- }
- CurrentTrackBuffer = &TrackDataBuffers[0];
- }
- void EVENT_USB_Device_ConfigurationChanged(void)
- {
- HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
- USB_Device_EnableSOFEvents();
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
- HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
- }
- void EVENT_USB_Device_StartOfFrame(void)
- {
- HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
- }
- bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
- uint8_t* const ReportID,
- const uint8_t ReportType,
- void* ReportData,
- uint16_t* const ReportSize)
- {
- USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
- static bool IsKeyReleaseReport;
-
- IsKeyReleaseReport = !IsKeyReleaseReport;
- if ((IsKeyReleaseReport) || (CurrentTrackBuffer == &TrackDataBuffers[TOTAL_TRACKS]))
- {
-
- KeyboardReport->KeyCode[0] = KEY_NONE;
- }
- else if (!(CurrentTrackBuffer->Elements))
- {
-
- KeyboardReport->KeyCode[0] = KEY_ENTER;
- CurrentTrackBuffer++;
- }
- else
- {
-
- KeyboardReport->KeyCode[0] = BitBuffer_GetNextBit(CurrentTrackBuffer) ? KEY_1 : KEY_0;
- }
- *ReportSize = sizeof(USB_KeyboardReport_Data_t);
- return false;
- }
- void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
- const uint8_t ReportID,
- const uint8_t ReportType,
- const void* ReportData,
- const uint16_t ReportSize)
- {
-
- }
|