From 2281210b1c4191d7350ecbb66330a50b2c03a478 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 19 Sep 2015 18:51:16 +0200 Subject: [PATCH] fixed some led issues, added leds for teensy seems to still not work for nkro and teensy? --- examples/KeyboardLed/KeyboardLed.ino | 14 +++++++++++--- src/ImprovedKeyboard.cpp | 11 +++++------ src/NKROKeyboard.cpp | 13 ++++++------- src/TeensyKeyboard.cpp | 12 ++++++++++++ src/TeensyKeyboard.h | 16 ++++++++++++++++ 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/examples/KeyboardLed/KeyboardLed.ino b/examples/KeyboardLed/KeyboardLed.ino index 54b90fc..06b6d4c 100644 --- a/examples/KeyboardLed/KeyboardLed.ino +++ b/examples/KeyboardLed/KeyboardLed.ino @@ -11,6 +11,14 @@ https://github.com/NicoHood/HID/wiki/Keyboard-API */ +// Choose your favourite keyboard +#define USBKEYBOARD Keyboard + +//#define USBKEYBOARD NKROKeyboard + +//#define USBKEYBOARD TeensyKeyboard +//#define USE_TEENSY_KEYBOARD + #include "HID-Project.h" const int pinLed = LED_BUILTIN; @@ -21,21 +29,21 @@ void setup() { pinMode(pinButton, INPUT_PULLUP); // Sends a clean report to the host. This is important on any Arduino type. - Keyboard.begin(); + USBKEYBOARD.begin(); } void loop() { // Update Led equal to the caps lock state. // Keep in mind that on a 16u2 and Arduino Micro HIGH and LOW for TX/RX Leds are inverted. - if (Keyboard.getLeds() & LED_CAPS_LOCK) + if (USBKEYBOARD.getLeds() & LED_CAPS_LOCK) digitalWrite(pinLed, HIGH); else digitalWrite(pinLed, LOW); // Trigger caps lock manually via button if (!digitalRead(pinButton)) { - Keyboard.write(KEY_CAPS_LOCK); + USBKEYBOARD.write(KEY_CAPS_LOCK); // Simple debounce delay(300); diff --git a/src/ImprovedKeyboard.cpp b/src/ImprovedKeyboard.cpp index aff0e21..f8bf184 100644 --- a/src/ImprovedKeyboard.cpp +++ b/src/ImprovedKeyboard.cpp @@ -52,18 +52,17 @@ static const u8 _hidReportDescriptor[] PROGMEM = { 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */ #if defined(HID_KEYBOARD_LEDS_ENABLED) -//TODO remove reserved bytes to add 3 more custom data bits for advanced users? - /* 5 LEDs for num lock etc */ + /* 5 LEDs for num lock etc, 3 left for advanced, custom usage */ 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */ 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ - 0x95, 0x05, /* REPORT_COUNT (5) */ + 0x95, 0x08, /* REPORT_COUNT (8) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ /* Reserved 3 bits */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0x75, 0x03, /* REPORT_SIZE (3) */ - 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ + //0x95, 0x01, /* REPORT_COUNT (1) */ + //0x75, 0x03, /* REPORT_SIZE (3) */ + //0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ #endif /* 6 Keyboard keys */ diff --git a/src/NKROKeyboard.cpp b/src/NKROKeyboard.cpp index 96befe6..feecd06 100644 --- a/src/NKROKeyboard.cpp +++ b/src/NKROKeyboard.cpp @@ -47,18 +47,17 @@ static const u8 _hidReportDescriptor[] PROGMEM = { 0x81, 0x02, /* INPUT (Data,Var,Abs) */ #if defined(HID_KEYBOARD_LEDS_ENABLED) -//TODO remove reserved bytes to add 3 more custom data bits for advanced users? - /* 5 LEDs for num lock etc */ + /* 5 LEDs for num lock etc, 3 left for advanced, custom usage */ 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */ 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ - 0x95, 0x05, /* REPORT_COUNT (5) */ + 0x95, 0x08, /* REPORT_COUNT (8) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ /* Reserved 3 bits */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0x75, 0x03, /* REPORT_SIZE (3) */ - 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ + //0x95, 0x01, /* REPORT_COUNT (1) */ + //0x75, 0x03, /* REPORT_SIZE (3) */ + //0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ #endif /* 104 Keys as bitmap */ @@ -124,7 +123,7 @@ void NKROKeyboard_::setReportData(const void* data, int len){ leds = *(uint8_t*)data; } -uint8_t Keyboard_::getLeds(void){ +uint8_t NKROKeyboard_::getLeds(void){ return leds; } #endif diff --git a/src/TeensyKeyboard.cpp b/src/TeensyKeyboard.cpp index 10f36a3..fd7ba3a 100644 --- a/src/TeensyKeyboard.cpp +++ b/src/TeensyKeyboard.cpp @@ -29,6 +29,18 @@ HIDDevice((uint8_t*)teensykeyboard_hid_report_desc, sizeof(teensykeyboard_hid_re // HID Descriptor is appended via the inherited HIDDevice class } +#if defined(HID_KEYBOARD_LEDS_ENABLED) +void usb_keyboard_class::setReportData(const void* data, int len){ + // Save led state + if(len == 1) + leds = *(uint8_t*)data; +} + +uint8_t usb_keyboard_class::getLeds(void){ + return leds; +} +#endif + // Step #1, decode UTF8 to Unicode code points // size_t usb_keyboard_class::write(uint8_t c) diff --git a/src/TeensyKeyboard.h b/src/TeensyKeyboard.h index 7b1fb2d..a2996f2 100644 --- a/src/TeensyKeyboard.h +++ b/src/TeensyKeyboard.h @@ -61,6 +61,7 @@ static const uint8_t PROGMEM teensykeyboard_hid_report_desc[] = { 0x09, 0xB6, // Usage (Scan Previous Track), 0x09, 0xB7, // Usage (Stop), 0x09, 0xB8, // Usage (Eject), +// Note: Teensy ledreport was not modified to 8 bit, nor left out when leds are deactivated 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys 0x95, 0x05, // Report Count (5), 0x75, 0x01, // Report Size (1), @@ -82,10 +83,20 @@ static const uint8_t PROGMEM teensykeyboard_hid_report_desc[] = { 0xc0 // End Collection }; +// Keyboard Leds +#define LED_NUM_LOCK 0x01 +#define LED_CAPS_LOCK 0x02 +#define LED_SCROLL_LOCK 0x04 + class usb_keyboard_class : public Print, private HIDDevice { public: usb_keyboard_class(void); + +#if defined(HID_KEYBOARD_LEDS_ENABLED) + uint8_t getLeds(void); +#endif + void begin(void) { } void end(void) { } virtual size_t write(uint8_t); @@ -115,6 +126,11 @@ class usb_keyboard_class : public Print, private HIDDevice uint8_t utf8_state; uint16_t unicode_wchar; uint8_t keyboard_report_data[8]; + +#if defined(HID_KEYBOARD_LEDS_ENABLED) + virtual void setReportData(const void* data, int len); + uint8_t leds; +#endif }; extern usb_keyboard_class TeensyKeyboard;