From 76b95ddfa8d60de4d29e270cbc74cfc8b5144538 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sat, 29 Sep 2018 07:33:27 +0300 Subject: improved hid protocol --- hid/Makefile | 6 +++--- hid/src/inline.h | 1 + hid/src/main.cpp | 21 ++++++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) (limited to 'hid') diff --git a/hid/Makefile b/hid/Makefile index 0d94b010..957c3a11 100644 --- a/hid/Makefile +++ b/hid/Makefile @@ -1,7 +1,4 @@ all: - @ cat Makefile - -build: platformio run update: @@ -16,3 +13,6 @@ serial: clean: rm -rf .pioenvs .piolibdeps + +help: + @ cat Makefile diff --git a/hid/src/inline.h b/hid/src/inline.h index 5b642a83..337aa9b9 100644 --- a/hid/src/inline.h +++ b/hid/src/inline.h @@ -1,3 +1,4 @@ #pragma once + #define INLINE inline __attribute__((always_inline)) diff --git a/hid/src/main.cpp b/hid/src/main.cpp index 7bfced13..9fad576c 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -4,14 +4,17 @@ #include "inline.h" #include "keymap.h" -#define CMD_SERIAL Serial1 -#define CMD_SERIAL_SPEED 115200 + +#define CMD_SERIAL Serial1 +#define CMD_SERIAL_SPEED 115200 #define CMD_MOUSE_LEFT 0b10000000 #define CMD_MOUSE_LEFT_STATE 0b00001000 #define CMD_MOUSE_RIGHT 0b01000000 #define CMD_MOUSE_RIGHT_STATE 0b00000100 +#define REPORT_INTERVAL 100 + // ----------------------------------------------------------------------------- INLINE void cmdResetHid() { // 0 bytes @@ -80,10 +83,12 @@ void setup() { CMD_SERIAL.begin(CMD_SERIAL_SPEED); BootKeyboard.begin(); SingleAbsoluteMouse.begin(); - CMD_SERIAL.write(0); } void loop() { + static unsigned long last_report = 0; + bool cmd_processed = false; + if (CMD_SERIAL.available() >= 5) { switch ((uint8_t)CMD_SERIAL.read()) { case 0: cmdResetHid(); break; @@ -93,6 +98,16 @@ void loop() { case 4: cmdMouseWheelEvent(); break; default: break; } + cmd_processed = true; + } + + unsigned long now = millis(); + if ( + cmd_processed + || (now >= last_report && now - last_report >= REPORT_INTERVAL) + || (now < last_report && ((unsigned long) -1) - last_report + now >= REPORT_INTERVAL) + ) { CMD_SERIAL.write(0); + last_report = now; } } -- cgit v1.2.3