diff options
author | Florian Didron <fd@librem.one> | 2019-11-05 19:02:07 +0900 |
---|---|---|
committer | Florian Didron <fdidron@users.noreply.github.com> | 2019-12-06 08:20:51 +0900 |
commit | f3edef8c69384d92c026c4ffbcc167464d045c03 (patch) | |
tree | abae48d2dbbe0229902e88059ff87dd6f7f4b9f9 | |
parent | 40e9813ba2620b9b11811f3a2e5d5879a60aee83 (diff) |
feat: adds pairing key
-rw-r--r-- | common_features.mk | 6 | ||||
-rw-r--r-- | quantum/quantum.c | 11 | ||||
-rw-r--r-- | quantum/quantum_keycodes.h | 3 | ||||
-rw-r--r-- | tmk_core/common/webusb.c | 24 | ||||
-rw-r--r-- | tmk_core/common/webusb.h | 24 | ||||
-rw-r--r-- | tmk_core/protocol/chibios/usb_main.c | 10 | ||||
-rw-r--r-- | tmk_core/protocol/lufa/lufa.c | 11 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.c | 2 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.h | 2 | ||||
-rw-r--r-- | tmk_core/protocol/webusb_descriptor.h (renamed from tmk_core/protocol/webusb.h) | 4 |
10 files changed, 88 insertions, 9 deletions
diff --git a/common_features.mk b/common_features.mk index cfa0358e51..401c8f43aa 100644 --- a/common_features.mk +++ b/common_features.mk @@ -234,7 +234,7 @@ ifeq ($(strip $(BACKLIGHT_ENABLE)), yes) CIE1931_CURVE = yes endif - + COMMON_VPATH += $(QUANTUM_DIR)/backlight SRC += $(QUANTUM_DIR)/backlight/backlight.c OPT_DEFS += -DBACKLIGHT_ENABLE @@ -288,6 +288,10 @@ ifeq ($(strip $(USB_HID_ENABLE)), yes) include $(TMK_DIR)/protocol/usb_hid.mk endif +ifeq ($(strip $(WEBUSB_ENABLE)), yes) + SRC += $(TMK_DIR)/common/webusb.c +endif + ifeq ($(strip $(ENCODER_ENABLE)), yes) SRC += $(QUANTUM_DIR)/encoder.c OPT_DEFS += -DENCODER_ENABLE diff --git a/quantum/quantum.c b/quantum/quantum.c index a268e0dc03..27d08996be 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -57,6 +57,10 @@ # include "encoder.h" #endif +#ifdef WEBUSB_ENABLE +# include "webusb.h" +#endif + #ifdef AUDIO_ENABLE # ifndef GOODBYE_SONG # define GOODBYE_SONG SONG(GOODBYE_SOUND) @@ -713,6 +717,13 @@ bool process_record_quantum(keyrecord_t *record) { return false; } #endif +#ifdef WEBUSB_ENABLE + case WEBUSB_PAIR: + if (record->event.pressed) { + webusb_state.paired = true; + } + return false; +#endif } return process_action_kb(record); diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h index af984a7cd7..872aa89bc4 100644 --- a/quantum/quantum_keycodes.h +++ b/quantum/quantum_keycodes.h @@ -503,6 +503,9 @@ enum quantum_keycodes { MAGIC_UNSWAP_CTL_GUI, MAGIC_TOGGLE_CTL_GUI, +#ifdef WEBUSB_ENABLE + WEBUSB_PAIR, +#endif // always leave at the end SAFE_RANGE }; diff --git a/tmk_core/common/webusb.c b/tmk_core/common/webusb.c new file mode 100644 index 0000000000..5183d77543 --- /dev/null +++ b/tmk_core/common/webusb.c @@ -0,0 +1,24 @@ +#include "webusb.h" +#include "wait.h" + +webusb_state_t webusb_state = { + .paired = false, + .pairing = false, +}; + +void webusb_set_pairing_state() { + webusb_state.pairing = true; + uint8_t tick = 0; + do { + tick++; + wait_ms(1000); + //TODO Blink some leds + } while(webusb_state.paired == false && tick <= 30); + webusb_state.pairing = false; +} + +void webusb_error(uint8_t code) { + uint8_t buffer[1]; + buffer[0] = code; + webusb_send(buffer, 1); +} diff --git a/tmk_core/common/webusb.h b/tmk_core/common/webusb.h new file mode 100644 index 0000000000..35d9610fc9 --- /dev/null +++ b/tmk_core/common/webusb.h @@ -0,0 +1,24 @@ +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +void webusb_receive(uint8_t *data, uint8_t length); +void webusb_send(uint8_t *data, uint8_t length); +void webusb_error(uint8_t); +void webusb_set_pairing_state(void); + +typedef struct{ + bool paired; + bool pairing; +} webusb_state_t; + +extern webusb_state_t webusb_state; + +enum Webusb_Status_Code { + WEBUSB_STATUS_NOT_PAIRED = -1, + WEBUSB_STATUS_OK, + WEBUSB_STATUS_UNKNOWN_COMMAND, +}; + + diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index e2440601e3..8a249e19e3 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -37,6 +37,9 @@ extern keymap_config_t keymap_config; #endif +#ifdef WEBUSB_ENABLE +#include "webusb.h" +#endif /* --------------------------------------------------------- * Global interface variables and declarations * --------------------------------------------------------- @@ -880,7 +883,12 @@ void webusb_task(void) { do { size_t size = chnReadTimeout(&drivers.webusb_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); if (size > 0) { - webusb_receive(buffer, size); + if(webusb_state.paired == true) { + webusb_receive(buffer, size); + } + else { + webusb_error(WEBUSB_STATUS_NOT_PAIRED); + } } } while (size > 0); } diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 998db1c6d8..c68c3c2e5b 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -90,6 +90,10 @@ extern keymap_config_t keymap_config; # include "raw_hid.h" #endif +#ifdef WEBUSB_ENABLE +#include "webusb.h" +#endif + uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ uint8_t keyboard_protocol = 1; @@ -307,7 +311,12 @@ static void webusb_task(void) { Endpoint_ClearOUT(); if (data_read) { - webusb_receive(data, sizeof(data)); + if(webusb_state.paired == true) { + webusb_receive(data, sizeof(data)); + } + else { + webusb_error(WEBUSB_STATUS_NOT_PAIRED); + } } } } diff --git a/tmk_core/protocol/usb_descriptor.c b/tmk_core/protocol/usb_descriptor.c index dff3d48bad..ac35a28772 100644 --- a/tmk_core/protocol/usb_descriptor.c +++ b/tmk_core/protocol/usb_descriptor.c @@ -40,7 +40,7 @@ #include "report.h" #include "usb_descriptor.h" #ifdef WEBUSB_ENABLE -#include "webusb.h" +#include "webusb_descriptor.h" #endif /* * HID report descriptors diff --git a/tmk_core/protocol/usb_descriptor.h b/tmk_core/protocol/usb_descriptor.h index 224068b674..8ee576cc0d 100644 --- a/tmk_core/protocol/usb_descriptor.h +++ b/tmk_core/protocol/usb_descriptor.h @@ -49,7 +49,7 @@ # include "hal.h" #endif #ifdef WEBUSB_ENABLE -#include "webusb.h" +#include "webusb_descriptor.h" #endif /* diff --git a/tmk_core/protocol/webusb.h b/tmk_core/protocol/webusb_descriptor.h index ff3b8f65b2..a2385b8c05 100644 --- a/tmk_core/protocol/webusb.h +++ b/tmk_core/protocol/webusb_descriptor.h @@ -1,9 +1,5 @@ #pragma once -void webusb_receive(uint8_t *data, uint8_t length); - -void webusb_send(uint8_t *data, uint8_t length); - #ifndef WORD_TO_BYTES_LE # define WORD_TO_BYTES_LE(n) n % 256, (n / 256) % 256 #endif |