From 2f0f5fec73f8d6690e183e25e58891f1ed465d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 24 Aug 2023 15:21:09 +0200 Subject: [PATCH] USB Host support stub. Doesn't work. --- arch/esp32/esp32c3.ini | 3 + arch/esp32/esp32s2.ini | 2 + arch/esp32/esp32s3.ini | 3 + src/input/kbUsbBase.cpp | 72 +++++++++++++++++++++ src/input/kbUsbBase.h | 21 ++++++ src/input/kbUsbImpl.cpp | 15 +++++ src/input/kbUsbImpl.h | 19 ++++++ src/modules/Modules.cpp | 5 ++ variants/bpi_picow_esp32_s3/platformio.ini | 4 +- variants/m5stack-stamp-c3/platformio.ini | 2 +- variants/my_esp32s3_diy_eink/platformio.ini | 6 +- variants/my_esp32s3_diy_oled/platformio.ini | 6 +- variants/tlora_t3s3_v1/platformio.ini | 4 +- 13 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 src/input/kbUsbBase.cpp create mode 100644 src/input/kbUsbBase.h create mode 100644 src/input/kbUsbImpl.cpp create mode 100644 src/input/kbUsbImpl.h diff --git a/arch/esp32/esp32c3.ini b/arch/esp32/esp32c3.ini index 619fdb28aa..295c9c2ce0 100644 --- a/arch/esp32/esp32c3.ini +++ b/arch/esp32/esp32c3.ini @@ -3,3 +3,6 @@ extends = esp32_base monitor_speed = 115200 monitor_filters = esp32_c3_exception_decoder + +lib_deps = ${esp32_base.lib_deps} + tanakamasayuki/EspUsbHost@^1.0.2 \ No newline at end of file diff --git a/arch/esp32/esp32s2.ini b/arch/esp32/esp32s2.ini index 3bde3465a1..0b196652ab 100644 --- a/arch/esp32/esp32s2.ini +++ b/arch/esp32/esp32s2.ini @@ -14,3 +14,5 @@ lib_ignore = ${esp32_base.lib_ignore} NimBLE-Arduino +lib_deps = ${esp32_base.lib_deps} + tanakamasayuki/EspUsbHost@^1.0.2 \ No newline at end of file diff --git a/arch/esp32/esp32s3.ini b/arch/esp32/esp32s3.ini index 6a1bdd3fd5..92382d99fd 100644 --- a/arch/esp32/esp32s3.ini +++ b/arch/esp32/esp32s3.ini @@ -3,3 +3,6 @@ extends = esp32_base monitor_speed = 115200 +lib_deps = ${esp32_base.lib_deps} + tanakamasayuki/EspUsbHost@^1.0.2 + diff --git a/src/input/kbUsbBase.cpp b/src/input/kbUsbBase.cpp new file mode 100644 index 0000000000..7c0de4a1d1 --- /dev/null +++ b/src/input/kbUsbBase.cpp @@ -0,0 +1,72 @@ +#include "kbUsbBase.h" +#include "configuration.h" + +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 + +KbUsbBase::KbUsbBase(const char *name) : concurrency::OSThread(name) +{ + this->_originName = name; +} + +int32_t KbUsbBase::runOnce() +{ + if (firstTime) { + // This is the first time the OSThread library has called this function, so init the USB HID routines + begin(); + firstTime = 0; + } else { + + task(); + } + return 100; +} + +void KbUsbBase::onKeyboardKey(uint8_t ascii, uint8_t keycode, uint8_t modifier) +{ + + if (ascii != 0) { + LOG_DEBUG("Key 0x%x Code 0x%x Mod 0x%x pressed\n", ascii, keycode, modifier); + // reset shift now that we have a keypress + InputEvent e; + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + e.source = this->_originName; + switch (ascii) { + case 0x1b: // ESC + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; + break; + case 0x08: // Back + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_BACK; + e.kbchar = ascii; + break; + case 0xb5: // Up + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_UP; + break; + case 0xb6: // Down + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; + break; + case 0xb4: // Left + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; + e.kbchar = ascii; + break; + case 0xb7: // Right + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; + e.kbchar = ascii; + break; + case 0x0d: // Enter + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; + break; + case 0x00: // nopress + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + break; + default: // all other keys + e.inputEvent = ANYKEY; + e.kbchar = ascii; + break; + } + if (e.inputEvent != meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { + this->notifyObservers(&e); + } + } +} + +#endif \ No newline at end of file diff --git a/src/input/kbUsbBase.h b/src/input/kbUsbBase.h new file mode 100644 index 0000000000..ee7ca5e72f --- /dev/null +++ b/src/input/kbUsbBase.h @@ -0,0 +1,21 @@ +#pragma once + +#include "InputBroker.h" +#include "concurrency/OSThread.h" +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 +#include "EspUsbHost.h" + +class KbUsbBase : public Observable, public concurrency::OSThread, public EspUsbHost +{ + public: + explicit KbUsbBase(const char *name); + + protected: + virtual int32_t runOnce() override; + + private: + void onKeyboardKey(uint8_t ascii, uint8_t keycode, uint8_t modifier); + const char *_originName; + bool firstTime = 1; +}; +#endif \ No newline at end of file diff --git a/src/input/kbUsbImpl.cpp b/src/input/kbUsbImpl.cpp new file mode 100644 index 0000000000..a96997c90c --- /dev/null +++ b/src/input/kbUsbImpl.cpp @@ -0,0 +1,15 @@ +#include "KbUsbImpl.h" +#include "InputBroker.h" + +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 + +KbUsbImpl *kbUsbImpl; + +KbUsbImpl::KbUsbImpl() : KbUsbBase("usbKB") {} + +void KbUsbImpl::init() +{ + inputBroker->registerSource(this); +} + +#endif // INPUTBROKER_MATRIX_TYPE \ No newline at end of file diff --git a/src/input/kbUsbImpl.h b/src/input/kbUsbImpl.h new file mode 100644 index 0000000000..42028e184f --- /dev/null +++ b/src/input/kbUsbImpl.h @@ -0,0 +1,19 @@ +#pragma once +#include "kbUsbBase.h" +#include "main.h" + +/** + * @brief The idea behind this class to have static methods for the event handlers. + * Check attachInterrupt() at RotaryEncoderInteruptBase.cpp + * Technically you can have as many rotary encoders hardver attached + * to your device as you wish, but you always need to have separate event + * handlers, thus you need to have a RotaryEncoderInterrupt implementation. + */ +class KbUsbImpl : public KbUsbBase +{ + public: + KbUsbImpl(); + void init(); +}; + +extern KbUsbImpl *kbUsbImpl; \ No newline at end of file diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index 526a1c7d8c..914261525e 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -5,6 +5,7 @@ #include "input/UpDownInterruptImpl1.h" #include "input/cardKbI2cImpl.h" #include "input/kbMatrixImpl.h" +#include "input/kbUsbImpl.h" #include "modules/AdminModule.h" #include "modules/CannedMessageModule.h" #include "modules/DetectionSensorModule.h" @@ -78,6 +79,10 @@ void setupModules() kbMatrixImpl = new KbMatrixImpl(); kbMatrixImpl->init(); #endif // INPUTBROKER_MATRIX_TYPE +#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 + kbUsbImpl = new KbUsbImpl(); + kbUsbImpl->init(); +#endif // INPUTBROKER_MATRIX_TYPE #endif // HAS_BUTTON #if HAS_TRACKBALL trackballInterruptImpl1 = new TrackballInterruptImpl1(); diff --git a/variants/bpi_picow_esp32_s3/platformio.ini b/variants/bpi_picow_esp32_s3/platformio.ini index 7e94cc97e2..992e14e150 100644 --- a/variants/bpi_picow_esp32_s3/platformio.ini +++ b/variants/bpi_picow_esp32_s3/platformio.ini @@ -8,7 +8,7 @@ board_level = extra upload_protocol = esptool ;upload_port = /dev/ttyACM2 lib_deps = - ${esp32_base.lib_deps} + ${esp32s3_base.lib_deps} caveman99/ESP32 Codec2@^1.0.1 build_flags = - ${esp32_base.build_flags} -D PRIVATE_HW -I variants/bpi_picow_esp32_s3 \ No newline at end of file + ${esp32s3_base.build_flags} -D PRIVATE_HW -I variants/bpi_picow_esp32_s3 \ No newline at end of file diff --git a/variants/m5stack-stamp-c3/platformio.ini b/variants/m5stack-stamp-c3/platformio.ini index bab65b6213..a48d5cc263 100644 --- a/variants/m5stack-stamp-c3/platformio.ini +++ b/variants/m5stack-stamp-c3/platformio.ini @@ -3,7 +3,7 @@ extends = esp32c3_base board = esp32-c3-devkitm-1 board_level = extra build_flags = - ${esp32_base.build_flags} + ${esp32c3_base.build_flags} -D PRIVATE_HW -I variants/m5stack-stamp-c3 monitor_speed = 115200 diff --git a/variants/my_esp32s3_diy_eink/platformio.ini b/variants/my_esp32s3_diy_eink/platformio.ini index d3c55afda1..2eaf9f1ae6 100644 --- a/variants/my_esp32s3_diy_eink/platformio.ini +++ b/variants/my_esp32s3_diy_eink/platformio.ini @@ -11,13 +11,13 @@ upload_speed = 921600 platform_packages = tool-esptoolpy@^1.40500.0 lib_deps = - ${esp32_base.lib_deps} + ${esp32s3_base.lib_deps} zinggjm/GxEPD2@^1.5.1 adafruit/Adafruit NeoPixel@^1.10.7 build_unflags = -DARDUINO_USB_MODE=1 build_flags = - ;${esp32_base.build_flags} -D MY_ESP32S3_DIY -I variants/my_esp32s3_diy_eink - ${esp32_base.build_flags} -D PRIVATE_HW -I variants/my_esp32s3_diy_eink + ;${esp32s3_base.build_flags} -D MY_ESP32S3_DIY -I variants/my_esp32s3_diy_eink + ${esp32s3_base.build_flags} -D PRIVATE_HW -I variants/my_esp32s3_diy_eink -Dmy -DTECHO_DISPLAY_MODEL=GxEPD2_290_T5D -DEPD_HEIGHT=128 diff --git a/variants/my_esp32s3_diy_oled/platformio.ini b/variants/my_esp32s3_diy_oled/platformio.ini index 9b8b09f7f0..ad4bc6b633 100644 --- a/variants/my_esp32s3_diy_oled/platformio.ini +++ b/variants/my_esp32s3_diy_oled/platformio.ini @@ -11,12 +11,12 @@ upload_speed = 921600 platform_packages = tool-esptoolpy@^1.40500.0 lib_deps = - ${esp32_base.lib_deps} + ${esp32s3_base.lib_deps} adafruit/Adafruit NeoPixel@^1.10.7 build_unflags = -DARDUINO_USB_MODE=1 build_flags = - ;${esp32_base.build_flags} -D MY_ESP32S3_DIY -I variants/my_esp32s3_diy_oled - ${esp32_base.build_flags} -D PRIVATE_HW -I variants/my_esp32s3_diy_oled + ;${esp32s3_base.build_flags} -D MY_ESP32S3_DIY -I variants/my_esp32s3_diy_oled + ${esp32s3_base.build_flags} -D PRIVATE_HW -I variants/my_esp32s3_diy_oled -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue -DARDUINO_USB_MODE=0 diff --git a/variants/tlora_t3s3_v1/platformio.ini b/variants/tlora_t3s3_v1/platformio.ini index fd3d393d91..c848177ee3 100644 --- a/variants/tlora_t3s3_v1/platformio.ini +++ b/variants/tlora_t3s3_v1/platformio.ini @@ -4,5 +4,5 @@ board = tlora-t3s3-v1 upload_protocol = esp-builtin build_flags = - ${esp32_base.build_flags} -D TLORA_T3S3_V1 -I variants/tlora_t3s3_v1 - -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely. \ No newline at end of file + ${esp32s3_base.build_flags} -D TLORA_T3S3_V1 -I variants/tlora_t3s3_v1 + -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely.