From 6d2f3ded1019b5745c63e65b044e754ae521c5ef Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 12 Apr 2022 11:04:35 +0200 Subject: [PATCH 01/13] Fix text. --- Sming/Arch/Esp32/Components/esp_wifi/README.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sming/Arch/Esp32/Components/esp_wifi/README.rst b/Sming/Arch/Esp32/Components/esp_wifi/README.rst index 1e6d05f976..2a401d40ff 100644 --- a/Sming/Arch/Esp32/Components/esp_wifi/README.rst +++ b/Sming/Arch/Esp32/Components/esp_wifi/README.rst @@ -1,5 +1,4 @@ -Esp8266 WiFi -============ +Esp32 WiFi +========== -All related libraries for WiFi support. Definitions are provided in the -Espressif Non-OS SDK. +All related libraries for WiFi support. From cb8c3129957eb171e0bdba4a13ce688e23b76865 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 12 Apr 2022 11:05:52 +0200 Subject: [PATCH 02/13] Enabled Bluetooth when network is enabled. --- .../Arch/Esp32/Components/esp32/component.mk | 25 +++++++++++++++++++ .../Esp32/Components/esp32/sdk/config/common | 8 ++++++ 2 files changed, 33 insertions(+) diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index affffe905e..012bd401c0 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -37,6 +37,29 @@ SDK_INCDIRS := \ app_update/include \ bootloader_support/include \ bootloader_support/include_bootloader \ + bt/include/$(ESP_VARIANT)/include \ + bt/common/api/include/api \ + bt/common/btc/profile/esp/blufi/include \ + bt/common/btc/profile/esp/include \ + bt/common/osi/include \ + bt/host/nimble/nimble/nimble/include \ + bt/host/nimble/nimble/nimble/host/include \ + bt/host/nimble/nimble/porting/nimble/include \ + bt/host/nimble/nimble/porting/npl/freertos/include \ + bt/host/nimble/nimble/nimble/host/services/ans/include \ + bt/host/nimble/nimble/nimble/host/services/bas/include \ + bt/host/nimble/nimble/nimble/host/services/dis/include \ + bt/host/nimble/nimble/nimble/host/services/gap/include \ + bt/host/nimble/nimble/nimble/host/services/gatt/include \ + bt/host/nimble/nimble/nimble/host/services/ias/include \ + bt/host/nimble/nimble/nimble/host/services/ipss/include \ + bt/host/nimble/nimble/nimble/host/services/lls/include \ + bt/host/nimble/nimble/nimble/host/services/tps/include \ + bt/host/nimble/nimble/nimble/host/util/include \ + bt/host/nimble/nimble/nimble/host/store/ram/include \ + bt/host/nimble/nimble/nimble/host/store/config/include \ + bt/host/nimble/esp-hci/include \ + bt/host/nimble/port/include \ driver/$(ESP_VARIANT)/include \ driver/include \ esp_pm/include \ @@ -118,6 +141,7 @@ SDK_COMPONENTS := \ ifneq ($(DISABLE_NETWORK),1) SDK_COMPONENTS += \ + bt \ esp_wifi \ esp_eth \ lwip \ @@ -142,6 +166,7 @@ SDK_COMPONENTS += xtensa endif SDK_ESP_WIFI_LIBS := \ + bt \ coexist \ core \ espnow \ diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/config/common b/Sming/Arch/Esp32/Components/esp32/sdk/config/common index 6bb6d89c55..7c75c83fcb 100644 --- a/Sming/Arch/Esp32/Components/esp32/sdk/config/common +++ b/Sming/Arch/Esp32/Components/esp32/sdk/config/common @@ -23,6 +23,14 @@ CONFIG_ETH_USE_SPI_ETHERNET=y CONFIG_ETH_SPI_ETHERNET_W5500=y CONFIG_ETH_SPI_ETHERNET_DM9051=y +# Bluetooth +CONFIG_BT_ENABLED=y +CONFIG_BT_BLUEDROID_ENABLED=n +CONFIG_BT_NIMBLE_ENABLED=y +CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n +CONFIG_BLE_MESH=n +CONFIG_BT_NIMBLE_MESH=n + # Mandatory Sming framework changes CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=16384 CONFIG_ESP_TASK_WDT_TIMEOUT_S=8 From 51934212ea5e8617ea04a6961a62bb03ef4807b8 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 12 Apr 2022 14:59:09 +0200 Subject: [PATCH 03/13] Initial BLE Keyboard and Gamepad libraries and samples. --- .gitmodules | 16 +++++- .../Arch/Esp32/Components/esp32/component.mk | 3 +- .../Esp32/Components/sming-arch/component.mk | 3 +- Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad | 1 + Sming/Libraries/BLEGamepad/component.mk | 9 +++ .../BLEGamepad/samples/Bluetooth_Gamepad/.cs | 0 .../samples/Bluetooth_Gamepad/Makefile | 9 +++ .../samples/Bluetooth_Gamepad/README.rst | 18 ++++++ .../Bluetooth_Gamepad/app/application.cpp | 57 +++++++++++++++++++ .../samples/Bluetooth_Gamepad/component.mk | 1 + .../Libraries/BLEKeyboard/ESP32-BLE-Keyboard | 1 + .../BLEKeyboard/ESP32-BLE-Keyboard.patch | 16 ++++++ Sming/Libraries/BLEKeyboard/component.mk | 9 +++ .../samples/Bluetooth_Keyboard/.cs | 0 .../samples/Bluetooth_Keyboard/Makefile | 9 +++ .../samples/Bluetooth_Keyboard/README.rst | 18 ++++++ .../Bluetooth_Keyboard/app/application.cpp | 48 ++++++++++++++++ .../samples/Bluetooth_Keyboard/component.mk | 1 + Sming/Libraries/NimBLE/component.mk | 5 ++ Sming/Libraries/NimBLE/esp-nimble-cpp | 1 + .../NimBLE/samples/Bluetooth_Client/Makefile | 9 +++ .../samples/Bluetooth_Client/README.rst | 18 ++++++ .../Bluetooth_Client/app/application.cpp | 7 +++ .../samples/Bluetooth_Client/component.mk | 1 + 24 files changed, 256 insertions(+), 4 deletions(-) create mode 160000 Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad create mode 100644 Sming/Libraries/BLEGamepad/component.mk create mode 100644 Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/.cs create mode 100644 Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/Makefile create mode 100644 Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst create mode 100644 Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp create mode 100644 Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk create mode 160000 Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard create mode 100644 Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard.patch create mode 100644 Sming/Libraries/BLEKeyboard/component.mk create mode 100644 Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/.cs create mode 100644 Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/Makefile create mode 100644 Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst create mode 100644 Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp create mode 100644 Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk create mode 100644 Sming/Libraries/NimBLE/component.mk create mode 160000 Sming/Libraries/NimBLE/esp-nimble-cpp create mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile create mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst create mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp create mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk diff --git a/.gitmodules b/.gitmodules index 7607e1ac02..10c75ea190 100644 --- a/.gitmodules +++ b/.gitmodules @@ -377,8 +377,18 @@ # # Esp32 libraries # - -### NONE ### +[submodule "Libraries.ESP32-BLE-Gamepad"] + path = Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad + url = https://github.com/lemmingDev/ESP32-BLE-Gamepad.git + ignore = dirty +[submodule "Libraries.ESP32-BLE-Keyboard"] + path = Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard + url = https://github.com/T-vK/ESP32-BLE-Keyboard.git + ignore = dirty +[submodule "Libraries.esp-nimble-cpp"] + path = Sming/Libraries/NimBLE/esp-nimble-cpp + url = https://github.com/h2zero/esp-nimble-cpp.git + ignore = dirty # @@ -393,3 +403,5 @@ # END OF .gitmodules # ################################################################################################# + + diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index 012bd401c0..72173263a2 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -20,7 +20,8 @@ SDK_LIBDIRS := \ xtensa/$(ESP_VARIANT)/ \ hal/$(ESP_VARIANT)/ \ $(ESP_VARIANT)/ld \ - esp_rom/$(ESP_VARIANT)/ld + esp_rom/$(ESP_VARIANT)/ld \ + bt/controller/lib_esp32/$(ESP_VARIANT)/ ESP32_COMPONENT_PATH := $(COMPONENT_PATH) SDK_DEFAULT_PATH := $(ESP32_COMPONENT_PATH)/sdk diff --git a/Sming/Arch/Esp32/Components/sming-arch/component.mk b/Sming/Arch/Esp32/Components/sming-arch/component.mk index 189237cbae..69d3a3cb76 100644 --- a/Sming/Arch/Esp32/Components/sming-arch/component.mk +++ b/Sming/Arch/Esp32/Components/sming-arch/component.mk @@ -18,7 +18,8 @@ COMPONENT_DEPENDS := \ heap \ esp32 \ gdbstub \ - esptool + esptool \ + bt # ELF and BIN files DEBUG_VARS += TARGET_BIN TARGET_OUT diff --git a/Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad b/Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad new file mode 160000 index 0000000000..544dc65029 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/ESP32-BLE-Gamepad @@ -0,0 +1 @@ +Subproject commit 544dc650296f4a4b9a22995b996458c2c66fa4e5 diff --git a/Sming/Libraries/BLEGamepad/component.mk b/Sming/Libraries/BLEGamepad/component.mk new file mode 100644 index 0000000000..b04c1ac3f3 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/component.mk @@ -0,0 +1,9 @@ +COMPONENT_SUBMODULES := ESP32-BLE-Gamepad +COMPONENT_SOC := esp32 +COMPONENT_DEPENDS := NimBLE + +COMPONENT_SRCDIRS := ESP32-BLE-Gamepad +COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) + +COMPONENT_CPPFLAGS:= -DUSE_NIMBLE=1 +APP_CFLAGS += -DUSE_NIMBLE=1 \ No newline at end of file diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/.cs b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/Makefile b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/Makefile new file mode 100644 index 0000000000..ff51b6c3a7 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/Makefile @@ -0,0 +1,9 @@ +##################################################################### +#### Please don't change this file. Use component.mk instead #### +##################################################################### + +ifndef SMING_HOME +$(error SMING_HOME is not set: please configure it as an environment variable) +endif + +include $(SMING_HOME)/project.mk diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst new file mode 100644 index 0000000000..62967a3a06 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst @@ -0,0 +1,18 @@ +Basic Blink +=========== + +Simple blink example to confirm that the basic build system is working with your system. + +We use Timer instead of a loop because we want to allow WiFi communications to work in the background. +See :doc:`/information/multitasking`. + +The LED on many development boards is connected to GPIO2, so this is the default. + +If you get no response then check the documentation or schematic as your system +may differ and change the LED_PIN definition accordingly. + +For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. + + +.. image:: blink.jpg + :height: 192px diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp new file mode 100644 index 0000000000..ef9af4e170 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp @@ -0,0 +1,57 @@ +/* + * This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis + * + * Possible buttons are: + * BUTTON_1 through to BUTTON_16 + * (16 buttons supported by default. Library can be configured to support up to 128) + * + * Possible DPAD/HAT switch position values are: + * DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT + * (or HAT_CENTERED, HAT_UP etc) + * + * bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, Left/Right Triggers plus slider1 and slider2, and hat switch position as above: + * (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch position + ^ (1 hat switch (dpad) supported by default. Library can be configured to support up to 4) + * + * Library can also be configured to support up to 5 simulation controls (can be set with setSimulationControls) + * (rudder, throttle, accelerator, brake, steering), but they are not enabled by default. + */ + +#include +#include + +BleGamepad bleGamepad; + +Timer procTimer; + +void loop() +{ + if(bleGamepad.isConnected()) { + return; + } + + Serial.println("Press buttons 5 and 16. Move all enabled axes to max. Set DPAD (hat 1) to down right."); + bleGamepad.press(BUTTON_5); + bleGamepad.press(BUTTON_16); + bleGamepad.setAxes(32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, DPAD_DOWN_RIGHT); + // All axes, sliders, hats etc can also be set independently. See the IndividualAxes.ino example + delay(500); + + Serial.println("Release button 5. Move all axes to min. Set DPAD (hat 1) to centred."); + bleGamepad.release(BUTTON_5); + bleGamepad.setAxes(-32767, -32767, -32767, -32767, -32767, -32767, -32767, -32767, DPAD_CENTERED); + delay(500); +} + +void init() +{ + Serial.begin(COM_SPEED_SERIAL); + Serial.println("Starting BLE Keyboard sample!"); + bleGamepad.begin(); + // The default bleGamepad.begin() above is the same as bleGamepad.begin(16, 1, true, true, true, true, true, true, true, true, false, false, false, false, false); + // which enables a gamepad with 16 buttons, 1 hat switch, enabled x, y, z, rZ, rX, rY, slider 1, slider 2 and disabled rudder, throttle, accelerator, brake, steering + // Auto reporting is enabled by default. + // Use bleGamepad.setAutoReport(false); to disable auto reporting, and then use bleGamepad.sendReport(); as needed + + procTimer.initializeMs(500, loop).start(); +} diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk new file mode 100644 index 0000000000..721effd302 --- /dev/null +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk @@ -0,0 +1 @@ +COMPONENT_DEPENDS := BLEGamepad \ No newline at end of file diff --git a/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard b/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard new file mode 160000 index 0000000000..f8dd485211 --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard @@ -0,0 +1 @@ +Subproject commit f8dd4852113a722a6b8dc8af987e94cf84d73ad5 diff --git a/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard.patch b/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard.patch new file mode 100644 index 0000000000..2f7cee53ce --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/ESP32-BLE-Keyboard.patch @@ -0,0 +1,16 @@ +diff --git a/BleKeyboard.cpp b/BleKeyboard.cpp +index 0d043f4..3c2677c 100644 +--- a/BleKeyboard.cpp ++++ b/BleKeyboard.cpp +@@ -539,8 +539,8 @@ void BleKeyboard::delay_ms(uint64_t ms) { + if(ms){ + uint64_t e = (m + (ms * 1000)); + if(m > e){ //overflow +- while(esp_timer_get_time() > e) { } ++ while(uint64_t(esp_timer_get_time()) > e) { } + } +- while(esp_timer_get_time() < e) {} ++ while(uint64_t(esp_timer_get_time()) < e) {} + } + } +\ No newline at end of file diff --git a/Sming/Libraries/BLEKeyboard/component.mk b/Sming/Libraries/BLEKeyboard/component.mk new file mode 100644 index 0000000000..2c1a2872e8 --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/component.mk @@ -0,0 +1,9 @@ +COMPONENT_SUBMODULES := ESP32-BLE-Keyboard +COMPONENT_SOC := esp32 +COMPONENT_DEPENDS := NimBLE + +COMPONENT_SRCDIRS := ESP32-BLE-Keyboard +COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) + +COMPONENT_CPPFLAGS:= -DUSE_NIMBLE=1 +APP_CFLAGS += -DUSE_NIMBLE=1 \ No newline at end of file diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/.cs b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/Makefile b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/Makefile new file mode 100644 index 0000000000..ff51b6c3a7 --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/Makefile @@ -0,0 +1,9 @@ +##################################################################### +#### Please don't change this file. Use component.mk instead #### +##################################################################### + +ifndef SMING_HOME +$(error SMING_HOME is not set: please configure it as an environment variable) +endif + +include $(SMING_HOME)/project.mk diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst new file mode 100644 index 0000000000..62967a3a06 --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst @@ -0,0 +1,18 @@ +Basic Blink +=========== + +Simple blink example to confirm that the basic build system is working with your system. + +We use Timer instead of a loop because we want to allow WiFi communications to work in the background. +See :doc:`/information/multitasking`. + +The LED on many development boards is connected to GPIO2, so this is the default. + +If you get no response then check the documentation or schematic as your system +may differ and change the LED_PIN definition accordingly. + +For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. + + +.. image:: blink.jpg + :height: 192px diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp new file mode 100644 index 0000000000..35936130ab --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp @@ -0,0 +1,48 @@ +/** + * This example turns the ESP32 into a Bluetooth LE keyboard that writes words, presses Enter, presses a media key and then Ctrl+Alt+Delete + */ + +#include +#include + +BleKeyboard bleKeyboard; +Timer procTimer; + +void loop() +{ + if(!bleKeyboard.isConnected()) { + return; + } + + Serial.println("Sending 'Hello world'..."); + bleKeyboard.print("Hello world"); + + Serial.println("Sending Enter key..."); + bleKeyboard.write(KEY_RETURN); + + delay(1000); + + Serial.println("Sending Play/Pause media key..."); + bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE); + + // + // Below is an example of pressing multiple keyboard modifiers + // which by default is commented out. + /* + Serial.println("Sending Ctrl+Alt+Delete..."); + bleKeyboard.press(KEY_LEFT_CTRL); + bleKeyboard.press(KEY_LEFT_ALT); + bleKeyboard.press(KEY_DELETE); + delay(100); + bleKeyboard.releaseAll(); + */ +} + +void init() +{ + Serial.begin(COM_SPEED_SERIAL); + Serial.println("Starting BLE Keyboard sample!"); + bleKeyboard.begin(); + + procTimer.initializeMs(1000, loop).start(); +} diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk new file mode 100644 index 0000000000..fb8faff53d --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk @@ -0,0 +1 @@ +COMPONENT_DEPENDS := BLEKeyboard \ No newline at end of file diff --git a/Sming/Libraries/NimBLE/component.mk b/Sming/Libraries/NimBLE/component.mk new file mode 100644 index 0000000000..72cd19c2c8 --- /dev/null +++ b/Sming/Libraries/NimBLE/component.mk @@ -0,0 +1,5 @@ +COMPONENT_SUBMODULES := esp-nimble-cpp +COMPONENT_SOC := esp32 + +COMPONENT_SRCDIRS := esp-nimble-cpp/src +COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) diff --git a/Sming/Libraries/NimBLE/esp-nimble-cpp b/Sming/Libraries/NimBLE/esp-nimble-cpp new file mode 160000 index 0000000000..9e5db157f8 --- /dev/null +++ b/Sming/Libraries/NimBLE/esp-nimble-cpp @@ -0,0 +1 @@ +Subproject commit 9e5db157f88444bb95415e08a92c7eb817ff533d diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile new file mode 100644 index 0000000000..ff51b6c3a7 --- /dev/null +++ b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile @@ -0,0 +1,9 @@ +##################################################################### +#### Please don't change this file. Use component.mk instead #### +##################################################################### + +ifndef SMING_HOME +$(error SMING_HOME is not set: please configure it as an environment variable) +endif + +include $(SMING_HOME)/project.mk diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst new file mode 100644 index 0000000000..62967a3a06 --- /dev/null +++ b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst @@ -0,0 +1,18 @@ +Basic Blink +=========== + +Simple blink example to confirm that the basic build system is working with your system. + +We use Timer instead of a loop because we want to allow WiFi communications to work in the background. +See :doc:`/information/multitasking`. + +The LED on many development boards is connected to GPIO2, so this is the default. + +If you get no response then check the documentation or schematic as your system +may differ and change the LED_PIN definition accordingly. + +For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. + + +.. image:: blink.jpg + :height: 192px diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp new file mode 100644 index 0000000000..f1edd6010e --- /dev/null +++ b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp @@ -0,0 +1,7 @@ +#include +#include + +void init() +{ + BLEDevice::init(""); +} diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk new file mode 100644 index 0000000000..fc07f88a55 --- /dev/null +++ b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk @@ -0,0 +1 @@ +COMPONENT_DEPENDS := NimBLE \ No newline at end of file From b019f00a9a58fdd32d47fdba3a68988b1a6fbe4b Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Wed, 13 Apr 2022 10:22:28 +0200 Subject: [PATCH 04/13] Updated docs and code. --- .gitmodules | 2 - Sming/Arch/Esp32/Components/bt/.cs | 0 Sming/Arch/Esp32/Components/bt/README.rst | 4 + Sming/Arch/Esp32/Components/bt/component.mk | 5 ++ Sming/Libraries/BLEGamepad/README.rst | 65 +++++++++++++++ Sming/Libraries/BLEGamepad/component.mk | 4 +- .../samples/Bluetooth_Gamepad/README.rst | 36 ++++++--- .../Bluetooth_Gamepad/app/application.cpp | 27 ++----- .../samples/Bluetooth_Gamepad/component.mk | 2 +- Sming/Libraries/BLEKeyboard/README.rst | 81 +++++++++++++++++++ Sming/Libraries/BLEKeyboard/component.mk | 4 +- .../samples/Bluetooth_Keyboard/README.rst | 31 +++---- .../Bluetooth_Keyboard/app/application.cpp | 10 +-- .../samples/Bluetooth_Keyboard/component.mk | 4 +- Sming/Libraries/NimBLE/README.rst | 24 ++++++ Sming/Libraries/NimBLE/component.mk | 2 +- .../NimBLE/samples/Bluetooth_Client/Makefile | 9 --- .../samples/Bluetooth_Client/README.rst | 18 ----- .../Bluetooth_Client/app/application.cpp | 7 -- .../samples/Bluetooth_Client/component.mk | 1 - 20 files changed, 237 insertions(+), 99 deletions(-) create mode 100644 Sming/Arch/Esp32/Components/bt/.cs create mode 100644 Sming/Arch/Esp32/Components/bt/README.rst create mode 100644 Sming/Arch/Esp32/Components/bt/component.mk create mode 100644 Sming/Libraries/BLEGamepad/README.rst create mode 100644 Sming/Libraries/BLEKeyboard/README.rst create mode 100644 Sming/Libraries/NimBLE/README.rst delete mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile delete mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst delete mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp delete mode 100644 Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk diff --git a/.gitmodules b/.gitmodules index 10c75ea190..c828c2366a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -403,5 +403,3 @@ # END OF .gitmodules # ################################################################################################# - - diff --git a/Sming/Arch/Esp32/Components/bt/.cs b/Sming/Arch/Esp32/Components/bt/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Arch/Esp32/Components/bt/README.rst b/Sming/Arch/Esp32/Components/bt/README.rst new file mode 100644 index 0000000000..408792b18c --- /dev/null +++ b/Sming/Arch/Esp32/Components/bt/README.rst @@ -0,0 +1,4 @@ +Esp32 Bluetooth +=============== + +All related libraries for Bluetooth support. diff --git a/Sming/Arch/Esp32/Components/bt/component.mk b/Sming/Arch/Esp32/Components/bt/component.mk new file mode 100644 index 0000000000..92649fa111 --- /dev/null +++ b/Sming/Arch/Esp32/Components/bt/component.mk @@ -0,0 +1,5 @@ +COMPONENT_LIBNAME := + +COMPONENT_DEPENDS := esp32 + +EXTRA_LIBS += btdm_app diff --git a/Sming/Libraries/BLEGamepad/README.rst b/Sming/Libraries/BLEGamepad/README.rst new file mode 100644 index 0000000000..c6a41f113f --- /dev/null +++ b/Sming/Libraries/BLEGamepad/README.rst @@ -0,0 +1,65 @@ +ESP32 BLE Gamepad +================= + +.. highlight:: c++ + +Introduction +------------ +This library allows you to make the ESP32 act as a Bluetooth gamepad and control what it does. +The library uses :library:`NimBLE` for faster and lighter communication. + +Features +-------- + +Using this library you can do the following: + + - Button press (128 buttons) + - Button release (128 buttons) + - Axes movement (6 axes (16 bit) (x, y, z, rZ, rX, rY) --> (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger)) + - 2 Sliders (16 bit) (Slider 1 and Slider 2) + - 4 point of view hats (ie. d-pad plus 3 other hat switches) + - Simulation controls (rudder, throttle, accelerator, brake, steering) + - Configurable HID descriptor + - Report optional battery level to host (basically works, but it doesn't show up in Android's status bar) + - Customize Bluetooth device name/manufacturer + - Uses efficient NimBLE bluetooth library + - Compatible with Windows + - Compatible with Android (Android OS maps default buttons / axes / hats slightly differently than Windows) + - Compatible with Linux (limited testing) + - Compatible with MacOS X (limited testing) + +Using +----- + +1. Add ``COMPONENT_DEPENDS += BLEGamepad`` to your application componenent.mk file. +2. Add these lines to your application:: + + #include + + namespace + { + BleGamepad bleGamepad; + + // ... + + } // namespace + + void init() + { + // ... + + bleGamepad.begin(); + } + + +Notes +----- +By default, reports are sent on every button press/release or axis/slider/hat/simulation movement, however this can be disabled, +and then you manually call sendReport on the gamepad instance as shown in the IndividualAxes.ino example. + +There is also Bluetooth specific information that you can use (optional): + +Instead of ``BleGamepad bleGamepad;`` you can do ``BleGamepad bleGamepad("Bluetooth Device Name", "Bluetooth Device Manufacturer", 100);``. +The third parameter is the initial battery level of your device. +Adjusting the battery level later on doesn't work. +By default the battery level will be set to 100%, the device name will be `ESP32 BLE Gamepad` and the manufacturer will be `Espressif`. \ No newline at end of file diff --git a/Sming/Libraries/BLEGamepad/component.mk b/Sming/Libraries/BLEGamepad/component.mk index b04c1ac3f3..397bfd7369 100644 --- a/Sming/Libraries/BLEGamepad/component.mk +++ b/Sming/Libraries/BLEGamepad/component.mk @@ -1,9 +1,9 @@ COMPONENT_SUBMODULES := ESP32-BLE-Gamepad -COMPONENT_SOC := esp32 +COMPONENT_SOC := esp32* COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Gamepad COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) COMPONENT_CPPFLAGS:= -DUSE_NIMBLE=1 -APP_CFLAGS += -DUSE_NIMBLE=1 \ No newline at end of file +APP_CFLAGS += -DUSE_NIMBLE=1 diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst index 62967a3a06..1572eddf96 100644 --- a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/README.rst @@ -1,18 +1,32 @@ -Basic Blink -=========== +Bluetooth Gamepad +================= -Simple blink example to confirm that the basic build system is working with your system. +Introduction +------------ +This sample turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis -We use Timer instead of a loop because we want to allow WiFi communications to work in the background. -See :doc:`/information/multitasking`. +Possible buttons are: BUTTON_1 through to BUTTON_16 +(16 buttons supported by default. Library can be configured to support up to 128) -The LED on many development boards is connected to GPIO2, so this is the default. +Possible DPAD/HAT switch position values are: +DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT +(or HAT_CENTERED, HAT_UP etc) -If you get no response then check the documentation or schematic as your system -may differ and change the LED_PIN definition accordingly. +bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, Left/Right Triggers plus slider1 and slider2, and hat switch position as above: +(Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch position ^ (1 hat switch (dpad) supported by default. Library can be configured to support up to 4) -For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. +Library can also be configured to support up to 5 simulation controls (can be set with setSimulationControls) +(rudder, throttle, accelerator, brake, steering), but they are not enabled by default. -.. image:: blink.jpg - :height: 192px +Testing +------- + +You can use one of the following applications on your PC to test and see all buttons that were clicked. + +On Linux install ``jstest-gtk`` to test the ESP32 gamepad. Under Ubuntu this can be done by typing the following command:: + + sudo apt install jstest-gtk + +On Windows use this `Windows test application `__. + \ No newline at end of file diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp index ef9af4e170..7e10dbbfd7 100644 --- a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp @@ -1,32 +1,15 @@ -/* - * This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis - * - * Possible buttons are: - * BUTTON_1 through to BUTTON_16 - * (16 buttons supported by default. Library can be configured to support up to 128) - * - * Possible DPAD/HAT switch position values are: - * DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT - * (or HAT_CENTERED, HAT_UP etc) - * - * bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, Left/Right Triggers plus slider1 and slider2, and hat switch position as above: - * (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch position - ^ (1 hat switch (dpad) supported by default. Library can be configured to support up to 4) - * - * Library can also be configured to support up to 5 simulation controls (can be set with setSimulationControls) - * (rudder, throttle, accelerator, brake, steering), but they are not enabled by default. - */ - #include #include +namespace +{ BleGamepad bleGamepad; Timer procTimer; void loop() { - if(bleGamepad.isConnected()) { + if(!bleGamepad.isConnected()) { return; } @@ -43,10 +26,12 @@ void loop() delay(500); } +} // namespace + void init() { Serial.begin(COM_SPEED_SERIAL); - Serial.println("Starting BLE Keyboard sample!"); + Serial.println("Starting BLE Gamepad sample!"); bleGamepad.begin(); // The default bleGamepad.begin() above is the same as bleGamepad.begin(16, 1, true, true, true, true, true, true, true, true, false, false, false, false, false); // which enables a gamepad with 16 buttons, 1 hat switch, enabled x, y, z, rZ, rX, rY, slider 1, slider 2 and disabled rudder, throttle, accelerator, brake, steering diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk index 721effd302..c28c735b08 100644 --- a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/component.mk @@ -1 +1 @@ -COMPONENT_DEPENDS := BLEGamepad \ No newline at end of file +COMPONENT_DEPENDS := BLEGamepad diff --git a/Sming/Libraries/BLEKeyboard/README.rst b/Sming/Libraries/BLEKeyboard/README.rst new file mode 100644 index 0000000000..d27c3e5ced --- /dev/null +++ b/Sming/Libraries/BLEKeyboard/README.rst @@ -0,0 +1,81 @@ +ESP32 BLE Keyboard +================== + +.. highlight:: c++ + +Introduction +------------ +This library allows you to make the ESP32 act as a Bluetooth keyboard and control what it does. +The library uses :library:`NimBLE` for faster and lighter communication. + +Features +-------- + +Using this library you can do the following: + + - Send key strokes + - Send text + - Press/release individual keys + - Media keys are supported + - Set battery level (basically works, but doesn't show up in Android's status bar) + - Compatible with Android + - Compatible with Windows + - Compatible with Linux + - Compatible with MacOS X (not stable, some people have issues, doesn't work with old devices) + - Compatible with iOS (not stable, some people have issues, doesn't work with old devices) + +Using +----- + +1. Add ``COMPONENT_DEPENDS += BLEKeyboard`` to your application componenent.mk file. +2. Add these lines to your application:: + + #include + + + namespace + { + BleKeyboard bleKeyboard; + + // ... + + } // namespace + + void init() + { + // ... + + bleKeyboard.begin(); + } + + +API documentation +----------------- +The BleKeyboard interface is almost identical to the Keyboard Interface, so you can use documentation right here: +https://www.arduino.cc/reference/en/language/functions/usb/keyboard/ + +In addition to that you can send media keys (which is not possible with the USB keyboard library). Supported are the following: + + - KEY_MEDIA_NEXT_TRACK + - KEY_MEDIA_PREVIOUS_TRACK + - KEY_MEDIA_STOP + - KEY_MEDIA_PLAY_PAUSE + - KEY_MEDIA_MUTE + - KEY_MEDIA_VOLUME_UP + - KEY_MEDIA_VOLUME_DOWN + - KEY_MEDIA_WWW_HOME + - KEY_MEDIA_LOCAL_MACHINE_BROWSER // Opens "My Computer" on Windows + - KEY_MEDIA_CALCULATOR + - KEY_MEDIA_WWW_BOOKMARKS + - KEY_MEDIA_WWW_SEARCH + - KEY_MEDIA_WWW_STOP + - KEY_MEDIA_WWW_BACK + - KEY_MEDIA_CONSUMER_CONTROL_CONFIGURATION // Media Selection + - KEY_MEDIA_EMAIL_READER + +There is also Bluetooth specific information that you can set (optional): +Instead of ``BleKeyboard bleKeyboard;`` you can do ``BleKeyboard bleKeyboard("Bluetooth Device Name", "Bluetooth Device Manufacturer", 100);``. (Max length is 15 characters, anything beyond that will be truncated.) +The third parameter is the initial battery level of your device. To adjust the battery level later on you can simply call e.g. ``bleKeyboard.setBatteryLevel(50)`` (set battery level to 50%). +By default the battery level will be set to 100%, the device name will be `ESP32 Bluetooth Keyboard` and the manufacturer will be `Espressif`. +There is also a ``setDelay`` method to set a delay between each key event. E.g. ``bleKeyboard.setDelay(10)`` (10 milliseconds). The default is `8`. +This feature is meant to compensate for some applications and devices that can't handle fast input and will skip letters if too many keys are sent in a small time frame. diff --git a/Sming/Libraries/BLEKeyboard/component.mk b/Sming/Libraries/BLEKeyboard/component.mk index 2c1a2872e8..52a3ab7f44 100644 --- a/Sming/Libraries/BLEKeyboard/component.mk +++ b/Sming/Libraries/BLEKeyboard/component.mk @@ -1,9 +1,9 @@ COMPONENT_SUBMODULES := ESP32-BLE-Keyboard -COMPONENT_SOC := esp32 +COMPONENT_SOC := esp32* COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Keyboard COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) COMPONENT_CPPFLAGS:= -DUSE_NIMBLE=1 -APP_CFLAGS += -DUSE_NIMBLE=1 \ No newline at end of file +APP_CFLAGS += -DUSE_NIMBLE=1 diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst index 62967a3a06..4d01eb7a89 100644 --- a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/README.rst @@ -1,18 +1,13 @@ -Basic Blink -=========== - -Simple blink example to confirm that the basic build system is working with your system. - -We use Timer instead of a loop because we want to allow WiFi communications to work in the background. -See :doc:`/information/multitasking`. - -The LED on many development boards is connected to GPIO2, so this is the default. - -If you get no response then check the documentation or schematic as your system -may differ and change the LED_PIN definition accordingly. - -For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. - - -.. image:: blink.jpg - :height: 192px +Bluetooth Keyboard +================== + +This sample demonstrates how to turn an Esp32 device into external keyboard. +The "keyboard" and your PC will be communicating using Bluetooth Low Energy (BLE). +The "keyboard" will write words, press Enter, press a media key and, if enabled in the sample code, Ctrl+Alt+Delete. + +Usage +----- +Once this sample is flashed and running on your ESP32 you can test it. +Open a new text editor on your PC. Then search from your PC for new bluetooth devices. +A device named "Sming BLE Keyboard" should show up. Connect to it and focus/open you text editor window. +Be fast. Soon enough a "Hello World" text will start to be "magically" typed inside your text editor. \ No newline at end of file diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp index 35936130ab..bc6d959e34 100644 --- a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp @@ -1,11 +1,9 @@ -/** - * This example turns the ESP32 into a Bluetooth LE keyboard that writes words, presses Enter, presses a media key and then Ctrl+Alt+Delete - */ - #include #include -BleKeyboard bleKeyboard; +namespace +{ +BleKeyboard bleKeyboard("Sming BLE Keyboard"); Timer procTimer; void loop() @@ -38,6 +36,8 @@ void loop() */ } +} // namespace + void init() { Serial.begin(COM_SPEED_SERIAL); diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk index fb8faff53d..c41366a32f 100644 --- a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk @@ -1 +1,3 @@ -COMPONENT_DEPENDS := BLEKeyboard \ No newline at end of file +COMPONENT_SOC := esp32* + +COMPONENT_DEPENDS := BLEKeyboard diff --git a/Sming/Libraries/NimBLE/README.rst b/Sming/Libraries/NimBLE/README.rst new file mode 100644 index 0000000000..0313349fb2 --- /dev/null +++ b/Sming/Libraries/NimBLE/README.rst @@ -0,0 +1,24 @@ +ESP32 NimBLE +============ + +.. highlight:: c++ + +Introduction +------------ +NimBLE is a completely open source Bluetooth Low Energy (BLE) stack produced by `Apache `_. +It is more suited to resource constrained devices than bluedroid and has now been ported to the ESP32 by Espressif. + +Using +----- + +1. Add ``COMPONENT_DEPENDS += NimBLE`` to your application componenent.mk file. +2. Add these lines to your application:: + + #include + + void init() + { + // ... + + BLEDevice::init(""); + } diff --git a/Sming/Libraries/NimBLE/component.mk b/Sming/Libraries/NimBLE/component.mk index 72cd19c2c8..d425619807 100644 --- a/Sming/Libraries/NimBLE/component.mk +++ b/Sming/Libraries/NimBLE/component.mk @@ -1,5 +1,5 @@ COMPONENT_SUBMODULES := esp-nimble-cpp -COMPONENT_SOC := esp32 +COMPONENT_SOC := esp32* COMPONENT_SRCDIRS := esp-nimble-cpp/src COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile deleted file mode 100644 index ff51b6c3a7..0000000000 --- a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -##################################################################### -#### Please don't change this file. Use component.mk instead #### -##################################################################### - -ifndef SMING_HOME -$(error SMING_HOME is not set: please configure it as an environment variable) -endif - -include $(SMING_HOME)/project.mk diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst deleted file mode 100644 index 62967a3a06..0000000000 --- a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/README.rst +++ /dev/null @@ -1,18 +0,0 @@ -Basic Blink -=========== - -Simple blink example to confirm that the basic build system is working with your system. - -We use Timer instead of a loop because we want to allow WiFi communications to work in the background. -See :doc:`/information/multitasking`. - -The LED on many development boards is connected to GPIO2, so this is the default. - -If you get no response then check the documentation or schematic as your system -may differ and change the LED_PIN definition accordingly. - -For example, the NodeMCU ESP-C3 kits have an RGB LED connected to GPIO 3, 4 & 5. - - -.. image:: blink.jpg - :height: 192px diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp deleted file mode 100644 index f1edd6010e..0000000000 --- a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/app/application.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void init() -{ - BLEDevice::init(""); -} diff --git a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk b/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk deleted file mode 100644 index fc07f88a55..0000000000 --- a/Sming/Libraries/NimBLE/samples/Bluetooth_Client/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_DEPENDS := NimBLE \ No newline at end of file From 658cf597afc6494546ef50d2958f58509c8474bf Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Mon, 2 May 2022 11:46:40 +0200 Subject: [PATCH 05/13] Added Switch Joycon sample. --- Sming/Libraries/SwitchJoycon/.cs | 0 Sming/Libraries/SwitchJoycon/README.rst | 76 +++ Sming/Libraries/SwitchJoycon/component.mk | 5 + .../SwitchJoycon/samples/Bluetooth_Joycon/.cs | 0 .../samples/Bluetooth_Joycon/Makefile | 9 + .../samples/Bluetooth_Joycon/README.rst | 23 + .../Bluetooth_Joycon/app/application.cpp | 54 +++ .../samples/Bluetooth_Joycon/component.mk | 1 + Sming/Libraries/SwitchJoycon/src/.cs | 0 .../SwitchJoycon/src/SwitchJoycon.cpp | 438 ++++++++++++++++++ .../Libraries/SwitchJoycon/src/SwitchJoycon.h | 154 ++++++ .../src/SwitchJoyconConnection.cpp | 17 + .../SwitchJoycon/src/SwitchJoyconConnection.h | 36 ++ 13 files changed, 813 insertions(+) create mode 100644 Sming/Libraries/SwitchJoycon/.cs create mode 100644 Sming/Libraries/SwitchJoycon/README.rst create mode 100644 Sming/Libraries/SwitchJoycon/component.mk create mode 100644 Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/.cs create mode 100644 Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/Makefile create mode 100644 Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/README.rst create mode 100644 Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp create mode 100644 Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/component.mk create mode 100644 Sming/Libraries/SwitchJoycon/src/.cs create mode 100644 Sming/Libraries/SwitchJoycon/src/SwitchJoycon.cpp create mode 100644 Sming/Libraries/SwitchJoycon/src/SwitchJoycon.h create mode 100644 Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.cpp create mode 100644 Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h diff --git a/Sming/Libraries/SwitchJoycon/.cs b/Sming/Libraries/SwitchJoycon/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Libraries/SwitchJoycon/README.rst b/Sming/Libraries/SwitchJoycon/README.rst new file mode 100644 index 0000000000..537b2c9a4a --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/README.rst @@ -0,0 +1,76 @@ +Switch Joycon +============= + +.. highlight:: c++ + +Introduction +------------ +This library allows you to make the ESP32 act as a Nintendo Switch Joycon and control what it does. +The library uses :library:`NimBLE` for faster and lighter communication. + +Disclaimer +---------- +We are not affiliated, associated, authorized, endorsed by, or in any way officially connected with Nintendo, +or any of its subsidiaries or its affiliates. +The names Nintendo, Nintendo Switch and Joycon as well as related names, marks, emblems and images are +registered trademarks of their respective owners. + +Features +-------- + +Using this library you can do the following: + + - Button press and release (16 buttons) + - Switch Hat (1 hat ) + - Rotate 4 Axis + +Using +----- + +1. Add ``COMPONENT_DEPENDS += SwitchJoycon`` to your application componenent.mk file. +2. Add these lines to your application:: + + #include + + namespace + { + SwitchJoycon joycon; + + // ... + + } // namespace + + void init() + { + // ... + + joycon.begin(); + } + + +Notes +----- +By default, reports are sent on every button press/release or axis/hat movement, however this can be disabled:: + + joycon.setAutoReport(false); + +and then you should manually call sendReport on the joycon instance as shown below:: + + joycon.sendReport(); + + +HID Debugging +------------- + +On Linux you can install `hid-tools `__ using the command below:: + + sudo pip3 install . + +Once installed hid-recorder can be used to check the device HID report description and sniff the different reports:: + + sudo hid-recorder + +Useful Links +------------ +- `Tutorial about USB HID Report Descriptors `__ +- `HID constants `__ diff --git a/Sming/Libraries/SwitchJoycon/component.mk b/Sming/Libraries/SwitchJoycon/component.mk new file mode 100644 index 0000000000..7d86756dd3 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/component.mk @@ -0,0 +1,5 @@ +COMPONENT_SOC := esp32* +COMPONENT_DEPENDS := NimBLE + +COMPONENT_SRCDIRS := src +COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) \ No newline at end of file diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/.cs b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/Makefile b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/Makefile new file mode 100644 index 0000000000..ff51b6c3a7 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/Makefile @@ -0,0 +1,9 @@ +##################################################################### +#### Please don't change this file. Use component.mk instead #### +##################################################################### + +ifndef SMING_HOME +$(error SMING_HOME is not set: please configure it as an environment variable) +endif + +include $(SMING_HOME)/project.mk diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/README.rst b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/README.rst new file mode 100644 index 0000000000..94e0171a65 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/README.rst @@ -0,0 +1,23 @@ +Switch Joycon +============= + +Introduction +------------ +This sample turns the ESP32 into a Switch Joycon (Bluetooth LE gamepad) that presses buttons and moves axis + +Possible buttons are 0 through to 15. + +Possible HAT switch position values are: +Centered, Up, UpRight, Right, DownRight, Down, DownLeft, Left, UpLeft. + + +Testing +------- + +You can use one of the following applications on your PC to test and see all buttons that were clicked. + +On Linux install ``jstest-gtk`` to test the ESP32 gamepad. Under Ubuntu this can be done by typing the following command:: + + sudo apt install jstest-gtk + +On Windows use this `Windows test application `__. \ No newline at end of file diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp new file mode 100644 index 0000000000..caa9ef8fe4 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp @@ -0,0 +1,54 @@ +#include +#include + +namespace +{ +Timer procTimer; + +void onConnect(NimBLEServer& server); +void onDisconnect(NimBLEServer& server); + +SwitchJoycon joycon(SwitchJoycon::Type::Left, 100, onConnect, onDisconnect); + +void loop() +{ + if(!joycon.isConnected()) { + return; + } + + uint8_t button = random(0, 15); + + joycon.press(button); + joycon.setHat(SwitchJoycon::JoystickPosition::UpLeft); + delay(5000); + + joycon.release(button); + joycon.setHat(SwitchJoycon::JoystickPosition::Center); + delay(5000); +} + +void onConnect(NimBLEServer& server) +{ + Serial.println("Connected :) !"); + + procTimer.initializeMs(500, loop).start(); +} + +void onDisconnect(NimBLEServer& server) +{ + procTimer.stop(); + Serial.println("Disconnected :(!"); +} + +} // namespace + +void init() +{ + Serial.begin(COM_SPEED_SERIAL); + Serial.systemDebugOutput(true); + + Serial.println("Starting Joycon Gamepad sample!"); + joycon.begin(); + // Auto reporting is enabled by default. + // Use joycon.setAutoReport(false); to disable auto reporting, and then use joycon.sendReport(); as needed +} diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/component.mk b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/component.mk new file mode 100644 index 0000000000..1b24857470 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/component.mk @@ -0,0 +1 @@ +COMPONENT_DEPENDS := SwitchJoycon diff --git a/Sming/Libraries/SwitchJoycon/src/.cs b/Sming/Libraries/SwitchJoycon/src/.cs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.cpp b/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.cpp new file mode 100644 index 0000000000..75f10086e9 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.cpp @@ -0,0 +1,438 @@ +#include "SwitchJoycon.h" + +#include +#include +#include +#include +#include +#include + +#if DEBUG_VERBOSE_LEVEL == 3 +#include +HexDump dump; +#endif + +// TODO: Move report description to progmem +uint8_t tempHidReportDescriptor[150]; +int hidReportDescriptorSize = 0; + +constexpr uint8_t GAMEPAD_DEFAULT_REPORT_ID = 63; +constexpr uint8_t JOYSTICK_TYPE_GAMEPAD = 0x05; + +void SwitchJoycon::resetButtons() +{ + memset(&state.buttons, 0, sizeof(state.buttons)); +} + +bool SwitchJoycon::begin() +{ + if(started) { + debug_w("Service already started"); + return false; + } + + uint8_t axisCount = 0; + buttonCount = 0; + hatSwitchCount = 0; + switch(controllerType) { + case Type::Left: + /* fall through */ + case Type::Right: + buttonCount = 16; // buttonCount; + hatSwitchCount = 1; + axisCount = 4; // x, y, z and z rotation + break; + case Type::ProController: + buttonCount = 16; // buttonCount; + hatSwitchCount = 1; + axisCount = 4; // x, y, z and z rotation + break; + } + + started = true; + state.hat = static_cast(JoystickPosition::Center); + state.leftX[0] = 0x00; + state.leftX[1] = 0x80; + state.leftY[0] = 0x00; + state.leftY[1] = 0x80; + state.rightX[0] = 0x00; + state.rightX[1] = 0x80; + state.rightY[0] = 0x00; + state.rightY[1] = 0x80; + + /** + * For HID debugging see: https://gitlab.freedesktop.org/libevdev/hid-tools + * For HID report descriptors see: https://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/ + */ + + hidReportDescriptorSize = 0; + + // USAGE_PAGE (Generic Desktop) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // USAGE (Joystick - 0x04; Gamepad - 0x05; Multi-axis Controller - 0x08) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = JOYSTICK_TYPE_GAMEPAD; + + // COLLECTION (Application) + tempHidReportDescriptor[hidReportDescriptorSize++] = COLLECTION(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // REPORT_ID (Default: 3) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_ID(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = GAMEPAD_DEFAULT_REPORT_ID; + + if(buttonCount > 0) { + // USAGE_PAGE (Button) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; + + // USAGE_MINIMUM (Button 1) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_MINIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // USAGE_MAXIMUM (Button 16) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_MAXIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = buttonCount; + + // LOGICAL_MINIMUM (0) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MINIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + + // LOGICAL_MAXIMUM (1) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MAXIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // REPORT_SIZE (1) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_SIZE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // REPORT_COUNT (# of buttons) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_COUNT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = buttonCount; + + // INPUT (Data,Var,Abs) + tempHidReportDescriptor[hidReportDescriptorSize++] = HIDINPUT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; + } // buttonCount + + if(hatSwitchCount > 0) { + // USAGE_PAGE (Generic Desktop) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // USAGE (Hat Switch) + for(int i = 0; i < hatSwitchCount; i++) { + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39; + } + + // Logical Min (0) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MINIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + + // Logical Max (7) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MAXIMUM(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x07; + + // Report Size (4) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_SIZE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04; + + // Report Count (1) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_COUNT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = hatSwitchCount; + + // Input (Data, Variable, Absolute) + tempHidReportDescriptor[hidReportDescriptorSize++] = HIDINPUT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x42; + + // -- Padding for the 4 unused bits in the hat switch byte -- + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09; + + // Report Size (4) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_SIZE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04; + + // Report Count (1) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_COUNT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // Input (Cnst,Arr,Abs) + tempHidReportDescriptor[hidReportDescriptorSize++] = HIDINPUT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + } + + if(axisCount > 0) { + // USAGE_PAGE (Generic Desktop) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; + + // USAGE (X) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x30; + + // USAGE (Y) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x31; + + // USAGE (Rx) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x33; + + // USAGE (Ry) + tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x34; + + // LOGICAL_MINIMUM(255) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MINIMUM(2); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + + // LOGICAL_MAXIMUM (255) + tempHidReportDescriptor[hidReportDescriptorSize++] = LOGICAL_MAXIMUM(3); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; + tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; + + // REPORT_SIZE (16) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_SIZE(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10; + + // REPORT_COUNT (axisCount) + tempHidReportDescriptor[hidReportDescriptorSize++] = REPORT_COUNT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = axisCount; + + // INPUT (Data,Var,Abs) + tempHidReportDescriptor[hidReportDescriptorSize++] = HIDINPUT(1); + tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02; + } + + // END_COLLECTION + tempHidReportDescriptor[hidReportDescriptorSize++] = END_COLLECTION(0); + + xTaskCreate(startServer, "server", 20000, (void*)this, 5, &taskHandle); + + return true; +} + +void SwitchJoycon::end() +{ + if(!started) { + return; + } + + if(taskHandle != nullptr) { + vTaskDelete(taskHandle); + } + + NimBLEDevice::deinit(true); + delete hid; + hid = nullptr; + delete inputGamepad; + inputGamepad = nullptr; + connectionStatus = nullptr; + memset(&state, 0, sizeof(state)); + + started = false; +} + +void SwitchJoycon::sendReport(void) +{ + if(!isConnected()) { + return; + } + + debug_d("Sending report ...."); + +#if DEBUG_VERBOSE_LEVEL == 3 + dump.resetAddr(); + dump.print(reinterpret_cast(&state), sizeof(state)); +#endif + + debug_d("================="); + + this->inputGamepad->setValue(state); + this->inputGamepad->notify(); +} + +bool SwitchJoycon::isPressed(uint8_t button) +{ + uint8_t index = button / 8; + uint8_t bit = button % 8; + + return bitRead(state.buttons[index], bit); +} +void SwitchJoycon::press(uint8_t button) +{ + if(isPressed(button)) { + return; + } + + uint8_t index = button / 8; + uint8_t bit = button % 8; + + bitSet(state.buttons[index], bit); + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::release(uint8_t button) +{ + if(!isPressed(button)) { + return; + } + + uint8_t index = button / 8; + uint8_t bit = button % 8; + + bitClear(state.buttons[index], bit); + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::setHat(JoystickPosition position) +{ + if(state.hat == static_cast(position)) { + return; + } + + state.hat = static_cast(position); + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::setXAxis(int16_t value) +{ + // TODO: add value checks + + state.leftX[0] = value; + state.leftX[1] = value >> 8; + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::setYAxis(int16_t value) +{ + // TODO: add value checks + + state.leftY[0] = value; + state.leftY[1] = value >> 8; + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::setZAxis(int16_t value) +{ + // TODO: add value checks + // if(value == -32768) { + // value = -32767; + // } + + state.rightX[0] = value; + state.rightX[1] = value >> 8; + + if(autoReport) { + sendReport(); + } +} + +void SwitchJoycon::setZAxisRotation(int16_t value) +{ + // TODO: add value checks + + state.rightY[0] = value; + state.rightY[1] = value >> 8; + + if(autoReport) { + sendReport(); + } +} + +bool SwitchJoycon::isConnected(void) +{ + if(connectionStatus == nullptr) { + return false; + } + + return connectionStatus->connected; +} + +void SwitchJoycon::setBatteryLevel(uint8_t level) +{ + batteryLevel = level; + if(hid != nullptr) { + hid->setBatteryLevel(batteryLevel); + } +} + +void SwitchJoycon::startServer(void* arg) +{ + SwitchJoycon* joycon = static_cast(arg); + + String deviceName; + uint16_t productId = 0; + // See: http://gtoal.com/vectrex/vecx-colour/SDL/src/joystick/controller_type.h + switch(joycon->controllerType) { + case Type::Left: + deviceName = F("Joy-Con (L)"); + productId = 0x2006; + break; + case Type::Right: + deviceName = F("Joy-Con (R)"); + productId = 0x2007; + break; + case Type::ProController: + deviceName = F("Pro Controller"); + productId = 0x2009; + break; + } + + NimBLEDevice::init(deviceName.c_str()); + NimBLEServer* server = NimBLEDevice::createServer(); + server->setCallbacks(joycon->connectionStatus); + + delete joycon->hid; // TODO: ?! + joycon->hid = new NimBLEHIDDevice(server); + joycon->inputGamepad = joycon->hid->inputReport(GAMEPAD_DEFAULT_REPORT_ID); + joycon->connectionStatus->inputGamepad = joycon->inputGamepad; + + joycon->hid->manufacturer()->setValue("Nintendo"); + joycon->hid->pnp(0x01, __builtin_bswap16(0x057e), __builtin_bswap16(productId), 0x0110); + joycon->hid->hidInfo(0x00, 0x01); + + NimBLEDevice::setSecurityAuth(true, true, true); + +#if DEBUG_VERBOSE_LEVEL == 3 + dump.resetAddr(); + dump.print(tempHidReportDescriptor, hidReportDescriptorSize); +#endif + + joycon->hid->reportMap(tempHidReportDescriptor, hidReportDescriptorSize); + joycon->hid->startServices(); + + joycon->onStarted(server); + + NimBLEAdvertising* pAdvertising = server->getAdvertising(); + pAdvertising->setAppearance(HID_GAMEPAD); + pAdvertising->addServiceUUID(joycon->hid->hidService()->getUUID()); + pAdvertising->start(); + joycon->hid->setBatteryLevel(joycon->batteryLevel); + + debug_d("Advertising started!"); + + vTaskDelay(portMAX_DELAY); +} diff --git a/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.h b/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.h new file mode 100644 index 0000000000..86a2e63011 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/src/SwitchJoycon.h @@ -0,0 +1,154 @@ +#pragma once + +#include + +#include "sdkconfig.h" +#if defined(CONFIG_BT_ENABLED) + +#include "nimconfig.h" +#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) + +#include "SwitchJoyconConnection.h" +#include +#include + +class SwitchJoycon +{ +public: + enum class Type { + Left = 0, + Right, + ProController = 3, + }; + + enum class Button { + ButtonA = 0, + ButtonX, + ButtonB, + ButtonY, + ButtonSl, + ButtonSr, + + ButtonMunus = 8, + ButtonPlus, + + ButtonHome = 12, + ButtonCapture, + ButtonStickrl, + ButtonZrl, + }; + + enum class JoystickPosition { + Right = 0, + DownRight, + Down, + DownLeft, + Left, + UpLeft, + Up, + UpRight, + Center, + }; + + struct Gamepad // {00 00} 08 {00 80} {00 80} {00 80} {00 80} + { + uint8_t buttons[2]; + uint8_t hat; + uint8_t leftX[2]; + uint8_t leftY[2]; + uint8_t rightX[2]; + uint8_t rightY[2]; + }; + + SwitchJoycon(Type type, uint8_t batteryLevel = 100, SwitchJoyconConnection::Callback onConnected = nullptr, + SwitchJoyconConnection::Callback onDisconnected = nullptr) + : controllerType(type), batteryLevel(batteryLevel) + { + connectionStatus = new SwitchJoyconConnection(onConnected, onDisconnected); + } + + virtual ~SwitchJoycon() + { + end(); + } + + bool begin(); + + void end(); + + void setType(Type type) + { + controllerType = type; + } + + void setBatteryLevel(uint8_t level); + + // Buttons + + void press(Button button) + { + press(static_cast(button)); + } + + void press(uint8_t button); + + void release(Button button) + { + release(static_cast(button)); + } + + void release(uint8_t button); + + // Set Axis Values + + void setXAxis(int16_t value); + void setYAxis(int16_t value); + void setZAxis(int16_t value); + void setZAxisRotation(int16_t value); + + // Hat + + void setHat(JoystickPosition position); + + void setAutoReport(bool autoReport) + { + this->autoReport = autoReport; + } + + void sendReport(); + bool isPressed(uint8_t button); + bool isConnected(); + void resetButtons(); + +protected: + virtual void onStarted(NimBLEServer* pServer){}; + +private: + bool started{false}; + TaskHandle_t taskHandle{nullptr}; // owned + + // Joystick Type + Type controllerType; + + // Gamepad State + Gamepad state; + uint8_t batteryLevel{0}; + uint8_t buttonCount{0}; + uint8_t hatSwitchCount{0}; + + bool autoReport{true}; + size_t reportSize{0}; + + // HID Settings + NimBLEHIDDevice* hid{nullptr}; // owned + uint8_t hidReportId{0}; + + // Connection status and gamepad + SwitchJoyconConnection* connectionStatus{nullptr}; + NimBLECharacteristic* inputGamepad{nullptr}; // owned + + static void startServer(void* pvParameter); +}; + +#endif // CONFIG_BT_NIMBLE_ROLE_PERIPHERAL +#endif // CONFIG_BT_ENABLED diff --git a/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.cpp b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.cpp new file mode 100644 index 0000000000..df2347e7d0 --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.cpp @@ -0,0 +1,17 @@ +#include "SwitchJoyconConnection.h" + +void SwitchJoyconConnection::onConnect(NimBLEServer* server) +{ + connected = true; + if(connectCallback) { + connectCallback(*server); + } +} + +void SwitchJoyconConnection::onDisconnect(NimBLEServer* server) +{ + connected = false; + if(connectCallback) { + disconnectCallback(*server); + } +} diff --git a/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h new file mode 100644 index 0000000000..c96a29cead --- /dev/null +++ b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h @@ -0,0 +1,36 @@ +#ifndef ESP32_BLE_CONNECTION_STATUS_H +#define ESP32_BLE_CONNECTION_STATUS_H +#include "sdkconfig.h" +#if defined(CONFIG_BT_ENABLED) + +#include "nimconfig.h" +#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) + +#include +#include + +class SwitchJoyconConnection : public NimBLEServerCallbacks +{ +public: + using Callback = Delegate; + + bool connected{false}; + NimBLECharacteristic* inputGamepad{nullptr}; + + SwitchJoyconConnection(Callback onConnected = nullptr, Callback onDisconnected = nullptr) + { + connectCallback = onConnected; + disconnectCallback = onDisconnected; + } + + void onConnect(NimBLEServer* pServer); + void onDisconnect(NimBLEServer* pServer); + +private: + Callback connectCallback; + Callback disconnectCallback; +}; + +#endif // CONFIG_BT_NIMBLE_ROLE_PERIPHERAL +#endif // CONFIG_BT_ENABLED +#endif // ESP32_BLE_CONNECTION_STATUS_H From e19ec25dc67a9308e8e428288d23cc2c59593e5a Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 10 May 2022 15:02:50 +0200 Subject: [PATCH 06/13] Introduce Bluetooth ESP32-only component and require it from NimBLE. --- .../Esp32/Components/{bt => bluetooth}/.cs | 0 .../Components/{bt => bluetooth}/README.rst | 0 .../Esp32/Components/bluetooth/component.mk | 44 +++++++++++++++++++ Sming/Arch/Esp32/Components/bt/component.mk | 5 --- .../Arch/Esp32/Components/esp32/component.mk | 30 +------------ .../Esp32/Components/sming-arch/component.mk | 3 +- Sming/Libraries/NimBLE/component.mk | 2 + 7 files changed, 49 insertions(+), 35 deletions(-) rename Sming/Arch/Esp32/Components/{bt => bluetooth}/.cs (100%) rename Sming/Arch/Esp32/Components/{bt => bluetooth}/README.rst (100%) create mode 100644 Sming/Arch/Esp32/Components/bluetooth/component.mk delete mode 100644 Sming/Arch/Esp32/Components/bt/component.mk diff --git a/Sming/Arch/Esp32/Components/bt/.cs b/Sming/Arch/Esp32/Components/bluetooth/.cs similarity index 100% rename from Sming/Arch/Esp32/Components/bt/.cs rename to Sming/Arch/Esp32/Components/bluetooth/.cs diff --git a/Sming/Arch/Esp32/Components/bt/README.rst b/Sming/Arch/Esp32/Components/bluetooth/README.rst similarity index 100% rename from Sming/Arch/Esp32/Components/bt/README.rst rename to Sming/Arch/Esp32/Components/bluetooth/README.rst diff --git a/Sming/Arch/Esp32/Components/bluetooth/component.mk b/Sming/Arch/Esp32/Components/bluetooth/component.mk new file mode 100644 index 0000000000..a111366301 --- /dev/null +++ b/Sming/Arch/Esp32/Components/bluetooth/component.mk @@ -0,0 +1,44 @@ +COMPONENT_LIBNAME := + +COMPONENT_DEPENDS := esp32 + +EXTRA_LIBS += \ + bt \ + btdm_app + +SDK_LIBDIRS := \ + bt/controller/lib_esp32/$(ESP_VARIANT)/ \ + bt/controller/lib_esp32c3_family/$(ESP_VARIANT)/ + +LIBDIRS += \ + $(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_LIBDIRS)) + +SDK_INCDIRS := \ + bt/include/$(ESP_VARIANT)/include \ + bt/common/api/include/api \ + bt/common/btc/profile/esp/blufi/include \ + bt/common/btc/profile/esp/include \ + bt/common/osi/include \ + bt/host/nimble/nimble/nimble/include \ + bt/host/nimble/nimble/nimble/host/include \ + bt/host/nimble/nimble/porting/nimble/include \ + bt/host/nimble/nimble/porting/npl/freertos/include \ + bt/host/nimble/nimble/nimble/host/services/ans/include \ + bt/host/nimble/nimble/nimble/host/services/bas/include \ + bt/host/nimble/nimble/nimble/host/services/dis/include \ + bt/host/nimble/nimble/nimble/host/services/gap/include \ + bt/host/nimble/nimble/nimble/host/services/gatt/include \ + bt/host/nimble/nimble/nimble/host/services/ias/include \ + bt/host/nimble/nimble/nimble/host/services/ipss/include \ + bt/host/nimble/nimble/nimble/host/services/lls/include \ + bt/host/nimble/nimble/nimble/host/services/tps/include \ + bt/host/nimble/nimble/nimble/host/util/include \ + bt/host/nimble/nimble/nimble/host/store/ram/include \ + bt/host/nimble/nimble/nimble/host/store/config/include \ + bt/host/nimble/esp-hci/include \ + bt/host/nimble/port/include + +COMPONENT_INCDIRS := \ + $(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_INCDIRS)) + +DISABLE_NETWORK := 0 diff --git a/Sming/Arch/Esp32/Components/bt/component.mk b/Sming/Arch/Esp32/Components/bt/component.mk deleted file mode 100644 index 92649fa111..0000000000 --- a/Sming/Arch/Esp32/Components/bt/component.mk +++ /dev/null @@ -1,5 +0,0 @@ -COMPONENT_LIBNAME := - -COMPONENT_DEPENDS := esp32 - -EXTRA_LIBS += btdm_app diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index 72173263a2..830517d16d 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -20,8 +20,7 @@ SDK_LIBDIRS := \ xtensa/$(ESP_VARIANT)/ \ hal/$(ESP_VARIANT)/ \ $(ESP_VARIANT)/ld \ - esp_rom/$(ESP_VARIANT)/ld \ - bt/controller/lib_esp32/$(ESP_VARIANT)/ + esp_rom/$(ESP_VARIANT)/ld ESP32_COMPONENT_PATH := $(COMPONENT_PATH) SDK_DEFAULT_PATH := $(ESP32_COMPONENT_PATH)/sdk @@ -38,29 +37,6 @@ SDK_INCDIRS := \ app_update/include \ bootloader_support/include \ bootloader_support/include_bootloader \ - bt/include/$(ESP_VARIANT)/include \ - bt/common/api/include/api \ - bt/common/btc/profile/esp/blufi/include \ - bt/common/btc/profile/esp/include \ - bt/common/osi/include \ - bt/host/nimble/nimble/nimble/include \ - bt/host/nimble/nimble/nimble/host/include \ - bt/host/nimble/nimble/porting/nimble/include \ - bt/host/nimble/nimble/porting/npl/freertos/include \ - bt/host/nimble/nimble/nimble/host/services/ans/include \ - bt/host/nimble/nimble/nimble/host/services/bas/include \ - bt/host/nimble/nimble/nimble/host/services/dis/include \ - bt/host/nimble/nimble/nimble/host/services/gap/include \ - bt/host/nimble/nimble/nimble/host/services/gatt/include \ - bt/host/nimble/nimble/nimble/host/services/ias/include \ - bt/host/nimble/nimble/nimble/host/services/ipss/include \ - bt/host/nimble/nimble/nimble/host/services/lls/include \ - bt/host/nimble/nimble/nimble/host/services/tps/include \ - bt/host/nimble/nimble/nimble/host/util/include \ - bt/host/nimble/nimble/nimble/host/store/ram/include \ - bt/host/nimble/nimble/nimble/host/store/config/include \ - bt/host/nimble/esp-hci/include \ - bt/host/nimble/port/include \ driver/$(ESP_VARIANT)/include \ driver/include \ esp_pm/include \ @@ -142,7 +118,6 @@ SDK_COMPONENTS := \ ifneq ($(DISABLE_NETWORK),1) SDK_COMPONENTS += \ - bt \ esp_wifi \ esp_eth \ lwip \ @@ -167,8 +142,6 @@ SDK_COMPONENTS += xtensa endif SDK_ESP_WIFI_LIBS := \ - bt \ - coexist \ core \ espnow \ mesh \ @@ -188,6 +161,7 @@ SDK_TARGET_ARCH_LIBS := hal xt_hal endif EXTRA_LIBS := \ + coexist \ $(SDK_COMPONENTS) \ $(SDK_TARGET_ARCH_LIBS) diff --git a/Sming/Arch/Esp32/Components/sming-arch/component.mk b/Sming/Arch/Esp32/Components/sming-arch/component.mk index 69d3a3cb76..189237cbae 100644 --- a/Sming/Arch/Esp32/Components/sming-arch/component.mk +++ b/Sming/Arch/Esp32/Components/sming-arch/component.mk @@ -18,8 +18,7 @@ COMPONENT_DEPENDS := \ heap \ esp32 \ gdbstub \ - esptool \ - bt + esptool # ELF and BIN files DEBUG_VARS += TARGET_BIN TARGET_OUT diff --git a/Sming/Libraries/NimBLE/component.mk b/Sming/Libraries/NimBLE/component.mk index d425619807..695db55dc1 100644 --- a/Sming/Libraries/NimBLE/component.mk +++ b/Sming/Libraries/NimBLE/component.mk @@ -1,5 +1,7 @@ COMPONENT_SUBMODULES := esp-nimble-cpp COMPONENT_SOC := esp32* +COMPONENT_DEPENDS := bluetooth + COMPONENT_SRCDIRS := esp-nimble-cpp/src COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) From 974a181d7b99e04167d6b641593dd5af4795666a Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Wed, 18 May 2022 17:19:10 +0200 Subject: [PATCH 07/13] Re-introduce coexistence for printf functions. --- Sming/Arch/Esp32/Components/esp_coexist/README.rst | 4 ++++ Sming/Arch/Esp32/Components/esp_coexist/component.mk | 8 ++++++++ Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore | 1 + Sming/Arch/Esp32/Components/sming-arch/component.mk | 4 ++++ 4 files changed, 17 insertions(+) create mode 100644 Sming/Arch/Esp32/Components/esp_coexist/README.rst create mode 100644 Sming/Arch/Esp32/Components/esp_coexist/component.mk create mode 100644 Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore diff --git a/Sming/Arch/Esp32/Components/esp_coexist/README.rst b/Sming/Arch/Esp32/Components/esp_coexist/README.rst new file mode 100644 index 0000000000..eb71cd798c --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp_coexist/README.rst @@ -0,0 +1,4 @@ +Esp32 Coexist +============= + +Only print functions related to `coexist support `__ in Esp32. diff --git a/Sming/Arch/Esp32/Components/esp_coexist/component.mk b/Sming/Arch/Esp32/Components/esp_coexist/component.mk new file mode 100644 index 0000000000..100cb7a55d --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp_coexist/component.mk @@ -0,0 +1,8 @@ +COMPONENT_DEPENDS := esp32 + +COMPONENT_SRCDIRS := src + +COMPONENT_PREREQUISITES := $(COMPONENT_PATH)/src/coex_printf.c + +$(COMPONENT_PATH)/src/coex_printf.c: $(IDF_PATH)/components/esp_wifi/src/lib_printf.c + $(Q) cp $< $@ \ No newline at end of file diff --git a/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore b/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore new file mode 100644 index 0000000000..bdfa23c3b4 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore @@ -0,0 +1 @@ +coex_printf.c \ No newline at end of file diff --git a/Sming/Arch/Esp32/Components/sming-arch/component.mk b/Sming/Arch/Esp32/Components/sming-arch/component.mk index 189237cbae..a2af757ad1 100644 --- a/Sming/Arch/Esp32/Components/sming-arch/component.mk +++ b/Sming/Arch/Esp32/Components/sming-arch/component.mk @@ -19,6 +19,10 @@ COMPONENT_DEPENDS := \ esp32 \ gdbstub \ esptool + +ifeq ($(DISABLE_WIFI),1) + COMPONENT_DEPENDS += esp_coexist +endif # ELF and BIN files DEBUG_VARS += TARGET_BIN TARGET_OUT From 6984d41228b47e68a428cfced04df677630f6d0e Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Wed, 18 May 2022 17:47:05 +0200 Subject: [PATCH 08/13] Revert "Re-introduce coexistence for printf functions." This reverts commit 974a181d7b99e04167d6b641593dd5af4795666a. --- Sming/Arch/Esp32/Components/esp_coexist/README.rst | 4 ---- Sming/Arch/Esp32/Components/esp_coexist/component.mk | 8 -------- Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore | 1 - Sming/Arch/Esp32/Components/sming-arch/component.mk | 4 ---- 4 files changed, 17 deletions(-) delete mode 100644 Sming/Arch/Esp32/Components/esp_coexist/README.rst delete mode 100644 Sming/Arch/Esp32/Components/esp_coexist/component.mk delete mode 100644 Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore diff --git a/Sming/Arch/Esp32/Components/esp_coexist/README.rst b/Sming/Arch/Esp32/Components/esp_coexist/README.rst deleted file mode 100644 index eb71cd798c..0000000000 --- a/Sming/Arch/Esp32/Components/esp_coexist/README.rst +++ /dev/null @@ -1,4 +0,0 @@ -Esp32 Coexist -============= - -Only print functions related to `coexist support `__ in Esp32. diff --git a/Sming/Arch/Esp32/Components/esp_coexist/component.mk b/Sming/Arch/Esp32/Components/esp_coexist/component.mk deleted file mode 100644 index 100cb7a55d..0000000000 --- a/Sming/Arch/Esp32/Components/esp_coexist/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -COMPONENT_DEPENDS := esp32 - -COMPONENT_SRCDIRS := src - -COMPONENT_PREREQUISITES := $(COMPONENT_PATH)/src/coex_printf.c - -$(COMPONENT_PATH)/src/coex_printf.c: $(IDF_PATH)/components/esp_wifi/src/lib_printf.c - $(Q) cp $< $@ \ No newline at end of file diff --git a/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore b/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore deleted file mode 100644 index bdfa23c3b4..0000000000 --- a/Sming/Arch/Esp32/Components/esp_coexist/src/.gitignore +++ /dev/null @@ -1 +0,0 @@ -coex_printf.c \ No newline at end of file diff --git a/Sming/Arch/Esp32/Components/sming-arch/component.mk b/Sming/Arch/Esp32/Components/sming-arch/component.mk index a2af757ad1..189237cbae 100644 --- a/Sming/Arch/Esp32/Components/sming-arch/component.mk +++ b/Sming/Arch/Esp32/Components/sming-arch/component.mk @@ -19,10 +19,6 @@ COMPONENT_DEPENDS := \ esp32 \ gdbstub \ esptool - -ifeq ($(DISABLE_WIFI),1) - COMPONENT_DEPENDS += esp_coexist -endif # ELF and BIN files DEBUG_VARS += TARGET_BIN TARGET_OUT From 509c7ae070d310cebd18325f5740b270b0ec2754 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Wed, 18 May 2022 17:47:41 +0200 Subject: [PATCH 09/13] Disable software coexistence. TODO - add documentation. --- Sming/Arch/Esp32/Components/esp32/sdk/config/common | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/config/common b/Sming/Arch/Esp32/Components/esp32/sdk/config/common index 7c75c83fcb..a4caeb16e7 100644 --- a/Sming/Arch/Esp32/Components/esp32/sdk/config/common +++ b/Sming/Arch/Esp32/Components/esp32/sdk/config/common @@ -31,6 +31,8 @@ CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n CONFIG_BLE_MESH=n CONFIG_BT_NIMBLE_MESH=n +CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE=n + # Mandatory Sming framework changes CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=16384 CONFIG_ESP_TASK_WDT_TIMEOUT_S=8 From 4656730830fbbe1b779dc9048daa0962da31a162 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Thu, 19 May 2022 08:52:48 +0200 Subject: [PATCH 10/13] Move coex back to its previous location. --- Sming/Arch/Esp32/Components/esp32/component.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index 830517d16d..affffe905e 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -142,6 +142,7 @@ SDK_COMPONENTS += xtensa endif SDK_ESP_WIFI_LIBS := \ + coexist \ core \ espnow \ mesh \ @@ -161,7 +162,6 @@ SDK_TARGET_ARCH_LIBS := hal xt_hal endif EXTRA_LIBS := \ - coexist \ $(SDK_COMPONENTS) \ $(SDK_TARGET_ARCH_LIBS) From b1cd4e491fe473a1f9726cbbc4b7e1868207fb28 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Thu, 19 May 2022 09:39:27 +0200 Subject: [PATCH 11/13] BLE for Esp32 without variants. --- Sming/Arch/Esp32/Components/bluetooth/component.mk | 3 +++ Sming/Libraries/BLEGamepad/component.mk | 3 ++- Sming/Libraries/BLEKeyboard/component.mk | 3 ++- .../BLEKeyboard/samples/Bluetooth_Keyboard/component.mk | 2 -- Sming/Libraries/NimBLE/component.mk | 3 ++- Sming/Libraries/SwitchJoycon/component.mk | 4 +++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Sming/Arch/Esp32/Components/bluetooth/component.mk b/Sming/Arch/Esp32/Components/bluetooth/component.mk index a111366301..302e88289c 100644 --- a/Sming/Arch/Esp32/Components/bluetooth/component.mk +++ b/Sming/Arch/Esp32/Components/bluetooth/component.mk @@ -1,5 +1,8 @@ COMPONENT_LIBNAME := +# TODO: Add later support for the other Esp32 variants +COMPONENT_SOC := esp32 + COMPONENT_DEPENDS := esp32 EXTRA_LIBS += \ diff --git a/Sming/Libraries/BLEGamepad/component.mk b/Sming/Libraries/BLEGamepad/component.mk index 397bfd7369..6b9e661175 100644 --- a/Sming/Libraries/BLEGamepad/component.mk +++ b/Sming/Libraries/BLEGamepad/component.mk @@ -1,5 +1,6 @@ COMPONENT_SUBMODULES := ESP32-BLE-Gamepad -COMPONENT_SOC := esp32* +# TODO: Add later support for the other Esp32 variants +COMPONENT_SOC := esp32 COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Gamepad diff --git a/Sming/Libraries/BLEKeyboard/component.mk b/Sming/Libraries/BLEKeyboard/component.mk index 52a3ab7f44..56921f7c57 100644 --- a/Sming/Libraries/BLEKeyboard/component.mk +++ b/Sming/Libraries/BLEKeyboard/component.mk @@ -1,5 +1,6 @@ COMPONENT_SUBMODULES := ESP32-BLE-Keyboard -COMPONENT_SOC := esp32* +# TODO: Add later support for the other Esp32 variants +COMPONENT_SOC := esp32 COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Keyboard diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk index c41366a32f..0140a4bec6 100644 --- a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/component.mk @@ -1,3 +1 @@ -COMPONENT_SOC := esp32* - COMPONENT_DEPENDS := BLEKeyboard diff --git a/Sming/Libraries/NimBLE/component.mk b/Sming/Libraries/NimBLE/component.mk index 695db55dc1..3205ed8dd7 100644 --- a/Sming/Libraries/NimBLE/component.mk +++ b/Sming/Libraries/NimBLE/component.mk @@ -1,5 +1,6 @@ COMPONENT_SUBMODULES := esp-nimble-cpp -COMPONENT_SOC := esp32* +# TODO: Add later support for the other Esp32 variants +COMPONENT_SOC := esp32 COMPONENT_DEPENDS := bluetooth diff --git a/Sming/Libraries/SwitchJoycon/component.mk b/Sming/Libraries/SwitchJoycon/component.mk index 7d86756dd3..5a8efa9e0b 100644 --- a/Sming/Libraries/SwitchJoycon/component.mk +++ b/Sming/Libraries/SwitchJoycon/component.mk @@ -1,4 +1,6 @@ -COMPONENT_SOC := esp32* +# TODO: Add later support for the other Esp32 variants +COMPONENT_SOC := esp32 + COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := src From 886aa88690b117b0c4c1ba829ed0e0bc1182ddcd Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 27 Jun 2022 09:17:48 +0100 Subject: [PATCH 12/13] Move bluetooth requirements into Esp32 Component Remove bluetooth Component Enable bluetooth for supported variants (esp32, esp32c3, esp32s3) --- Sming/Arch/Esp32/Components/bluetooth/.cs | 0 .../Esp32/Components/bluetooth/README.rst | 4 -- .../Esp32/Components/bluetooth/component.mk | 47 ---------------- .../Arch/Esp32/Components/esp32/component.mk | 54 ++++++++++++++++++- Sming/Libraries/BLEKeyboard/component.mk | 3 +- Sming/Libraries/NimBLE/component.mk | 4 +- Sming/Libraries/SwitchJoycon/component.mk | 3 -- 7 files changed, 54 insertions(+), 61 deletions(-) delete mode 100644 Sming/Arch/Esp32/Components/bluetooth/.cs delete mode 100644 Sming/Arch/Esp32/Components/bluetooth/README.rst delete mode 100644 Sming/Arch/Esp32/Components/bluetooth/component.mk diff --git a/Sming/Arch/Esp32/Components/bluetooth/.cs b/Sming/Arch/Esp32/Components/bluetooth/.cs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/Sming/Arch/Esp32/Components/bluetooth/README.rst b/Sming/Arch/Esp32/Components/bluetooth/README.rst deleted file mode 100644 index 408792b18c..0000000000 --- a/Sming/Arch/Esp32/Components/bluetooth/README.rst +++ /dev/null @@ -1,4 +0,0 @@ -Esp32 Bluetooth -=============== - -All related libraries for Bluetooth support. diff --git a/Sming/Arch/Esp32/Components/bluetooth/component.mk b/Sming/Arch/Esp32/Components/bluetooth/component.mk deleted file mode 100644 index 302e88289c..0000000000 --- a/Sming/Arch/Esp32/Components/bluetooth/component.mk +++ /dev/null @@ -1,47 +0,0 @@ -COMPONENT_LIBNAME := - -# TODO: Add later support for the other Esp32 variants -COMPONENT_SOC := esp32 - -COMPONENT_DEPENDS := esp32 - -EXTRA_LIBS += \ - bt \ - btdm_app - -SDK_LIBDIRS := \ - bt/controller/lib_esp32/$(ESP_VARIANT)/ \ - bt/controller/lib_esp32c3_family/$(ESP_VARIANT)/ - -LIBDIRS += \ - $(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_LIBDIRS)) - -SDK_INCDIRS := \ - bt/include/$(ESP_VARIANT)/include \ - bt/common/api/include/api \ - bt/common/btc/profile/esp/blufi/include \ - bt/common/btc/profile/esp/include \ - bt/common/osi/include \ - bt/host/nimble/nimble/nimble/include \ - bt/host/nimble/nimble/nimble/host/include \ - bt/host/nimble/nimble/porting/nimble/include \ - bt/host/nimble/nimble/porting/npl/freertos/include \ - bt/host/nimble/nimble/nimble/host/services/ans/include \ - bt/host/nimble/nimble/nimble/host/services/bas/include \ - bt/host/nimble/nimble/nimble/host/services/dis/include \ - bt/host/nimble/nimble/nimble/host/services/gap/include \ - bt/host/nimble/nimble/nimble/host/services/gatt/include \ - bt/host/nimble/nimble/nimble/host/services/ias/include \ - bt/host/nimble/nimble/nimble/host/services/ipss/include \ - bt/host/nimble/nimble/nimble/host/services/lls/include \ - bt/host/nimble/nimble/nimble/host/services/tps/include \ - bt/host/nimble/nimble/nimble/host/util/include \ - bt/host/nimble/nimble/nimble/host/store/ram/include \ - bt/host/nimble/nimble/nimble/host/store/config/include \ - bt/host/nimble/esp-hci/include \ - bt/host/nimble/port/include - -COMPONENT_INCDIRS := \ - $(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_INCDIRS)) - -DISABLE_NETWORK := 0 diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index affffe905e..e568d0efa6 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -17,11 +17,22 @@ SDKCONFIG_H := $(SDK_BUILD_BASE)/config/sdkconfig.h SDK_LIBDIRS := \ esp_wifi/lib/$(ESP_VARIANT) \ - xtensa/$(ESP_VARIANT)/ \ - hal/$(ESP_VARIANT)/ \ + xtensa/$(ESP_VARIANT) \ + hal/$(ESP_VARIANT) \ $(ESP_VARIANT)/ld \ esp_rom/$(ESP_VARIANT)/ld +# BLUETOOTH +ifeq ($(ESP_VARIANT),esp32) +SDK_LIBDIRS += bt/controller/lib_esp32/$(ESP_VARIANT) +ENABLE_BLUETOOTH := 1 +else ifneq (,$(findstring $(ESP_VARIANT),esp32c3 esp32s3)) +SDK_LIBDIRS += bt/controller/lib_esp32c3_family/$(ESP_VARIANT) +ENABLE_BLUETOOTH := 1 +else +ENABLE_BLUETOOTH := 0 +endif + ESP32_COMPONENT_PATH := $(COMPONENT_PATH) SDK_DEFAULT_PATH := $(ESP32_COMPONENT_PATH)/sdk @@ -71,6 +82,33 @@ SDK_INCDIRS := \ lwip/include/apps/sntp \ wpa_supplicant/include/esp_supplicant +ifeq ($(ENABLE_BLUETOOTH),1) +SDK_INCDIRS += \ + bt/include/$(ESP_VARIANT)/include \ + bt/common/api/include/api \ + bt/common/btc/profile/esp/blufi/include \ + bt/common/btc/profile/esp/include \ + bt/common/osi/include \ + bt/host/nimble/nimble/nimble/include \ + bt/host/nimble/nimble/nimble/host/include \ + bt/host/nimble/nimble/porting/nimble/include \ + bt/host/nimble/nimble/porting/npl/freertos/include \ + bt/host/nimble/nimble/nimble/host/services/ans/include \ + bt/host/nimble/nimble/nimble/host/services/bas/include \ + bt/host/nimble/nimble/nimble/host/services/dis/include \ + bt/host/nimble/nimble/nimble/host/services/gap/include \ + bt/host/nimble/nimble/nimble/host/services/gatt/include \ + bt/host/nimble/nimble/nimble/host/services/ias/include \ + bt/host/nimble/nimble/nimble/host/services/ipss/include \ + bt/host/nimble/nimble/nimble/host/services/lls/include \ + bt/host/nimble/nimble/nimble/host/services/tps/include \ + bt/host/nimble/nimble/nimble/host/util/include \ + bt/host/nimble/nimble/nimble/host/store/ram/include \ + bt/host/nimble/nimble/nimble/host/store/config/include \ + bt/host/nimble/esp-hci/include \ + bt/host/nimble/port/include +endif + ifdef IDF_TARGET_ARCH_RISCV SDK_INCDIRS += \ freertos/port/riscv/include \ @@ -151,6 +189,13 @@ SDK_ESP_WIFI_LIBS := \ pp \ smartconfig +ifeq ($(ENABLE_BLUETOOTH),1) +SDK_ESP_BLUETOOTH_LIBS := bt btdm_app +ifneq (,$(filter $(ESP_VARIANT),esp32c3 esp32s3)) +SDK_ESP_BLUETOOTH_LIBS += btbb +endif +endif + ifeq ($(ESP_VARIANT),esp32) SDK_ESP_WIFI_LIBS += rtc endif @@ -167,6 +212,11 @@ EXTRA_LIBS := \ ifneq ($(DISABLE_WIFI),1) EXTRA_LIBS += $(SDK_ESP_WIFI_LIBS) + +ifeq ($(ENABLE_BLUETOOTH),1) +EXTRA_LIBS += $(SDK_ESP_BLUETOOTH_LIBS) +endif + endif LinkerScript = -T $(ESP_VARIANT).$1.ld diff --git a/Sming/Libraries/BLEKeyboard/component.mk b/Sming/Libraries/BLEKeyboard/component.mk index 56921f7c57..6016b6b22a 100644 --- a/Sming/Libraries/BLEKeyboard/component.mk +++ b/Sming/Libraries/BLEKeyboard/component.mk @@ -1,6 +1,5 @@ COMPONENT_SUBMODULES := ESP32-BLE-Keyboard -# TODO: Add later support for the other Esp32 variants -COMPONENT_SOC := esp32 + COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Keyboard diff --git a/Sming/Libraries/NimBLE/component.mk b/Sming/Libraries/NimBLE/component.mk index 3205ed8dd7..dd9d540ce3 100644 --- a/Sming/Libraries/NimBLE/component.mk +++ b/Sming/Libraries/NimBLE/component.mk @@ -1,8 +1,6 @@ COMPONENT_SUBMODULES := esp-nimble-cpp -# TODO: Add later support for the other Esp32 variants -COMPONENT_SOC := esp32 -COMPONENT_DEPENDS := bluetooth +COMPONENT_SOC := esp32 esp32c3 esp32s3 COMPONENT_SRCDIRS := esp-nimble-cpp/src COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) diff --git a/Sming/Libraries/SwitchJoycon/component.mk b/Sming/Libraries/SwitchJoycon/component.mk index 5a8efa9e0b..e07e0f70a7 100644 --- a/Sming/Libraries/SwitchJoycon/component.mk +++ b/Sming/Libraries/SwitchJoycon/component.mk @@ -1,6 +1,3 @@ -# TODO: Add later support for the other Esp32 variants -COMPONENT_SOC := esp32 - COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := src From b8c09b5cfded670fb10dddb8b9cbfd60e391d703 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 28 Jun 2022 10:32:31 +0200 Subject: [PATCH 13/13] Minor changes. --- Sming/Libraries/BLEGamepad/component.mk | 2 -- Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h | 5 ++--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Sming/Libraries/BLEGamepad/component.mk b/Sming/Libraries/BLEGamepad/component.mk index 6b9e661175..1bec8c0d31 100644 --- a/Sming/Libraries/BLEGamepad/component.mk +++ b/Sming/Libraries/BLEGamepad/component.mk @@ -1,6 +1,4 @@ COMPONENT_SUBMODULES := ESP32-BLE-Gamepad -# TODO: Add later support for the other Esp32 variants -COMPONENT_SOC := esp32 COMPONENT_DEPENDS := NimBLE COMPONENT_SRCDIRS := ESP32-BLE-Gamepad diff --git a/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h index c96a29cead..ffaaef0aa1 100644 --- a/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h +++ b/Sming/Libraries/SwitchJoycon/src/SwitchJoyconConnection.h @@ -1,5 +1,5 @@ -#ifndef ESP32_BLE_CONNECTION_STATUS_H -#define ESP32_BLE_CONNECTION_STATUS_H +#pragma once + #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) @@ -33,4 +33,3 @@ class SwitchJoyconConnection : public NimBLEServerCallbacks #endif // CONFIG_BT_NIMBLE_ROLE_PERIPHERAL #endif // CONFIG_BT_ENABLED -#endif // ESP32_BLE_CONNECTION_STATUS_H