3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2009 Denver Gingerich (denver [at] ossguy [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 "KeyboardMouse.h"
34 USB_ClassInfo_HID_t Keyboard_HID_Interface
=
38 .ReportINEndpointNumber
= KEYBOARD_IN_EPNUM
,
39 .ReportINEndpointSize
= HID_EPSIZE
,
41 .ReportOUTEndpointNumber
= KEYBOARD_OUT_EPNUM
,
42 .ReportOUTEndpointSize
= HID_EPSIZE
,
44 .ReportINBufferSize
= sizeof(USB_KeyboardReport_Data_t
),
49 USB_ClassInfo_HID_t Mouse_HID_Interface
=
53 .ReportINEndpointNumber
= MOUSE_IN_EPNUM
,
54 .ReportINEndpointSize
= HID_EPSIZE
,
56 .ReportINBufferSize
= sizeof(USB_MouseReport_Data_t
),
58 .ReportOUTEndpointNumber
= 0,
59 .ReportOUTEndpointSize
= 0,
66 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
70 USB_HID_USBTask(&Keyboard_HID_Interface
);
71 USB_HID_USBTask(&Mouse_HID_Interface
);
78 /* Disable watchdog if enabled by bootloader/fuses */
79 MCUSR
&= ~(1 << WDRF
);
82 /* Disable clock division */
83 clock_prescale_set(clock_div_1
);
85 /* Hardware Initialization */
90 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
91 OCR0A
= ((F_CPU
/ 64) / 1000);
92 TCCR0A
= (1 << WGM01
);
93 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
94 TIMSK0
= (1 << OCIE0A
);
97 void EVENT_USB_Connect(void)
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
102 void EVENT_USB_Disconnect(void)
104 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
107 void EVENT_USB_ConfigurationChanged(void)
109 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
111 if (!(USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface
)))
112 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
114 if (!(USB_HID_ConfigureEndpoints(&Mouse_HID_Interface
)))
115 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
118 void EVENT_USB_UnhandledControlPacket(void)
120 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface
);
121 USB_HID_ProcessControlPacket(&Mouse_HID_Interface
);
124 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
126 if (Keyboard_HID_Interface
.IdleMSRemaining
)
127 Keyboard_HID_Interface
.IdleMSRemaining
--;
129 if (Mouse_HID_Interface
.IdleMSRemaining
)
130 Mouse_HID_Interface
.IdleMSRemaining
--;
133 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
)
135 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
136 uint8_t ButtonStatus_LCL
= Buttons_GetStatus();
138 if (HIDInterfaceInfo
== &Keyboard_HID_Interface
)
140 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
142 /* If first board button not being held down, no keyboard report */
143 if (!(ButtonStatus_LCL
& BUTTONS_BUTTON1
))
146 if (JoyStatus_LCL
& JOY_UP
)
147 KeyboardReport
->KeyCode
[0] = 0x04; // A
148 else if (JoyStatus_LCL
& JOY_DOWN
)
149 KeyboardReport
->KeyCode
[0] = 0x05; // B
151 if (JoyStatus_LCL
& JOY_LEFT
)
152 KeyboardReport
->KeyCode
[0] = 0x06; // C
153 else if (JoyStatus_LCL
& JOY_RIGHT
)
154 KeyboardReport
->KeyCode
[0] = 0x07; // D
156 if (JoyStatus_LCL
& JOY_PRESS
)
157 KeyboardReport
->KeyCode
[0] = 0x08; // E
159 return sizeof(USB_KeyboardReport_Data_t
);
163 USB_MouseReport_Data_t
* MouseReport
= (USB_MouseReport_Data_t
*)ReportData
;
165 /* If first board button being held down, no mouse report */
166 if (ButtonStatus_LCL
& BUTTONS_BUTTON1
)
169 if (JoyStatus_LCL
& JOY_UP
)
171 else if (JoyStatus_LCL
& JOY_DOWN
)
174 if (JoyStatus_LCL
& JOY_RIGHT
)
176 else if (JoyStatus_LCL
& JOY_LEFT
)
179 if (JoyStatus_LCL
& JOY_PRESS
)
180 MouseReport
->Button
= (1 << 0);
182 return sizeof(USB_MouseReport_Data_t
);
186 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
, uint16_t ReportSize
)
188 if (HIDInterfaceInfo
== &Keyboard_HID_Interface
)
190 uint8_t LEDMask
= LEDS_NO_LEDS
;
191 uint8_t* LEDReport
= (uint8_t*)ReportData
;
193 if (*LEDReport
& 0x01) // NUM Lock
194 LEDMask
|= LEDS_LED1
;
196 if (*LEDReport
& 0x02) // CAPS Lock
197 LEDMask
|= LEDS_LED3
;
199 if (*LEDReport
& 0x04) // SCROLL Lock
200 LEDMask
|= LEDS_LED4
;
202 LEDs_SetAllLEDs(LEDMask
);