123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #include "Mouse.h"
- static bool UsingReportProtocol = true;
- static uint16_t IdleCount = 0;
- static uint16_t IdleMSRemaining = 0;
- int main(void)
- {
- SetupHardware();
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- GlobalInterruptEnable();
- for (;;)
- {
- Mouse_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();
- Buttons_Init();
- USB_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(MOUSE_EPADDR, EP_TYPE_INTERRUPT, MOUSE_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_MouseReport_Data_t MouseReportData;
-
- CreateMouseReport(&MouseReportData);
- Endpoint_ClearSETUP();
-
- Endpoint_Write_Control_Stream_LE(&MouseReportData, sizeof(MouseReportData));
- Endpoint_ClearOUT();
-
- memset(&MouseReportData, 0, sizeof(MouseReportData));
- }
- 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 CreateMouseReport(USB_MouseReport_Data_t* const ReportData)
- {
- uint8_t JoyStatus_LCL = Joystick_GetStatus();
- uint8_t ButtonStatus_LCL = Buttons_GetStatus();
-
- memset(ReportData, 0, sizeof(USB_MouseReport_Data_t));
- if (JoyStatus_LCL & JOY_UP)
- ReportData->Y = -1;
- else if (JoyStatus_LCL & JOY_DOWN)
- ReportData->Y = 1;
- if (JoyStatus_LCL & JOY_LEFT)
- ReportData->X = -1;
- else if (JoyStatus_LCL & JOY_RIGHT)
- ReportData->X = 1;
- if (JoyStatus_LCL & JOY_PRESS)
- ReportData->Button |= (1 << 0);
- if (ButtonStatus_LCL & BUTTONS_BUTTON1)
- ReportData->Button |= (1 << 1);
- }
- void SendNextReport(void)
- {
- static USB_MouseReport_Data_t PrevMouseReportData;
- USB_MouseReport_Data_t MouseReportData;
- bool SendReport;
-
- CreateMouseReport(&MouseReportData);
-
- SendReport = (memcmp(&PrevMouseReportData, &MouseReportData, sizeof(USB_MouseReport_Data_t)) != 0);
-
- if ((MouseReportData.Y != 0) || (MouseReportData.X != 0))
- SendReport = true;
-
- if (IdleCount && (!(IdleMSRemaining)))
- {
-
- IdleMSRemaining = IdleCount;
-
- SendReport = true;
- }
-
- Endpoint_SelectEndpoint(MOUSE_EPADDR);
-
- if (Endpoint_IsReadWriteAllowed() && SendReport)
- {
-
- PrevMouseReportData = MouseReportData;
-
- Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData), NULL);
-
- Endpoint_ClearIN();
- }
- }
- void Mouse_Task(void)
- {
-
- if (USB_DeviceState != DEVICE_STATE_Configured)
- return;
-
- SendNextReport();
- }
|