123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- #include "KeyboardHostWithParser.h"
- int main(void)
- {
- SetupHardware();
- puts_P(PSTR(ESC_FG_CYAN "Keyboard HID Parser Host Demo running.\r\n" ESC_FG_WHITE));
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- GlobalInterruptEnable();
- for (;;)
- {
- KeyboardHost_Task();
- USB_USBTask();
- }
- }
- void SetupHardware(void)
- {
- #if (ARCH == ARCH_AVR8)
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
- #endif
-
- Serial_Init(9600, false);
- LEDs_Init();
- USB_Init();
-
- Serial_CreateStream(NULL);
- }
- void EVENT_USB_Host_DeviceAttached(void)
- {
- puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
- LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
- }
- void EVENT_USB_Host_DeviceUnattached(void)
- {
- puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- }
- void EVENT_USB_Host_DeviceEnumerationComplete(void)
- {
- puts_P(PSTR("Getting Config Data.\r\n"));
- uint8_t ErrorCode;
-
- if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
- {
- if (ErrorCode == ControlError)
- puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
- else
- puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
- return;
- }
-
- if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
- {
- puts_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"));
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
- return;
- }
- printf_P(PSTR("Processing HID Report (Size %d Bytes).\r\n"), HIDReportSize);
-
- if ((ErrorCode = GetHIDReportData()) != ParseSuccessful)
- {
- puts_P(PSTR(ESC_FG_RED "Report Parse Error.\r\n"));
- if (!(HIDReportInfo.TotalReportItems))
- puts_P(PSTR("Not a valid Keyboard." ESC_FG_WHITE));
- else
- printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
- USB_Host_SetDeviceConfiguration(0);
- return;
- }
- puts_P(PSTR("Keyboard Enumerated.\r\n"));
- LEDs_SetAllLEDs(LEDMASK_USB_READY);
- }
- void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
- {
- USB_Disable();
- printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
- " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
- for(;;);
- }
- void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
- const uint8_t SubErrorCode)
- {
- printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
- " -- Error Code %d\r\n"
- " -- Sub Error Code %d\r\n"
- " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
- LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
- }
- void KeyboardHost_Task(void)
- {
- if (USB_HostState != HOST_STATE_Configured)
- return;
-
- Pipe_SelectPipe(KEYBOARD_DATA_IN_PIPE);
- Pipe_Unfreeze();
-
- if (Pipe_IsINReceived())
- {
-
- if (Pipe_IsReadWriteAllowed())
- {
-
- uint8_t KeyboardReport[Pipe_BytesInPipe()];
-
- Pipe_Read_Stream_LE(KeyboardReport, Pipe_BytesInPipe(), NULL);
-
- ProcessKeyboardReport(KeyboardReport);
- }
-
- Pipe_ClearIN();
- }
-
- Pipe_Freeze();
- }
- void ProcessKeyboardReport(uint8_t* KeyboardReport)
- {
-
- for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)
- {
-
- HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber];
-
- if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_KEYBOARD) &&
- (ReportItem->Attributes.BitSize == 8) &&
- (ReportItem->Attributes.Logical.Maximum > 1) &&
- (ReportItem->ItemType == HID_REPORT_ITEM_In))
- {
-
- bool FoundData = USB_GetHIDReportItemInfo(KeyboardReport, ReportItem);
-
- if (!(FoundData))
- continue;
-
- uint8_t KeyCode = (uint8_t)ReportItem->Value;
-
- if (KeyCode)
- {
-
- LEDs_ToggleLEDs(LEDS_LED2);
- char PressedKey = 0;
-
- if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z))
- {
- PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
- }
- else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
- (KeyCode < HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS))
- {
- PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
- }
- else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS)
- {
- PressedKey = '0';
- }
- else if (KeyCode == HID_KEYBOARD_SC_SPACE)
- {
- PressedKey = ' ';
- }
- else if (KeyCode == HID_KEYBOARD_SC_ENTER)
- {
- PressedKey = '\n';
- }
-
- if (PressedKey)
- putchar(PressedKey);
- }
-
- break;
- }
- }
- }
|