Skip to content

Commit

Permalink
Updated HIDDevices for new, modified Pluggable HID version
Browse files Browse the repository at this point in the history
Keyboard got improved a bit as well.
  • Loading branch information
NicoHood committed Sep 19, 2015
1 parent 36fdbb4 commit 78c763e
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 75 deletions.
1 change: 0 additions & 1 deletion examples/AbsoluteMouse/AbsoluteMouse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://github.com/NicoHood/HID/wiki/AbsoluteMouse-API
*/

#include "HID.h"
#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
Expand Down
17 changes: 8 additions & 9 deletions examples/Consumer/Consumer.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
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
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
*/

#include "HID.h"
#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
Expand Down
1 change: 0 additions & 1 deletion examples/Gamepad/Gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
https://github.com/NicoHood/HID/wiki/Gamepad-API
*/

#include "HID.h"
#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
Expand Down
5 changes: 2 additions & 3 deletions examples/System/System.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
https://github.com/NicoHood/HID/wiki/System-API
*/

#include "HID.h"
#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
Expand All @@ -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);

Expand All @@ -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);

Expand Down
15 changes: 6 additions & 9 deletions src/AbsoluteMouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ typedef union{
};
} HID_MouseAbsoluteReport_Data_t;

class AbsMouse_
class AbsMouse_ : private HIDDevice
{
private:
int16_t xAxis = 0;
Expand Down Expand Up @@ -131,13 +131,10 @@ class AbsMouse_
}

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){
Expand Down Expand Up @@ -165,7 +162,7 @@ class AbsMouse_
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){
Expand Down
19 changes: 8 additions & 11 deletions src/Consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -115,7 +112,7 @@ class Consumer_ {

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) {
Expand All @@ -131,7 +128,7 @@ class Consumer_ {
break;
}
}
HID.SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report));
SendReport(&_report, sizeof(_report));
}

inline void release(uint16_t m) {
Expand All @@ -142,7 +139,7 @@ class Consumer_ {
// no break to delete multiple keys
}
}
HID.SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report));
SendReport(&_report, sizeof(_report));
}

inline void releaseAll(void) {
Expand Down
17 changes: 7 additions & 10 deletions src/Gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -172,10 +169,10 @@ class Gamepad_{

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)); }
Expand Down
8 changes: 4 additions & 4 deletions src/HID-Project.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/HID.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "PluggableHID/HID.h"
#include "PluggableHID/HIDDevice.h"
3 changes: 1 addition & 2 deletions src/ImprovedKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/PluggableHID/HIDDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/PluggableHID/HIDDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HIDDevice
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;

Expand All @@ -50,8 +51,9 @@ class HIDDevice

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);
};

Expand Down
2 changes: 1 addition & 1 deletion src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 12 additions & 9 deletions src/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
13 changes: 5 additions & 8 deletions src/TeensyKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}


Expand Down
4 changes: 2 additions & 2 deletions src/TeensyKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 78c763e

Please sign in to comment.