From 78c763e438b333de99de2b689aea318f89c408a0 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 19 Sep 2015 13:15:32 +0200 Subject: [PATCH] Updated HIDDevices for new, modified Pluggable HID version Keyboard got improved a bit as well. --- examples/AbsoluteMouse/AbsoluteMouse.ino | 1 - examples/Consumer/Consumer.ino | 15 +++++++-------- examples/Gamepad/Gamepad.ino | 1 - examples/System/System.ino | 5 ++--- src/AbsoluteMouse.h | 15 ++++++--------- src/Consumer.h | 19 ++++++++----------- src/Gamepad.h | 17 +++++++---------- src/HID-Project.h | 8 ++++---- src/HID.h | 1 + src/ImprovedKeyboard.h | 3 +-- src/PluggableHID/HIDDevice.cpp | 3 --- src/PluggableHID/HIDDevice.h | 6 ++++-- src/System.cpp | 2 +- src/System.h | 21 ++++++++++++--------- src/TeensyKeyboard.cpp | 13 +++++-------- src/TeensyKeyboard.h | 4 ++-- 16 files changed, 60 insertions(+), 74 deletions(-) diff --git a/examples/AbsoluteMouse/AbsoluteMouse.ino b/examples/AbsoluteMouse/AbsoluteMouse.ino index 4abccd9..872ee63 100644 --- a/examples/AbsoluteMouse/AbsoluteMouse.ino +++ b/examples/AbsoluteMouse/AbsoluteMouse.ino @@ -10,7 +10,6 @@ https://github.com/NicoHood/HID/wiki/AbsoluteMouse-API */ -#include "HID.h" #include "HID-Project.h" const int pinLed = LED_BUILTIN; diff --git a/examples/Consumer/Consumer.ino b/examples/Consumer/Consumer.ino index fa75e26..0c566e9 100644 --- a/examples/Consumer/Consumer.ino +++ b/examples/Consumer/Consumer.ino @@ -1,15 +1,14 @@ /* - Copyright (c) 2014-2015 NicoHood - See the readme for credit to other people. + Copyright (c) 2014-2015 NicoHood + See the readme for credit to other people. - Consumer example - Press a button to play/pause music player - - See HID Project documentation for more Consumer keys. - https://github.com/NicoHood/HID/wiki/Consumer-API + Consumer example + Press a button to play/pause music player + + See HID Project documentation for more Consumer keys. + https://github.com/NicoHood/HID/wiki/Consumer-API */ -#include "HID.h" #include "HID-Project.h" const int pinLed = LED_BUILTIN; diff --git a/examples/Gamepad/Gamepad.ino b/examples/Gamepad/Gamepad.ino index bff18da..c567c0a 100644 --- a/examples/Gamepad/Gamepad.ino +++ b/examples/Gamepad/Gamepad.ino @@ -9,7 +9,6 @@ https://github.com/NicoHood/HID/wiki/Gamepad-API */ -#include "HID.h" #include "HID-Project.h" const int pinLed = LED_BUILTIN; diff --git a/examples/System/System.ino b/examples/System/System.ino index 92445d6..696a82a 100644 --- a/examples/System/System.ino +++ b/examples/System/System.ino @@ -9,7 +9,6 @@ https://github.com/NicoHood/HID/wiki/System-API */ -#include "HID.h" #include "HID-Project.h" const int pinLed = LED_BUILTIN; @@ -30,7 +29,7 @@ void loop() { if (!digitalRead(pinButtonS)) { digitalWrite(pinLed, HIGH); - // Puts pc into sleep mode/shuts it down + // Puts PC into sleep mode/shuts it down System.write(SYSTEM_SLEEP); //System.write(SYSTEM_POWER_DOWN); @@ -42,7 +41,7 @@ void loop() { if (!digitalRead(pinButtonW)) { digitalWrite(pinLed, HIGH); - // Tries to wake up the PC + // Try to wake up the PC // This might fail on some PCs/Laptops where USB wakeup is not supported System.write(SYSTEM_WAKE_UP); diff --git a/src/AbsoluteMouse.h b/src/AbsoluteMouse.h index 9aadca6..be646a4 100644 --- a/src/AbsoluteMouse.h +++ b/src/AbsoluteMouse.h @@ -98,7 +98,7 @@ typedef union{ }; } HID_MouseAbsoluteReport_Data_t; -class AbsMouse_ +class AbsMouse_ : private HIDDevice { private: int16_t xAxis = 0; @@ -131,13 +131,10 @@ private: } public: - inline AbsMouse_(void) { - static HID_Descriptor cb = { - .length = sizeof(_absmouseReportDescriptor), - .descriptor = _absmouseReportDescriptor, - }; - static HIDDescriptorListNode node(&cb); - HID.AppendDescriptor(&node); + inline AbsMouse_(void): + HIDDevice((uint8_t*)_absmouseReportDescriptor, sizeof(_absmouseReportDescriptor), HID_REPORTID_MOUSE_ABSOLUTE) + { + // HID Descriptor is appended via the inherited HIDDevice class } inline void begin(void){ @@ -165,7 +162,7 @@ public: report.xAxis = x; report.yAxis = y; report.wheel = wheel; - HID.SendReport(HID_REPORTID_MOUSE_ABSOLUTE, &report, sizeof(report)); + SendReport(&report, sizeof(report)); } inline void move(int x, int y, signed char wheel = 0){ diff --git a/src/Consumer.h b/src/Consumer.h index f5f9fd8..80da75d 100644 --- a/src/Consumer.h +++ b/src/Consumer.h @@ -97,15 +97,12 @@ typedef union { }; } HID_ConsumerControlReport_Data_t; -class Consumer_ { +class Consumer_ : private HIDDevice { public: - inline Consumer_(void) { - static HID_Descriptor cb = { - .length = sizeof(_consumerReportDescriptor), - .descriptor = _consumerReportDescriptor, - }; - static HIDDescriptorListNode node(&cb); - HID.AppendDescriptor(&node); + inline Consumer_(void) : + HIDDevice((uint8_t*)_consumerReportDescriptor, sizeof(_consumerReportDescriptor), HID_REPORTID_CONSUMERCONTROL) + { + // HID Descriptor is appended via the inherited HIDDevice class } inline void begin(void) { @@ -115,7 +112,7 @@ public: inline void end(void) { memset(&_report, 0, sizeof(_report)); - HID.SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + SendReport(&_report, sizeof(_report)); } inline void write(uint16_t m) { @@ -131,7 +128,7 @@ public: break; } } - HID.SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + SendReport(&_report, sizeof(_report)); } inline void release(uint16_t m) { @@ -142,7 +139,7 @@ public: // no break to delete multiple keys } } - HID.SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + SendReport(&_report, sizeof(_report)); } inline void releaseAll(void) { diff --git a/src/Gamepad.h b/src/Gamepad.h index 1a09c24..d9671be 100644 --- a/src/Gamepad.h +++ b/src/Gamepad.h @@ -154,15 +154,12 @@ typedef union { }; } HID_GamepadReport_Data_t; -class Gamepad_{ +class Gamepad_ : private HIDDevice{ public: - inline Gamepad_(void){ - static HID_Descriptor cb = { - .length = sizeof(_gamepadReportDescriptor), - .descriptor = _gamepadReportDescriptor, - }; - static HIDDescriptorListNode node(&cb); - HID.AppendDescriptor(&node); + inline Gamepad_(void) : + HIDDevice((uint8_t*)_gamepadReportDescriptor, sizeof(_gamepadReportDescriptor), HID_REPORTID_GAMEPAD) + { + // HID Descriptor is appended via the inherited HIDDevice class } inline void begin(void){ @@ -172,10 +169,10 @@ public: inline void end(void){ memset(&_report, 0, sizeof(_report)); - HID.SendReport(HID_REPORTID_GAMEPAD, &_report, sizeof(_report)); + SendReport(&_report, sizeof(_report)); } - inline void write(void){ HID.SendReport(HID_REPORTID_GAMEPAD, &_report, sizeof(_report)); } + inline void write(void){ SendReport(&_report, sizeof(_report)); } inline void press(uint8_t b){ _report.buttons |= (uint32_t)1 << (b - 1); } inline void release(uint8_t b){ _report.buttons &= ~((uint32_t)1 << (b - 1)); } inline void releaseAll(void){ memset(&_report, 0x00, sizeof(_report)); } diff --git a/src/HID-Project.h b/src/HID-Project.h index ed7d460..bd8286f 100644 --- a/src/HID-Project.h +++ b/src/HID-Project.h @@ -109,10 +109,10 @@ THE SOFTWARE. #include "HID-Tables.h" // Include all HID libraries (.a linkage required to work) properly -//#include "AbsoluteMouse.h" -//#include "Consumer.h" -//#include "Gamepad.h" -//#include "System.h" +#include "AbsoluteMouse.h" +#include "Consumer.h" +#include "Gamepad.h" +#include "System.h" // Include Teensy HID afterwards to overwrite key definitions if used #ifdef USE_TEENSY_KEYBOARD diff --git a/src/HID.h b/src/HID.h index f7644ba..775c617 100644 --- a/src/HID.h +++ b/src/HID.h @@ -1 +1,2 @@ #include "PluggableHID/HID.h" +#include "PluggableHID/HIDDevice.h" diff --git a/src/ImprovedKeyboard.h b/src/ImprovedKeyboard.h index 6a46cc8..c6119e2 100644 --- a/src/ImprovedKeyboard.h +++ b/src/ImprovedKeyboard.h @@ -36,7 +36,6 @@ THE SOFTWARE. // Keyboard #include "HID-Project.h" -#include "PluggableHID/HIDDevice.h" #include "ImprovedKeylayouts.h" // Low level key report: up to 6 keys and shift, ctrl etc at once @@ -52,7 +51,7 @@ typedef union{ }; } HID_KeyboardReport_Data_t; -class Keyboard_ : public Print, public HIDDevice +class Keyboard_ : public Print, private HIDDevice { private: HID_KeyboardReport_Data_t _keyReport; diff --git a/src/PluggableHID/HIDDevice.cpp b/src/PluggableHID/HIDDevice.cpp index a0c311e..b29295d 100644 --- a/src/PluggableHID/HIDDevice.cpp +++ b/src/PluggableHID/HIDDevice.cpp @@ -12,9 +12,6 @@ HIDDevice::HIDDevice(uint8_t* data, uint16_t length, uint8_t ID) : descriptorData(data), descriptorLength(length), reportID(ID) { - // TODO call Append - // TODO init const data - // TODO template? HID.AppendDescriptor(this); } diff --git a/src/PluggableHID/HIDDevice.h b/src/PluggableHID/HIDDevice.h index 6d43866..6663263 100644 --- a/src/PluggableHID/HIDDevice.h +++ b/src/PluggableHID/HIDDevice.h @@ -40,6 +40,7 @@ public: HIDDevice(uint8_t* data, uint16_t length, uint8_t ID); // Needs to be public for static HID_ function access + // Inherit this device private and everything should be fine //private: HIDDevice* next = NULL; @@ -50,8 +51,9 @@ public: virtual void setReportData(const void* data, int len); - // Public for custom, professional usage, like raw Keyboard -//protected: +protected: + // Could be used and inherited public for custom, professional usage, like raw Keyboard + // As an alternative you may still call the HID singleton. void SendReport(const void* data, int len); }; diff --git a/src/System.cpp b/src/System.cpp index 4b59ec1..f4cd4e9 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -37,7 +37,7 @@ void System_::press(uint8_t s){ USBDevice.wakeupHost(); else #endif - HID.SendReport(HID_REPORTID_SYSTEMCONTROL, &s, sizeof(s)); + SendReport(&s, sizeof(s)); } #endif diff --git a/src/System.h b/src/System.h index 22baebe..816403c 100644 --- a/src/System.h +++ b/src/System.h @@ -65,32 +65,35 @@ typedef union{ uint8_t key; } HID_SystemControlReport_Data_t; -class System_{ +class System_ : private HIDDevice{ public: - inline System_(void){ - static HID_Descriptor cb = { - .length = sizeof(_systemReportDescriptor), - .descriptor = _systemReportDescriptor, - }; - static HIDDescriptorListNode node(&cb); - HID.AppendDescriptor(&node); + inline System_(void) : + HIDDevice((uint8_t*)_systemReportDescriptor, sizeof(_systemReportDescriptor), HID_REPORTID_SYSTEMCONTROL) + { + // HID Descriptor is appended via the inherited HIDDevice class } + inline void begin(void){ // release all buttons end(); } + inline void end(void){ uint8_t _report = 0; - HID.SendReport(HID_REPORTID_SYSTEMCONTROL, &_report, sizeof(_report)); + SendReport(&_report, sizeof(_report)); } + inline void write(uint8_t s){ press(s); release(); } + void press(uint8_t s); + inline void release(void){ begin(); } + inline void releaseAll(void){ begin(); } diff --git a/src/TeensyKeyboard.cpp b/src/TeensyKeyboard.cpp index 46d7e48..2a8f8b6 100644 --- a/src/TeensyKeyboard.cpp +++ b/src/TeensyKeyboard.cpp @@ -23,13 +23,10 @@ #include "TeensyKeyboard.h" -usb_keyboard_class::usb_keyboard_class(void){ - static HID_Descriptor cb = { - .length = sizeof(keyboard_hid_report_desc), - .descriptor = keyboard_hid_report_desc, - }; - static HIDDescriptorListNode node(&cb); - HID.AppendDescriptor(&node); +usb_keyboard_class::usb_keyboard_class(void) : +HIDDevice((uint8_t*)teensykeyboard_hid_report_desc, sizeof(teensykeyboard_hid_report_desc), HID_REPORTID_TEENSY_KEYBOARD) +{ + // HID Descriptor is appended via the inherited HIDDevice class } // Step #1, decode UTF8 to Unicode code points @@ -256,7 +253,7 @@ void usb_keyboard_class::set_media(uint8_t c) void usb_keyboard_class::send_now(void) { - HID.SendReport(HID_REPORTID_TEENSY_KEYBOARD,keyboard_report_data,sizeof(keyboard_report_data)); + SendReport(keyboard_report_data,sizeof(keyboard_report_data)); } diff --git a/src/TeensyKeyboard.h b/src/TeensyKeyboard.h index 82a6335..7b1fb2d 100644 --- a/src/TeensyKeyboard.h +++ b/src/TeensyKeyboard.h @@ -35,7 +35,7 @@ #include "TeensyKeylayouts.h" // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60 -static const uint8_t PROGMEM keyboard_hid_report_desc[] = { +static const uint8_t PROGMEM teensykeyboard_hid_report_desc[] = { 0x05, 0x01, // Usage Page (Generic Desktop), 0x09, 0x06, // Usage (Keyboard), 0xA1, 0x01, // Collection (Application), @@ -82,7 +82,7 @@ static const uint8_t PROGMEM keyboard_hid_report_desc[] = { 0xc0 // End Collection }; -class usb_keyboard_class : public Print +class usb_keyboard_class : public Print, private HIDDevice { public: usb_keyboard_class(void);