3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
11 Based on code by Dean Camera (dean [at] fourwalledcubicle [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
34 USB_ClassInfo_HID_t Keyboard_HID_Interface
=
38 .ReportINEndpointNumber
= KEYBOARD_EPNUM
,
39 .ReportINEndpointSize
= KEYBOARD_EPSIZE
,
41 .ReportOUTEndpointNumber
= KEYBOARD_LEDS_EPNUM
,
42 .ReportOUTEndpointSize
= KEYBOARD_EPSIZE
,
44 .ReportINBufferSize
= sizeof(USB_KeyboardReport_Data_t
),
53 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
57 USB_HID_USBTask(&Keyboard_HID_Interface
);
64 /* Disable watchdog if enabled by bootloader/fuses */
65 MCUSR
&= ~(1 << WDRF
);
68 /* Disable clock division */
69 clock_prescale_set(clock_div_1
);
71 /* Hardware Initialization */
77 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
78 OCR0A
= ((F_CPU
/ 64) / 1000);
79 TCCR0A
= (1 << WGM01
);
80 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
81 TIMSK0
= (1 << OCIE0A
);
84 void EVENT_USB_Connect(void)
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
89 void EVENT_USB_Disconnect(void)
91 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
94 void EVENT_USB_ConfigurationChanged(void)
96 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
98 if (!(USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface
)))
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
102 void EVENT_USB_UnhandledControlPacket(void)
104 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface
);
107 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
109 if (Keyboard_HID_Interface
.IdleMSRemaining
)
110 Keyboard_HID_Interface
.IdleMSRemaining
--;
113 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
)
115 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
117 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
118 uint8_t ButtonStatus_LCL
= Buttons_GetStatus();
120 if (JoyStatus_LCL
& JOY_UP
)
121 KeyboardReport
->KeyCode
[0] = 0x04; // A
122 else if (JoyStatus_LCL
& JOY_DOWN
)
123 KeyboardReport
->KeyCode
[0] = 0x05; // B
125 if (JoyStatus_LCL
& JOY_LEFT
)
126 KeyboardReport
->KeyCode
[0] = 0x06; // C
127 else if (JoyStatus_LCL
& JOY_RIGHT
)
128 KeyboardReport
->KeyCode
[0] = 0x07; // D
130 if (JoyStatus_LCL
& JOY_PRESS
)
131 KeyboardReport
->KeyCode
[0] = 0x08; // E
133 if (ButtonStatus_LCL
& BUTTONS_BUTTON1
)
134 KeyboardReport
->KeyCode
[0] = 0x09; // F
136 return sizeof(USB_KeyboardReport_Data_t
);
139 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t
* HIDInterfaceInfo
, void* ReportData
, uint16_t ReportSize
)
141 uint8_t LEDMask
= LEDS_NO_LEDS
;
142 uint8_t* LEDReport
= (uint8_t*)ReportData
;
144 if (*LEDReport
& 0x01) // NUM Lock
145 LEDMask
|= LEDS_LED1
;
147 if (*LEDReport
& 0x02) // CAPS Lock
148 LEDMask
|= LEDS_LED3
;
150 if (*LEDReport
& 0x04) // SCROLL Lock
151 LEDMask
|= LEDS_LED4
;
153 LEDs_SetAllLEDs(LEDMask
);