From 48c6b6dd819b607358717e006748967d543d933f Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 26 Dec 2014 22:13:33 +0100 Subject: [PATCH] Added Consumer + System HID APIs --- Consumer.cpp | 27 ++++++++++++ Consumer.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++ HID.h | 2 + HIDAPI.h | 3 +- Readme.md | 3 +- System.cpp | 26 ++++++++++++ System.h | 72 ++++++++++++++++++++++++++++++++ 7 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 Consumer.cpp create mode 100644 Consumer.h create mode 100644 System.cpp create mode 100644 System.h diff --git a/Consumer.cpp b/Consumer.cpp new file mode 100644 index 0000000..6c685d9 --- /dev/null +++ b/Consumer.cpp @@ -0,0 +1,27 @@ +/* +Consumer.cpp +Copyright (c) 2005-2014 Arduino. All right reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Consumer.h" + +//================================================================================ +// Consumer +//================================================================================ + +// object instance +Consumer_ Consumer; \ No newline at end of file diff --git a/Consumer.h b/Consumer.h new file mode 100644 index 0000000..70e3cd0 --- /dev/null +++ b/Consumer.h @@ -0,0 +1,113 @@ +/* +Consumer.h +Copyright (c) 2005-2014 Arduino. All right reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __CONSUMERAPI__ +#define __CONSUMERAPI__ + +// to access the HID_SendReport via USBAPI.h and report number +#include "Arduino.h" + +//TODO workaround to access the weak sending function +void HID_SendReport(uint8_t id, const void* data, int len); + +//================================================================================ +// Consumer +//================================================================================ + +// Media key definitions, see official USB docs for more +#define MEDIA_FAST_FORWARD 0xB3 +#define MEDIA_REWIND 0xB4 +#define MEDIA_NEXT 0xB5 +#define MEDIA_PREVIOUS 0xB6 +#define MEDIA_STOP 0xB7 +#define MEDIA_PLAY_PAUSE 0xCD + +#define MEDIA_VOLUME_MUTE 0xE2 +#define MEDIA_VOLUME_UP 0xE9 +#define MEDIA_VOLUME_DOWN 0xEA + +#define CONSUMER_EMAIL_READER 0x18A +#define CONSUMER_CALCULATOR 0x192 +#define CONSUMER_EXPLORER 0x194 + +#define CONSUMER_BROWSER_HOME 0x223 +#define CONSUMER_BROWSER_BACK 0x224 +#define CONSUMER_BROWSER_FORWARD 0x225 +#define CONSUMER_BROWSER_REFRESH 0x227 +#define CONSUMER_BROWSER_BOOKMARKS 0x22A + +typedef union{ + // every usable Consumer key possible, up to 4 keys presses possible + uint8_t whole8[8]; + uint16_t whole16[8 / 2]; + uint32_t whole32[8 / 4]; + struct{ + uint16_t key1; + uint16_t key2; + uint16_t key3; + uint16_t key4; + }; +} HID_ConsumerReport_Data_t; + +class Consumer_{ +public: + inline Consumer_(void){ + // empty + } + inline void begin(void){ + // release all buttons + end(); + } + inline void end(void){ + memset(&_report, 0, sizeof(_report)); + HID_SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + } + inline void write(uint16_t m){ + press(m); + release(m); + } + inline void press(uint16_t m){ + // search for a free spot + for (int i = 0; i < sizeof(HID_ConsumerReport_Data_t) / 2; i++) { + if (_report.whole16[i] == 0x00) { + _report.whole16[i] = m; + break; + } + } + HID_SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + } + inline void release(uint16_t m){ + // search and release the keypress + for (int i = 0; i < sizeof(HID_ConsumerReport_Data_t) / 2; i++) { + if (_report.whole16[i] == m) { + _report.whole16[i] = 0x00; + // no break to delete multiple keys + } + } + HID_SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); + } + inline void releaseAll(void){ + begin(); + } +private: + HID_ConsumerReport_Data_t _report; +}; +extern Consumer_ Consumer; + +#endif \ No newline at end of file diff --git a/HID.h b/HID.h index 66a86b2..89dc0f5 100644 --- a/HID.h +++ b/HID.h @@ -262,6 +262,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "USBDesc.h" #include "USBCore.h" +// only include HIDAPI if we have an USB AVR MCU. +// The use can overwrite HID_SendReport() and manually include the APIs. #include "HIDAPI.h" //================================================================================ diff --git a/HIDAPI.h b/HIDAPI.h index 6ce37be..0acabaf 100644 --- a/HIDAPI.h +++ b/HIDAPI.h @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // include all HID APIs #include "Keyboard.h" #include "Mouse.h" -//TODO +#include "Consumer.h" +#include "System.h" #endif \ No newline at end of file diff --git a/Readme.md b/Readme.md index 2a3b917..63dfc1c 100644 --- a/Readme.md +++ b/Readme.md @@ -7,7 +7,6 @@ Under Construction. This is a todo list for myself and a feature list so far. Keyboard Layout english only? more usb definitions instead of fixed values, noone understands someone has to add the keywords.txt definitions as well -keyboard music vol (change descriptor) keyboard led move to usb function not to keyboard and from keyboard call this function? weak hidsendreport implement somewhere the prototype? move HID-Core to a seperate folder? @@ -16,6 +15,8 @@ keycode/raw for keyboard magic key fix? add examples void Recv(volatile u8* data, u8 count) static inline?? +improve workaround for consumer + system weak hid send function prototype +add gamepad Bugs Mouse Abs only works with system report under special circumstances. diff --git a/System.cpp b/System.cpp new file mode 100644 index 0000000..1d1d78d --- /dev/null +++ b/System.cpp @@ -0,0 +1,26 @@ +/* +System.cpp +Copyright (c) 2005-2014 Arduino. All right reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "System.h" + +//================================================================================ +// System +//================================================================================ + +System_ System; \ No newline at end of file diff --git a/System.h b/System.h new file mode 100644 index 0000000..815b2af --- /dev/null +++ b/System.h @@ -0,0 +1,72 @@ +/* +System.h +Copyright (c) 2005-2014 Arduino. All right reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __SYSTEMAPI__ +#define __SYSTEMAPI__ + +// to access the HID_SendReport via USBAPI.h and report number +#include "Arduino.h" + +//TODO workaround to access the weak sending function +void HID_SendReport(uint8_t id, const void* data, int len); + +//================================================================================ +// System +//================================================================================ + +#define SYSTEM_POWER_DOWN 0x81 +#define SYSTEM_SLEEP 0x82 +#define SYSTEM_WAKE_UP 0x83 + +typedef union{ + // every usable system control key possible + uint8_t whole8[1]; + uint8_t key; +} HID_SystemReport_Data_t; + +class System_{ +public: + inline System_(void){ + // empty + } + inline void begin(void){ + // release all buttons + end(); + } + inline void end(void){ + uint8_t _report = 0; + HID_SendReport(HID_REPORTID_SYSTEMCONTROL, &_report, sizeof(_report)); + } + inline void write(uint8_t s){ + press(s); + release(); + } + inline void press(uint8_t s){ + HID_SendReport(HID_REPORTID_SYSTEMCONTROL, &s, sizeof(s)); + } + inline void release(void){ + begin(); + } + inline void releaseAll(void){ + begin(); + } +}; +extern System_ System; + +#endif \ No newline at end of file