123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #include "Keyboard.h"
- static bool UsingReportProtocol = true;
- static uint16_t IdleCount = 500;
- static uint16_t IdleMSRemaining = 0;
- int main(void)
- {
- SetupHardware();
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- GlobalInterruptEnable();
- for (;;)
- {
- HID_Task();
- USB_USBTask();
- }
- }
- void SetupHardware(void)
- {
- #if (ARCH == ARCH_AVR8)
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
- #elif (ARCH == ARCH_XMEGA)
-
- XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
- XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
-
- XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
- XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
- PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
- #endif
-
- Joystick_Init();
- LEDs_Init();
- USB_Init();
- Buttons_Init();
- }
- void EVENT_USB_Device_Connect(void)
- {
-
- LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
-
- UsingReportProtocol = true;
- }
- void EVENT_USB_Device_Disconnect(void)
- {
-
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- }
- void EVENT_USB_Device_ConfigurationChanged(void)
- {
- bool ConfigSuccess = true;
-
- ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPADDR, EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1);
- ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1);
-
- USB_Device_EnableSOFEvents();
-
- LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
-
- switch (USB_ControlRequest.bRequest)
- {
- case HID_REQ_GetReport:
- if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- USB_KeyboardReport_Data_t KeyboardReportData;
-
- CreateKeyboardReport(&KeyboardReportData);
- Endpoint_ClearSETUP();
-
- Endpoint_Write_Control_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
- Endpoint_ClearOUT();
- }
- break;
- case HID_REQ_SetReport:
- if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
-
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
-
- uint8_t LEDStatus = Endpoint_Read_8();
- Endpoint_ClearOUT();
- Endpoint_ClearStatusStage();
-
- ProcessLEDReport(LEDStatus);
- }
- break;
- case HID_REQ_GetProtocol:
- if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
-
- Endpoint_Write_8(UsingReportProtocol);
- Endpoint_ClearIN();
- Endpoint_ClearStatusStage();
- }
- break;
- case HID_REQ_SetProtocol:
- if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
- Endpoint_ClearStatusStage();
-
- UsingReportProtocol = (USB_ControlRequest.wValue != 0);
- }
- break;
- case HID_REQ_SetIdle:
- if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
- Endpoint_ClearStatusStage();
-
- IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
- }
- break;
- case HID_REQ_GetIdle:
- if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
-
- Endpoint_Write_8(IdleCount >> 2);
- Endpoint_ClearIN();
- Endpoint_ClearStatusStage();
- }
- break;
- }
- }
- void EVENT_USB_Device_StartOfFrame(void)
- {
-
- if (IdleMSRemaining)
- IdleMSRemaining--;
- }
- void CreateKeyboardReport(USB_KeyboardReport_Data_t* const ReportData)
- {
- uint8_t JoyStatus_LCL = Joystick_GetStatus();
- uint8_t ButtonStatus_LCL = Buttons_GetStatus();
- uint8_t UsedKeyCodes = 0;
-
- memset(ReportData, 0, sizeof(USB_KeyboardReport_Data_t));
-
- ReportData->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
- if (JoyStatus_LCL & JOY_UP)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;
- else if (JoyStatus_LCL & JOY_DOWN)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_B;
- if (JoyStatus_LCL & JOY_LEFT)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_C;
- else if (JoyStatus_LCL & JOY_RIGHT)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_D;
- if (JoyStatus_LCL & JOY_PRESS)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_E;
- if (ButtonStatus_LCL & BUTTONS_BUTTON1)
- ReportData->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_F;
- }
- void ProcessLEDReport(const uint8_t LEDReport)
- {
- uint8_t LEDMask = LEDS_LED2;
- if (LEDReport & HID_KEYBOARD_LED_NUMLOCK)
- LEDMask |= LEDS_LED1;
- if (LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
- LEDMask |= LEDS_LED3;
- if (LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
- LEDMask |= LEDS_LED4;
-
- LEDs_SetAllLEDs(LEDMask);
- }
- void SendNextReport(void)
- {
- static USB_KeyboardReport_Data_t PrevKeyboardReportData;
- USB_KeyboardReport_Data_t KeyboardReportData;
- bool SendReport = false;
-
- CreateKeyboardReport(&KeyboardReportData);
-
- if (IdleCount && (!(IdleMSRemaining)))
- {
-
- IdleMSRemaining = IdleCount;
-
- SendReport = true;
- }
- else
- {
-
- SendReport = (memcmp(&PrevKeyboardReportData, &KeyboardReportData, sizeof(USB_KeyboardReport_Data_t)) != 0);
- }
-
- Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR);
-
- if (Endpoint_IsReadWriteAllowed() && SendReport)
- {
-
- PrevKeyboardReportData = KeyboardReportData;
-
- Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL);
-
- Endpoint_ClearIN();
- }
- }
- void ReceiveNextReport(void)
- {
-
- Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR);
-
- if (Endpoint_IsOUTReceived())
- {
-
- if (Endpoint_IsReadWriteAllowed())
- {
-
- uint8_t LEDReport = Endpoint_Read_8();
-
- ProcessLEDReport(LEDReport);
- }
-
- Endpoint_ClearOUT();
- }
- }
- void HID_Task(void)
- {
-
- if (USB_DeviceState != DEVICE_STATE_Configured)
- return;
-
- SendNextReport();
-
- ReceiveNextReport();
- }
|