Merge pull request #30 from vlee489/master

Updates DigiKeyboard.h to add arrows keys.
This commit is contained in:
Erik Tylek Kettenburg 2016-03-07 17:25:05 -08:00
commit 41c25785db

View File

@ -23,39 +23,39 @@
typedef uint8_t byte; typedef uint8_t byte;
#define BUFFER_SIZE 2 // Minimum of 2: 1 for modifiers + 1 for keystroke #define BUFFER_SIZE 2 // Minimum of 2: 1 for modifiers + 1 for keystroke
static uchar idleRate; // in 4 ms units static uchar idleRate; // in 4 ms units
/* We use a simplifed keyboard report descriptor which does not support the /* We use a simplifed keyboard report descriptor which does not support the
* boot protocol. We don't allow setting status LEDs and but we do allow * boot protocol. We don't allow setting status LEDs and but we do allow
* simultaneous key presses. * simultaneous key presses.
* The report descriptor has been created with usb.org's "HID Descriptor Tool" * The report descriptor has been created with usb.org's "HID Descriptor Tool"
* which can be downloaded from http://www.usb.org/developers/hidpage/. * which can be downloaded from http://www.usb.org/developers/hidpage/.
* Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
* for the second INPUT item. * for the second INPUT item.
*/ */
const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */ const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard) 0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application) 0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard) 0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0) 0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1) 0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8) 0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs) 0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (simultaneous keystrokes) 0x95, 0x01, // REPORT_COUNT (simultaneous keystrokes)
0x75, 0x08, // REPORT_SIZE (8) 0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101) 0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs) 0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION 0xc0 // END_COLLECTION
}; };
@ -126,8 +126,10 @@ const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]
#define KEY_F11 68 #define KEY_F11 68
#define KEY_F12 69 #define KEY_F12 69
#define KEY_ARROW_LEFT 0x50 #define KEY_ARROW_UP 82
#define KEY_ARROW_DOWN 81
#define KEY_ARROW_LEFT 80
#define KEY_ARROW_RIGHT 79
class DigiKeyboardDevice : public Print { class DigiKeyboardDevice : public Print {
public: public:
@ -139,19 +141,19 @@ class DigiKeyboardDevice : public Print {
usbInit(); usbInit();
sei(); sei();
// TODO: Remove the next two lines once we fix // TODO: Remove the next two lines once we fix
// missing first keystroke bug properly. // missing first keystroke bug properly.
memset(reportBuffer, 0, sizeof(reportBuffer)); memset(reportBuffer, 0, sizeof(reportBuffer));
usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
} }
void update() { void update() {
usbPoll(); usbPoll();
} }
// delay while updating until we are finished delaying // delay while updating until we are finished delaying
void delay(long milli) { void delay(long milli) {
unsigned long last = millis(); unsigned long last = millis();
@ -162,7 +164,7 @@ class DigiKeyboardDevice : public Print {
update(); update();
} }
} }
//sendKeyStroke: sends a key press AND release //sendKeyStroke: sends a key press AND release
void sendKeyStroke(byte keyStroke) { void sendKeyStroke(byte keyStroke) {
sendKeyStroke(keyStroke, 0); sendKeyStroke(keyStroke, 0);
@ -191,21 +193,21 @@ class DigiKeyboardDevice : public Print {
usbPoll(); usbPoll();
_delay_ms(5); _delay_ms(5);
} }
memset(reportBuffer, 0, sizeof(reportBuffer)); memset(reportBuffer, 0, sizeof(reportBuffer));
reportBuffer[0] = modifiers; reportBuffer[0] = modifiers;
reportBuffer[1] = keyPress; reportBuffer[1] = keyPress;
usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
} }
size_t write(uint8_t chr) { size_t write(uint8_t chr) {
uint8_t data = pgm_read_byte_near(ascii_to_scan_code_table + (chr - 8)); uint8_t data = pgm_read_byte_near(ascii_to_scan_code_table + (chr - 8));
sendKeyStroke(data & 0b01111111, data >> 7 ? MOD_SHIFT_RIGHT : 0); sendKeyStroke(data & 0b01111111, data >> 7 ? MOD_SHIFT_RIGHT : 0);
return 1; return 1;
} }
//private: TODO: Make friend? //private: TODO: Make friend?
uchar reportBuffer[2]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes] uchar reportBuffer[2]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
using Print::write; using Print::write;
@ -215,7 +217,7 @@ DigiKeyboardDevice DigiKeyboard = DigiKeyboardDevice();
#ifdef __cplusplus #ifdef __cplusplus
extern "C"{ extern "C"{
#endif #endif
// USB_PUBLIC uchar usbFunctionSetup // USB_PUBLIC uchar usbFunctionSetup
uchar usbFunctionSetup(uchar data[8]) { uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (usbRequest_t *)((void *)data); usbRequest_t *rq = (usbRequest_t *)((void *)data);
@ -228,22 +230,22 @@ extern "C"{
/* wValue: ReportType (highbyte), ReportID (lowbyte) */ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */ /* we only have one report type, so don't look at wValue */
// TODO: Ensure it's okay not to return anything here? // TODO: Ensure it's okay not to return anything here?
return 0; return 0;
} else if (rq->bRequest == USBRQ_HID_GET_IDLE) { } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
//usbMsgPtr = &idleRate; //usbMsgPtr = &idleRate;
//return 1; //return 1;
return 0; return 0;
} else if (rq->bRequest == USBRQ_HID_SET_IDLE) { } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
idleRate = rq->wValue.bytes[1]; idleRate = rq->wValue.bytes[1];
} }
} else { } else {
/* no vendor specific requests implemented */ /* no vendor specific requests implemented */
} }
return 0; return 0;
} }
#ifdef __cplusplus #ifdef __cplusplus