From 4cfa0c6964028ba74a543a24d49365ca8a7d89c4 Mon Sep 17 00:00:00 2001 From: Nico Date: Sat, 14 Feb 2015 12:37:35 +0100 Subject: [PATCH] Minor fixes + HIDBridge * increased HW Serial1 RX buffer size from 16 to 32 (TX still 16) * removed fixed size in report buffers * used HID_KeyboardReport_Data_t now in Keyboard API HID Bridge works for transmitting --- Readme.md | 3 + avr/cores/hid/HardwareSerial.h | 2 +- avr/cores/hid/USB-Core/Consumer.h | 8 +- avr/cores/hid/USB-Core/Gamepad.h | 6 +- avr/cores/hid/USB-Core/Keyboard.cpp | 4 +- avr/cores/hid/USB-Core/Keyboard.h | 18 ++--- avr/cores/hid/USB-Core/Mouse.h | 12 +-- avr/cores/hid/USB-Core/RawHID.h | 6 +- avr/cores/hid/USB-Core/System.h | 2 +- avr/libraries/HIDBridge/HIDBridge.cpp | 102 +++++++++++++++++++------- avr/libraries/HIDBridge/HIDBridge.h | 18 ++++- 11 files changed, 118 insertions(+), 63 deletions(-) diff --git a/Readme.md b/Readme.md index 4812d02..47f64b8 100644 --- a/Readme.md +++ b/Readme.md @@ -65,7 +65,10 @@ Version History ``` 2.2 Release (xx.xx.2015) * added HID-Bridge +* increased HW Serial1 RX buffer size from 16 to 32 (TX still 16) * added colour highlighting (through HID-Bridge library) +* removed fixed size in report buffers +* used HID_KeyboardReport_Data_t now in Keyboard API 2.1 Release (28.01.2015) * Reworked the whole USB-Core from scratch diff --git a/avr/cores/hid/HardwareSerial.h b/avr/cores/hid/HardwareSerial.h index 7f4f162..71ad331 100644 --- a/avr/cores/hid/HardwareSerial.h +++ b/avr/cores/hid/HardwareSerial.h @@ -43,7 +43,7 @@ #endif #if !defined(SERIAL_RX_BUFFER_SIZE) #if (RAMEND < 1000) -#define SERIAL_RX_BUFFER_SIZE 16 +#define SERIAL_RX_BUFFER_SIZE 32 #else #define SERIAL_RX_BUFFER_SIZE 64 #endif diff --git a/avr/cores/hid/USB-Core/Consumer.h b/avr/cores/hid/USB-Core/Consumer.h index 3397f52..84fc082 100644 --- a/avr/cores/hid/USB-Core/Consumer.h +++ b/avr/cores/hid/USB-Core/Consumer.h @@ -55,9 +55,9 @@ THE SOFTWARE. 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]; + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; struct{ uint16_t key1; uint16_t key2; @@ -104,7 +104,7 @@ public: HID_SendReport(HID_REPORTID_CONSUMERCONTROL, &_report, sizeof(_report)); } inline void releaseAll(void){ - begin(); + end(); } private: HID_ConsumerControlReport_Data_t _report; diff --git a/avr/cores/hid/USB-Core/Gamepad.h b/avr/cores/hid/USB-Core/Gamepad.h index 3a9682f..7c3927b 100644 --- a/avr/cores/hid/USB-Core/Gamepad.h +++ b/avr/cores/hid/USB-Core/Gamepad.h @@ -44,9 +44,9 @@ THE SOFTWARE. typedef union { // 32 Buttons, 6 Axis, 2 D-Pads - uint8_t whole8[15]; - uint16_t whole16[15 / 2]; - uint32_t whole32[15 / 4]; + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; uint32_t buttons; struct{ diff --git a/avr/cores/hid/USB-Core/Keyboard.cpp b/avr/cores/hid/USB-Core/Keyboard.cpp index 148f1e3..8f2c935 100644 --- a/avr/cores/hid/USB-Core/Keyboard.cpp +++ b/avr/cores/hid/USB-Core/Keyboard.cpp @@ -48,9 +48,9 @@ THE SOFTWARE. Keyboard_ Keyboard; -void Keyboard_::sendReport(KeyReport* keys) +void Keyboard_::sendReport(HID_KeyboardReport_Data_t* keys) { - HID_SendReport(HID_REPORTID_KEYBOARD, keys, sizeof(KeyReport)); + HID_SendReport(HID_REPORTID_KEYBOARD, keys, sizeof(HID_KeyboardReport_Data_t)); } extern diff --git a/avr/cores/hid/USB-Core/Keyboard.h b/avr/cores/hid/USB-Core/Keyboard.h index f166650..156fc3a 100644 --- a/avr/cores/hid/USB-Core/Keyboard.h +++ b/avr/cores/hid/USB-Core/Keyboard.h @@ -145,9 +145,9 @@ THE SOFTWARE. typedef union{ // Low level key report: up to 6 keys and shift, ctrl etc at once - uint8_t whole8[8]; - uint16_t whole16[8 / 2]; - uint32_t whole32[8 / 4]; + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; struct{ uint8_t modifiers; uint8_t reserved; @@ -155,19 +155,11 @@ typedef union{ }; } HID_KeyboardReport_Data_t; -// Low level key report: up to 6 keys and shift, ctrl etc at once -typedef struct -{ - uint8_t modifiers; - uint8_t reserved; - uint8_t keys[6]; -} KeyReport; //TODO typedef union above - class Keyboard_ : public Print { protected: - KeyReport _keyReport; - void sendReport(KeyReport* keys); + HID_KeyboardReport_Data_t _keyReport; + void sendReport(HID_KeyboardReport_Data_t* keys); public: inline Keyboard_(void){ // empty diff --git a/avr/cores/hid/USB-Core/Mouse.h b/avr/cores/hid/USB-Core/Mouse.h index 507607a..fa5b255 100644 --- a/avr/cores/hid/USB-Core/Mouse.h +++ b/avr/cores/hid/USB-Core/Mouse.h @@ -61,9 +61,9 @@ THE SOFTWARE. typedef union{ // mouse report: 8 buttons, position, wheel - uint8_t whole8[4]; - uint16_t whole16[4 / 2]; - uint32_t whole32[4 / 4]; + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; struct{ uint8_t buttons; int8_t xAxis; @@ -74,9 +74,9 @@ typedef union{ typedef union{ // mouse absolute report: 2 absolute axis - uint8_t whole8[4]; - uint16_t whole16[4 / 2]; - uint32_t whole32[4 / 4]; + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; struct{ int16_t xAxis; int16_t yAxis; diff --git a/avr/cores/hid/USB-Core/RawHID.h b/avr/cores/hid/USB-Core/RawHID.h index bd31630..f77a368 100644 --- a/avr/cores/hid/USB-Core/RawHID.h +++ b/avr/cores/hid/USB-Core/RawHID.h @@ -33,9 +33,9 @@ THE SOFTWARE. typedef union{ // a RAWHID_TX_SIZE byte buffer for rx or tx -uint8_t whole8[RAWHID_TX_SIZE]; -uint16_t whole16[RAWHID_TX_SIZE / 2]; -uint32_t whole32[RAWHID_TX_SIZE / 4]; +uint8_t whole8[]; +uint16_t whole16[]; +uint32_t whole32[]; uint8_t buff[RAWHID_TX_SIZE]; } HID_RawKeyboardReport_Data_t; diff --git a/avr/cores/hid/USB-Core/System.h b/avr/cores/hid/USB-Core/System.h index 5c0a2d0..c8f3a8e 100644 --- a/avr/cores/hid/USB-Core/System.h +++ b/avr/cores/hid/USB-Core/System.h @@ -37,7 +37,7 @@ THE SOFTWARE. typedef union{ // every usable system control key possible - uint8_t whole8[1]; + uint8_t whole8[]; uint8_t key; } HID_SystemControlReport_Data_t; diff --git a/avr/libraries/HIDBridge/HIDBridge.cpp b/avr/libraries/HIDBridge/HIDBridge.cpp index ee9a237..6f03476 100644 --- a/avr/libraries/HIDBridge/HIDBridge.cpp +++ b/avr/libraries/HIDBridge/HIDBridge.cpp @@ -43,64 +43,107 @@ void HIDBridge_::err(uint8_t error) } -void HIDBridge_::task(void) +void HIDBridge_::readSerial(void) { static NHP_Read_Data_t n = { 0 }; uint8_t error = 0x00; + // read as long as the Serial is available + // but do not block forever + rx_buffer_index_t i = 0; //TODO availabel -> read -1 while (Serial.available()) { - if (NHPread(Serial.read(), &n)) { + // read in new Serial byte and process with NHP protocol + uint8_t b = Serial.read(); + bool newInput = NHPread(b, &n); + + // proceed new valid NHP input + if (newInput) { if (n.mode == NHP_ADDRESS) { switch (n.address) { - case 0: // received a control address command - if (n.data == 0) { + case HIDBRIDGE_ADDRESS_CONTROL: + // acknowledge/request + if (n.data == HIDBRIDGE_CONTROL_ISREADY) isReady = true; - return; - } - else - error = 3; - break; - default: - error = 1; - break; + // pause + else if (n.data == HIDBRIDGE_CONTROL_NOTREADY) + isReady = false; + + // not + else + err(HID_BRIDGE_ERR_CONTROL); + + break; + // received HID out report TODO + default: + err(HID_BRIDGE_ERR_ADDRESS); + break; } } - else if (n.mode == NHP_COMMAND) { - + // received HID out report TODO + else if (n.mode == NHP_COMMAND) { + error = 4; } - else { - error = 2; - } - } + + // NHP reading error + else if (n.errorLevel) { + error = 2; + } + + // do not block forever + if (++i >= SERIAL_RX_BUFFER_SIZE) + break; } - if (error) + if (error){ err(error); + isReady = false; // revert + } } -bool HIDBridge_::ready(void) + +bool HIDBridge_::waitForReady(void) { - //if (!hidReady) { + + uint32_t currentMillis = millis(); + do{ + // check for new state information + // maybe the host sended a pause signal + readSerial(); + + // check for timeout //TODO move 1 up? + if ((millis() - currentMillis) > HIDBRIDGE_TX_TIMEOUT) { + err(1); + return false; + } + } + // try to wait for a new request/acknowledge + while (!isReady) + + return isReady; + + //if (!isReady) { //TODO remove? // // try to wait for a new request/acknowledge // uint32_t currentMillis = millis(); - // while (!hidReady) { - // readHIDReady(); - // if ((millis() - currentMillis) > 1000) { - // errorHID(0); - // return; + // while (!isReady) { + // readSerial(); + // // check for timeout //TODO move 1 up? + // if ((millis() - currentMillis) > HIDBRIDGE_TX_TIMEOUT) { + // err(1); + // return false; // } // } //} + //return true; } // overwrites the HID_SendReport function which is empty/not used on a 328/2560 void HID_SendReport(uint8_t reportID, const void* data, int len) { - // check if we got a request/acknowledge - if (!HIDBridge.ready()){ + // check the latest request/acknowledge, pause, error + if (!HIDBridge.waitForReady()){ HIDBridge.err(0); return; } @@ -118,4 +161,7 @@ void HID_SendReport(uint8_t reportID, const void* data, int len) // end transfer with zero command Serial.write(NHPwriteCommand(0)); + + // need a request/acknowledge next time again + HIDBridge.isReady = false; } \ No newline at end of file diff --git a/avr/libraries/HIDBridge/HIDBridge.h b/avr/libraries/HIDBridge/HIDBridge.h index 9874342..bdf1ba4 100644 --- a/avr/libraries/HIDBridge/HIDBridge.h +++ b/avr/libraries/HIDBridge/HIDBridge.h @@ -36,6 +36,20 @@ THE SOFTWARE. // Definitions //================================================================================ +#define HIDBRIDGE_TX_TIMEOUT 1000 + +#define HIDBRIDGE_ADDRESS_CONTROL 0 + +#define HIDBRIDGE_CONTROL_ISREADY 0 +#define HIDBRIDGE_CONTROL_NOTREADY 1 + +#define HID_BRIDGE_ERR_TIMEOUT 0 +#define HID_BRIDGE_ERR_NHP_ERR 1 +#define HID_BRIDGE_ERR_COMMAND 2 +#define HID_BRIDGE_ERR_ADDRESS 3 +#define HID_BRIDGE_ERR_CONTROL 4 + + //================================================================================ // HIDBridge //================================================================================ @@ -46,8 +60,8 @@ public: HIDBridge_(void); bool begin(void); - - bool ready(void); + void readSerial(void); + bool waitForReady(void); bool isReady; void task(void); void err(uint8_t error);