diff options
-rw-r--r-- | hid/src/main.cpp | 38 | ||||
-rw-r--r-- | hid/src/proto.h | 84 |
2 files changed, 72 insertions, 50 deletions
diff --git a/hid/src/main.cpp b/hid/src/main.cpp index f87c719f..8b48f82a 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -61,11 +61,11 @@ // ----------------------------------------------------------------------------- uint8_t cmdPong(const uint8_t *_=NULL) { // 0 bytes return ( - ((uint8_t)PROTO_RESP_PONG_PREFIX) - | hid_kbd.getLedsAs(PROTO_RESP_PONG_CAPS, PROTO_RESP_PONG_SCROLL, PROTO_RESP_PONG_NUM) - | (hid_kbd.isOnline() ? 0 : PROTO_RESP_PONG_KEYBOARD_OFFLINE) + PROTO::PONG::PREFIX + | hid_kbd.getLedsAs(PROTO::PONG::CAPS, PROTO::PONG::SCROLL, PROTO::PONG::NUM) + | (hid_kbd.isOnline() ? 0 : PROTO::PONG::KEYBOARD_OFFLINE) # ifdef HID_USB_MOUSE - | (hid_mouse.isOnline() ? 0 : PROTO_RESP_PONG_MOUSE_OFFLINE) + | (hid_mouse.isOnline() ? 0 : PROTO::PONG::MOUSE_OFFLINE) # endif ); } @@ -91,8 +91,8 @@ uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 2 bytes uint8_t extra_state = buffer[1]; # define MOUSE_PAIR(_state, _button) \ - _state & PROTO_CMD_MOUSE_BUTTON_##_button##_SELECT, \ - _state & PROTO_CMD_MOUSE_BUTTON_##_button##_STATE + _state & PROTO::CMD::MOUSE::_button::SELECT, \ + _state & PROTO::CMD::MOUSE::_button::STATE hid_mouse.sendButtons( MOUSE_PAIR(main_state, LEFT), MOUSE_PAIR(main_state, RIGHT), @@ -134,18 +134,18 @@ uint8_t handleCmdBuffer(const uint8_t *buffer) { // 8 bytes if (protoCrc16(buffer, 6) == crc) { # define HANDLE(_handler) { return _handler(buffer + 2); } switch (buffer[1]) { - case PROTO_CMD_RESET_HID: HANDLE(cmdResetHid); - case PROTO_CMD_KEY_EVENT: HANDLE(cmdKeyEvent); - case PROTO_CMD_MOUSE_BUTTON_EVENT: HANDLE(cmdMouseButtonEvent); - case PROTO_CMD_MOUSE_MOVE_EVENT: HANDLE(cmdMouseMoveEvent); - case PROTO_CMD_MOUSE_WHEEL_EVENT: HANDLE(cmdMouseWheelEvent); - case PROTO_CMD_PING: HANDLE(cmdPong); - case PROTO_CMD_REPEAT: return 0; - default: return PROTO_RESP_INVALID_ERROR; + case PROTO::CMD::RESET_HID: HANDLE(cmdResetHid); + case PROTO::CMD::KEYBOARD::KEY: HANDLE(cmdKeyEvent); + case PROTO::CMD::MOUSE::BUTTON: HANDLE(cmdMouseButtonEvent); + case PROTO::CMD::MOUSE::MOVE: HANDLE(cmdMouseMoveEvent); + case PROTO::CMD::MOUSE::WHEEL: HANDLE(cmdMouseWheelEvent); + case PROTO::CMD::PING: HANDLE(cmdPong); + case PROTO::CMD::REPEAT: return 0; + default: return PROTO::RESP::INVALID_ERROR; } # undef HANDLE } - return PROTO_RESP_CRC_ERROR; + return PROTO::RESP::CRC_ERROR; } @@ -182,7 +182,7 @@ ISR(SPI_STC_vect) { } } else { static bool receiving = false; - if (!receiving && in == PROTO_MAGIC) { + if (!receiving && in == PROTO::MAGIC) { receiving = true; } if (receiving && spi_in_index < 8) { @@ -200,7 +200,7 @@ ISR(SPI_STC_vect) { // ----------------------------------------------------------------------------- void sendCmdResponse(uint8_t code) { - static uint8_t prev_code = PROTO_RESP_NONE; + static uint8_t prev_code = PROTO::RESP::NONE; if (code == 0) { code = prev_code; // Repeat the last code } else { @@ -208,7 +208,7 @@ void sendCmdResponse(uint8_t code) { } uint8_t buffer[4]; - buffer[0] = PROTO_MAGIC; + buffer[0] = PROTO::MAGIC; buffer[1] = code; uint16_t crc = protoCrc16(buffer, 2); buffer[2] = (uint8_t)(crc >> 8); @@ -263,7 +263,7 @@ void loop() { (now >= last && now - last > CMD_SERIAL_TIMEOUT) || (now < last && ((unsigned long)-1) - last + now > CMD_SERIAL_TIMEOUT) ) { - sendCmdResponse(PROTO_RESP_TIMEOUT_ERROR); + sendCmdResponse(PROTO::RESP::TIMEOUT_ERROR); index = 0; } } diff --git a/hid/src/proto.h b/hid/src/proto.h index 617711dd..ca9b4f2c 100644 --- a/hid/src/proto.h +++ b/hid/src/proto.h @@ -23,41 +23,63 @@ #pragma once -#define PROTO_MAGIC 0x33 -#define PROTO_CRC_POLINOM 0xA001 +namespace PROTO { + const uint8_t MAGIC = 0x33; + const uint16_t CRC_POLINOM = 0xA001; -// #define PROTO_RESP_OK 0x20 // Legacy -#define PROTO_RESP_NONE 0x24 -#define PROTO_RESP_CRC_ERROR 0x40 -#define PROTO_RESP_INVALID_ERROR 0x45 -#define PROTO_RESP_TIMEOUT_ERROR 0x48 + namespace RESP { // Plain responses + // const uint8_t OK = 0x20; // Legacy + const uint8_t NONE = 0x24; + const uint8_t CRC_ERROR = 0x40; + const uint8_t INVALID_ERROR = 0x45; + const uint8_t TIMEOUT_ERROR = 0x48; + }; -#define PROTO_RESP_PONG_PREFIX 0x80 -#define PROTO_RESP_PONG_CAPS 0b00000001 -#define PROTO_RESP_PONG_SCROLL 0b00000010 -#define PROTO_RESP_PONG_NUM 0b00000100 -#define PROTO_RESP_PONG_KEYBOARD_OFFLINE 0b00001000 -#define PROTO_RESP_PONG_MOUSE_OFFLINE 0b00010000 + namespace PONG { // Complex response + const uint8_t PREFIX = 0x80; + const uint8_t CAPS = 0b00000001; + const uint8_t SCROLL = 0b00000010; + const uint8_t NUM = 0b00000100; + const uint8_t KEYBOARD_OFFLINE = 0b00001000; + const uint8_t MOUSE_OFFLINE = 0b00010000; + }; -#define PROTO_CMD_PING 0x01 -#define PROTO_CMD_REPEAT 0x02 -#define PROTO_CMD_RESET_HID 0x10 -#define PROTO_CMD_KEY_EVENT 0x11 -#define PROTO_CMD_MOUSE_BUTTON_EVENT 0x13 // Legacy sequence -#define PROTO_CMD_MOUSE_MOVE_EVENT 0x12 -#define PROTO_CMD_MOUSE_WHEEL_EVENT 0x14 + namespace CMD { + const uint8_t PING = 0x01; + const uint8_t REPEAT = 0x02; + const uint8_t RESET_HID = 0x10; -#define PROTO_CMD_MOUSE_BUTTON_LEFT_SELECT 0b10000000 -#define PROTO_CMD_MOUSE_BUTTON_LEFT_STATE 0b00001000 -#define PROTO_CMD_MOUSE_BUTTON_RIGHT_SELECT 0b01000000 -#define PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE 0b00000100 -#define PROTO_CMD_MOUSE_BUTTON_MIDDLE_SELECT 0b00100000 -#define PROTO_CMD_MOUSE_BUTTON_MIDDLE_STATE 0b00000010 + namespace KEYBOARD { + const uint8_t KEY = 0x11; + }; -#define PROTO_CMD_MOUSE_BUTTON_EXTRA_UP_SELECT 0b10000000 -#define PROTO_CMD_MOUSE_BUTTON_EXTRA_UP_STATE 0b00001000 -#define PROTO_CMD_MOUSE_BUTTON_EXTRA_DOWN_SELECT 0b01000000 -#define PROTO_CMD_MOUSE_BUTTON_EXTRA_DOWN_STATE 0b00000100 + namespace MOUSE { + const uint8_t MOVE = 0x12; + const uint8_t BUTTON = 0x13; + const uint8_t WHEEL = 0x14; + namespace LEFT { + const uint8_t SELECT = 0b10000000; + const uint8_t STATE = 0b00001000; + }; + namespace RIGHT { + const uint8_t SELECT = 0b01000000; + const uint8_t STATE = 0b00000100; + }; + namespace MIDDLE { + const uint8_t SELECT = 0b00100000; + const uint8_t STATE = 0b00000010; + }; + namespace EXTRA_UP { + const uint8_t SELECT = 0b10000000; + const uint8_t STATE = 0b00001000; + }; + namespace EXTRA_DOWN { + const uint8_t SELECT = 0b01000000; + const uint8_t STATE = 0b00000100; + }; + }; + }; +}; uint16_t protoCrc16(const uint8_t *buffer, unsigned length) { @@ -70,7 +92,7 @@ uint16_t protoCrc16(const uint8_t *buffer, unsigned length) { crc = crc >> 1; } else { crc = crc >> 1; - crc = crc ^ PROTO_CRC_POLINOM; + crc = crc ^ PROTO::CRC_POLINOM; } } } |