From b1ea2f8425edff661d7cfcba32b7b268bba681ef Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Fri, 10 Mar 2023 12:37:47 +0400 Subject: [PATCH 01/35] fix(bHaptics): TactGlove mappings description --- firmware/mode_configs/bhaptics/tactglove.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/firmware/mode_configs/bhaptics/tactglove.cpp b/firmware/mode_configs/bhaptics/tactglove.cpp index bd9ba904..2916ede7 100644 --- a/firmware/mode_configs/bhaptics/tactglove.cpp +++ b/firmware/mode_configs/bhaptics/tactglove.cpp @@ -36,7 +36,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = { // Thumb, Index, Middle, Ring, Pinky make_point(0, 0), make_point(1, 0), make_point(2, 0), make_point(3, 0), make_point(4, 0), - // Palm + // Wrist make_point(5, 0) // clang-format on @@ -48,7 +48,12 @@ void setupMode(OpenHaptics* app) { // Configure PWM pins to their positions on the glove auto gloveOutputs = mapMatrixCoordinates({ // clang-format off - {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, + { + // Thumb, Index, Middle, Ring, Pinky + new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26), new PWMOutputWriter(27), + // Wrist + new PWMOutputWriter(14) + }, // clang-format on }); From 9ab39ae27e280afde90ed6e05e64787acb44ce84 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Sun, 26 Mar 2023 16:03:34 +0300 Subject: [PATCH 02/35] docs: update readme --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 71726120..26e36157 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Build your own DIY haptic vest, haptic gloves and more! [![GitHub](https://img.shields.io/github/stars/openhaptics/openhaptics-firmware.svg)](https://github.com/openhaptics/openhaptics-firmware) [![Contributor Covenant](https://img.shields.io/badge/code_of_conduct-contributor_covenant_v2.1-ff69b4)](/CODE_OF_CONDUCT.md) - ## Configuration, flashing and connecting The main documentation on this project is located on [openhaptics.github.io](https://openhaptics.github.io/) * [Getting Started](https://openhaptics.github.io/docs/getting-started) +* **NEW** [Build Guides](https://openhaptics.github.io/guides/getting-started) * [Hardware Schematics](https://github.com/openhaptics/openhaptics-hardware) * [Flashing Firmware](https://openhaptics.github.io/docs/category/flashing-firmware) @@ -58,9 +58,9 @@ Please note, that documentation is still work-in-progress | Board | Status | | :------------------- | :----------: | -| Internal ADC | 🚧 | -| MAX1704x | 🚧 | -| INA219 | 🚧 | +| Internal ADC | ✅ | +| MAX1704x | ✅ | +| INA219 | ✅ | | MCP3021 | 🚧 | | STUSB4500 | 🚧 | @@ -73,15 +73,15 @@ Please note, that documentation is still work-in-progress ## Supported devices -| Device | Supported Devices | Retail price | Hardware | -| :-------------------- | :------------------- | -----------: | :-------------------------------------------------------------------------------------------------- | -| Haptics Face Interface | Tactal | US $149 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-face-interface) | -| Haptic Gloves | TactGlove | US $299 | PWM on pins `32`, `33`, `25`, `26`, `27`, `14` | -| Haptic Sleeves | Tactosy for arms | US $249 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-forearm-sleeve) | -| Haptic Hand Gauntlet | Tactosy for hands | US $249 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-gauntlet) | -| Haptic Feet Device | Tactosy for feet | US $249 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-feet-device) | -| X16 Haptic Vest | TactSuit X16 | US $299 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#x16-haptic-vest) | -| X40 Haptic Vest | TactSuit X40, Tactot | US $499 | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#x40-haptic-vest) | +| Device | Supported Devices | Retail price | DIY Price | Hardware | +| :-------------------- | :------------------- | -----------: | --------: | :-------------------------------------------------------------------------------------------------- | +| Haptics Face Interface | Tactal | US $149 | ~20$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-face-interface) | +| Haptic Gloves | TactGlove | US $299 | ~20$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-glove) | +| Haptic Sleeves | Tactosy for arms | US $249 | ~20$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-forearm-sleeve) | +| Haptic Hand Gauntlet | Tactosy for hands | US $249 | ~20$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-gauntlet) | +| Haptic Feet Device | Tactosy for feet | US $249 | ~20$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#haptic-feet-device) | +| X16 Haptic Vest | TactSuit X16 | US $299 | ~40$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#x16-haptic-vest) | +| X40 Haptic Vest | TactSuit X40, Tactot | US $499 | ~70$ | See [Hardware Reference](https://github.com/openhaptics/openhaptics-hardware#x40-haptic-vest) | ## For Developers From 999625852c8dcb3f865ba5ac659616d0e505ef5c Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 23 Nov 2022 22:33:36 +0400 Subject: [PATCH 03/35] feat(BLE): option to use NimBLE library --- .github/codeql/codeql-config.yml | 5 ++ .github/workflows/build-bhaptics.yml | 5 ++ .github/workflows/codeql-analysis.yml | 12 +-- include/config/bluetooth.h | 24 +++--- lib/bh/connection_bhble.cpp | 110 ++++++++++++++------------ lib/bh/connection_bhble.hpp | 8 +- platformio.ini | 2 + 7 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 .github/codeql/codeql-config.yml diff --git a/.github/codeql/codeql-config.yml b/.github/codeql/codeql-config.yml new file mode 100644 index 00000000..d234b37a --- /dev/null +++ b/.github/codeql/codeql-config.yml @@ -0,0 +1,5 @@ +paths: + - firmware + - include +paths-ignore: + - .pio/libdeps diff --git a/.github/workflows/build-bhaptics.yml b/.github/workflows/build-bhaptics.yml index f31fcc3c..9fa1058c 100644 --- a/.github/workflows/build-bhaptics.yml +++ b/.github/workflows/build-bhaptics.yml @@ -98,6 +98,9 @@ jobs: serial_plotter_flag: - SERIAL_PLOTTER=false - SERIAL_PLOTTER=true + nimble_flag: + - BLUETOOTH_USE_NIMBLE=true + - BLUETOOTH_USE_NIMBLE=false steps: - uses: actions/checkout@v3 @@ -128,12 +131,14 @@ jobs: run: | sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini - name: Update build flags (macOS) if: runner.os == 'macOS' run: | sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini + sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini - name: Install PlatformIO run: | diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index c13a50f0..ecc5cffd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -1,14 +1,3 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# name: "CodeQL" on: @@ -58,6 +47,7 @@ jobs: uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} + config-file: ./.github/codeql/codeql-config.yml # If you wish to specify custom queries, you can do so here or in a config file. # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. diff --git a/include/config/bluetooth.h b/include/config/bluetooth.h index a9bca958..85c8c8f7 100644 --- a/include/config/bluetooth.h +++ b/include/config/bluetooth.h @@ -1,10 +1,14 @@ -#pragma once - -#ifndef BLUETOOTH_NAME -#define BLUETOOTH_NAME "OpenHaptics" -#endif - -#ifndef BLUETOOTH_ADDRESS -#define BLUETOOTH_ADDRESS \ - { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB } -#endif +#pragma once + +#ifndef BLUETOOTH_NAME +#define BLUETOOTH_NAME "OpenHaptics" +#endif + +#ifndef BLUETOOTH_ADDRESS +#define BLUETOOTH_ADDRESS \ + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB } +#endif + +#ifndef BLUETOOTH_USE_NIMBLE +#define BLUETOOTH_USE_NIMBLE false +#endif diff --git a/lib/bh/connection_bhble.cpp b/lib/bh/connection_bhble.cpp index 02d20d2a..43a866f4 100644 --- a/lib/bh/connection_bhble.cpp +++ b/lib/bh/connection_bhble.cpp @@ -1,12 +1,31 @@ -#include +#include "connection_bhble.hpp" #include #include #include -#include #include +#if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true + // BLE2902 not needed: https://github.com/h2zero/NimBLE-Arduino/blob/release/1.4/docs/Migration_guide.md#descriptors + + #define PROPERTY_READ NIMBLE_PROPERTY::READ + #define PROPERTY_WRITE NIMBLE_PROPERTY::WRITE + #define PROPERTY_WRITE_NR NIMBLE_PROPERTY::WRITE_NR + #define PROPERTY_BROADCAST NIMBLE_PROPERTY::BROADCAST + #define PROPERTY_NOTIFY NIMBLE_PROPERTY::NOTIFY + #define PROPERTY_INDICATE NIMBLE_PROPERTY::INDICATE +#else + #include + + #define PROPERTY_READ BLECharacteristic::PROPERTY_READ + #define PROPERTY_WRITE BLECharacteristic::PROPERTY_WRITE + #define PROPERTY_WRITE_NR BLECharacteristic::PROPERTY_WRITE_NR + #define PROPERTY_BROADCAST BLECharacteristic::PROPERTY_BROADCAST + #define PROPERTY_NOTIFY BLECharacteristic::PROPERTY_NOTIFY + #define PROPERTY_INDICATE BLECharacteristic::PROPERTY_INDICATE +#endif + class BHServerCallbacks final : public BLEServerCallbacks { public: BHServerCallbacks() {} @@ -41,9 +60,12 @@ class SerialOutputCharCallbacks : public BLECharacteristicCallbacks { pCharacteristic->getValue().length()); }; - void onStatus(BLECharacteristic* pCharacteristic, - Status s, - uint32_t code) override { + #if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true + void onStatus(BLECharacteristic* pCharacteristic, Status s, int code) override + #else + void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) override + #endif + { Serial.printf(">>\tonStatus (UUID: %s) \n", pCharacteristic->getUUID().toString().c_str()); Serial.printf("\tstatus: %d, code: %u \n", s, code); @@ -58,11 +80,10 @@ class MotorCharCallbacks : public BLECharacteristicCallbacks { bh_motor_handler_t motorTransformer; public: - MotorCharCallbacks(bh_motor_handler_t motorTransformer) - : motorTransformer(motorTransformer) {} + MotorCharCallbacks(bh_motor_handler_t motorTransformer) : motorTransformer(motorTransformer) {} void onWrite(BLECharacteristic* pCharacteristic) override { - auto value = pCharacteristic->getValue(); + std::string value = pCharacteristic->getValue(); this->motorTransformer(value); }; @@ -100,34 +121,37 @@ void BH::ConnectionBHBLE::begin() { // Each characteristic needs 2 handles and descriptor 1 handle. this->motorService = - this->bleServer->createService(BH_BLE_SERVICE_MOTOR_UUID, 25); + this->bleServer->createService(BH_BLE_SERVICE_MOTOR_UUID); { - MotorCharCallbacks* motorCallbacks = - new MotorCharCallbacks(this->motorHandler); + MotorCharCallbacks* motorCallbacks = new MotorCharCallbacks(this->motorHandler); auto* motorChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_MOTOR_UUID, - BLECharacteristic::PROPERTY_WRITE_NR); + PROPERTY_WRITE_NR + ); motorChar->setCallbacks(motorCallbacks); auto* motorCharStable = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_MOTOR_STABLE_UUID, - BLECharacteristic::PROPERTY_WRITE); + PROPERTY_WRITE + ); motorCharStable->setCallbacks(motorCallbacks); } { auto* configChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_CONFIG_UUID, - BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); + PROPERTY_READ | PROPERTY_WRITE + ); configChar->setCallbacks(new ConfigCharCallbacks()); } { auto* serialNumberChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_SERIAL_KEY_UUID, - BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); + PROPERTY_READ | PROPERTY_WRITE + ); serialNumberChar->setValue(this->config.serialNumber, BH_SERIAL_NUMBER_LENGTH); serialNumberChar->setCallbacks(new SerialOutputCharCallbacks()); } @@ -135,28 +159,29 @@ void BH::ConnectionBHBLE::begin() { { this->batteryChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_BATTERY_UUID, - BLECharacteristic::PROPERTY_READ | - BLECharacteristic:: - PROPERTY_WRITE_NR // for whatever reason, it have to be - // writable, otherwise Desktop app crashes + PROPERTY_READ | PROPERTY_WRITE_NR // for whatever reason, it have to bewritable, otherwise Desktop app crashes ); + +#if !defined(BLUETOOTH_USE_NIMBLE) || BLUETOOTH_USE_NIMBLE != true batteryChar->addDescriptor(new BLE2902()); +#endif // original bHaptics Player require non-null value for battery level, otherwise it crashes #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - uint16_t defaultLevel = 0; + uint16_t defaultLevel = 0; #else - uint16_t defaultLevel = 100; + uint16_t defaultLevel = 100; #endif - this->batteryChar->setValue(defaultLevel); - // this->batteryChar->notify(); + this->batteryChar->setValue(defaultLevel); + // this->batteryChar->notify(); } { auto* versionChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_VERSION_UUID, - BLECharacteristic::PROPERTY_READ); + PROPERTY_READ + ); versionChar->setCallbacks(new SerialOutputCharCallbacks()); uint16_t firmwareVersion = BH_FIRMWARE_VERSION; versionChar->setValue(firmwareVersion); @@ -165,54 +190,39 @@ void BH::ConnectionBHBLE::begin() { { auto* monitorChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_TACTSUIT_MONITOR_UUID, - BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | - BLECharacteristic::PROPERTY_NOTIFY | - BLECharacteristic::PROPERTY_BROADCAST | - BLECharacteristic::PROPERTY_INDICATE | - BLECharacteristic::PROPERTY_WRITE_NR); + PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR + ); monitorChar->setCallbacks(new SerialOutputCharCallbacks()); + +#if !defined(BLUETOOTH_USE_NIMBLE) || BLUETOOTH_USE_NIMBLE != true monitorChar->addDescriptor(new BLE2902()); +#endif + uint16_t audioCableState = NO_AUDIO_CABLE; monitorChar->setValue(audioCableState); } // auto* athGlobalChar = this->motorService->createCharacteristic( // BH_BLE_SERVICE_MOTOR_CHAR_ATH_GLOBAL_CONF_UUID, - // BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | - // BLECharacteristic::PROPERTY_NOTIFY | - // BLECharacteristic::PROPERTY_BROADCAST| - // BLECharacteristic::PROPERTY_INDICATE| - // BLECharacteristic::PROPERTY_WRITE_NR + // PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR // ); // athGlobalChar->setCallbacks(new SerialOutputCharCallbacks()); // auto* athThemeChar = this->motorService->createCharacteristic( // BH_BLE_SERVICE_MOTOR_CHAR_ATH_THEME_UUID, - // BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | - // BLECharacteristic::PROPERTY_NOTIFY | - // BLECharacteristic::PROPERTY_BROADCAST| - // BLECharacteristic::PROPERTY_INDICATE| - // BLECharacteristic::PROPERTY_WRITE_NR + // PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR // ); // athThemeChar->setCallbacks(new SerialOutputCharCallbacks()); // auto* motorMappingChar = this->motorService->createCharacteristic( // BH_BLE_SERVICE_MOTOR_CHAR_MOTTOR_MAPPING_UUID, - // BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | - // BLECharacteristic::PROPERTY_NOTIFY | - // BLECharacteristic::PROPERTY_BROADCAST| - // BLECharacteristic::PROPERTY_INDICATE| - // BLECharacteristic::PROPERTY_WRITE_NR + // PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR // ); // motorMappingChar->setCallbacks(new SerialOutputCharCallbacks()); // auto* signatureMappingChar = this->motorService->createCharacteristic( // BH_BLE_SERVICE_MOTOR_CHAR_SIGNATURE_PATTERN_UUID, - // BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | - // BLECharacteristic::PROPERTY_NOTIFY | - // BLECharacteristic::PROPERTY_BROADCAST| - // BLECharacteristic::PROPERTY_INDICATE| - // BLECharacteristic::PROPERTY_WRITE_NR + // PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR // ); // signatureMappingChar->setCallbacks(new SerialOutputCharCallbacks()); @@ -223,7 +233,7 @@ void BH::ConnectionBHBLE::begin() { auto* dfuControlChar = dfuService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_SIGNATURE_PATTERN_UUID, - BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); + PROPERTY_READ | PROPERTY_WRITE); dfuService->start(); } diff --git a/lib/bh/connection_bhble.hpp b/lib/bh/connection_bhble.hpp index b7070e15..fe5ffc1a 100644 --- a/lib/bh/connection_bhble.hpp +++ b/lib/bh/connection_bhble.hpp @@ -6,12 +6,16 @@ #include #include -#include #include +#if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true + #include +#else + #include +#endif + #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true #include "abstract_battery.hpp" -#include #endif // typedef void (*bh_motor_handler_t)(std::string&); diff --git a/platformio.ini b/platformio.ini index 4115d612..9877431f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,7 @@ build_flags = ; -D DEBUG_ESP_PORT=Serial ; -D SERIAL_PLOTTER=true -D BATTERY_ENABLED=true + -D BLUETOOTH_USE_NIMBLE=true build_unflags = -std=gnu++11 build_src_filter = @@ -36,6 +37,7 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.0 adafruit/Adafruit INA219@^1.2.1 sparkfun/SparkFun MAX1704x Fuel Gauge Arduino Library@^1.0.4 + h2zero/NimBLE-Arduino@^1.4.0 [env] framework = arduino From e58b1a602a5c3b72155fadc8f707c4c9f188d6b8 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 17:47:27 +0400 Subject: [PATCH 04/35] refactor: remove sumb Serial logging --- .../mode_configs/bhaptics/tactsuit_x40.cpp | 1 - firmware/openhaptics.cpp | 2 +- lib/bh/connection_bhble.cpp | 36 +++++-------------- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp index a2275dc5..49e22804 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp @@ -70,7 +70,6 @@ void setupMode() { .serialNumber = serialNumber, }; AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - Serial.println(value.c_str()); vestOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize); }, app); bhBleConnection->begin(); diff --git a/firmware/openhaptics.cpp b/firmware/openhaptics.cpp index 2f48d44f..88824472 100644 --- a/firmware/openhaptics.cpp +++ b/firmware/openhaptics.cpp @@ -27,7 +27,7 @@ void OpenHaptics::registerComponent(OH::IComponent* component) { } void OpenHaptics::postEvent(const OH::IEvent* event) { - log_i("Event dispatched at %u: %s (%p)", millis(), event->eventName.c_str(), event); + log_d("Event dispatched at %u: %s (%p)", millis(), event->eventName.c_str(), event); for (auto* listener : this->eventListeners) { listener->handleEvent(event); diff --git a/lib/bh/connection_bhble.cpp b/lib/bh/connection_bhble.cpp index 43a866f4..1c2fca67 100644 --- a/lib/bh/connection_bhble.cpp +++ b/lib/bh/connection_bhble.cpp @@ -4,7 +4,6 @@ #include #include -#include #if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true // BLE2902 not needed: https://github.com/h2zero/NimBLE-Arduino/blob/release/1.4/docs/Migration_guide.md#descriptors @@ -37,27 +36,15 @@ class BHServerCallbacks final : public BLEServerCallbacks { class SerialOutputCharCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic* pCharacteristic) override { - Serial.printf(">>\tonWrite (UUID: %s) \n", - pCharacteristic->getUUID().toString().c_str()); - Serial.printf("\tvalue: `%s`, len: %u \n", - pCharacteristic->getValue().c_str(), - pCharacteristic->getValue().length()); + log_d(">>\tonWrite (UUID: %s)\n\tvalue: `%s`, len: %u", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; void onRead(BLECharacteristic* pCharacteristic) override { - Serial.printf(">>\tonRead (UUID: %s) \n", - pCharacteristic->getUUID().toString().c_str()); - Serial.printf("\tvalue: `%s`, len: %u \n", - pCharacteristic->getValue().c_str(), - pCharacteristic->getValue().length()); + log_d(">>\tonRead (UUID: %s)\n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; void onNotify(BLECharacteristic* pCharacteristic) override { - Serial.printf(">>\tonNotify (UUID: %s) \n", - pCharacteristic->getUUID().toString().c_str()); - Serial.printf("\tvalue: `%s`, len: %u \n", - pCharacteristic->getValue().c_str(), - pCharacteristic->getValue().length()); + log_d(">>\tonNotify (UUID: %s)\n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; #if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true @@ -66,12 +53,7 @@ class SerialOutputCharCallbacks : public BLECharacteristicCallbacks { void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) override #endif { - Serial.printf(">>\tonStatus (UUID: %s) \n", - pCharacteristic->getUUID().toString().c_str()); - Serial.printf("\tstatus: %d, code: %u \n", s, code); - Serial.printf("\tvalue: `%s`, len: %u \n", - pCharacteristic->getValue().c_str(), - pCharacteristic->getValue().length()); + log_d(">>\tonNotify (UUID: %s)\n\tstatus: %d, code: %u \n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), s, code, pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; }; @@ -97,11 +79,11 @@ class ConfigCharCallbacks : public BLECharacteristicCallbacks { return; } - auto byte_0 = value[0], byte_1 = value[1], - byte_2 = value[2]; // this is the only byte, that ever changes + auto byte_0 = value[0], + byte_1 = value[1], + byte_2 = value[2]; - // Serial.printf(">>\tonWrite (Config Char): %3hhu %2hhu %2hhu \n", byte_0, - // byte_1, byte_2); + log_d(">>\tonWrite (Config Char): %3hhu %2hhu %2hhu", byte_0, byte_1, byte_2); }; }; @@ -159,7 +141,7 @@ void BH::ConnectionBHBLE::begin() { { this->batteryChar = this->motorService->createCharacteristic( BH_BLE_SERVICE_MOTOR_CHAR_BATTERY_UUID, - PROPERTY_READ | PROPERTY_WRITE_NR // for whatever reason, it have to bewritable, otherwise Desktop app crashes + PROPERTY_READ | PROPERTY_WRITE_NR | PROPERTY_NOTIFY // for whatever reason, it have to bewritable, otherwise Desktop app crashes ); #if !defined(BLUETOOTH_USE_NIMBLE) || BLUETOOTH_USE_NIMBLE != true From 3f0211f7d1310265d9e69b0b527ed2ddef0c33a4 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 17:50:40 +0400 Subject: [PATCH 05/35] build(PlatformIO): use defines for repeated configs Close #34 --- firmware/firmware.cpp | 8 +++++--- lib/arduino/components/serial_plotter.hpp | 6 +++++- platformio.ini | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index 43a84b53..6ddf337a 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -8,6 +8,10 @@ #include #endif // SERIAL_PLOTTER +#ifndef SERIAL_PLOTTER_PORT +#define SERIAL_PLOTTER_PORT Serial +#endif // SERIAL_PLOTTER_PORT + #ifndef PIO_UNIT_TESTING extern void setupMode(); @@ -17,12 +21,10 @@ extern void setupMode(); OpenHaptics App; void setup() { - Serial.begin(115200); - setupMode(); #if defined(SERIAL_PLOTTER) && SERIAL_PLOTTER == true - auto* serialOutputState = new OH::SerialPlotter_OutputStates(Serial, App.getOutput()); + auto* serialOutputState = new OH::SerialPlotter_OutputStates(SERIAL_PLOTTER_PORT, App.getOutput()); serialOutputState->begin(); #endif // SERIAL_PLOTTER diff --git a/lib/arduino/components/serial_plotter.hpp b/lib/arduino/components/serial_plotter.hpp index 1ec93a53..b7b63d41 100644 --- a/lib/arduino/components/serial_plotter.hpp +++ b/lib/arduino/components/serial_plotter.hpp @@ -5,6 +5,10 @@ #include +#ifndef SERIAL_PLOTTER_BAUD +#define SERIAL_PLOTTER_BAUD 115200 +#endif // SERIAL_PLOTTER_BAUD + namespace OH { /** * Component, that prints the current state of the output to the serial port in Arduino's Serial Plotter format @@ -42,7 +46,7 @@ namespace OH { */ template<> inline void SerialPlotter_OutputStates::setup() { - this->serial->begin(115200); + this->serial->begin(SERIAL_PLOTTER_BAUD); } template class SerialPlotter_OutputStates; diff --git a/platformio.ini b/platformio.ini index 9877431f..9f9bf122 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,8 +23,9 @@ build_flags = ; -D DEBUG_MODE=0 ; -D DEBUG_ESP_PORT=Serial ; -D SERIAL_PLOTTER=true - -D BATTERY_ENABLED=true - -D BLUETOOTH_USE_NIMBLE=true +; -D BATTERY_ENABLED=true +; -D BLUETOOTH_USE_NIMBLE=false + build_unflags = -std=gnu++11 build_src_filter = From 320174b2732cefc3bc99882947ea2dc3370bc9a6 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 18:44:07 +0400 Subject: [PATCH 06/35] chore: update test --- firmware/mode_configs/test.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index e1e04c8d..f8acce8f 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -6,12 +6,21 @@ #include "openhaptics.h" -#include "connections/bhaptics.h" +#include +#include +#include +#include #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true -#include "battery/adc_battery.h" +#include #endif +using namespace OH; +using namespace BH; + +extern OpenHaptics App; +OpenHaptics* app = &App; + class TestOutput : public OH::AbstractOutputWriter { private: uint8_t channel; @@ -38,9 +47,14 @@ void setupMode() { kv.first.y); } - auto test = new ClosestOutputComponent(testOutputs); + auto test = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, testOutputs); + + oh_output_writers_map_t layout{ + {oh_output_point_t(0, 0), new TestOutput(8)}, + }; + auto test2 = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, layout); - App.getOutput()->addComponent(OUTPUT_PATH_ACCESSORY, test); + App.getOutput()->addComponent(test); for (auto i = 0; i < testOutputs.size(); i++) { oh_output_data_t outData{ From 6c3a1e9db40dd738921be544ca7bcb954416355a Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 19:00:25 +0400 Subject: [PATCH 07/35] refactor(Output): add margins CC #26 --- lib/output/output.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/output/output.hpp b/lib/output/output.hpp index a798d07f..b1bea3c0 100644 --- a/lib/output/output.hpp +++ b/lib/output/output.hpp @@ -34,9 +34,9 @@ namespace OH { * @tparam _Tp The type of the point index. */ template - oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { - const oh_output_coord_t x_coord = map(x, 0, x_max != 0 ? x_max : 1, 0, OH_OUTPUT_COORD_MAX); - const oh_output_coord_t y_coord = map(y, 0, y_max != 0 ? y_max : 1, 0, OH_OUTPUT_COORD_MAX); + inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { + const oh_output_coord_t x_coord = map(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); + const oh_output_coord_t y_coord = map(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); return new oh_output_point_t(x_coord, y_coord); } From 25f4752743a883c8d0bdefc873b22a7f5888b44b Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 19:05:42 +0400 Subject: [PATCH 08/35] ci(GitHub): update release action --- .github/workflows/release.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8c1e59d5..a0621f45 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,6 +27,10 @@ jobs: - bhaptics_tactal - bhaptics_tactglove_left - bhaptics_tactglove_right + battery_flag: + - BATTERY_ENABLED=true + nimble_flag: + - BLUETOOTH_USE_NIMBLE=true steps: - uses: actions/checkout@v3 @@ -64,6 +68,11 @@ jobs: sed -i '/\[env\]/p; s/\[env\]/upload_protocol = custom/' platformio.ini sed -i '/\[env\]/p; s/\[env\]/upload_command = \$PYTHONEXE .\/scripts\/ci\/create-archive.py \$FLASH_EXTRA_IMAGES \$ESP32_APP_OFFSET \$SOURCE/' platformio.ini + - name: Update build flags (non-macOS) + run: | + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini + - name: Build run: | mkdir build From b85f251d7d9b284f2edc3d1281f019bfc522bc78 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 22:40:21 +0400 Subject: [PATCH 09/35] style(bHaptics): unify modes configuration --- firmware/mode_configs/bhaptics/tactal.cpp | 13 ++----------- firmware/mode_configs/bhaptics/tactosyf.cpp | 6 +++--- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/firmware/mode_configs/bhaptics/tactal.cpp b/firmware/mode_configs/bhaptics/tactal.cpp index 719694d9..51f8da52 100644 --- a/firmware/mode_configs/bhaptics/tactal.cpp +++ b/firmware/mode_configs/bhaptics/tactal.cpp @@ -20,17 +20,8 @@ using namespace BH; extern OpenHaptics App; OpenHaptics* app = &App; -// ========= Output ========= - -const size_t bhLayoutSize = BH_LAYOUT_TACTAL_SIZE; -const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; - -// ========= Connection ========= - -// ========= Battery ========= - -#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true -#endif +static const size_t bhLayoutSize = BH_LAYOUT_TACTAL_SIZE; +static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; void setupMode() { // Configure PWM pins to their positions on the face diff --git a/firmware/mode_configs/bhaptics/tactosyf.cpp b/firmware/mode_configs/bhaptics/tactosyf.cpp index 29fb0ded..ad498997 100644 --- a/firmware/mode_configs/bhaptics/tactosyf.cpp +++ b/firmware/mode_configs/bhaptics/tactosyf.cpp @@ -20,10 +20,10 @@ using namespace BH; extern OpenHaptics App; OpenHaptics* app = &App; -void setupMode() { - static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYF_SIZE; - static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; +static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYF_SIZE; +static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; +void setupMode() { // Configure PWM pins to their positions on the feet auto footOutputs = mapMatrixCoordinates({ // clang-format off From 56cfa1b070a6d673848f24df24b5624a164427fb Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Sun, 26 Mar 2023 18:13:23 +0400 Subject: [PATCH 10/35] refactor(Core): add global throttled sensor component --- lib/arduino/battery/adc_naive.cpp | 2 +- lib/arduino/battery/adc_naive.hpp | 2 +- lib/arduino/components/serial_plotter.cpp | 2 +- lib/core/abstract_battery.cpp | 8 ++++---- lib/core/abstract_battery.hpp | 1 - lib/core/abstract_component.hpp | 3 --- lib/core/sensor.hpp | 22 ++++++++++++++++++++++ lib/core/task.hpp | 3 --- lib/ina219/battery/ina219.cpp | 2 +- lib/ina219/battery/ina219.hpp | 2 +- lib/max17048/battery/max17048.cpp | 2 +- lib/max17048/battery/max17048.hpp | 2 +- lib/output/output_component.hpp | 2 +- 13 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/arduino/battery/adc_naive.cpp b/lib/arduino/battery/adc_naive.cpp index d16dcddc..7d396997 100644 --- a/lib/arduino/battery/adc_naive.cpp +++ b/lib/arduino/battery/adc_naive.cpp @@ -6,6 +6,6 @@ void OH::ADCNaiveBattery::setup() { pinMode(this->pin, INPUT); } -uint8_t OH::ADCNaiveBattery::updateLevel() { +uint8_t OH::ADCNaiveBattery::updateValue() { return map(analogRead(this->pin), 0.0f, 4095.0f, 0, 255); } diff --git a/lib/arduino/battery/adc_naive.hpp b/lib/arduino/battery/adc_naive.hpp index c877caa6..6ab5a257 100644 --- a/lib/arduino/battery/adc_naive.hpp +++ b/lib/arduino/battery/adc_naive.hpp @@ -8,7 +8,7 @@ namespace OH { uint8_t pin; protected: - uint8_t updateLevel() override; + uint8_t updateValue() override; public: ADCNaiveBattery(const uint8_t pin, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, TaskConfig taskConfig = { "ADC Battery", 2048, BATTERY_TASK_PRIORITY, tskNO_AFFINITY }) diff --git a/lib/arduino/components/serial_plotter.cpp b/lib/arduino/components/serial_plotter.cpp index 8d5a6cbc..f91a77f1 100644 --- a/lib/arduino/components/serial_plotter.cpp +++ b/lib/arduino/components/serial_plotter.cpp @@ -2,7 +2,7 @@ template void OH::SerialPlotter_OutputStates<_Tp>::run() { - while (1) { + while (true) { for (auto& _c : *output->getComponents()) { oh_output_path_t path = _c.first; OH::OutputComponent* component = _c.second; diff --git a/lib/core/abstract_battery.cpp b/lib/core/abstract_battery.cpp index 6bf5b9fa..01c0cd52 100644 --- a/lib/core/abstract_battery.cpp +++ b/lib/core/abstract_battery.cpp @@ -5,10 +5,10 @@ #endif void OH::AbstractBattery::run() { - while (1) { - this->level = this->updateLevel(); - this->eventDispatcher->postEvent(new BatteryLevelEvent(this->level)); + while (true) { + this->value = this->updateValue(); + this->eventDispatcher->postEvent(new BatteryLevelEvent(this->value)); - delay(this->config.sampleRate); + delay(this->rate); } }; diff --git a/lib/core/abstract_battery.hpp b/lib/core/abstract_battery.hpp index 43537e33..3538507c 100644 --- a/lib/core/abstract_battery.hpp +++ b/lib/core/abstract_battery.hpp @@ -28,7 +28,6 @@ namespace OH { private: void run(void); protected: - BatteryConfig config; IEventDispatcher* eventDispatcher; uint8_t level = 0; // 0 = min, 255 = max diff --git a/lib/core/abstract_component.hpp b/lib/core/abstract_component.hpp index 7610bc76..2fde48a1 100644 --- a/lib/core/abstract_component.hpp +++ b/lib/core/abstract_component.hpp @@ -13,9 +13,6 @@ namespace OH { virtual void setup(void) {}; }; - class AbstractComponent : public IComponent { - }; - template class IComponentRegistry { public: diff --git a/lib/core/sensor.hpp b/lib/core/sensor.hpp index 554c2307..3043acb9 100644 --- a/lib/core/sensor.hpp +++ b/lib/core/sensor.hpp @@ -1,5 +1,7 @@ #pragma once +#include + namespace OH { template @@ -7,4 +9,24 @@ namespace OH public: virtual _Tp getValue() = 0; }; + + template + class RatePollingComponent : public Task>, public ISensor<_Tp> { + template friend class RatePollingComponent; + + protected: + _Tp value; + int rate; + + public: + RatePollingComponent(TaskConfig taskConfig, int rate) : TaskComponent(taskConfig), rate(rate) {}; + void run(void) override { + while (true) { + this->value = this->updateValue(); + delay(this->rate); + } + }; + _Tp getValue() override { return this->value; }; + virtual _Tp updateValue(void) = 0; + }; } // namespace OH diff --git a/lib/core/task.hpp b/lib/core/task.hpp index 43b482b7..5caf27d9 100644 --- a/lib/core/task.hpp +++ b/lib/core/task.hpp @@ -14,8 +14,6 @@ namespace OH { const BaseType_t coreId = tskNO_AFFINITY; }; - class TaskComponent; - // Static polymorphic abstract base class for a FreeRTOS task using CRTP // pattern. Concrete implementations should implement a run() method. // @@ -23,7 +21,6 @@ namespace OH { template class Task { template friend class Task; - friend class TaskComponent; private: TaskConfig taskConfig; diff --git a/lib/ina219/battery/ina219.cpp b/lib/ina219/battery/ina219.cpp index 02993f9f..ed882f00 100644 --- a/lib/ina219/battery/ina219.cpp +++ b/lib/ina219/battery/ina219.cpp @@ -4,7 +4,7 @@ void OH::INA219_Battery::setup() { this->active = this->sensor->begin(); } -uint8_t OH::INA219_Battery::updateLevel() { +uint8_t OH::INA219_Battery::updateValue() { if (!this->active) { return 0; } diff --git a/lib/ina219/battery/ina219.hpp b/lib/ina219/battery/ina219.hpp index 416b7b2d..d6d305cc 100644 --- a/lib/ina219/battery/ina219.hpp +++ b/lib/ina219/battery/ina219.hpp @@ -11,7 +11,7 @@ namespace OH { Adafruit_INA219* sensor; protected: - uint8_t updateLevel() override; + uint8_t updateValue() override; public: INA219_Battery(Adafruit_INA219* sensor, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, const BaseType_t coreId = tskNO_AFFINITY) diff --git a/lib/max17048/battery/max17048.cpp b/lib/max17048/battery/max17048.cpp index a32560c8..04216506 100644 --- a/lib/max17048/battery/max17048.cpp +++ b/lib/max17048/battery/max17048.cpp @@ -17,7 +17,7 @@ void OH::MAX1704_Battery::setup() { } } -uint8_t OH::MAX1704_Battery::updateLevel() { +uint8_t OH::MAX1704_Battery::updateValue() { if (!this->active) { return 0; } diff --git a/lib/max17048/battery/max17048.hpp b/lib/max17048/battery/max17048.hpp index cf5f7659..4558f6c3 100644 --- a/lib/max17048/battery/max17048.hpp +++ b/lib/max17048/battery/max17048.hpp @@ -11,7 +11,7 @@ namespace OH { SFE_MAX1704X* gauge; protected: - uint8_t updateLevel() override; + uint8_t updateValue() override; public: MAX1704_Battery(SFE_MAX1704X* gauge, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, const BaseType_t coreId) diff --git a/lib/output/output_component.hpp b/lib/output/output_component.hpp index c4bfe643..8be99de2 100644 --- a/lib/output/output_component.hpp +++ b/lib/output/output_component.hpp @@ -13,7 +13,7 @@ namespace OH { /** * Output "plane" (e.g. Chest, Palm, Finger, etc.) */ - class OutputComponent : public OH::AbstractComponent { + class OutputComponent : public OH::IComponent { private: oh_output_path_t path; protected: From 2fb05b1904eea9e1f5617c9eaf9766ab588bd5fb Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 28 Mar 2023 15:57:56 +0400 Subject: [PATCH 11/35] feat(OpenGloves): add communication base --- lib/opengloves/og_communication.hpp | 9 +++++++ .../og_communication/bluetooth_serial.hpp | 22 +++++++++++++++++ lib/opengloves/og_communication/serial.hpp | 24 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/opengloves/og_communication.hpp create mode 100644 lib/opengloves/og_communication/bluetooth_serial.hpp create mode 100644 lib/opengloves/og_communication/serial.hpp diff --git a/lib/opengloves/og_communication.hpp b/lib/opengloves/og_communication.hpp new file mode 100644 index 00000000..4915e1ff --- /dev/null +++ b/lib/opengloves/og_communication.hpp @@ -0,0 +1,9 @@ +#pragma once + +namespace OG { + struct ICommunication { + virtual void begin() = 0; + + virtual void send(char* data) = 0; + }; +} diff --git a/lib/opengloves/og_communication/bluetooth_serial.hpp b/lib/opengloves/og_communication/bluetooth_serial.hpp new file mode 100644 index 00000000..fa0753c1 --- /dev/null +++ b/lib/opengloves/og_communication/bluetooth_serial.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "og_communication.hpp" +#include "og_communication/serial.hpp" + +#include + +namespace OG { + class OG_BluetoothSerialCommunication : public OG_SerialCommunication { + private: + BluetoothSerial* serial; + const char* deviceName; + + public: + OG_BluetoothSerialCommunication(BluetoothSerial& serial, const char* deviceName) + : OG_SerialCommunication(serial), serial(&serial), deviceName(deviceName) {}; + + void begin() override { + this->serial->begin(deviceName); + }; + }; +} diff --git a/lib/opengloves/og_communication/serial.hpp b/lib/opengloves/og_communication/serial.hpp new file mode 100644 index 00000000..a0505567 --- /dev/null +++ b/lib/opengloves/og_communication/serial.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "og_communication.hpp" + +#include + +namespace OG { + + /** + * @tparam _Tp the type of the serial port + */ + template + class OG_SerialCommunication : public ICommunication { + private: + _Tp* serial; + + public: + OG_SerialCommunication(_Tp& serial) : serial(&serial) {}; + + void send(char* data) override { + this->serial->write(data); + }; + }; +} // namespace OG From c28fa80c03bec95cc33faad4cf28f684268d266f Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 14:19:02 +0400 Subject: [PATCH 12/35] refactor(Battery): use Tasked Sensor --- include/config/battery.h | 2 +- lib/arduino/battery/adc_naive.hpp | 4 ++-- lib/core/abstract_battery.hpp | 17 ++++++++--------- lib/core/sensor.hpp | 19 +++++++++++-------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/config/battery.h b/include/config/battery.h index b77cea1c..9c34c5e8 100644 --- a/include/config/battery.h +++ b/include/config/battery.h @@ -5,7 +5,7 @@ #endif #ifndef BATTERY_SAMPLE_RATE -#define BATTERY_SAMPLE_RATE 60000 +#define BATTERY_SAMPLE_RATE 10000 #endif #ifndef BATTERY_THRESHOLD_PERCENTAGE diff --git a/lib/arduino/battery/adc_naive.hpp b/lib/arduino/battery/adc_naive.hpp index 6ab5a257..886292ed 100644 --- a/lib/arduino/battery/adc_naive.hpp +++ b/lib/arduino/battery/adc_naive.hpp @@ -11,10 +11,10 @@ namespace OH { uint8_t updateValue() override; public: - ADCNaiveBattery(const uint8_t pin, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, TaskConfig taskConfig = { "ADC Battery", 2048, BATTERY_TASK_PRIORITY, tskNO_AFFINITY }) + ADCNaiveBattery(const uint8_t pin, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, TaskConfig taskConfig = { "ADC Battery", 4096, BATTERY_TASK_PRIORITY, tskNO_AFFINITY }) : AbstractBattery(config, eventDispatcher, taskConfig), pin(pin) {}; ADCNaiveBattery(const uint8_t pin, OH::BatteryConfig config, OH::IEventDispatcher* eventDispatcher, const BaseType_t coreId = tskNO_AFFINITY) - : AbstractBattery(config, eventDispatcher, { "ADC Battery", 2048, BATTERY_TASK_PRIORITY, coreId }), pin(pin) {}; + : AbstractBattery(config, eventDispatcher, { "ADC Battery", 4096, BATTERY_TASK_PRIORITY, coreId }), pin(pin) {}; void setup() override; }; diff --git a/lib/core/abstract_battery.hpp b/lib/core/abstract_battery.hpp index 3538507c..2d4b62b3 100644 --- a/lib/core/abstract_battery.hpp +++ b/lib/core/abstract_battery.hpp @@ -19,27 +19,26 @@ namespace OH { }; struct BatteryConfig { - int sampleRate; + uint sampleRate; }; - class AbstractBattery : public Task, public ISensor { - friend class Task; + class AbstractBattery : public RatePollingComponent { + friend class Task>; + friend class RatePollingComponent; private: - void run(void); + void run(void) override; protected: IEventDispatcher* eventDispatcher; - uint8_t level = 0; // 0 = min, 255 = max virtual void setup(void) {}; - virtual uint8_t updateLevel(void) = 0; + uint8_t updateValue() override; public: - AbstractBattery(BatteryConfig config, IEventDispatcher* eventDispatcher, TaskConfig taskConfig) : Task(taskConfig), config(config), eventDispatcher(eventDispatcher) {}; - uint8_t getValue() override { return this->level; }; + AbstractBattery(BatteryConfig config, IEventDispatcher* eventDispatcher, TaskConfig taskConfig) : RatePollingComponent(taskConfig, config.sampleRate), eventDispatcher(eventDispatcher) {}; void begin() override { this->setup(); - OH::Task::begin(); + RatePollingComponent::begin(); }; }; diff --git a/lib/core/sensor.hpp b/lib/core/sensor.hpp index 3043acb9..09c8eaa6 100644 --- a/lib/core/sensor.hpp +++ b/lib/core/sensor.hpp @@ -13,20 +13,23 @@ namespace OH template class RatePollingComponent : public Task>, public ISensor<_Tp> { template friend class RatePollingComponent; + friend class Task>; - protected: - _Tp value; - int rate; - - public: - RatePollingComponent(TaskConfig taskConfig, int rate) : TaskComponent(taskConfig), rate(rate) {}; - void run(void) override { + private: + virtual void run(void) { while (true) { this->value = this->updateValue(); delay(this->rate); } }; - _Tp getValue() override { return this->value; }; + + protected: + _Tp value; + uint rate; virtual _Tp updateValue(void) = 0; + + public: + RatePollingComponent(TaskConfig taskConfig, uint rate) : Task>(taskConfig), rate(rate) {}; + _Tp getValue() override { return this->value; }; }; } // namespace OH From d38865f9e8f2e40802bde053a7538a45bf7d0fe1 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 15:42:50 +0400 Subject: [PATCH 13/35] feat(bHaptics): add BLE callbacks/hooks --- firmware/firmware.cpp | 7 ++----- firmware/mode_configs/bhaptics/tactal.cpp | 5 +++++ firmware/mode_configs/bhaptics/tactglove.cpp | 5 +++++ firmware/mode_configs/bhaptics/tactosy2.cpp | 5 +++++ firmware/mode_configs/bhaptics/tactosyf.cpp | 5 +++++ firmware/mode_configs/bhaptics/tactosyh.cpp | 5 +++++ .../mode_configs/bhaptics/tactsuit_x16.cpp | 5 +++++ .../bhaptics/tactsuit_x16_pca9685.cpp | 5 +++++ .../mode_configs/bhaptics/tactsuit_x40.cpp | 5 +++++ firmware/mode_configs/test.cpp | 5 +++++ lib/{bh => bhaptics}/bh_constants.hpp | 0 lib/{bh => bhaptics}/bh_types.hpp | 0 lib/{bh => bhaptics}/bh_types_c.h | 0 lib/{bh => bhaptics}/bh_utils.cpp | 0 lib/{bh => bhaptics}/bh_utils.hpp | 0 lib/{bh => bhaptics}/connection_bhble.cpp | 8 +++++--- lib/{bh => bhaptics}/connection_bhble.hpp | 19 +++++++++++++++++++ 17 files changed, 71 insertions(+), 8 deletions(-) rename lib/{bh => bhaptics}/bh_constants.hpp (100%) rename lib/{bh => bhaptics}/bh_types.hpp (100%) rename lib/{bh => bhaptics}/bh_types_c.h (100%) rename lib/{bh => bhaptics}/bh_utils.cpp (100%) rename lib/{bh => bhaptics}/bh_utils.hpp (100%) rename lib/{bh => bhaptics}/connection_bhble.cpp (93%) rename lib/{bh => bhaptics}/connection_bhble.hpp (80%) diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index 6ddf337a..e3126d3e 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -15,6 +15,7 @@ #ifndef PIO_UNIT_TESTING extern void setupMode(); +extern void loopMode(); #if defined(ARDUINO) @@ -27,14 +28,10 @@ void setup() { auto* serialOutputState = new OH::SerialPlotter_OutputStates(SERIAL_PLOTTER_PORT, App.getOutput()); serialOutputState->begin(); #endif // SERIAL_PLOTTER - - // Free up the Arduino loop task - vTaskDelete(NULL); } void loop() { - // Arduino loop task is deleted in setup() - // Delete the `vTaskDelete(NULL);` line in setup() to enable this function + loopMode(); } #endif // ARDUINO diff --git a/firmware/mode_configs/bhaptics/tactal.cpp b/firmware/mode_configs/bhaptics/tactal.cpp index 51f8da52..b0f64f29 100644 --- a/firmware/mode_configs/bhaptics/tactal.cpp +++ b/firmware/mode_configs/bhaptics/tactal.cpp @@ -52,3 +52,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactglove.cpp b/firmware/mode_configs/bhaptics/tactglove.cpp index 1a642a05..7aadf413 100644 --- a/firmware/mode_configs/bhaptics/tactglove.cpp +++ b/firmware/mode_configs/bhaptics/tactglove.cpp @@ -81,3 +81,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactosy2.cpp b/firmware/mode_configs/bhaptics/tactosy2.cpp index 36129d18..d42bc0cb 100644 --- a/firmware/mode_configs/bhaptics/tactosy2.cpp +++ b/firmware/mode_configs/bhaptics/tactosy2.cpp @@ -53,3 +53,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactosyf.cpp b/firmware/mode_configs/bhaptics/tactosyf.cpp index ad498997..aeadd6c1 100644 --- a/firmware/mode_configs/bhaptics/tactosyf.cpp +++ b/firmware/mode_configs/bhaptics/tactosyf.cpp @@ -54,3 +54,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactosyh.cpp b/firmware/mode_configs/bhaptics/tactosyh.cpp index 2f6c9a1e..d40cec5d 100644 --- a/firmware/mode_configs/bhaptics/tactosyh.cpp +++ b/firmware/mode_configs/bhaptics/tactosyh.cpp @@ -54,3 +54,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp index 13e79cc2..23dd5ce4 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp @@ -66,3 +66,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp index 381ea9ec..accafac9 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp @@ -71,3 +71,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp index 49e22804..2e968081 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp @@ -79,3 +79,8 @@ void setupMode() { battery->begin(); #endif } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index f8acce8f..d18b47ab 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -64,3 +64,8 @@ void setupMode() { App.getOutput()->writeOutput(OUTPUT_PATH_ACCESSORY, outData); } } + +void loopMode() { + // Free up the Arduino loop task + vTaskDelete(NULL); +} diff --git a/lib/bh/bh_constants.hpp b/lib/bhaptics/bh_constants.hpp similarity index 100% rename from lib/bh/bh_constants.hpp rename to lib/bhaptics/bh_constants.hpp diff --git a/lib/bh/bh_types.hpp b/lib/bhaptics/bh_types.hpp similarity index 100% rename from lib/bh/bh_types.hpp rename to lib/bhaptics/bh_types.hpp diff --git a/lib/bh/bh_types_c.h b/lib/bhaptics/bh_types_c.h similarity index 100% rename from lib/bh/bh_types_c.h rename to lib/bhaptics/bh_types_c.h diff --git a/lib/bh/bh_utils.cpp b/lib/bhaptics/bh_utils.cpp similarity index 100% rename from lib/bh/bh_utils.cpp rename to lib/bhaptics/bh_utils.cpp diff --git a/lib/bh/bh_utils.hpp b/lib/bhaptics/bh_utils.hpp similarity index 100% rename from lib/bh/bh_utils.hpp rename to lib/bhaptics/bh_utils.hpp diff --git a/lib/bh/connection_bhble.cpp b/lib/bhaptics/connection_bhble.cpp similarity index 93% rename from lib/bh/connection_bhble.cpp rename to lib/bhaptics/connection_bhble.cpp index 1c2fca67..fb86ed89 100644 --- a/lib/bh/connection_bhble.cpp +++ b/lib/bhaptics/connection_bhble.cpp @@ -40,11 +40,11 @@ class SerialOutputCharCallbacks : public BLECharacteristicCallbacks { }; void onRead(BLECharacteristic* pCharacteristic) override { - log_d(">>\tonRead (UUID: %s)\n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); + log_d(">>\tonRead (UUID: %s)\n\tvalue: `%s`, len: %u", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; void onNotify(BLECharacteristic* pCharacteristic) override { - log_d(">>\tonNotify (UUID: %s)\n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); + log_d(">>\tonNotify (UUID: %s)\n\tvalue: `%s`, len: %u", pCharacteristic->getUUID().toString().c_str(), pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; #if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true @@ -53,7 +53,7 @@ class SerialOutputCharCallbacks : public BLECharacteristicCallbacks { void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) override #endif { - log_d(">>\tonNotify (UUID: %s)\n\tstatus: %d, code: %u \n\tvalue: `%s`, len: %u \n", pCharacteristic->getUUID().toString().c_str(), s, code, pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); + log_d(">>\tonNotify (UUID: %s)\n\tstatus: %d, code: %u \n\tvalue: `%s`, len: %u", pCharacteristic->getUUID().toString().c_str(), s, code, pCharacteristic->getValue().c_str(), pCharacteristic->getValue().length()); }; }; @@ -90,6 +90,8 @@ class ConfigCharCallbacks : public BLECharacteristicCallbacks { void BH::ConnectionBHBLE::begin() { BLEDevice::init(this->config.deviceName); + this->callbacks->postInit(); + this->bleServer = BLEDevice::createServer(); this->bleServer->setCallbacks(new BHServerCallbacks()); diff --git a/lib/bh/connection_bhble.hpp b/lib/bhaptics/connection_bhble.hpp similarity index 80% rename from lib/bh/connection_bhble.hpp rename to lib/bhaptics/connection_bhble.hpp index fe5ffc1a..f5c92290 100644 --- a/lib/bh/connection_bhble.hpp +++ b/lib/bhaptics/connection_bhble.hpp @@ -23,6 +23,15 @@ typedef std::function bh_motor_handler_t; namespace BH { + class BHBLEConnectionCallbacks { + public: + virtual void postInit() { + log_v("Default postInit"); + }; + }; + + static BHBLEConnectionCallbacks defaultCallback; + struct ConnectionBHBLE_Config { std::string deviceName; uint16_t appearance; @@ -39,6 +48,8 @@ namespace BH BLEService* motorService = nullptr; BLECharacteristic* batteryChar = nullptr; + BHBLEConnectionCallbacks* callbacks = &defaultCallback; + #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true void handleBatteryChange(const OH::BatteryLevelEvent* event) const; #endif @@ -63,5 +74,13 @@ namespace BH } #endif }; + + void setCallbacks(BHBLEConnectionCallbacks* pCallbacks) { + if (pCallbacks != nullptr) { + this->callbacks = pCallbacks; + } else { + this->callbacks = &defaultCallback; + } + }; }; } // namespace OH From b86c32d432c39ab726beb5e71e13eefb5e7585ff Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 17:09:43 +0400 Subject: [PATCH 14/35] revert(bHaptics): bring back connection events --- firmware/openhaptics.cpp | 2 +- lib/bhaptics/connection_bhble.cpp | 12 ++++++++++-- platformio.ini | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/firmware/openhaptics.cpp b/firmware/openhaptics.cpp index 88824472..2f48d44f 100644 --- a/firmware/openhaptics.cpp +++ b/firmware/openhaptics.cpp @@ -27,7 +27,7 @@ void OpenHaptics::registerComponent(OH::IComponent* component) { } void OpenHaptics::postEvent(const OH::IEvent* event) { - log_d("Event dispatched at %u: %s (%p)", millis(), event->eventName.c_str(), event); + log_i("Event dispatched at %u: %s (%p)", millis(), event->eventName.c_str(), event); for (auto* listener : this->eventListeners) { listener->handleEvent(event); diff --git a/lib/bhaptics/connection_bhble.cpp b/lib/bhaptics/connection_bhble.cpp index fb86ed89..97cf4013 100644 --- a/lib/bhaptics/connection_bhble.cpp +++ b/lib/bhaptics/connection_bhble.cpp @@ -26,10 +26,18 @@ #endif class BHServerCallbacks final : public BLEServerCallbacks { + private: + OH::IEventDispatcher* dispatcher; + public: - BHServerCallbacks() {} + BHServerCallbacks(OH::IEventDispatcher* eventDispatcher) : dispatcher(eventDispatcher) {} + + void onConnect(BLEServer* pServer) { + this->dispatcher->postEvent(new OH::IEvent(OH_EVENT_CONNECTED)); + } void onDisconnect(BLEServer* pServer) { + this->dispatcher->postEvent(new OH::IEvent(OH_EVENT_DISCONNECTED)); pServer->startAdvertising(); } }; @@ -94,7 +102,7 @@ void BH::ConnectionBHBLE::begin() { this->bleServer = BLEDevice::createServer(); - this->bleServer->setCallbacks(new BHServerCallbacks()); + this->bleServer->setCallbacks(new BHServerCallbacks(this->eventDispatcher)); auto scanResponseData = new BLEAdvertisementData(); scanResponseData->setAppearance(this->config.appearance); diff --git a/platformio.ini b/platformio.ini index 9f9bf122..a6c82460 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,7 +23,7 @@ build_flags = ; -D DEBUG_MODE=0 ; -D DEBUG_ESP_PORT=Serial ; -D SERIAL_PLOTTER=true -; -D BATTERY_ENABLED=true + -D BATTERY_ENABLED=true ; -D BLUETOOTH_USE_NIMBLE=false build_unflags = From d8729a6784a5b5d34f54e3acb9813ffadf56fe14 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 18:39:36 +0400 Subject: [PATCH 15/35] refactor(Haptics): refactor names and libs --- .gitignore | 3 +++ firmware/firmware.cpp | 2 +- firmware/mode_configs/bhaptics/tactal.cpp | 14 +++++------ firmware/mode_configs/bhaptics/tactglove.cpp | 14 +++++------ firmware/mode_configs/bhaptics/tactosy2.cpp | 14 +++++------ firmware/mode_configs/bhaptics/tactosyf.cpp | 14 +++++------ firmware/mode_configs/bhaptics/tactosyh.cpp | 14 +++++------ .../mode_configs/bhaptics/tactsuit_x16.cpp | 20 ++++++++-------- .../bhaptics/tactsuit_x16_pca9685.cpp | 20 ++++++++-------- .../mode_configs/bhaptics/tactsuit_x40.cpp | 24 +++++++++---------- firmware/mode_configs/test.cpp | 14 +++++------ firmware/openhaptics.cpp | 16 +------------ include/openhaptics.h | 15 +++++------- lib/arduino/components/serial_plotter.cpp | 2 +- lib/arduino/components/serial_plotter.hpp | 8 +++---- lib/arduino/output_writers/pwm.cpp | 8 +++++++ lib/arduino/output_writers/pwm.hpp | 5 ++-- lib/bhaptics/bh_utils.cpp | 6 ++--- lib/bhaptics/bh_utils.hpp | 8 +++---- lib/bhaptics/connection_bhble.cpp | 2 +- lib/core/types.hpp | 4 ++-- .../output.cpp => haptics/haptic_body.cpp} | 13 +++++----- .../output.hpp => haptics/haptic_body.hpp} | 15 ++++++------ .../haptic_constants.h} | 0 .../haptic_plane.cpp} | 12 +++++----- .../haptic_plane.hpp} | 17 ++++--------- ...utput_writer.hpp => abstract_actuator.hpp} | 2 +- lib/pca9685/output_writers/pca9685.hpp | 5 ++-- 28 files changed, 137 insertions(+), 154 deletions(-) rename lib/{output/output.cpp => haptics/haptic_body.cpp} (72%) rename lib/{output/output.hpp => haptics/haptic_body.hpp} (74%) rename lib/{output/output_constants.h => haptics/haptic_constants.h} (100%) rename lib/{output/output_component.cpp => haptics/haptic_plane.cpp} (81%) rename lib/{output/output_component.hpp => haptics/haptic_plane.hpp} (67%) rename lib/output/{abstract_output_writer.hpp => abstract_actuator.hpp} (92%) diff --git a/.gitignore b/.gitignore index 38011f71..70d17d02 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ *.bak *.code-workspace +.idea +cmake-build-debug + # PlatformIO files/dirs .pio* .pioenvs diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index e3126d3e..2df62cb8 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -25,7 +25,7 @@ void setup() { setupMode(); #if defined(SERIAL_PLOTTER) && SERIAL_PLOTTER == true - auto* serialOutputState = new OH::SerialPlotter_OutputStates(SERIAL_PLOTTER_PORT, App.getOutput()); + auto* serialOutputState = new OH::SerialPlotter_OutputStates(SERIAL_PLOTTER_PORT, App.getHapticBody()); serialOutputState->begin(); #endif // SERIAL_PLOTTER } diff --git a/firmware/mode_configs/bhaptics/tactal.cpp b/firmware/mode_configs/bhaptics/tactal.cpp index b0f64f29..ac092980 100644 --- a/firmware/mode_configs/bhaptics/tactal.cpp +++ b/firmware/mode_configs/bhaptics/tactal.cpp @@ -25,16 +25,16 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; void setupMode() { // Configure PWM pins to their positions on the face - auto faceOutputs = mapMatrixCoordinates({ + auto faceOutputs = mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, // clang-format on }); - ClosestOutputComponent* face = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, faceOutputs); - app->getOutput()->addComponent(face); + auto* face = new HapticPlane_Closest(faceOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, face); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -42,13 +42,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - plainOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, &App, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, &App, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactglove.cpp b/firmware/mode_configs/bhaptics/tactglove.cpp index 7aadf413..505f07b6 100644 --- a/firmware/mode_configs/bhaptics/tactglove.cpp +++ b/firmware/mode_configs/bhaptics/tactglove.cpp @@ -49,7 +49,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = { void setupMode() { // Configure PWM pins to their positions on the glove - auto gloveOutputs = mapMatrixCoordinates({ + auto gloveOutputs = mapMatrixCoordinates({ // clang-format off { // Thumb, Index, Middle, Ring, Pinky @@ -60,10 +60,10 @@ void setupMode() { // clang-format on }); - OutputComponent* glove = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, gloveOutputs); - app->getOutput()->addComponent(glove); + auto* glove = new HapticPlane_Closest(gloveOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, glove); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -71,13 +71,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - plainOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactosy2.cpp b/firmware/mode_configs/bhaptics/tactosy2.cpp index d42bc0cb..ab09e472 100644 --- a/firmware/mode_configs/bhaptics/tactosy2.cpp +++ b/firmware/mode_configs/bhaptics/tactosy2.cpp @@ -25,17 +25,17 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSY2; void setupMode() { // Configure PWM pins to their positions on the forearm - auto forearmOutputs = mapMatrixCoordinates({ + auto forearmOutputs = mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25)}, {new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, // clang-format on }); - OutputComponent* forearm = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, forearmOutputs); - app->getOutput()->addComponent(forearm); + auto* forearm = new HapticPlane_Closest(forearmOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, forearm); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -43,13 +43,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - plainOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactosyf.cpp b/firmware/mode_configs/bhaptics/tactosyf.cpp index aeadd6c1..598e2388 100644 --- a/firmware/mode_configs/bhaptics/tactosyf.cpp +++ b/firmware/mode_configs/bhaptics/tactosyf.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; void setupMode() { // Configure PWM pins to their positions on the feet - auto footOutputs = mapMatrixCoordinates({ + auto footOutputs = mapMatrixCoordinates({ // clang-format off { new PWMOutputWriter(32) }, { new PWMOutputWriter(33) }, @@ -33,10 +33,10 @@ void setupMode() { // clang-format on }); - OutputComponent* foot = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, footOutputs); - app->getOutput()->addComponent(foot); + auto* foot = new HapticPlane_Closest(footOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, foot); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -44,13 +44,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - plainOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactosyh.cpp b/firmware/mode_configs/bhaptics/tactosyh.cpp index d40cec5d..9fc03a5b 100644 --- a/firmware/mode_configs/bhaptics/tactosyh.cpp +++ b/firmware/mode_configs/bhaptics/tactosyh.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYH; void setupMode() { // Configure PWM pins to their positions on the hands - auto handOutputs = mapMatrixCoordinates({ + auto handOutputs = mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32)}, {new PWMOutputWriter(33)}, @@ -33,10 +33,10 @@ void setupMode() { // clang-format on }); - OutputComponent* hand = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, handOutputs); - app->getOutput()->addComponent(hand); + auto* hand = new HapticPlane_Closest(handOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, hand); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -44,13 +44,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - plainOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp index 23dd5ce4..980455c7 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp @@ -29,26 +29,26 @@ static const uint8_t layoutGroups[layoutGroupsSize] = BH_LAYOUT_TACTSUITX16_GROU void setupMode() { // Configure PWM pins to their positions on the vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26)}, {new PWMOutputWriter(27), new PWMOutputWriter(14), new PWMOutputWriter(12), new PWMOutputWriter(13)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(19), new PWMOutputWriter(18), new PWMOutputWriter(5), new PWMOutputWriter(17)}, {new PWMOutputWriter(16), new PWMOutputWriter(4), new PWMOutputWriter(2), new PWMOutputWriter(15)}, // clang-format on }); - OutputComponent* chestFront = new ClosestOutputComponent(OUTPUT_PATH_CHEST_FRONT, frontOutputs); - OutputComponent* chestBack = new ClosestOutputComponent(OUTPUT_PATH_CHEST_BACK, backOutputs); + auto* chestFront = new HapticPlane_Closest(frontOutputs); + auto* chestBack = new HapticPlane_Closest(backOutputs); - app->getOutput()->addComponent(chestFront); - app->getOutput()->addComponent(chestBack); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_FRONT, chestFront); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_BACK, chestBack); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -56,13 +56,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - vestX16OutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, layoutGroups, layoutGroupsSize); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + vestX16OutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, layoutGroups, layoutGroupsSize); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp index accafac9..295139c7 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp @@ -34,26 +34,26 @@ void setupMode() { pwm->setPWMFreq(PWM_FREQUENCY); // Assign the pins on the configured PCA9685 to positions on the vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm, 0), new PCA9685OutputWriter(pwm, 1), new PCA9685OutputWriter(pwm, 2), new PCA9685OutputWriter(pwm, 3)}, {new PCA9685OutputWriter(pwm, 4), new PCA9685OutputWriter(pwm, 5), new PCA9685OutputWriter(pwm, 6), new PCA9685OutputWriter(pwm, 7)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm, 8), new PCA9685OutputWriter(pwm, 9), new PCA9685OutputWriter(pwm, 10), new PCA9685OutputWriter(pwm, 11)}, {new PCA9685OutputWriter(pwm, 12), new PCA9685OutputWriter(pwm, 13), new PCA9685OutputWriter(pwm, 14), new PCA9685OutputWriter(pwm, 15)}, // clang-format on }); - OutputComponent* chestFront = new ClosestOutputComponent(OUTPUT_PATH_CHEST_FRONT, frontOutputs); - OutputComponent* chestBack = new ClosestOutputComponent(OUTPUT_PATH_CHEST_BACK, backOutputs); + auto* chestFront = new HapticPlane_Closest(frontOutputs); + auto* chestBack = new HapticPlane_Closest(backOutputs); - app->getOutput()->addComponent(chestFront); - app->getOutput()->addComponent(chestBack); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_FRONT, chestFront); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_BACK, chestBack); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -61,13 +61,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - vestX16OutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize, layoutGroups, layoutGroupsSize); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + vestX16OutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, layoutGroups, layoutGroupsSize); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp index 2e968081..b5b316fd 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp @@ -26,17 +26,17 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTSUITX40; void setupMode() { // Configure the PCA9685s - Adafruit_PWMServoDriver* pwm0 = new Adafruit_PWMServoDriver(0x40); + auto* pwm0 = new Adafruit_PWMServoDriver(0x40); pwm0->begin(); pwm0->setPWMFreq(PWM_FREQUENCY); - Adafruit_PWMServoDriver* pwm1 = new Adafruit_PWMServoDriver(0x41); + auto* pwm1 = new Adafruit_PWMServoDriver(0x41); pwm1->begin(); pwm1->setPWMFreq(PWM_FREQUENCY); // Assign the pins on the configured PCA9685s and PWM pins to locations on the // vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm0, 0), new PCA9685OutputWriter(pwm0, 1), new PCA9685OutputWriter(pwm0, 2), new PCA9685OutputWriter(pwm0, 3)}, {new PCA9685OutputWriter(pwm0, 4), new PCA9685OutputWriter(pwm0, 5), new PCA9685OutputWriter(pwm0, 6), new PCA9685OutputWriter(pwm0, 7)}, @@ -45,7 +45,7 @@ void setupMode() { {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm1, 0), new PCA9685OutputWriter(pwm1, 1), new PCA9685OutputWriter(pwm1, 2), new PCA9685OutputWriter(pwm1, 3)}, {new PCA9685OutputWriter(pwm1, 4), new PCA9685OutputWriter(pwm1, 5), new PCA9685OutputWriter(pwm1, 6), new PCA9685OutputWriter(pwm1, 7)}, @@ -55,13 +55,13 @@ void setupMode() { // clang-format on }); - OutputComponent* chestFront = new ClosestOutputComponent(OUTPUT_PATH_CHEST_FRONT, frontOutputs); - OutputComponent* chestBack = new ClosestOutputComponent(OUTPUT_PATH_CHEST_BACK, backOutputs); + auto* chestFront = new HapticPlane_Closest(frontOutputs); + auto* chestBack = new HapticPlane_Closest(backOutputs); - app->getOutput()->addComponent(chestFront); - app->getOutput()->addComponent(chestBack); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_FRONT, chestFront); + app->getHapticBody()->addComponent(OUTPUT_PATH_CHEST_BACK, chestBack); - app->getOutput()->setup(); + app->getHapticBody()->setup(); uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; ConnectionBHBLE_Config config = { @@ -69,13 +69,13 @@ void setupMode() { .appearance = BH_BLE_APPEARANCE, .serialNumber = serialNumber, }; - AbstractConnection* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { - vestOutputTransformer(app->getOutput(), value, bhLayout, bhLayoutSize); + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + vestOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize); }, app); bhBleConnection->begin(); #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true - AbstractBattery* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); + auto* battery = new ADCNaiveBattery(36, { .sampleRate = BATTERY_SAMPLE_RATE }, app, tskNO_AFFINITY); battery->begin(); #endif } diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index d18b47ab..39824129 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true #include @@ -21,7 +21,7 @@ using namespace BH; extern OpenHaptics App; OpenHaptics* app = &App; -class TestOutput : public OH::AbstractOutputWriter { +class TestOutput : public OH::AbstractActuator { private: uint8_t channel; @@ -34,7 +34,7 @@ class TestOutput : public OH::AbstractOutputWriter { }; void setupMode() { - auto testOutputs = mapMatrixCoordinates({ + auto testOutputs = mapMatrixCoordinates({ {new TestOutput(0), new TestOutput(1)}, {new TestOutput(2), new TestOutput(3)}, {new TestOutput(4), new TestOutput(5)}, @@ -47,21 +47,21 @@ void setupMode() { kv.first.y); } - auto test = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, testOutputs); + auto test = new HapticPlane_Closest(OUTPUT_PATH_ACCESSORY, testOutputs); oh_output_writers_map_t layout{ {oh_output_point_t(0, 0), new TestOutput(8)}, }; - auto test2 = new ClosestOutputComponent(OUTPUT_PATH_ACCESSORY, layout); + auto test2 = new HapticPlane_Closest(OUTPUT_PATH_ACCESSORY, layout); - App.getOutput()->addComponent(test); + App.getHapticBody()->addComponent(test); for (auto i = 0; i < testOutputs.size(); i++) { oh_output_data_t outData{ oh_output_point_t(0, 0), (uint8_t)map(i, 0, testOutputs.size() - 1, 0, 255), }; - App.getOutput()->writeOutput(OUTPUT_PATH_ACCESSORY, outData); + App.getHapticBody()->writeOutput(OUTPUT_PATH_ACCESSORY, outData); } } diff --git a/firmware/openhaptics.cpp b/firmware/openhaptics.cpp index 2f48d44f..60b2b743 100644 --- a/firmware/openhaptics.cpp +++ b/firmware/openhaptics.cpp @@ -9,21 +9,7 @@ #include OpenHaptics::OpenHaptics() { - this->output = new OH::Output(); -} - -void OpenHaptics::registerComponent(OH::IComponent* component) { - if (component == nullptr) { - return; - } - - for (auto* c : this->components) { - if (c == component) { - return; - } - } - - this->components.insert(component); + this->pHapticBody = new OH::HapticBody(); } void OpenHaptics::postEvent(const OH::IEvent* event) { diff --git a/include/openhaptics.h b/include/openhaptics.h index 8f1bba47..df787271 100644 --- a/include/openhaptics.h +++ b/include/openhaptics.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true @@ -13,12 +13,11 @@ #include -class OpenHaptics final : public OH::IComponentRegistry, public OH::IEventDispatcher +class OpenHaptics final : public OH::IEventDispatcher { private: - std::set components{}; std::vector eventListeners{}; - OH::Output* output; + OH::HapticBody* pHapticBody; #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true OH::AbstractBattery* battery; @@ -26,12 +25,10 @@ class OpenHaptics final : public OH::IComponentRegistry, public public: OpenHaptics(); - std::set getComponents() { - return this->components; - }; - void registerComponent(OH::IComponent*); - OH::Output* getOutput() { return this->output; }; + OH::HapticBody* getHapticBody() { + return this->pHapticBody; + }; void postEvent(const OH::IEvent* event) override; void addEventListener(const OH::IEventListener* listener) override; diff --git a/lib/arduino/components/serial_plotter.cpp b/lib/arduino/components/serial_plotter.cpp index f91a77f1..6231d8d1 100644 --- a/lib/arduino/components/serial_plotter.cpp +++ b/lib/arduino/components/serial_plotter.cpp @@ -5,7 +5,7 @@ void OH::SerialPlotter_OutputStates<_Tp>::run() { while (true) { for (auto& _c : *output->getComponents()) { oh_output_path_t path = _c.first; - OH::OutputComponent* component = _c.second; + OH::HapticPlane* component = _c.second; for (auto& _s : *component->getOutputStates()) { oh_output_point_t point = _s.first; diff --git a/lib/arduino/components/serial_plotter.hpp b/lib/arduino/components/serial_plotter.hpp index b7b63d41..f5bfbb71 100644 --- a/lib/arduino/components/serial_plotter.hpp +++ b/lib/arduino/components/serial_plotter.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include @@ -21,19 +21,19 @@ namespace OH { private: _Tp* serial; - Output* output; + HapticBody* output; uint32_t sampleRate; void setup(void) {}; void run(void); public: - SerialPlotter_OutputStates(_Tp& serial, Output* output, uint32_t sampleRate, TaskConfig taskConfig = { "Serial Plotter", 2048, 1, tskNO_AFFINITY }) + SerialPlotter_OutputStates(_Tp& serial, HapticBody* output, uint32_t sampleRate, TaskConfig taskConfig = { "Serial Plotter", 2048, 1, tskNO_AFFINITY }) : Task>(taskConfig), serial(&serial), output(output), sampleRate(sampleRate) {}; - SerialPlotter_OutputStates(_Tp& serial, Output* output) : SerialPlotter_OutputStates(serial, output, 100) {}; + SerialPlotter_OutputStates(_Tp& serial, HapticBody* output) : SerialPlotter_OutputStates(serial, output, 100) {}; void begin() override { this->setup(); diff --git a/lib/arduino/output_writers/pwm.cpp b/lib/arduino/output_writers/pwm.cpp index 83ba178d..693b03af 100644 --- a/lib/arduino/output_writers/pwm.cpp +++ b/lib/arduino/output_writers/pwm.cpp @@ -7,10 +7,18 @@ uint8_t PWMOutputWriter::CHANNELS = 0; void PWMOutputWriter::setup() { this->chan = PWMOutputWriter::CHANNELS++; +#if defined(ARDUINO_ARCH_ESP32) ledcSetup(this->chan, this->freq, this->resolution); ledcAttachPin(this->pin, this->chan); +#else + pinMode(this->pin, OUTPUT); +#endif }; void PWMOutputWriter::writeOutput(oh_output_intensity_t intensity) { +#if defined(ARDUINO_ARCH_ESP32) ledcWrite(chan, (uint16_t)map(intensity, 0, OH_OUTPUT_INTENSITY_MAX, 0, 4096)); +#else + analogWrite(this->pin, (uint16_t)map(intensity, 0, OH_OUTPUT_INTENSITY_MAX, 0, 255)); +#endif } diff --git a/lib/arduino/output_writers/pwm.hpp b/lib/arduino/output_writers/pwm.hpp index 324c8c5c..7c19ac4e 100644 --- a/lib/arduino/output_writers/pwm.hpp +++ b/lib/arduino/output_writers/pwm.hpp @@ -1,9 +1,8 @@ #pragma once -#include -#include +#include -class PWMOutputWriter : public OH::AbstractOutputWriter { +class PWMOutputWriter : public OH::AbstractActuator { private: static uint8_t CHANNELS; uint8_t pin, chan; diff --git a/lib/bhaptics/bh_utils.cpp b/lib/bhaptics/bh_utils.cpp index ada179e5..1c059809 100644 --- a/lib/bhaptics/bh_utils.cpp +++ b/lib/bhaptics/bh_utils.cpp @@ -2,7 +2,7 @@ #include -void BH::plainOutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path) { +void BH::plainOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path) { for (size_t i = 0; i < layoutSize; i++) { uint8_t byte = value[i]; @@ -15,7 +15,7 @@ void BH::plainOutputTransformer(OH::Output* output, std::string& value, const oh } } -void BH::vestOutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize) { +void BH::vestOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize) { for (size_t i = 0; i < layoutSize / 2; i++) { uint8_t byte = value[i]; uint actIndex = i * 2; @@ -36,7 +36,7 @@ void BH::vestOutputTransformer(OH::Output* output, std::string& value, const oh_ } } -void BH::vestX16OutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const uint8_t layoutGroups[], const size_t layoutGroupsSize) { +void BH::vestX16OutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const uint8_t layoutGroups[], const size_t layoutGroupsSize) { uint8_t result[layoutSize]; // Unpack values diff --git a/lib/bhaptics/bh_utils.hpp b/lib/bhaptics/bh_utils.hpp index f661475d..f3ac91bd 100644 --- a/lib/bhaptics/bh_utils.hpp +++ b/lib/bhaptics/bh_utils.hpp @@ -1,10 +1,10 @@ #pragma once #include -#include +#include namespace BH { - void plainOutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path); - void vestOutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize); - void vestX16OutputTransformer(OH::Output* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const uint8_t layoutGroups[], const size_t layoutGroupsSize); + void plainOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path); + void vestOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize); + void vestX16OutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const uint8_t layoutGroups[], const size_t layoutGroupsSize); } // namespace BH diff --git a/lib/bhaptics/connection_bhble.cpp b/lib/bhaptics/connection_bhble.cpp index 97cf4013..ff573869 100644 --- a/lib/bhaptics/connection_bhble.cpp +++ b/lib/bhaptics/connection_bhble.cpp @@ -1,6 +1,6 @@ #include "connection_bhble.hpp" -#include +#include #include #include diff --git a/lib/core/types.hpp b/lib/core/types.hpp index 4f029163..56de9b5a 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -1,6 +1,6 @@ #pragma once -#include "abstract_output_writer.hpp" +#include "abstract_actuator.hpp" #include "point2.hpp" #include @@ -25,5 +25,5 @@ namespace OH { typedef OH::OutputData oh_output_data_t; typedef OH::OutputState oh_output_state_t; -typedef std::map +typedef std::map oh_output_writers_map_t; diff --git a/lib/output/output.cpp b/lib/haptics/haptic_body.cpp similarity index 72% rename from lib/output/output.cpp rename to lib/haptics/haptic_body.cpp index 03a0e874..120bf2ae 100644 --- a/lib/output/output.cpp +++ b/lib/haptics/haptic_body.cpp @@ -1,17 +1,16 @@ -#include "output.hpp" +#include "haptic_body.hpp" #include -void OH::Output::addComponent(OH::OutputComponent* c) { - auto path = c->getPath(); +void OH::HapticBody::addComponent(const oh_output_path_t path, OH::HapticPlane* c) { this->components[path] = c; } -std::map* OH::Output::getComponents() { +std::map* OH::HapticBody::getComponents() { return &this->components; } -void OH::Output::writeOutput(const oh_output_path_t path, const oh_output_data_t& data) { +void OH::HapticBody::writeOutput(const oh_output_path_t path, const oh_output_data_t& data) { if (this->getComponents()->count(path) == 0) { // if no requested component exists, skip log_w("No component found for path %d", path); @@ -35,7 +34,7 @@ inline std::map OH::mapMatrixCoordinates(std::vector OH::mapMatrixCoordinates(std::vector(std::vector>); +template oh_output_writers_map_t OH::mapMatrixCoordinates(std::vector>); diff --git a/lib/output/output.hpp b/lib/haptics/haptic_body.hpp similarity index 74% rename from lib/output/output.hpp rename to lib/haptics/haptic_body.hpp index b1bea3c0..56472b27 100644 --- a/lib/output/output.hpp +++ b/lib/haptics/haptic_body.hpp @@ -1,23 +1,22 @@ #pragma once -#include "output_constants.h" -#include "output_component.hpp" +#include "haptic_constants.h" +#include "haptic_plane.hpp" #include -#include #include namespace OH { - class Output { + class HapticBody { private: - typedef std::map oh_output_components_map_t; - std::map components{}; + typedef std::map oh_output_components_map_t; + std::map components{}; public: - Output() {}; + HapticBody() {}; - void addComponent(OutputComponent*); + void addComponent(const oh_output_path_t, HapticPlane*); oh_output_components_map_t* getComponents(); void writeOutput(const oh_output_path_t, const oh_output_data_t&); diff --git a/lib/output/output_constants.h b/lib/haptics/haptic_constants.h similarity index 100% rename from lib/output/output_constants.h rename to lib/haptics/haptic_constants.h diff --git a/lib/output/output_component.cpp b/lib/haptics/haptic_plane.cpp similarity index 81% rename from lib/output/output_component.cpp rename to lib/haptics/haptic_plane.cpp index ed4b919f..a7779662 100644 --- a/lib/output/output_component.cpp +++ b/lib/haptics/haptic_plane.cpp @@ -1,8 +1,8 @@ -#include "output_component.hpp" +#include "haptic_plane.hpp" #include -void OH::OutputComponent::setOutputs(oh_output_writers_map_t& outputs) { +void OH::HapticPlane::setOutputs(oh_output_writers_map_t& outputs) { this->writers.clear(); this->writers = outputs; @@ -17,13 +17,13 @@ void OH::OutputComponent::setOutputs(oh_output_writers_map_t& outputs) { } } -void OH::OutputComponent::setup() { +void OH::HapticPlane::setup() { for (const auto& kv : this->writers) { kv.second->setup(); } } -void OH::OutputComponent::writeOutput(const oh_output_data_t& data) { +void OH::HapticPlane::writeOutput(const oh_output_data_t& data) { if (this->writers.count(data.point) == 0) { log_w("No writer for point (%u, %u)", data.point.x, data.point.y); return; @@ -32,7 +32,7 @@ void OH::OutputComponent::writeOutput(const oh_output_data_t& data) { this->writers[data.point]->writeOutput(data.intensity); } -oh_output_point_t OH::ClosestOutputComponent::findClosestPoints( +oh_output_point_t OH::HapticPlane_Closest::findClosestPoints( std::list& pts, const oh_output_point_t& target) { std::multimap mp = {}; @@ -60,7 +60,7 @@ oh_output_point_t OH::ClosestOutputComponent::findClosestPoints( return nearest->second; } -void OH::ClosestOutputComponent::writeOutput(const oh_output_data_t& data) { +void OH::HapticPlane_Closest::writeOutput(const oh_output_data_t& data) { auto closestPoint = this->findClosestPoints(this->points, data.point); auto state = &this->states[closestPoint]; diff --git a/lib/output/output_component.hpp b/lib/haptics/haptic_plane.hpp similarity index 67% rename from lib/output/output_component.hpp rename to lib/haptics/haptic_plane.hpp index 8be99de2..20f25c6b 100644 --- a/lib/output/output_component.hpp +++ b/lib/haptics/haptic_plane.hpp @@ -1,7 +1,6 @@ #pragma once -#include "abstract_output_writer.hpp" - +#include #include #include @@ -13,9 +12,7 @@ namespace OH { /** * Output "plane" (e.g. Chest, Palm, Finger, etc.) */ - class OutputComponent : public OH::IComponent { - private: - oh_output_path_t path; + class HapticPlane : public OH::IComponent { protected: std::list points{}; oh_output_writers_map_t writers{}; @@ -24,12 +21,9 @@ namespace OH { void setOutputs(oh_output_writers_map_t&); public: - OutputComponent(oh_output_path_t path, oh_output_writers_map_t& outputs) : path(path) { + HapticPlane(oh_output_writers_map_t& outputs) { this->setOutputs(outputs); }; - oh_output_path_t getPath(void) { - return this->path; - }; std::list* getOutputPoints(void) { return &this->points; }; @@ -40,14 +34,13 @@ namespace OH { void setup() override; }; - class ClosestOutputComponent : public OutputComponent { + class HapticPlane_Closest : public HapticPlane { protected: oh_output_point_t findClosestPoints(std::list& pts, const oh_output_point_t& target); void setOutputs(oh_output_writers_map_t&); public: - ClosestOutputComponent(oh_output_path_t path, oh_output_writers_map_t& outputs) - : OutputComponent(path, outputs) {}; + HapticPlane_Closest(oh_output_writers_map_t& outputs) : HapticPlane(outputs) {}; void writeOutput(const oh_output_data_t&) override; }; } // namespace OH diff --git a/lib/output/abstract_output_writer.hpp b/lib/output/abstract_actuator.hpp similarity index 92% rename from lib/output/abstract_output_writer.hpp rename to lib/output/abstract_actuator.hpp index 6871550f..67d94dfe 100644 --- a/lib/output/abstract_output_writer.hpp +++ b/lib/output/abstract_actuator.hpp @@ -9,7 +9,7 @@ typedef OH_OUTPUT_INTENSITY_T oh_output_intensity_t; namespace OH { //! Singular output point (e.g. vibration motor) - class AbstractOutputWriter { + class AbstractActuator { public: virtual void setup(){}; virtual void writeOutput(oh_output_intensity_t intensity) = 0; diff --git a/lib/pca9685/output_writers/pca9685.hpp b/lib/pca9685/output_writers/pca9685.hpp index eded1981..be3d3f06 100644 --- a/lib/pca9685/output_writers/pca9685.hpp +++ b/lib/pca9685/output_writers/pca9685.hpp @@ -4,11 +4,10 @@ #include #include -#include -#include +#include namespace OH { - class PCA9685OutputWriter : public OH::AbstractOutputWriter { + class PCA9685OutputWriter : public OH::AbstractActuator { private: Adafruit_PWMServoDriver* driver; uint8_t num; From 7c1b768fd1c6d91662b2ba48bfe2852e4ecc083b Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 20:00:40 +0400 Subject: [PATCH 16/35] chore: remove unused components --- lib/opengloves/og_communication.hpp | 9 ------- .../og_communication/bluetooth_serial.hpp | 22 ----------------- lib/opengloves/og_communication/serial.hpp | 24 ------------------- platformio.ini | 2 +- 4 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 lib/opengloves/og_communication.hpp delete mode 100644 lib/opengloves/og_communication/bluetooth_serial.hpp delete mode 100644 lib/opengloves/og_communication/serial.hpp diff --git a/lib/opengloves/og_communication.hpp b/lib/opengloves/og_communication.hpp deleted file mode 100644 index 4915e1ff..00000000 --- a/lib/opengloves/og_communication.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -namespace OG { - struct ICommunication { - virtual void begin() = 0; - - virtual void send(char* data) = 0; - }; -} diff --git a/lib/opengloves/og_communication/bluetooth_serial.hpp b/lib/opengloves/og_communication/bluetooth_serial.hpp deleted file mode 100644 index fa0753c1..00000000 --- a/lib/opengloves/og_communication/bluetooth_serial.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "og_communication.hpp" -#include "og_communication/serial.hpp" - -#include - -namespace OG { - class OG_BluetoothSerialCommunication : public OG_SerialCommunication { - private: - BluetoothSerial* serial; - const char* deviceName; - - public: - OG_BluetoothSerialCommunication(BluetoothSerial& serial, const char* deviceName) - : OG_SerialCommunication(serial), serial(&serial), deviceName(deviceName) {}; - - void begin() override { - this->serial->begin(deviceName); - }; - }; -} diff --git a/lib/opengloves/og_communication/serial.hpp b/lib/opengloves/og_communication/serial.hpp deleted file mode 100644 index a0505567..00000000 --- a/lib/opengloves/og_communication/serial.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include "og_communication.hpp" - -#include - -namespace OG { - - /** - * @tparam _Tp the type of the serial port - */ - template - class OG_SerialCommunication : public ICommunication { - private: - _Tp* serial; - - public: - OG_SerialCommunication(_Tp& serial) : serial(&serial) {}; - - void send(char* data) override { - this->serial->write(data); - }; - }; -} // namespace OG diff --git a/platformio.ini b/platformio.ini index a6c82460..9f9bf122 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,7 +23,7 @@ build_flags = ; -D DEBUG_MODE=0 ; -D DEBUG_ESP_PORT=Serial ; -D SERIAL_PLOTTER=true - -D BATTERY_ENABLED=true +; -D BATTERY_ENABLED=true ; -D BLUETOOTH_USE_NIMBLE=false build_unflags = From 026765f50f1f4e91819d2b2a93ec3c3847aff18b Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 22:22:21 +0400 Subject: [PATCH 17/35] refactor(Core): remove component interface --- include/openhaptics.h | 1 - lib/arduino/components/serial_plotter.hpp | 2 +- lib/bhaptics/bh_utils.cpp | 1 + lib/core/abstract_battery.hpp | 1 - lib/core/abstract_component.cpp | 1 - lib/core/abstract_component.hpp | 22 ---------------------- lib/core/abstract_connection.hpp | 1 - lib/core/sensor.hpp | 2 +- lib/haptics/haptic_body.hpp | 1 + lib/haptics/haptic_plane.cpp | 2 ++ lib/haptics/haptic_plane.hpp | 5 ++--- 11 files changed, 8 insertions(+), 31 deletions(-) delete mode 100644 lib/core/abstract_component.cpp delete mode 100644 lib/core/abstract_component.hpp diff --git a/include/openhaptics.h b/include/openhaptics.h index df787271..64c8427d 100644 --- a/include/openhaptics.h +++ b/include/openhaptics.h @@ -2,7 +2,6 @@ #include "config/all.h" -#include #include #include #include diff --git a/lib/arduino/components/serial_plotter.hpp b/lib/arduino/components/serial_plotter.hpp index f5bfbb71..77de15bd 100644 --- a/lib/arduino/components/serial_plotter.hpp +++ b/lib/arduino/components/serial_plotter.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/lib/bhaptics/bh_utils.cpp b/lib/bhaptics/bh_utils.cpp index 1c059809..285704cd 100644 --- a/lib/bhaptics/bh_utils.cpp +++ b/lib/bhaptics/bh_utils.cpp @@ -1,6 +1,7 @@ #include "bh_utils.hpp" #include +#include void BH::plainOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path) { for (size_t i = 0; i < layoutSize; i++) { diff --git a/lib/core/abstract_battery.hpp b/lib/core/abstract_battery.hpp index 2d4b62b3..0947ede7 100644 --- a/lib/core/abstract_battery.hpp +++ b/lib/core/abstract_battery.hpp @@ -1,6 +1,5 @@ #pragma once -#include "abstract_component.hpp" #include "sensor.hpp" #include "events.hpp" diff --git a/lib/core/abstract_component.cpp b/lib/core/abstract_component.cpp deleted file mode 100644 index ee0d59fc..00000000 --- a/lib/core/abstract_component.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "abstract_component.hpp" diff --git a/lib/core/abstract_component.hpp b/lib/core/abstract_component.hpp deleted file mode 100644 index 2fde48a1..00000000 --- a/lib/core/abstract_component.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include -#include - -#include "task.hpp" - -#include - -namespace OH { - class IComponent { - public: - virtual void setup(void) {}; - }; - - template - class IComponentRegistry { - public: - virtual std::set<_Tp*> getComponents() = 0; - virtual void registerComponent(_Tp*) = 0; - }; -} // namespace OH diff --git a/lib/core/abstract_connection.hpp b/lib/core/abstract_connection.hpp index fd77b8de..75f899b6 100644 --- a/lib/core/abstract_connection.hpp +++ b/lib/core/abstract_connection.hpp @@ -1,6 +1,5 @@ #pragma once -#include "abstract_component.hpp" #include "events.hpp" namespace OH { class AbstractConnection { diff --git a/lib/core/sensor.hpp b/lib/core/sensor.hpp index 09c8eaa6..5f75d843 100644 --- a/lib/core/sensor.hpp +++ b/lib/core/sensor.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "task.hpp" namespace OH { diff --git a/lib/haptics/haptic_body.hpp b/lib/haptics/haptic_body.hpp index 56472b27..0fd7f26f 100644 --- a/lib/haptics/haptic_body.hpp +++ b/lib/haptics/haptic_body.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace OH { class HapticBody { diff --git a/lib/haptics/haptic_plane.cpp b/lib/haptics/haptic_plane.cpp index a7779662..f46a819c 100644 --- a/lib/haptics/haptic_plane.cpp +++ b/lib/haptics/haptic_plane.cpp @@ -1,5 +1,7 @@ #include "haptic_plane.hpp" +#include +#include #include void OH::HapticPlane::setOutputs(oh_output_writers_map_t& outputs) { diff --git a/lib/haptics/haptic_plane.hpp b/lib/haptics/haptic_plane.hpp index 20f25c6b..82bf5b0c 100644 --- a/lib/haptics/haptic_plane.hpp +++ b/lib/haptics/haptic_plane.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -12,7 +11,7 @@ namespace OH { /** * Output "plane" (e.g. Chest, Palm, Finger, etc.) */ - class HapticPlane : public OH::IComponent { + class HapticPlane { protected: std::list points{}; oh_output_writers_map_t writers{}; @@ -31,7 +30,7 @@ namespace OH { return &this->states; }; virtual void writeOutput(const oh_output_data_t&); - void setup() override; + void setup(); }; class HapticPlane_Closest : public HapticPlane { From 1491f258285551c1252447dbce671ee710f7baec Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 11 Apr 2023 23:45:37 +0400 Subject: [PATCH 18/35] ci(GitHub): enable features for CodeQL --- .github/workflows/codeql-analysis.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index ecc5cffd..7e624290 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -29,6 +29,12 @@ jobs: # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + # Enabling all flags to test build for every feature + battery_flag: + - BATTERY_ENABLED=true + serial_plotter_flag: + - SERIAL_PLOTTER=true + steps: - name: Checkout repository uses: actions/checkout@v3 @@ -90,6 +96,12 @@ jobs: with: python-version: '3.9' + - name: Update build flags (non-macOS) + if: runner.os != 'macOS' + run: | + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini + - name: Install PlatformIO run: | python -m pip install --upgrade pip From c36b50f6fbb8f902e8bc1b6dfc29a27cd20ddf46 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 12 Apr 2023 15:44:31 +0400 Subject: [PATCH 19/35] refactor(Core): remove Arduino imports --- lib/bhaptics/bh_utils.cpp | 13 ++++++++----- lib/core/abstract_battery.cpp | 4 ---- lib/core/task.hpp | 15 ++++++++++++++- lib/core/utility.hpp | 5 +++++ lib/haptics/haptic_body.hpp | 6 +++--- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/bhaptics/bh_utils.cpp b/lib/bhaptics/bh_utils.cpp index 285704cd..223ea4e0 100644 --- a/lib/bhaptics/bh_utils.cpp +++ b/lib/bhaptics/bh_utils.cpp @@ -1,7 +1,6 @@ #include "bh_utils.hpp" #include -#include void BH::plainOutputTransformer(OH::HapticBody* output, std::string& value, const oh_output_point_t* layout[], const size_t layoutSize, const oh_output_path_t path) { for (size_t i = 0; i < layoutSize; i++) { @@ -9,7 +8,8 @@ void BH::plainOutputTransformer(OH::HapticBody* output, std::string& value, cons oh_output_data_t outputData{ .point = *layout[i], - .intensity = static_cast(map(byte, 0, 100, 0, OH_OUTPUT_INTENSITY_MAX)), + // TODO: optimize generic type + .intensity = static_cast(OH::map(byte, 0, 100, 0, OH_OUTPUT_INTENSITY_MAX)), }; output->writeOutput(path, outputData); @@ -24,12 +24,14 @@ void BH::vestOutputTransformer(OH::HapticBody* output, std::string& value, const const oh_output_data_t outputData0{ .point = *layout[actIndex], - .intensity = static_cast(map(((byte >> 4) & 0xf), 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), + // TODO: optimize generic type + .intensity = static_cast(OH::map(((byte >> 4) & 0xf), 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), }; const oh_output_data_t outputData1{ .point = *layout[actIndex + 1], - .intensity = static_cast(map((byte & 0xf), 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), + // TODO: optimize generic type + .intensity = static_cast(OH::map((byte & 0xf), 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), }; output->writeOutput(path, outputData0); @@ -78,7 +80,8 @@ void BH::vestX16OutputTransformer(OH::HapticBody* output, std::string& value, co const auto path = (i < 10 || i >= 30) ? OUTPUT_PATH_CHEST_FRONT : OUTPUT_PATH_CHEST_BACK; const oh_output_data_t outputData{ .point = *layout[i], - .intensity = static_cast(map(result[i], 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), + // TODO: optimize generic type + .intensity = static_cast(OH::map(result[i], 0, 15, 0, OH_OUTPUT_INTENSITY_MAX)), }; output->writeOutput(path, outputData); diff --git a/lib/core/abstract_battery.cpp b/lib/core/abstract_battery.cpp index 01c0cd52..76c6d31a 100644 --- a/lib/core/abstract_battery.cpp +++ b/lib/core/abstract_battery.cpp @@ -1,9 +1,5 @@ #include "abstract_battery.hpp" -#ifdef ARDUINO -#include -#endif - void OH::AbstractBattery::run() { while (true) { this->value = this->updateValue(); diff --git a/lib/core/task.hpp b/lib/core/task.hpp index 5caf27d9..a5dab82f 100644 --- a/lib/core/task.hpp +++ b/lib/core/task.hpp @@ -2,10 +2,23 @@ #include "logging.hpp" -#include +extern "C" void delay(uint32_t ms); + +#if defined(ARDUINO_ARCH_ESP32) #include // Include the base FreeRTOS definitions. #include // Include the task definitions. +extern "C" BaseType_t xTaskCreateUniversal( + TaskFunction_t pxTaskCode, + const char * const pcName, + const uint32_t usStackDepth, + void * const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t * const pxCreatedTask, + const BaseType_t xCoreID +); +#endif + namespace OH { struct TaskConfig { const char *name; diff --git a/lib/core/utility.hpp b/lib/core/utility.hpp index b60c3aa9..606375e4 100644 --- a/lib/core/utility.hpp +++ b/lib/core/utility.hpp @@ -24,4 +24,9 @@ namespace OH { inline bool contains(const _Tp* arr, const std::size_t size, const _Tp& val) { return std::find(arr, arr + size, val) != arr + size; }; + + template + inline _Tp map(_Tp x, _Tp in_min, _Tp in_max, _Tp out_min, _Tp out_max) { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + } } // namespace OH diff --git a/lib/haptics/haptic_body.hpp b/lib/haptics/haptic_body.hpp index 0fd7f26f..10d55233 100644 --- a/lib/haptics/haptic_body.hpp +++ b/lib/haptics/haptic_body.hpp @@ -4,9 +4,9 @@ #include "haptic_plane.hpp" #include +#include #include -#include namespace OH { class HapticBody { @@ -35,8 +35,8 @@ namespace OH { */ template inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { - const oh_output_coord_t x_coord = map(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); - const oh_output_coord_t y_coord = map(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); + const oh_output_coord_t x_coord = map<_Tp>(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); + const oh_output_coord_t y_coord = map<_Tp>(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); return new oh_output_point_t(x_coord, y_coord); } From 5b8a5e329f840c996aadba1a2e01c92c5908aa13 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 12 Apr 2023 21:33:29 +0400 Subject: [PATCH 20/35] refactor: remove framework and platform from top-level config --- ini/bhaptics.ini | 198 ++++++++++++++++------- platformio.ini | 13 +- test/test_common/output/test_closest.cpp | 27 +--- 3 files changed, 156 insertions(+), 82 deletions(-) diff --git a/ini/bhaptics.ini b/ini/bhaptics.ini index 06d22bb1..7b94115c 100644 --- a/ini/bhaptics.ini +++ b/ini/bhaptics.ini @@ -1,140 +1,230 @@ [bhaptics] -build_flags = ${common.build_flags} +platform = platformio/espressif32@^6.1.0 +platform_packages = + platformio/framework-arduinoespressif32@^3.20007.0 +framework = arduino +board = esp32doit-devkit-v1 +upload_speed = 921600 +monitor_speed = 115200 + +build_flags = ${common.build_flags} -D BHAPTICS -build_unflags = ${common.build_unflags} -build_src_filter = ${common.build_src_filter} - + - + -lib_deps = ${common.lib_deps} +build_unflags = ${common.build_unflags} +build_src_filter = ${common.build_src_filter} +lib_deps = ${common.lib_deps} [env:bhaptics_tactsuit_x16] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTSUITX16 -D BH_BLE_APPEARANCE=510 '-D BLUETOOTH_NAME="TactSuitX16"' '-D BH_SERIAL_NUMBER={ 0x0d, 0x3a, 0xeb, 0x77, 0xbe, 0xf8, 0x7a, 0x1e, 0x3b, 0x2a }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactsuit_x16_pca9685] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTSUITX16 -D BH_BLE_APPEARANCE=510 '-D BLUETOOTH_NAME="TactSuitX16"' '-D BH_SERIAL_NUMBER={ 0x0d, 0x3a, 0xeb, 0x77, 0xbe, 0xf8, 0x7a, 0x1e, 0x3b, 0x2a }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactsuit_x40] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTSUITX40 -D BH_BLE_APPEARANCE=509 '-D BLUETOOTH_NAME="TactSuitX40"' '-D BH_SERIAL_NUMBER={ 0xcf, 0xcb, 0x0d, 0x95, 0x5f, 0xf6, 0xee, 0x2c, 0xbd, 0x73 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosy2_forearm_left] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSY2 -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="Tactosy2_L"' '-D BH_SERIAL_NUMBER={ 0xa0, 0xba, 0x0a, 0xd1, 0xbf, 0x36, 0x11, 0x30, 0xa4, 0xff }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosy2_forearm_right] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSY2 -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="Tactosy2_R"' '-D BH_SERIAL_NUMBER={ 0xb0, 0x1c, 0xc1, 0xf8, 0xec, 0x12, 0x18, 0x4e, 0x09, 0x77 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosyh_hand_left] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSYH -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactosyH_L"' '-D BH_SERIAL_NUMBER={ 0xc1, 0x36, 0xdc, 0x21, 0xc9, 0xd4, 0x17, 0x85, 0xbb, 0x90 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosyh_hand_right] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSYH -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactosyH_R"' '-D BH_SERIAL_NUMBER={ 0xc7, 0x5f, 0x3b, 0x06, 0x38, 0xba, 0x34, 0xfa, 0x36, 0xc1 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosyf_foot_left] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSYF -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactosyF_L"' '-D BH_SERIAL_NUMBER={ 0x1a, 0x45, 0x83, 0x44, 0x03, 0xc5, 0xf3, 0xc3, 0xf3, 0xb8 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactosyf_foot_right] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTOSYF -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactosyF_R"' '-D BH_SERIAL_NUMBER={ 0x14, 0xb9, 0x02, 0x62, 0x41, 0xe4, 0x04, 0xb2, 0xc5, 0x11 }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactal] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTAL -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="Tactal"' '-D BH_SERIAL_NUMBER={ 0xed, 0xcb, 0x55, 0x7c, 0xd7, 0xb9, 0x16, 0xc5, 0x18, 0x2a }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactglove_left] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTGLOVE -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactGlove (L"' '-D BH_SERIAL_NUMBER={ 0xcd, 0x0b, 0x81, 0x45, 0x85, 0xf9, 0x2b, 0x6c, 0xed, 0x5b }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} [env:bhaptics_tactglove_right] -build_flags = ${bhaptics.build_flags} +platform = ${bhaptics.platform} +platform_packages = ${bhaptics.platform_packages} +framework = ${bhaptics.framework} +board = ${bhaptics.board} +upload_speed = ${bhaptics.upload_speed} +monitor_speed = ${bhaptics.monitor_speed} + +build_flags = ${bhaptics.build_flags} -D BH_DEVICE_TACTGLOVE -D BH_BLE_APPEARANCE=508 '-D BLUETOOTH_NAME="TactGlove (R"' '-D BH_SERIAL_NUMBER={ 0x12, 0x0b, 0xae, 0xbf, 0xbc, 0x90, 0x3b, 0x0d, 0x84, 0xdd }' -build_unflags = ${bhaptics.build_unflags} -build_src_filter = ${bhaptics.build_src_filter} +build_unflags = ${bhaptics.build_unflags} +build_src_filter = ${bhaptics.build_src_filter} + -lib_deps = ${bhaptics.lib_deps} +lib_deps = ${bhaptics.lib_deps} diff --git a/platformio.ini b/platformio.ini index 9f9bf122..d22c041f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,7 @@ build_unflags = build_src_filter = +<*> - + lib_deps = Wire SPI @@ -41,21 +42,17 @@ lib_deps = h2zero/NimBLE-Arduino@^1.4.0 [env] -framework = arduino -platform = espressif32 -board = esp32doit-devkit-v1 - build_flags = ${common.build_flags} build_unflags = ${common.build_unflags} build_src_filter = ${common.build_src_filter} lib_deps = ${common.lib_deps} -upload_speed = 921600 -monitor_speed = 115200 - -test_ignore = test_desktop [env:test] +platform = native + build_flags = ${common.build_flags} build_unflags = ${common.build_unflags} build_src_filter = ${common.build_src_filter} + + +test_ignore = test_embedded diff --git a/test/test_common/output/test_closest.cpp b/test/test_common/output/test_closest.cpp index 0f831eb2..2824713b 100644 --- a/test/test_common/output/test_closest.cpp +++ b/test/test_common/output/test_closest.cpp @@ -1,34 +1,23 @@ -#include "unity.h" +#include -#ifdef UNIT_TEST - -class TestWriter : public OutputWriter { - public: - void writeOutput(oh_output_intensity_t intensity) override{}; -}; - -void test_coordinates(void) { - oh_output_map_t testOutputs{ - {oh_output_point_t(10, 10), new TestWriter()}, - {oh_output_point_t(UINT16_MAX - 10, UINT16_MAX - 10), new TestWriter()}, - }; - - auto output = new ClosestOutputComponent(testOutputs); +void setUp(void) { + // set stuff up here +} - auto points = output->getOutputPoints(); +void tearDown(void) { + // clean stuff up here } int process(void) { UNITY_BEGIN(); - RUN_TEST(test_coordinates); - return UNITY_END(); } #ifdef ARDUINO #include + void setup() { process(); } @@ -42,5 +31,3 @@ int main(int argc, char** argv) { } #endif - -#endif From 7c3eb91ec30d790f386ae8db7bdde69852975c18 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 12 Apr 2023 23:36:42 +0400 Subject: [PATCH 21/35] refactor(Core): remove non-native-compatible modules from core --- firmware/mode_configs/test.cpp | 7 +++++-- lib/core/logging.hpp | 8 +++++++- lib/{core => freertos}/task.hpp | 0 lib/{core => sensor}/abstract_battery.cpp | 0 lib/{core => sensor}/abstract_battery.hpp | 0 lib/{core => sensor}/sensor.hpp | 4 ++-- 6 files changed, 14 insertions(+), 5 deletions(-) rename lib/{core => freertos}/task.hpp (100%) rename lib/{core => sensor}/abstract_battery.cpp (100%) rename lib/{core => sensor}/abstract_battery.hpp (100%) rename lib/{core => sensor}/sensor.hpp (82%) diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index 39824129..99c1f943 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -1,8 +1,11 @@ // Override you configs in this file (Ctrl+Click) #include "config/all.h" -#include -#include +#ifdef UNIT_TEST + #include "ArduinoFake.h" +#else + #include "Arduino.h" +#endif #include "openhaptics.h" diff --git a/lib/core/logging.hpp b/lib/core/logging.hpp index f0ca9e33..94970935 100644 --- a/lib/core/logging.hpp +++ b/lib/core/logging.hpp @@ -1,5 +1,11 @@ #pragma once #if defined(ARDUINO_ARCH_ESP32) -#include + #include +#else + #define log_e(...) + #define log_w(...) + #define log_i(...) + #define log_d(...) + #define log_t(...) #endif diff --git a/lib/core/task.hpp b/lib/freertos/task.hpp similarity index 100% rename from lib/core/task.hpp rename to lib/freertos/task.hpp diff --git a/lib/core/abstract_battery.cpp b/lib/sensor/abstract_battery.cpp similarity index 100% rename from lib/core/abstract_battery.cpp rename to lib/sensor/abstract_battery.cpp diff --git a/lib/core/abstract_battery.hpp b/lib/sensor/abstract_battery.hpp similarity index 100% rename from lib/core/abstract_battery.hpp rename to lib/sensor/abstract_battery.hpp diff --git a/lib/core/sensor.hpp b/lib/sensor/sensor.hpp similarity index 82% rename from lib/core/sensor.hpp rename to lib/sensor/sensor.hpp index 5f75d843..48c11675 100644 --- a/lib/core/sensor.hpp +++ b/lib/sensor/sensor.hpp @@ -25,11 +25,11 @@ namespace OH protected: _Tp value; - uint rate; + uint32_t rate; virtual _Tp updateValue(void) = 0; public: - RatePollingComponent(TaskConfig taskConfig, uint rate) : Task>(taskConfig), rate(rate) {}; + RatePollingComponent(TaskConfig taskConfig, uint32_t rate) : Task>(taskConfig), rate(rate) {}; _Tp getValue() override { return this->value; }; }; } // namespace OH From d8df17bc0013e273470d6b9a57d46c26b5d29758 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 12 Apr 2023 23:37:05 +0400 Subject: [PATCH 22/35] feat: add barebones unit testing --- platformio.ini | 13 ++- .../test_common/haptics/test_haptic_plane.cpp | 99 +++++++++++++++++++ test/test_common/output/test_closest.cpp | 33 ------- 3 files changed, 108 insertions(+), 37 deletions(-) create mode 100644 test/test_common/haptics/test_haptic_plane.cpp delete mode 100644 test/test_common/output/test_closest.cpp diff --git a/platformio.ini b/platformio.ini index d22c041f..dbf961b2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,6 +16,8 @@ extra_configs = ini/bhaptics.ini [common] +build_unflags = + -std=gnu++11 build_flags = -std=gnu++17 -D __OH_FIRMWARE__ @@ -26,8 +28,6 @@ build_flags = ; -D BATTERY_ENABLED=true ; -D BLUETOOTH_USE_NIMBLE=false -build_unflags = - -std=gnu++11 build_src_filter = +<*> - @@ -50,9 +50,14 @@ lib_deps = ${common.lib_deps} [env:test] platform = native -build_flags = ${common.build_flags} build_unflags = ${common.build_unflags} +build_flags = ${common.build_flags} build_src_filter = ${common.build_src_filter} + +lib_deps = ${common.lib_deps} +; fabiobatsilva/ArduinoFake@^0.3.1 -test_ignore = test_embedded +test_ignore = test_embedded +test_filter = + test_common + test_native/* diff --git a/test/test_common/haptics/test_haptic_plane.cpp b/test/test_common/haptics/test_haptic_plane.cpp new file mode 100644 index 00000000..2b268041 --- /dev/null +++ b/test/test_common/haptics/test_haptic_plane.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace OH; + +static HapticPlane* plane = nullptr; + +class TestActuator : public AbstractActuator { + public: + bool isSetup = false; + oh_output_intensity_t intensity = 0; + + TestActuator() : AbstractActuator() {} + void setup() override { + this->isSetup = true; + } + void writeOutput(oh_output_intensity_t intensity) override { + this->intensity = intensity; + } +}; + +void setUp(void) { + // +} + +void tearDown(void) { + plane = nullptr; +} + +void test_it_sets_up_actuators(void) { + oh_output_writers_map_t outputs = { + {{0, 0}, new TestActuator()}, + {{0, 1}, new TestActuator()}, + {{1, 0}, new TestActuator()}, + {{1, 1}, new TestActuator()}, + }; + + plane = new HapticPlane(outputs); + + plane->setup(); + + for (auto& kv : outputs) { + TEST_ASSERT_TRUE(static_cast(kv.second)->isSetup); + } +} + +void test_it_writes_to_correct_output(void) { + auto actuator = new TestActuator(), + actuator2 = new TestActuator(), + actuator3 = new TestActuator(), + actuator4 = new TestActuator(); + + oh_output_writers_map_t outputs = { + {{0, 0}, actuator}, + {{0, 1}, actuator2}, + {{1, 0}, actuator3}, + {{1, 1}, actuator4}, + }; + + plane = new HapticPlane(outputs); + plane->setup(); + + plane->writeOutput({{0, 0}, 64}); + plane->writeOutput({{0, 1}, 128}); + plane->writeOutput({{1, 0}, 192}); + plane->writeOutput({{1, 1}, 255}); + + TEST_ASSERT_EQUAL_UINT8(64, actuator->intensity); + TEST_ASSERT_EQUAL_UINT8(128, actuator2->intensity); + TEST_ASSERT_EQUAL_UINT8(192, actuator3->intensity); + TEST_ASSERT_EQUAL_UINT8(255, actuator4->intensity); +} + +int process(void) { + UNITY_BEGIN(); + + RUN_TEST(test_it_sets_up_actuators); + RUN_TEST(test_it_writes_to_correct_output); + + return UNITY_END(); +} + +#ifdef ARDUINO + +#include + +void setup(void) { + process(); +} + +void loop(void) {} + +#else + +int main(int argc, char** argv) { + return process(); +} + +#endif diff --git a/test/test_common/output/test_closest.cpp b/test/test_common/output/test_closest.cpp deleted file mode 100644 index 2824713b..00000000 --- a/test/test_common/output/test_closest.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -void setUp(void) { - // set stuff up here -} - -void tearDown(void) { - // clean stuff up here -} - -int process(void) { - UNITY_BEGIN(); - - return UNITY_END(); -} - -#ifdef ARDUINO - -#include - -void setup() { - process(); -} - -void loop() {} - -#else - -int main(int argc, char** argv) { - return process(); -} - -#endif From 0fc44d508611e4d5a1132199c8f46d812e610358 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Thu, 13 Apr 2023 01:03:59 +0400 Subject: [PATCH 23/35] feat: add coverage report for unit tests --- .gitignore | 6 ++++++ platformio.ini | 13 ++++++++++--- scripts/test-cover.sh | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100755 scripts/test-cover.sh diff --git a/.gitignore b/.gitignore index 70d17d02..ef82776d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +# IDE files/dirs .vscode .vscode/.browse.c_cpp.db* .vscode/c_cpp_properties.json @@ -18,5 +19,10 @@ cmake-build-debug .clang_complete .gcc-flags.json +# Test files/dirs +lcov.info +lcov.info.* +lcov-report/ + # CI /build/ diff --git a/platformio.ini b/platformio.ini index dbf961b2..6a7c427f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -47,17 +47,24 @@ build_unflags = ${common.build_unflags} build_src_filter = ${common.build_src_filter} lib_deps = ${common.lib_deps} -[env:test] +[env:native-test] platform = native build_unflags = ${common.build_unflags} build_flags = ${common.build_flags} + -lgcov + --coverage build_src_filter = ${common.build_src_filter} + lib_deps = ${common.lib_deps} ; fabiobatsilva/ArduinoFake@^0.3.1 -test_ignore = test_embedded -test_filter = +test_ignore = test_embedded +test_filter = test_common test_native/* +test_testing_command = + scripts/test-cover.sh + ${platformio.build_dir} + ${this.__env__} + ${platformio.build_dir}/${this.__env__}/program diff --git a/scripts/test-cover.sh b/scripts/test-cover.sh new file mode 100755 index 00000000..474b9885 --- /dev/null +++ b/scripts/test-cover.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -euxo pipefail + +build_dir=$1 +env=$2 +shift 2 + +# Generate initial coverage report +lcov -i -d $build_dir/$env/ -c -o lcov.info.initial + +# Run tests +$@ + +lcov -d $build_dir/$env/ -c -o lcov.info +lcov --add-tracefile lcov.info.initial -a lcov.info -o lcov.info.merged +lcov --remove lcov.info.merged '/usr/include/*' '*.pio/*' '*/tool-unity/*' '*/test/*' '*/MockArduino/*' -o lcov.info.cleaned +genhtml -p $PWD -o build/coverage/ --demangle-cpp lcov.info.cleaned From db08a2861f67a6ca42cc69550f3298c4cd0c43a7 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Thu, 13 Apr 2023 12:10:46 +0400 Subject: [PATCH 24/35] test(Point2): add tests for operators --- firmware/mode_configs/test.cpp | 38 +--------- lib/core/logging.hpp | 6 ++ lib/freertos/task.hpp | 20 +++--- lib/haptics/haptic_body.cpp | 25 ------- lib/haptics/haptic_body.hpp | 15 ---- lib/haptics/haptic_plane.cpp | 13 ++-- lib/haptics/haptic_plane.hpp | 36 ++++++++++ platformio.ini | 5 +- .../main.cpp} | 69 +++++++++++++++---- test/test_math_point2/main.cpp | 59 ++++++++++++++++ 10 files changed, 178 insertions(+), 108 deletions(-) rename test/{test_common/haptics/test_haptic_plane.cpp => test_haptics_plane/main.cpp} (52%) create mode 100644 test/test_math_point2/main.cpp diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index 99c1f943..602b6d87 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -9,17 +9,9 @@ #include "openhaptics.h" -#include -#include -#include #include -#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true -#include -#endif - using namespace OH; -using namespace BH; extern OpenHaptics App; OpenHaptics* app = &App; @@ -31,9 +23,7 @@ class TestOutput : public OH::AbstractActuator { public: TestOutput(uint8_t channel) : channel(channel){}; uint8_t getChannel() { return channel; }; - void writeOutput(oh_output_intensity_t intensity) { - Serial.printf("\t> Channel %2u: %3u\n", channel, intensity); - }; + void writeOutput(oh_output_intensity_t intensity) {}; }; void setupMode() { @@ -44,31 +34,9 @@ void setupMode() { {new TestOutput(6), new TestOutput(7)}, }); - for (const auto& kv : testOutputs) { - Serial.printf("> Channel %2u: (%5u, %5u)\n", - ((TestOutput*)kv.second)->getChannel(), kv.first.x, - kv.first.y); - } - - auto test = new HapticPlane_Closest(OUTPUT_PATH_ACCESSORY, testOutputs); - - oh_output_writers_map_t layout{ - {oh_output_point_t(0, 0), new TestOutput(8)}, - }; - auto test2 = new HapticPlane_Closest(OUTPUT_PATH_ACCESSORY, layout); - - App.getHapticBody()->addComponent(test); - - for (auto i = 0; i < testOutputs.size(); i++) { - oh_output_data_t outData{ - oh_output_point_t(0, 0), - (uint8_t)map(i, 0, testOutputs.size() - 1, 0, 255), - }; - App.getHapticBody()->writeOutput(OUTPUT_PATH_ACCESSORY, outData); - } + auto test = new HapticPlane_Closest(testOutputs); + App.getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, test); } void loopMode() { - // Free up the Arduino loop task - vTaskDelete(NULL); } diff --git a/lib/core/logging.hpp b/lib/core/logging.hpp index 94970935..1679cc2d 100644 --- a/lib/core/logging.hpp +++ b/lib/core/logging.hpp @@ -2,6 +2,12 @@ #if defined(ARDUINO_ARCH_ESP32) #include +#elif defined(UNITY_INCLUDE_PRINT_FORMATTED) + #define log_e(...) TEST_PRINTF(__VA_ARGS__) + #define log_w(...) TEST_PRINTF(__VA_ARGS__) + #define log_i(...) TEST_PRINTF(__VA_ARGS__) + #define log_d(...) TEST_PRINTF(__VA_ARGS__) + #define log_t(...) TEST_PRINTF(__VA_ARGS__) #else #define log_e(...) #define log_w(...) diff --git a/lib/freertos/task.hpp b/lib/freertos/task.hpp index a5dab82f..5804b5f5 100644 --- a/lib/freertos/task.hpp +++ b/lib/freertos/task.hpp @@ -8,15 +8,17 @@ extern "C" void delay(uint32_t ms); #include // Include the base FreeRTOS definitions. #include // Include the task definitions. -extern "C" BaseType_t xTaskCreateUniversal( - TaskFunction_t pxTaskCode, - const char * const pcName, - const uint32_t usStackDepth, - void * const pvParameters, - UBaseType_t uxPriority, - TaskHandle_t * const pxCreatedTask, - const BaseType_t xCoreID -); +extern "C" { + BaseType_t xTaskCreateUniversal( + TaskFunction_t pxTaskCode, + const char * const pcName, + const uint32_t usStackDepth, + void * const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t * const pxCreatedTask, + const BaseType_t xCoreID + ); +} #endif namespace OH { diff --git a/lib/haptics/haptic_body.cpp b/lib/haptics/haptic_body.cpp index 120bf2ae..5da168c3 100644 --- a/lib/haptics/haptic_body.cpp +++ b/lib/haptics/haptic_body.cpp @@ -20,28 +20,3 @@ void OH::HapticBody::writeOutput(const oh_output_path_t path, const oh_output_da auto componentSearch = this->getComponents()->find(path); (*componentSearch).second->writeOutput(data); } - -template -inline std::map OH::mapMatrixCoordinates(std::vector> map2d) { - std::map points{}; - - size_t y_size = map2d.size(); - size_t y_max = y_size - 1; - - for (size_t y = 0; y < y_size; ++y) { - auto row = map2d.at(y); - size_t x_size = row.size(); - size_t x_max = x_size - 1; - - for (size_t x = 0; x < x_size; ++x) { - AbstractActuator* wr = row.at(x); - oh_output_point_t* coord = mapPoint(x, y, x_max, y_max); - - points[*coord] = wr; - } - } - - return points; -}; - -template oh_output_writers_map_t OH::mapMatrixCoordinates(std::vector>); diff --git a/lib/haptics/haptic_body.hpp b/lib/haptics/haptic_body.hpp index 10d55233..14cf0c70 100644 --- a/lib/haptics/haptic_body.hpp +++ b/lib/haptics/haptic_body.hpp @@ -28,19 +28,4 @@ namespace OH { } }; }; - - /** - * Re-maps a point index to output coordinate. - * @tparam _Tp The type of the point index. - */ - template - inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { - const oh_output_coord_t x_coord = map<_Tp>(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); - const oh_output_coord_t y_coord = map<_Tp>(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); - - return new oh_output_point_t(x_coord, y_coord); - } - - template - std::map mapMatrixCoordinates(std::vector> map2d); } // namespace OH diff --git a/lib/haptics/haptic_plane.cpp b/lib/haptics/haptic_plane.cpp index f46a819c..712cc452 100644 --- a/lib/haptics/haptic_plane.cpp +++ b/lib/haptics/haptic_plane.cpp @@ -2,6 +2,7 @@ #include #include +#include #include void OH::HapticPlane::setOutputs(oh_output_writers_map_t& outputs) { @@ -34,16 +35,14 @@ void OH::HapticPlane::writeOutput(const oh_output_data_t& data) { this->writers[data.point]->writeOutput(data.intensity); } -oh_output_point_t OH::HapticPlane_Closest::findClosestPoints( - std::list& pts, - const oh_output_point_t& target) { +oh_output_point_t OH::HapticPlane_Closest::findClosestPoints(std::list& pts, const oh_output_point_t& target) { + if (contains(pts, target)) { + return target; + } + std::multimap mp = {}; for (auto& _p : pts) { - if (target == _p) { - return _p; // if coord == target, no other needed - } - float dx = abs(((float)target.x / OH_OUTPUT_COORD_MAX) - ((float)_p.x / OH_OUTPUT_COORD_MAX)), dy = abs(((float)target.y / OH_OUTPUT_COORD_MAX) - ((float)_p.y / OH_OUTPUT_COORD_MAX)); diff --git a/lib/haptics/haptic_plane.hpp b/lib/haptics/haptic_plane.hpp index 82bf5b0c..4abcfb60 100644 --- a/lib/haptics/haptic_plane.hpp +++ b/lib/haptics/haptic_plane.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -42,4 +43,39 @@ namespace OH { HapticPlane_Closest(oh_output_writers_map_t& outputs) : HapticPlane(outputs) {}; void writeOutput(const oh_output_data_t&) override; }; + + /** + * Re-maps a point index to output coordinate. + * @tparam _Tp The type of the point index. + */ + template + inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { + const oh_output_coord_t x_coord = map<_Tp>(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); + const oh_output_coord_t y_coord = map<_Tp>(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); + + return new oh_output_point_t(x_coord, y_coord); + } + + template + inline std::map mapMatrixCoordinates(std::vector> map2d) { + std::map points{}; + + size_t y_size = map2d.size(); + size_t y_max = y_size - 1; + + for (size_t y = 0; y < y_size; ++y) { + auto row = map2d.at(y); + size_t x_size = row.size(); + size_t x_max = x_size - 1; + + for (size_t x = 0; x < x_size; ++x) { + AbstractActuator* wr = row.at(x); + oh_output_point_t* coord = mapPoint(x, y, x_max, y_max); + + points[*coord] = wr; + } + } + + return points; + }; } // namespace OH diff --git a/platformio.ini b/platformio.ini index 6a7c427f..16ab2419 100644 --- a/platformio.ini +++ b/platformio.ini @@ -57,12 +57,9 @@ build_flags = ${common.build_flags} build_src_filter = ${common.build_src_filter} + lib_deps = ${common.lib_deps} -; fabiobatsilva/ArduinoFake@^0.3.1 + fabiobatsilva/ArduinoFake@^0.3.1 test_ignore = test_embedded -test_filter = - test_common - test_native/* test_testing_command = scripts/test-cover.sh ${platformio.build_dir} diff --git a/test/test_common/haptics/test_haptic_plane.cpp b/test/test_haptics_plane/main.cpp similarity index 52% rename from test/test_common/haptics/test_haptic_plane.cpp rename to test/test_haptics_plane/main.cpp index 2b268041..e877057c 100644 --- a/test/test_common/haptics/test_haptic_plane.cpp +++ b/test/test_haptics_plane/main.cpp @@ -3,8 +3,6 @@ using namespace OH; -static HapticPlane* plane = nullptr; - class TestActuator : public AbstractActuator { public: bool isSetup = false; @@ -19,14 +17,6 @@ class TestActuator : public AbstractActuator { } }; -void setUp(void) { - // -} - -void tearDown(void) { - plane = nullptr; -} - void test_it_sets_up_actuators(void) { oh_output_writers_map_t outputs = { {{0, 0}, new TestActuator()}, @@ -35,8 +25,7 @@ void test_it_sets_up_actuators(void) { {{1, 1}, new TestActuator()}, }; - plane = new HapticPlane(outputs); - + auto plane = new HapticPlane(outputs); plane->setup(); for (auto& kv : outputs) { @@ -57,7 +46,7 @@ void test_it_writes_to_correct_output(void) { {{1, 1}, actuator4}, }; - plane = new HapticPlane(outputs); + auto plane = new HapticPlane(outputs); plane->setup(); plane->writeOutput({{0, 0}, 64}); @@ -71,11 +60,65 @@ void test_it_writes_to_correct_output(void) { TEST_ASSERT_EQUAL_UINT8(255, actuator4->intensity); } +void test_closest_it_writes_to_correct_if_exact(void) { + auto actuator = new TestActuator(), + actuator2 = new TestActuator(), + actuator3 = new TestActuator(), + actuator4 = new TestActuator(); + + oh_output_writers_map_t outputs = { + {{0, 0}, actuator}, + {{0, 1}, actuator2}, + {{1, 0}, actuator3}, + {{1, 1}, actuator4}, + }; + + auto plane = new HapticPlane_Closest(outputs); + plane->setup(); + + plane->writeOutput({{0, 0}, 1}); + plane->writeOutput({{0, 1}, 2}); + plane->writeOutput({{1, 0}, 3}); + plane->writeOutput({{1, 1}, 4}); + + TEST_ASSERT_EQUAL(1, actuator->intensity); + TEST_ASSERT_EQUAL(2, actuator2->intensity); + TEST_ASSERT_EQUAL(3, actuator3->intensity); + TEST_ASSERT_EQUAL(4, actuator4->intensity); +} + +void test_closest_it_correctly_finds_closest(void) { + auto actuator = new TestActuator(), + actuator2 = new TestActuator(), + actuator3 = new TestActuator(), + actuator4 = new TestActuator(); + + oh_output_writers_map_t outputs = { + {{0, 0}, actuator}, + {{0, 64}, actuator2}, + {{64, 0}, actuator3}, + {{64, 64}, actuator4}, + }; + + auto plane = new HapticPlane_Closest(outputs); + plane->setup(); + + plane->writeOutput({{16, 16}, 16}); + plane->writeOutput({{65, 65}, 65}); + + TEST_ASSERT_EQUAL(16, actuator->intensity); + TEST_ASSERT_EQUAL(0, actuator2->intensity); + TEST_ASSERT_EQUAL(0, actuator3->intensity); + TEST_ASSERT_EQUAL(65, actuator4->intensity); +} + int process(void) { UNITY_BEGIN(); RUN_TEST(test_it_sets_up_actuators); RUN_TEST(test_it_writes_to_correct_output); + RUN_TEST(test_closest_it_writes_to_correct_if_exact); + RUN_TEST(test_closest_it_correctly_finds_closest); return UNITY_END(); } diff --git a/test/test_math_point2/main.cpp b/test/test_math_point2/main.cpp new file mode 100644 index 00000000..d568a635 --- /dev/null +++ b/test/test_math_point2/main.cpp @@ -0,0 +1,59 @@ +#include +#include + +using namespace OH; + +void test_operator_equal(void) { + Point2 p1 = {1, 2}; + Point2 p2 = {1, 2}; + Point2 p3 = {2, 1}; + + TEST_ASSERT_TRUE(p1 == p2); + TEST_ASSERT_FALSE(p1 == p3); +} + +void test_operator_not_equal(void) { + Point2 p1 = {1, 2}; + Point2 p2 = {1, 2}; + Point2 p3 = {2, 1}; + + TEST_ASSERT_FALSE(p1 != p2); + TEST_ASSERT_TRUE(p1 != p3); +} + +void test_operator_less_than(void) { + Point2 p1 = {1, 2}; + Point2 p2 = {1, 2}; + Point2 p3 = {2, 1}; + + TEST_ASSERT_FALSE(p1 < p2); + TEST_ASSERT_TRUE(p1 < p3); +} + +int process(void) { + UNITY_BEGIN(); + + RUN_TEST(test_operator_equal); + RUN_TEST(test_operator_not_equal); + RUN_TEST(test_operator_less_than); + + return UNITY_END(); +} + +#ifdef ARDUINO + +#include + +void setup(void) { + process(); +} + +void loop(void) {} + +#else + +int main(int argc, char** argv) { + return process(); +} + +#endif From eeba26787346e9d72e25593d982855ff28d78c7a Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Thu, 13 Apr 2023 12:22:37 +0400 Subject: [PATCH 25/35] ci(GitHub): run unit tests --- .github/workflows/ci.yml | 56 +++++++++++++++++++++++++++++++++ test/test_core_utility/main.cpp | 44 ++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 test/test_core_utility/main.cpp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..41214a07 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,56 @@ +name: PlatformIO CI + +on: + pull_request: + branches: + - master + - develop + paths-ignore: + - '**/*.md' + push: + branches: + - master + - develop + paths-ignore: + - '**/*.md' + +jobs: + native-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Setup LCOV + uses: hrishikesh-kadam/setup-lcov@v1 + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + - name: Cache PlatformIO + uses: actions/cache@v3 + with: + path: ~/.platformio + key: ${{ runner.os }}-pio-${{ hashFiles('**/lockfiles') }} + restore-keys: | + ${{ runner.os }}-pio- + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install --upgrade platformio + pio upgrade --dev + pio pkg update --global + + - name: Run Unit Tests + run: pio test -e native-test diff --git a/test/test_core_utility/main.cpp b/test/test_core_utility/main.cpp new file mode 100644 index 00000000..90359741 --- /dev/null +++ b/test/test_core_utility/main.cpp @@ -0,0 +1,44 @@ +#include +#include + +using namespace OH; + +void test_map(void) { + TEST_ASSERT_EQUAL_UINT16(0, map(0, 0, 4095, 0, 255)); + TEST_ASSERT_EQUAL_UINT16(127, map(2047, 0, 4095, 0, 255)); + TEST_ASSERT_EQUAL_UINT16(255, map(4095, 0, 4095, 0, 255)); + + TEST_ASSERT_EQUAL_UINT16(127, map(0, 0, 4095, 127, 255)); + TEST_ASSERT_EQUAL_UINT16(190, map(2047, 0, 4095, 127, 255)); + TEST_ASSERT_EQUAL_UINT16(255, map(4095, 0, 4095, 127, 255)); + + TEST_ASSERT_EQUAL_UINT16(0, map(2048, 2048, 4095, 0, 255)); + TEST_ASSERT_EQUAL_UINT16(127, map(3071, 2048, 4095, 0, 255)); + TEST_ASSERT_EQUAL_UINT16(255, map(4095, 2048, 4095, 0, 255)); +} + +int process(void) { + UNITY_BEGIN(); + + RUN_TEST(test_map); + + return UNITY_END(); +} + +#ifdef ARDUINO + +#include + +void setup(void) { + process(); +} + +void loop(void) {} + +#else + +int main(int argc, char** argv) { + return process(); +} + +#endif From 85f26995e03766d54db6803f3a240a142c6c7cde Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Thu, 13 Apr 2023 13:26:02 +0400 Subject: [PATCH 26/35] test(Haptics): add point mapper test --- lib/core/types.hpp | 7 +++---- lib/core/utility.cpp | 1 - test/test_haptics_plane/main.cpp | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) delete mode 100644 lib/core/utility.cpp diff --git a/lib/core/types.hpp b/lib/core/types.hpp index 56de9b5a..d5e7d8ba 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -1,7 +1,7 @@ #pragma once -#include "abstract_actuator.hpp" -#include "point2.hpp" +#include +#include #include @@ -25,5 +25,4 @@ namespace OH { typedef OH::OutputData oh_output_data_t; typedef OH::OutputState oh_output_state_t; -typedef std::map - oh_output_writers_map_t; +typedef std::map oh_output_writers_map_t; diff --git a/lib/core/utility.cpp b/lib/core/utility.cpp deleted file mode 100644 index 6b851aaa..00000000 --- a/lib/core/utility.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "utility.hpp" diff --git a/test/test_haptics_plane/main.cpp b/test/test_haptics_plane/main.cpp index e877057c..15607453 100644 --- a/test/test_haptics_plane/main.cpp +++ b/test/test_haptics_plane/main.cpp @@ -112,6 +112,23 @@ void test_closest_it_correctly_finds_closest(void) { TEST_ASSERT_EQUAL(65, actuator4->intensity); } +void test_map_points(void) { + auto point = mapPoint(0, 0, 0, 0); + + TEST_ASSERT_EQUAL(127, point->x); + TEST_ASSERT_EQUAL(127, point->y); + + point = mapPoint(0, 0, 1, 1); + + TEST_ASSERT_EQUAL(85, point->x); + TEST_ASSERT_EQUAL(85, point->y); + + point = mapPoint(1, 1, 1, 1); + + TEST_ASSERT_EQUAL(170, point->x); + TEST_ASSERT_EQUAL(170, point->y); +} + int process(void) { UNITY_BEGIN(); @@ -119,6 +136,7 @@ int process(void) { RUN_TEST(test_it_writes_to_correct_output); RUN_TEST(test_closest_it_writes_to_correct_if_exact); RUN_TEST(test_closest_it_correctly_finds_closest); + RUN_TEST(test_map_points); return UNITY_END(); } From 9e86ef77c3daeae057bcf3d77c5dc0cb63958a35 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Mon, 17 Apr 2023 12:28:18 +0400 Subject: [PATCH 27/35] ci(GitHub): collect initial coverage upon build --- .github/workflows/build-bhaptics.yml | 227 --------------------------- .github/workflows/ci.yml | 201 +++++++++++++++++++++++- .vscode/settings.json | 3 + platformio.ini | 7 +- scripts/test-cover.sh | 6 +- 5 files changed, 204 insertions(+), 240 deletions(-) delete mode 100644 .github/workflows/build-bhaptics.yml diff --git a/.github/workflows/build-bhaptics.yml b/.github/workflows/build-bhaptics.yml deleted file mode 100644 index 9fa1058c..00000000 --- a/.github/workflows/build-bhaptics.yml +++ /dev/null @@ -1,227 +0,0 @@ -name: Check bHaptics builds - -on: - pull_request: - branches: - - master - - develop - paths-ignore: - - '**/*.md' - push: - branches: - - master - paths-ignore: - - '**/*.md' - -jobs: - check-build-os: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: - - windows-latest - - ubuntu-latest - - macos-latest - target: - - bhaptics_tactsuit_x40 - - # Enabling all flags to test build for every feature - battery_flag: - - BATTERY_ENABLED=true - serial_plotter_flag: - - SERIAL_PLOTTER=true - - # C'mon, GitHub, give us YAML anchors, it's a basic feature! cc actions/runner#1182 - steps: - - uses: actions/checkout@v3 - - - name: Cache pip - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - - name: Cache PlatformIO - uses: actions/cache@v3 - with: - path: ~/.platformio - key: ${{ runner.os }}-pio-${{ hashFiles('**/lockfiles') }} - restore-keys: | - ${{ runner.os }}-pio- - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.9' - - - name: Update build flags (non-macOS) - if: runner.os != 'macOS' - run: | - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - - - name: Update build flags (macOS) - if: runner.os == 'macOS' - run: | - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - - - name: Install PlatformIO - run: | - python -m pip install --upgrade pip - pip install --upgrade platformio - pio upgrade --dev - pio pkg update --global - - - name: Build - run: | - echo "::group::platformio.ini" - cat platformio.ini - echo "::endgroup::" - pio run --environment ${{matrix.target}} - - build-flags: - needs: check-build-os - runs-on: ubuntu-latest - strategy: - fail-fast: true - matrix: - target: - # Using x40 as it is uses both ledc and pca9685 outputs - - bhaptics_tactsuit_x40 - battery_flag: - - BATTERY_ENABLED=true - - BATTERY_ENABLED=false - serial_plotter_flag: - - SERIAL_PLOTTER=false - - SERIAL_PLOTTER=true - nimble_flag: - - BLUETOOTH_USE_NIMBLE=true - - BLUETOOTH_USE_NIMBLE=false - - steps: - - uses: actions/checkout@v3 - - - name: Cache pip - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - - name: Cache PlatformIO - uses: actions/cache@v3 - with: - path: ~/.platformio - key: ${{ runner.os }}-pio-${{ hashFiles('**/lockfiles') }} - restore-keys: | - ${{ runner.os }}-pio- - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.9' - - - name: Update build flags (non-macOS) - if: runner.os != 'macOS' - run: | - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini - - - name: Update build flags (macOS) - if: runner.os == 'macOS' - run: | - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini - - - name: Install PlatformIO - run: | - python -m pip install --upgrade pip - pip install --upgrade platformio - pio upgrade --dev - pio pkg update --global - - - name: Build - run: | - echo "::group::platformio.ini" - cat platformio.ini - echo "::endgroup::" - pio run --environment ${{matrix.target}} - - build-targets: - needs: build-flags - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - target: - - bhaptics_tactsuit_x16 - - bhaptics_tactsuit_x16_pca9685 - - bhaptics_tactsuit_x40 - - bhaptics_tactosy2_forearm_right - - bhaptics_tactosyh_hand_right - - bhaptics_tactosyf_foot_right - - bhaptics_tactal - - bhaptics_tactglove_right - - # Enabling all flags to test build for every feature - battery_flag: - - BATTERY_ENABLED=true - serial_plotter_flag: - - SERIAL_PLOTTER=true - - steps: - - uses: actions/checkout@v3 - - - name: Cache pip - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - - name: Cache PlatformIO - uses: actions/cache@v3 - with: - path: ~/.platformio - key: ${{ runner.os }}-pio-${{ hashFiles('**/lockfiles') }} - restore-keys: | - ${{ runner.os }}-pio- - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.9' - - - name: Update build flags (non-macOS) - if: runner.os != 'macOS' - run: | - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - - - name: Update build flags (macOS) - if: runner.os == 'macOS' - run: | - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini - sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini - - - name: Install PlatformIO - run: | - python -m pip install --upgrade pip - pip install --upgrade platformio - pio upgrade --dev - pio pkg update --global - - - name: Build - run: | - echo "::group::platformio.ini" - cat platformio.ini - echo "::endgroup::" - pio run --environment ${{matrix.target}} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41214a07..3a99c420 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,8 +15,152 @@ on: - '**/*.md' jobs: - native-test: + build: + name: Build ${{ matrix.target }} on ${{ matrix.os }} ${{ matrix.coverage && 'with coverage' || 'without coverage' }}, -D ${{ matrix.battery_flag }} -D ${{ matrix.serial_plotter_flag }} -D ${{ matrix.nimble_flag }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest ] + target: + - bhaptics_tactsuit_x16 + - bhaptics_tactsuit_x16_pca9685 + - bhaptics_tactsuit_x40 + - bhaptics_tactosy2_forearm_right + - bhaptics_tactosyh_hand_right + - bhaptics_tactosyf_foot_right + - bhaptics_tactal + - bhaptics_tactglove_right + battery_flag: [ BATTERY_ENABLED=true ] + serial_plotter_flag: [ SERIAL_PLOTTER=false ] + nimble_flag: [ BLUETOOTH_USE_NIMBLE=false ] + coverage: [ false ] + + include: + # Extra tests for x40, as it uses the most features + - target: bhaptics_tactsuit_x40 + os: ubuntu-latest + coverage: true + battery_flag: BATTERY_ENABLED=true + serial_plotter_flag: SERIAL_PLOTTER=true + nimble_flag: BLUETOOTH_USE_NIMBLE=true + # - target: bhaptics_tactsuit_x40 + # os: ubuntu-latest + # coverage: true + # battery_flag: BATTERY_ENABLED=true + # serial_plotter_flag: SERIAL_PLOTTER=true + # nimble_flag: BLUETOOTH_USE_NIMBLE=false + + - target: bhaptics_tactsuit_x40 + os: windows-latest + coverage: false + battery_flag: BATTERY_ENABLED=true + serial_plotter_flag: SERIAL_PLOTTER=false + nimble_flag: BLUETOOTH_USE_NIMBLE=true + - target: bhaptics_tactsuit_x40 + os: windows-latest + coverage: false + battery_flag: BATTERY_ENABLED=true + serial_plotter_flag: SERIAL_PLOTTER=false + nimble_flag: BLUETOOTH_USE_NIMBLE=false + - target: bhaptics_tactsuit_x40 + os: macos-latest + battery_flag: BATTERY_ENABLED=true + serial_plotter_flag: SERIAL_PLOTTER=false + nimble_flag: BLUETOOTH_USE_NIMBLE=false + + steps: + - uses: actions/checkout@v3 + + - name: Setup LCOV + if: matrix.coverage + uses: hrishikesh-kadam/setup-lcov@v1 + + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + - uses: actions/cache@v3 + with: + path: ~/.platformio/.cache + key: ${{ runner.os }}-pio-${{ matrix.target }} + restore-keys: | + ${{ runner.os }}-pio- + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + + - name: Enable coverage (non-macOS) + if: runner.os != 'macOS' && matrix.coverage + run: | + sed -i '/__OH_FIRMWARE__/p; s/-D __OH_FIRMWARE__/-lgcov --coverage/' platformio.ini + - name: Enable coverage (macOS) + if: runner.os == 'macOS' && matrix.coverage + run: | + sed -i '' '/__OH_FIRMWARE__/p; s/-D __OH_FIRMWARE__/-lgcov --coverage/' platformio.ini + + - name: Update build flags (non-macOS) + if: runner.os != 'macOS' + run: | + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini + sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini + - name: Update build flags (macOS) + if: runner.os == 'macOS' + run: | + sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini + sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini + sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini + + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install --upgrade platformio + pio upgrade --dev + pio pkg update --global + + - name: Install libs + run: pio lib -e ${{matrix.target}} install + + - name: Change memory segments + if: matrix.coverage + run: | + sed -i "s/len\s=\s0x2c200\s-\s0xdb5c/len = 289888/" ~/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/ld/memory.ld + + - name: Build + run: | + echo "::group::platformio.ini" + cat platformio.ini + echo "::endgroup::" + pio run --environment ${{matrix.target}} + + - name: Collect initial coverage + if: matrix.coverage + run: | + mkdir -p ./build/lcov + lcov -i -d ./.pio/build/${{matrix.target}}/ -c -o ./build/lcov/lcov.info.${{matrix.target}} -gcov-tool ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-gcov + + - name: Upload coverage Artifact + uses: actions/upload-artifact@v3 + if: matrix.coverage + with: + name: lcov.info.${{matrix.target}} + path: ./build/lcov/lcov.info.${{matrix.target}} + retention-days: 5 + + test: runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + target: + - native steps: - uses: actions/checkout@v3 @@ -53,4 +197,57 @@ jobs: pio pkg update --global - name: Run Unit Tests - run: pio test -e native-test + run: pio test -e ${{matrix.target}} + + - name: Collect coverage report + run: | + mkdir -p ./build/lcov + lcov -d ./.pio/build/${{matrix.target}}/ -c -o ./build/lcov/lcov.info.${{matrix.target}}.test + + - name: Upload coverage Artifact + uses: actions/upload-artifact@v3 + with: + name: lcov.info.${{matrix.target}}.test + path: ./build/lcov/lcov.info.${{matrix.target}}.test + retention-days: 5 + + coverage-report: + needs: [build, test] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup LCOV + uses: hrishikesh-kadam/setup-lcov@v1 + + - name: Download all workflow run artifacts + id: download + uses: actions/download-artifact@v3 + with: + path: build/artifacts + + - name: Merge lcov files + run: | + mkdir ./build/lcov/ + find ./build/artifacts -mindepth 2 -type f -exec mv '{}' ./build/lcov/ \; + ls -lahR ./build/lcov/ + + find ./build/lcov -name 'lcov.info.*' -exec echo -a {} \; | xargs lcov -o ./build/lcov/lcov.info + lcov --remove ./build/lcov/lcov.info '/usr/include/*' '*.platformio/*' '*/.pio/*' '*/tool-unity/*' '*/test/*' '*/MockArduino/*' -o ./build/lcov/lcov.info.cleaned + + - name: Generate HTML report + run: genhtml -p $PWD -o ./build/coverage/ --demangle-cpp ./build/lcov/lcov.info.cleaned + + - name: Upload coverage Artifact + uses: actions/upload-artifact@v3 + with: + name: coverage-report + path: | + ./build/lcov/ + ./build/coverage/ + retention-days: 5 + + - uses: manarbenkraouda/lcov-reporter-action@v0.3.5.3 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + lcov-file: ./build/lcov/lcov.info.cleaned diff --git a/.vscode/settings.json b/.vscode/settings.json index 5a32a597..ae85168d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -50,6 +50,9 @@ "map": "cpp", "memory_resource": "cpp", "string_view": "cpp", + "bit": "cpp", + "set": "cpp", + "iostream": "cpp", "*.hpp": "cpp" }, "files.eol": "\n", diff --git a/platformio.ini b/platformio.ini index 16ab2419..61b31ea2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -47,7 +47,7 @@ build_unflags = ${common.build_unflags} build_src_filter = ${common.build_src_filter} lib_deps = ${common.lib_deps} -[env:native-test] +[env:native] platform = native build_unflags = ${common.build_unflags} @@ -60,8 +60,3 @@ lib_deps = ${common.lib_deps} fabiobatsilva/ArduinoFake@^0.3.1 test_ignore = test_embedded -test_testing_command = - scripts/test-cover.sh - ${platformio.build_dir} - ${this.__env__} - ${platformio.build_dir}/${this.__env__}/program diff --git a/scripts/test-cover.sh b/scripts/test-cover.sh index 474b9885..d15330f7 100755 --- a/scripts/test-cover.sh +++ b/scripts/test-cover.sh @@ -5,13 +5,9 @@ build_dir=$1 env=$2 shift 2 -# Generate initial coverage report -lcov -i -d $build_dir/$env/ -c -o lcov.info.initial - # Run tests $@ lcov -d $build_dir/$env/ -c -o lcov.info -lcov --add-tracefile lcov.info.initial -a lcov.info -o lcov.info.merged -lcov --remove lcov.info.merged '/usr/include/*' '*.pio/*' '*/tool-unity/*' '*/test/*' '*/MockArduino/*' -o lcov.info.cleaned +lcov --remove lcov.info '/usr/include/*' '*.platformio/*' '*.pio/*' '*/tool-unity/*' '*/test/*' '*/MockArduino/*' -o lcov.info.cleaned genhtml -p $PWD -o build/coverage/ --demangle-cpp lcov.info.cleaned From 05efe0924fb2bcaa9467a913f3b3988b90c7c7cc Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Mon, 17 Apr 2023 23:34:16 +0400 Subject: [PATCH 28/35] test: add tests for outputs and contains --- lib/haptics/haptic_plane.cpp | 17 ++++++--- test/test_core_utility/main.cpp | 42 +++++++++++++++++++++ test/test_haptic_body/main.cpp | 63 ++++++++++++++++++++++++++++++++ test/test_haptics_plane/main.cpp | 54 +++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 test/test_haptic_body/main.cpp diff --git a/lib/haptics/haptic_plane.cpp b/lib/haptics/haptic_plane.cpp index 712cc452..c399872b 100644 --- a/lib/haptics/haptic_plane.cpp +++ b/lib/haptics/haptic_plane.cpp @@ -32,7 +32,10 @@ void OH::HapticPlane::writeOutput(const oh_output_data_t& data) { return; } - this->writers[data.point]->writeOutput(data.intensity); + auto state = &this->states[data.point]; + state->intensity = data.intensity; + + this->writers.at(data.point)->writeOutput(state->intensity); } oh_output_point_t OH::HapticPlane_Closest::findClosestPoints(std::list& pts, const oh_output_point_t& target) { @@ -52,11 +55,13 @@ oh_output_point_t OH::HapticPlane_Closest::findClosestPoints(std::list& a, - const std::pair& b) { - return a.first < b.first; - }); + std::min_element( + mp.begin(), + mp.end(), + [](const std::pair& a, const std::pair& b) { + return a.first < b.first; + } + ); return nearest->second; } diff --git a/test/test_core_utility/main.cpp b/test/test_core_utility/main.cpp index 90359741..4b4335fb 100644 --- a/test/test_core_utility/main.cpp +++ b/test/test_core_utility/main.cpp @@ -3,6 +3,45 @@ using namespace OH; +void test_contains_container(void) { + std::vector v = {1, 2, 3, 4, 5}; + TEST_ASSERT_TRUE(contains(v, 1)); + TEST_ASSERT_TRUE(contains(v, 2)); + TEST_ASSERT_TRUE(contains(v, 3)); + TEST_ASSERT_TRUE(contains(v, 4)); + TEST_ASSERT_TRUE(contains(v, 5)); + TEST_ASSERT_FALSE(contains(v, 6)); +} + +void test_contains_iterator(void) { + int arr[] = {1, 2, 3, 4, 5}; + TEST_ASSERT_TRUE(contains(arr, arr + 5, 1)); + TEST_ASSERT_TRUE(contains(arr, arr + 5, 2)); + TEST_ASSERT_TRUE(contains(arr, arr + 5, 3)); + TEST_ASSERT_TRUE(contains(arr, arr + 5, 4)); + TEST_ASSERT_TRUE(contains(arr, arr + 5, 5)); + TEST_ASSERT_FALSE(contains(arr, arr + 5, 6)); +} + +void test_contains_string(void) { + std::string s = "Hello, World!"; + TEST_ASSERT_TRUE(contains(s, 'H')); + TEST_ASSERT_TRUE(contains(s, 'e')); + TEST_ASSERT_TRUE(contains(s, 'l')); + TEST_ASSERT_TRUE(contains(s, 'o')); + TEST_ASSERT_TRUE(contains(s, ',')); + TEST_ASSERT_TRUE(contains(s, ' ')); + TEST_ASSERT_TRUE(contains(s, 'W')); + TEST_ASSERT_TRUE(contains(s, 'r')); + TEST_ASSERT_TRUE(contains(s, 'd')); + TEST_ASSERT_TRUE(contains(s, '!')); + TEST_ASSERT_FALSE(contains(s, 'h')); + TEST_ASSERT_FALSE(contains(s, 'w')); + TEST_ASSERT_FALSE(contains(s, 'x')); + TEST_ASSERT_FALSE(contains(s, 'y')); + TEST_ASSERT_FALSE(contains(s, 'z')); +} + void test_map(void) { TEST_ASSERT_EQUAL_UINT16(0, map(0, 0, 4095, 0, 255)); TEST_ASSERT_EQUAL_UINT16(127, map(2047, 0, 4095, 0, 255)); @@ -20,6 +59,9 @@ void test_map(void) { int process(void) { UNITY_BEGIN(); + RUN_TEST(test_contains_container); + RUN_TEST(test_contains_iterator); + RUN_TEST(test_contains_string); RUN_TEST(test_map); return UNITY_END(); diff --git a/test/test_haptic_body/main.cpp b/test/test_haptic_body/main.cpp new file mode 100644 index 00000000..cbad27b1 --- /dev/null +++ b/test/test_haptic_body/main.cpp @@ -0,0 +1,63 @@ +#include +#include + +using namespace OH; + +class TestActuator : public AbstractActuator { + public: + bool isSetup = false; + oh_output_intensity_t intensity = 0; + + TestActuator() : AbstractActuator() {} + void setup() override { + this->isSetup = true; + } + void writeOutput(oh_output_intensity_t intensity) override { + this->intensity = intensity; + } +}; + +void test_it_sets_up_planes(void) { + auto body = new HapticBody(); + + oh_output_writers_map_t outputs = { + {{0, 0}, new TestActuator()}, + {{0, 1}, new TestActuator()}, + {{1, 0}, new TestActuator()}, + {{1, 1}, new TestActuator()}, + }; + auto plane = new HapticPlane(outputs); + + body->addComponent(0, plane); + body->setup(); + + for (auto& kv : outputs) { + TEST_ASSERT_TRUE(static_cast(kv.second)->isSetup); + } +} + +int process(void) { + UNITY_BEGIN(); + + RUN_TEST(test_it_sets_up_planes); + + return UNITY_END(); +} + +#ifdef ARDUINO + +#include + +void setup(void) { + process(); +} + +void loop(void) {} + +#else + +int main(int argc, char** argv) { + return process(); +} + +#endif diff --git a/test/test_haptics_plane/main.cpp b/test/test_haptics_plane/main.cpp index 15607453..d76da48e 100644 --- a/test/test_haptics_plane/main.cpp +++ b/test/test_haptics_plane/main.cpp @@ -60,6 +60,33 @@ void test_it_writes_to_correct_output(void) { TEST_ASSERT_EQUAL_UINT8(255, actuator4->intensity); } +void test_it_updates_state(void) { + auto actuator = new TestActuator(), + actuator2 = new TestActuator(), + actuator3 = new TestActuator(), + actuator4 = new TestActuator(); + + oh_output_writers_map_t outputs = { + {{0, 0}, actuator}, + {{0, 1}, actuator2}, + {{1, 0}, actuator3}, + {{1, 1}, actuator4}, + }; + + auto plane = new HapticPlane(outputs); + plane->setup(); + + plane->writeOutput({{0, 0}, 64}); + plane->writeOutput({{0, 1}, 128}); + plane->writeOutput({{1, 0}, 192}); + plane->writeOutput({{1, 1}, 255}); + + TEST_ASSERT_EQUAL_UINT8(64, plane->getOutputStates()->at({0, 0}).intensity); + TEST_ASSERT_EQUAL_UINT8(128, plane->getOutputStates()->at({0, 1}).intensity); + TEST_ASSERT_EQUAL_UINT8(192, plane->getOutputStates()->at({1, 0}).intensity); + TEST_ASSERT_EQUAL_UINT8(255, plane->getOutputStates()->at({1, 1}).intensity); +} + void test_closest_it_writes_to_correct_if_exact(void) { auto actuator = new TestActuator(), actuator2 = new TestActuator(), @@ -112,6 +139,31 @@ void test_closest_it_correctly_finds_closest(void) { TEST_ASSERT_EQUAL(65, actuator4->intensity); } +void test_closest_it_updates_state(void) { + auto actuator = new TestActuator(), + actuator2 = new TestActuator(), + actuator3 = new TestActuator(), + actuator4 = new TestActuator(); + + oh_output_writers_map_t outputs = { + {{0, 0}, actuator}, + {{0, 64}, actuator2}, + {{64, 0}, actuator3}, + {{64, 64}, actuator4}, + }; + + auto plane = new HapticPlane_Closest(outputs); + plane->setup(); + + plane->writeOutput({{16, 16}, 16}); + plane->writeOutput({{65, 65}, 65}); + + TEST_ASSERT_EQUAL(16, plane->getOutputStates()->at({0, 0}).intensity); + TEST_ASSERT_EQUAL(0, plane->getOutputStates()->at({0, 64}).intensity); + TEST_ASSERT_EQUAL(0, plane->getOutputStates()->at({64, 0}).intensity); + TEST_ASSERT_EQUAL(65, plane->getOutputStates()->at({64, 64}).intensity); +} + void test_map_points(void) { auto point = mapPoint(0, 0, 0, 0); @@ -134,8 +186,10 @@ int process(void) { RUN_TEST(test_it_sets_up_actuators); RUN_TEST(test_it_writes_to_correct_output); + RUN_TEST(test_it_updates_state); RUN_TEST(test_closest_it_writes_to_correct_if_exact); RUN_TEST(test_closest_it_correctly_finds_closest); + RUN_TEST(test_closest_it_updates_state); RUN_TEST(test_map_points); return UNITY_END(); From 9801427ce96cc424fc723fbc58ad2765ae2df867 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Mon, 17 Apr 2023 23:41:14 +0400 Subject: [PATCH 29/35] refactor: move battery to lib --- lib/{sensor => battery}/abstract_battery.cpp | 0 lib/{sensor => battery}/abstract_battery.hpp | 10 +++++----- lib/sensor/sensor.hpp | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) rename lib/{sensor => battery}/abstract_battery.cpp (100%) rename lib/{sensor => battery}/abstract_battery.hpp (71%) diff --git a/lib/sensor/abstract_battery.cpp b/lib/battery/abstract_battery.cpp similarity index 100% rename from lib/sensor/abstract_battery.cpp rename to lib/battery/abstract_battery.cpp diff --git a/lib/sensor/abstract_battery.hpp b/lib/battery/abstract_battery.hpp similarity index 71% rename from lib/sensor/abstract_battery.hpp rename to lib/battery/abstract_battery.hpp index 0947ede7..d70973c7 100644 --- a/lib/sensor/abstract_battery.hpp +++ b/lib/battery/abstract_battery.hpp @@ -21,9 +21,9 @@ namespace OH { uint sampleRate; }; - class AbstractBattery : public RatePollingComponent { - friend class Task>; - friend class RatePollingComponent; + class AbstractBattery : public ThrottledSensor { + friend class Task>; + friend class ThrottledSensor; private: void run(void) override; @@ -34,10 +34,10 @@ namespace OH { uint8_t updateValue() override; public: - AbstractBattery(BatteryConfig config, IEventDispatcher* eventDispatcher, TaskConfig taskConfig) : RatePollingComponent(taskConfig, config.sampleRate), eventDispatcher(eventDispatcher) {}; + AbstractBattery(BatteryConfig config, IEventDispatcher* eventDispatcher, TaskConfig taskConfig) : ThrottledSensor(taskConfig, config.sampleRate), eventDispatcher(eventDispatcher) {}; void begin() override { this->setup(); - RatePollingComponent::begin(); + ThrottledSensor::begin(); }; }; diff --git a/lib/sensor/sensor.hpp b/lib/sensor/sensor.hpp index 48c11675..fe70ed29 100644 --- a/lib/sensor/sensor.hpp +++ b/lib/sensor/sensor.hpp @@ -11,9 +11,9 @@ namespace OH }; template - class RatePollingComponent : public Task>, public ISensor<_Tp> { + class ThrottledSensor : public Task>, public ISensor<_Tp> { template friend class RatePollingComponent; - friend class Task>; + friend class Task>; private: virtual void run(void) { @@ -29,7 +29,7 @@ namespace OH virtual _Tp updateValue(void) = 0; public: - RatePollingComponent(TaskConfig taskConfig, uint32_t rate) : Task>(taskConfig), rate(rate) {}; + ThrottledSensor(TaskConfig taskConfig, uint32_t rate) : Task>(taskConfig), rate(rate) {}; _Tp getValue() override { return this->value; }; }; } // namespace OH From 159eca1c07786123dee459d283f3ecd560cdfb0f Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Mon, 17 Apr 2023 23:54:14 +0400 Subject: [PATCH 30/35] ci(GitHub): optimize apt --- .github/workflows/ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a99c420..841864ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,6 +72,10 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Speedup package installation + if: matrix.coverage + uses: abbbi/github-actions-tune@v1 + - name: Setup LCOV if: matrix.coverage uses: hrishikesh-kadam/setup-lcov@v1 @@ -165,6 +169,9 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Speedup package installation + uses: abbbi/github-actions-tune@v1 + - name: Setup LCOV uses: hrishikesh-kadam/setup-lcov@v1 @@ -217,6 +224,9 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Speedup package installation + uses: abbbi/github-actions-tune@v1 + - name: Setup LCOV uses: hrishikesh-kadam/setup-lcov@v1 From 5c71f9d60efc8f470fa444b4d5fa59b54aaca2da Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 18 Apr 2023 13:49:36 +0400 Subject: [PATCH 31/35] chore: rename code entries of openhaptics to senseshift --- firmware/firmware.cpp | 4 +-- firmware/mode_configs/bhaptics/tactal.cpp | 6 ++-- firmware/mode_configs/bhaptics/tactglove.cpp | 6 ++-- firmware/mode_configs/bhaptics/tactosy2.cpp | 6 ++-- firmware/mode_configs/bhaptics/tactosyf.cpp | 6 ++-- firmware/mode_configs/bhaptics/tactosyh.cpp | 6 ++-- .../mode_configs/bhaptics/tactsuit_x16.cpp | 6 ++-- .../bhaptics/tactsuit_x16_pca9685.cpp | 6 ++-- .../mode_configs/bhaptics/tactsuit_x40.cpp | 6 ++-- firmware/mode_configs/test.cpp | 6 ++-- firmware/{openhaptics.cpp => senseshift.cpp} | 8 +++--- include/config/bluetooth.h | 28 +++++++++---------- include/{openhaptics.h => senseshift.h} | 4 +-- 13 files changed, 49 insertions(+), 49 deletions(-) rename firmware/{openhaptics.cpp => senseshift.cpp} (67%) rename include/{openhaptics.h => senseshift.h} (90%) diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index 2df62cb8..943d46b2 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -1,4 +1,4 @@ -#include "openhaptics.h" +#include "senseshift.h" #if defined(ARDUINO) #include @@ -19,7 +19,7 @@ extern void loopMode(); #if defined(ARDUINO) -OpenHaptics App; +SenseShift App; void setup() { setupMode(); diff --git a/firmware/mode_configs/bhaptics/tactal.cpp b/firmware/mode_configs/bhaptics/tactal.cpp index ac092980..455ee48b 100644 --- a/firmware/mode_configs/bhaptics/tactal.cpp +++ b/firmware/mode_configs/bhaptics/tactal.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const size_t bhLayoutSize = BH_LAYOUT_TACTAL_SIZE; static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; diff --git a/firmware/mode_configs/bhaptics/tactglove.cpp b/firmware/mode_configs/bhaptics/tactglove.cpp index 505f07b6..7a432873 100644 --- a/firmware/mode_configs/bhaptics/tactglove.cpp +++ b/firmware/mode_configs/bhaptics/tactglove.cpp @@ -6,7 +6,7 @@ #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -19,8 +19,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; #pragma region bHaptics_trash diff --git a/firmware/mode_configs/bhaptics/tactosy2.cpp b/firmware/mode_configs/bhaptics/tactosy2.cpp index ab09e472..d14fe802 100644 --- a/firmware/mode_configs/bhaptics/tactosy2.cpp +++ b/firmware/mode_configs/bhaptics/tactosy2.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const size_t bhLayoutSize = BH_LAYOUT_TACTOSY2_SIZE; static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSY2; diff --git a/firmware/mode_configs/bhaptics/tactosyf.cpp b/firmware/mode_configs/bhaptics/tactosyf.cpp index 598e2388..677c2bcb 100644 --- a/firmware/mode_configs/bhaptics/tactosyf.cpp +++ b/firmware/mode_configs/bhaptics/tactosyf.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYF_SIZE; static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; diff --git a/firmware/mode_configs/bhaptics/tactosyh.cpp b/firmware/mode_configs/bhaptics/tactosyh.cpp index 9fc03a5b..0d3d00b4 100644 --- a/firmware/mode_configs/bhaptics/tactosyh.cpp +++ b/firmware/mode_configs/bhaptics/tactosyh.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYH_SIZE; static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYH; diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp index 980455c7..fa4a5517 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const oh_output_point_t* bhLayout[] = BH_LAYOUT_TACTSUITX16; static const size_t bhLayoutSize = BH_LAYOUT_TACTSUITX16_SIZE; diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp index 295139c7..c465af32 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -17,8 +17,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const oh_output_point_t* bhLayout[] = BH_LAYOUT_TACTSUITX16; static const size_t bhLayoutSize = BH_LAYOUT_TACTSUITX16_SIZE; diff --git a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp index b5b316fd..de1754bd 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp @@ -4,7 +4,7 @@ #include #include -#include "openhaptics.h" +#include "senseshift.h" #include #include @@ -18,8 +18,8 @@ using namespace OH; using namespace BH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; static const size_t bhLayoutSize = BH_LAYOUT_TACTSUITX40_SIZE; static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTSUITX40; diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index 602b6d87..0557a986 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -7,14 +7,14 @@ #include "Arduino.h" #endif -#include "openhaptics.h" +#include "senseshift.h" #include using namespace OH; -extern OpenHaptics App; -OpenHaptics* app = &App; +extern SenseShift App; +SenseShift* app = &App; class TestOutput : public OH::AbstractActuator { private: diff --git a/firmware/openhaptics.cpp b/firmware/senseshift.cpp similarity index 67% rename from firmware/openhaptics.cpp rename to firmware/senseshift.cpp index 60b2b743..38a95f46 100644 --- a/firmware/openhaptics.cpp +++ b/firmware/senseshift.cpp @@ -1,6 +1,6 @@ #include "config/all.h" -#include "openhaptics.h" +#include "senseshift.h" #ifdef ARDUINO #include @@ -8,11 +8,11 @@ #include -OpenHaptics::OpenHaptics() { +SenseShift::SenseShift() { this->pHapticBody = new OH::HapticBody(); } -void OpenHaptics::postEvent(const OH::IEvent* event) { +void SenseShift::postEvent(const OH::IEvent* event) { log_i("Event dispatched at %u: %s (%p)", millis(), event->eventName.c_str(), event); for (auto* listener : this->eventListeners) { @@ -22,6 +22,6 @@ void OpenHaptics::postEvent(const OH::IEvent* event) { delete event; } -void OpenHaptics::addEventListener(const OH::IEventListener* listener) { +void SenseShift::addEventListener(const OH::IEventListener* listener) { this->eventListeners.push_back(listener); } diff --git a/include/config/bluetooth.h b/include/config/bluetooth.h index 85c8c8f7..737344d5 100644 --- a/include/config/bluetooth.h +++ b/include/config/bluetooth.h @@ -1,14 +1,14 @@ -#pragma once - -#ifndef BLUETOOTH_NAME -#define BLUETOOTH_NAME "OpenHaptics" -#endif - -#ifndef BLUETOOTH_ADDRESS -#define BLUETOOTH_ADDRESS \ - { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB } -#endif - -#ifndef BLUETOOTH_USE_NIMBLE -#define BLUETOOTH_USE_NIMBLE false -#endif +#pragma once + +#ifndef BLUETOOTH_NAME +#define BLUETOOTH_NAME "SenseShift" +#endif + +#ifndef BLUETOOTH_ADDRESS +#define BLUETOOTH_ADDRESS \ + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB } +#endif + +#ifndef BLUETOOTH_USE_NIMBLE +#define BLUETOOTH_USE_NIMBLE false +#endif diff --git a/include/openhaptics.h b/include/senseshift.h similarity index 90% rename from include/openhaptics.h rename to include/senseshift.h index 64c8427d..12b0bf9d 100644 --- a/include/openhaptics.h +++ b/include/senseshift.h @@ -12,7 +12,7 @@ #include -class OpenHaptics final : public OH::IEventDispatcher +class SenseShift final : public OH::IEventDispatcher { private: std::vector eventListeners{}; @@ -23,7 +23,7 @@ class OpenHaptics final : public OH::IEventDispatcher #endif public: - OpenHaptics(); + SenseShift(); OH::HapticBody* getHapticBody() { return this->pHapticBody; From 00ce82c08236ca67042279c5ca5f58c5d227f395 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 18 Apr 2023 21:22:37 +0400 Subject: [PATCH 32/35] refactor(Output): move matrix mappers to static class --- firmware/mode_configs/bhaptics/tactal.cpp | 2 +- firmware/mode_configs/bhaptics/tactglove.cpp | 4 +- firmware/mode_configs/bhaptics/tactosy2.cpp | 2 +- firmware/mode_configs/bhaptics/tactosyf.cpp | 2 +- firmware/mode_configs/bhaptics/tactosyh.cpp | 2 +- .../mode_configs/bhaptics/tactsuit_x16.cpp | 4 +- .../bhaptics/tactsuit_x16_pca9685.cpp | 4 +- .../mode_configs/bhaptics/tactsuit_x40.cpp | 4 +- firmware/mode_configs/test.cpp | 2 +- lib/bhaptics/bh_constants.hpp | 40 ++++++------- .../connection_bhble.cpp | 0 .../connection_bhble.hpp | 2 +- lib/haptics/haptic_plane.hpp | 56 ++++++++++--------- test/test_haptics_plane/main.cpp | 10 ++-- 14 files changed, 70 insertions(+), 64 deletions(-) rename lib/{bhaptics => bhaptics_ble}/connection_bhble.cpp (100%) rename lib/{bhaptics => bhaptics_ble}/connection_bhble.hpp (98%) diff --git a/firmware/mode_configs/bhaptics/tactal.cpp b/firmware/mode_configs/bhaptics/tactal.cpp index 455ee48b..7f0561e8 100644 --- a/firmware/mode_configs/bhaptics/tactal.cpp +++ b/firmware/mode_configs/bhaptics/tactal.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; void setupMode() { // Configure PWM pins to their positions on the face - auto faceOutputs = mapMatrixCoordinates({ + auto faceOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, // clang-format on diff --git a/firmware/mode_configs/bhaptics/tactglove.cpp b/firmware/mode_configs/bhaptics/tactglove.cpp index 7a432873..e365ddc5 100644 --- a/firmware/mode_configs/bhaptics/tactglove.cpp +++ b/firmware/mode_configs/bhaptics/tactglove.cpp @@ -30,7 +30,7 @@ static const uint16_t _bh_size_x = 6; static const uint16_t _bh_size_y = 1; inline oh_output_point_t* make_point(oh_output_coord_t x, oh_output_coord_t y) { - return mapPoint(x, y, (oh_output_coord_t) (_bh_size_x - 1), (oh_output_coord_t) (_bh_size_y - 1)); + return PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (_bh_size_x - 1), (oh_output_coord_t) (_bh_size_y - 1)); } static const uint16_t bhLayoutSize = _bh_size_x * _bh_size_y; @@ -49,7 +49,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = { void setupMode() { // Configure PWM pins to their positions on the glove - auto gloveOutputs = mapMatrixCoordinates({ + auto gloveOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off { // Thumb, Index, Middle, Ring, Pinky diff --git a/firmware/mode_configs/bhaptics/tactosy2.cpp b/firmware/mode_configs/bhaptics/tactosy2.cpp index d14fe802..5846b08f 100644 --- a/firmware/mode_configs/bhaptics/tactosy2.cpp +++ b/firmware/mode_configs/bhaptics/tactosy2.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSY2; void setupMode() { // Configure PWM pins to their positions on the forearm - auto forearmOutputs = mapMatrixCoordinates({ + auto forearmOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25)}, {new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, diff --git a/firmware/mode_configs/bhaptics/tactosyf.cpp b/firmware/mode_configs/bhaptics/tactosyf.cpp index 677c2bcb..94b68a8c 100644 --- a/firmware/mode_configs/bhaptics/tactosyf.cpp +++ b/firmware/mode_configs/bhaptics/tactosyf.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; void setupMode() { // Configure PWM pins to their positions on the feet - auto footOutputs = mapMatrixCoordinates({ + auto footOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off { new PWMOutputWriter(32) }, { new PWMOutputWriter(33) }, diff --git a/firmware/mode_configs/bhaptics/tactosyh.cpp b/firmware/mode_configs/bhaptics/tactosyh.cpp index 0d3d00b4..95a31b4c 100644 --- a/firmware/mode_configs/bhaptics/tactosyh.cpp +++ b/firmware/mode_configs/bhaptics/tactosyh.cpp @@ -25,7 +25,7 @@ static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYH; void setupMode() { // Configure PWM pins to their positions on the hands - auto handOutputs = mapMatrixCoordinates({ + auto handOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32)}, {new PWMOutputWriter(33)}, diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp index fa4a5517..3b60dc0b 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16.cpp @@ -29,13 +29,13 @@ static const uint8_t layoutGroups[layoutGroupsSize] = BH_LAYOUT_TACTSUITX16_GROU void setupMode() { // Configure PWM pins to their positions on the vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26)}, {new PWMOutputWriter(27), new PWMOutputWriter(14), new PWMOutputWriter(12), new PWMOutputWriter(13)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PWMOutputWriter(19), new PWMOutputWriter(18), new PWMOutputWriter(5), new PWMOutputWriter(17)}, {new PWMOutputWriter(16), new PWMOutputWriter(4), new PWMOutputWriter(2), new PWMOutputWriter(15)}, diff --git a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp index c465af32..34163680 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp @@ -34,13 +34,13 @@ void setupMode() { pwm->setPWMFreq(PWM_FREQUENCY); // Assign the pins on the configured PCA9685 to positions on the vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm, 0), new PCA9685OutputWriter(pwm, 1), new PCA9685OutputWriter(pwm, 2), new PCA9685OutputWriter(pwm, 3)}, {new PCA9685OutputWriter(pwm, 4), new PCA9685OutputWriter(pwm, 5), new PCA9685OutputWriter(pwm, 6), new PCA9685OutputWriter(pwm, 7)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm, 8), new PCA9685OutputWriter(pwm, 9), new PCA9685OutputWriter(pwm, 10), new PCA9685OutputWriter(pwm, 11)}, {new PCA9685OutputWriter(pwm, 12), new PCA9685OutputWriter(pwm, 13), new PCA9685OutputWriter(pwm, 14), new PCA9685OutputWriter(pwm, 15)}, diff --git a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp index de1754bd..b8a52446 100644 --- a/firmware/mode_configs/bhaptics/tactsuit_x40.cpp +++ b/firmware/mode_configs/bhaptics/tactsuit_x40.cpp @@ -36,7 +36,7 @@ void setupMode() { // Assign the pins on the configured PCA9685s and PWM pins to locations on the // vest - auto frontOutputs = mapMatrixCoordinates({ + auto frontOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm0, 0), new PCA9685OutputWriter(pwm0, 1), new PCA9685OutputWriter(pwm0, 2), new PCA9685OutputWriter(pwm0, 3)}, {new PCA9685OutputWriter(pwm0, 4), new PCA9685OutputWriter(pwm0, 5), new PCA9685OutputWriter(pwm0, 6), new PCA9685OutputWriter(pwm0, 7)}, @@ -45,7 +45,7 @@ void setupMode() { {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26)}, // clang-format on }); - auto backOutputs = mapMatrixCoordinates({ + auto backOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ // clang-format off {new PCA9685OutputWriter(pwm1, 0), new PCA9685OutputWriter(pwm1, 1), new PCA9685OutputWriter(pwm1, 2), new PCA9685OutputWriter(pwm1, 3)}, {new PCA9685OutputWriter(pwm1, 4), new PCA9685OutputWriter(pwm1, 5), new PCA9685OutputWriter(pwm1, 6), new PCA9685OutputWriter(pwm1, 7)}, diff --git a/firmware/mode_configs/test.cpp b/firmware/mode_configs/test.cpp index 0557a986..a34397df 100644 --- a/firmware/mode_configs/test.cpp +++ b/firmware/mode_configs/test.cpp @@ -27,7 +27,7 @@ class TestOutput : public OH::AbstractActuator { }; void setupMode() { - auto testOutputs = mapMatrixCoordinates({ + auto testOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ {new TestOutput(0), new TestOutput(1)}, {new TestOutput(2), new TestOutput(3)}, {new TestOutput(4), new TestOutput(5)}, diff --git a/lib/bhaptics/bh_constants.hpp b/lib/bhaptics/bh_constants.hpp index 3fc895c7..a93c5be2 100644 --- a/lib/bhaptics/bh_constants.hpp +++ b/lib/bhaptics/bh_constants.hpp @@ -1,6 +1,8 @@ #pragma once -#include +#if defined(ARDUINO_ARCH_ESP32) + #include +#endif #define BH_SERIAL_NUMBER_LENGTH 10 @@ -11,11 +13,11 @@ #define NO_AUDIO_CABLE 0 #define AUDIO_CABLE 1 -#ifdef BH_DEVICE_TACTSUITX16 +#pragma region BH_DEVICE_TACTSUITX16 #define BH_LAYOUT_TACTSUITX16_SIZE_X 4 #define BH_LAYOUT_TACTSUITX16_SIZE_Y 2 -#define BH_LAYOUT_TACTSUITX16_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTSUITX16_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTSUITX16_SIZE_Y - 1)) +#define BH_LAYOUT_TACTSUITX16_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTSUITX16_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTSUITX16_SIZE_Y - 1)) // X16 suit uses the same packets structure as x40 suit and performs motor grouping in firmware #define BH_LAYOUT_TACTSUITX16_SIZE 40 @@ -76,13 +78,13 @@ #define BH_LAYOUT_TACTSUITX16_GROUPS { 0, 1, 4, 5, 10, 11, 14, 15, 20, 21, 24, 25, 30, 31, 34, 35 } #define BH_LAYOUT_TACTSUITX16_GROUPS_SIZE 16 -#endif +#pragma endregion BH_DEVICE_TACTSUITX16 -#ifdef BH_DEVICE_TACTSUITX40 +#pragma region BH_DEVICE_TACTSUITX40 #define BH_LAYOUT_TACTSUITX40_SIZE_X 4 #define BH_LAYOUT_TACTSUITX40_SIZE_Y 5 -#define BH_LAYOUT_TACTSUITX40_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTSUITX40_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTSUITX40_SIZE_Y - 1)) +#define BH_LAYOUT_TACTSUITX40_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTSUITX40_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTSUITX40_SIZE_Y - 1)) // X * Y for front and back #define BH_LAYOUT_TACTSUITX40_SIZE 40 @@ -135,13 +137,13 @@ /* 39 */ BH_LAYOUT_TACTSUITX40_MAKE_POINT(3, 4) \ } -#endif +#pragma endregion BH_DEVICE_TACTSUITX40 -#ifdef BH_DEVICE_TACTAL +#pragma region BH_DEVICE_TACTAL #define BH_LAYOUT_TACTAL_SIZE_X 6 #define BH_LAYOUT_TACTAL_SIZE_Y 1 -#define BH_LAYOUT_TACTAL_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTAL_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTAL_SIZE_Y - 1)) +#define BH_LAYOUT_TACTAL_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTAL_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTAL_SIZE_Y - 1)) #define BH_LAYOUT_TACTAL_SIZE (BH_LAYOUT_TACTAL_SIZE_X * BH_LAYOUT_TACTAL_SIZE_Y) #define BH_LAYOUT_TACTAL { \ @@ -153,13 +155,13 @@ BH_LAYOUT_TACTAL_MAKE_POINT(5, 0), \ } -#endif +#pragma endregion BH_DEVICE_TACTAL -#ifdef BH_DEVICE_TACTOSY2 +#pragma region BH_DEVICE_TACTOSY2 #define BH_LAYOUT_TACTOSY2_SIZE_X 3 #define BH_LAYOUT_TACTOSY2_SIZE_Y 2 -#define BH_LAYOUT_TACTOSY2_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSY2_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSY2_SIZE_Y - 1)) +#define BH_LAYOUT_TACTOSY2_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSY2_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSY2_SIZE_Y - 1)) #define BH_LAYOUT_TACTOSY2_SIZE (BH_LAYOUT_TACTOSY2_SIZE_X * BH_LAYOUT_TACTOSY2_SIZE_Y) #define BH_LAYOUT_TACTOSY2 { \ @@ -167,29 +169,29 @@ BH_LAYOUT_TACTOSY2_MAKE_POINT(0, 1), BH_LAYOUT_TACTOSY2_MAKE_POINT(1, 1), BH_LAYOUT_TACTOSY2_MAKE_POINT(2, 1), \ } -#endif +#pragma endregion BH_DEVICE_TACTOSY2 -#ifdef BH_DEVICE_TACTOSYH +#pragma region BH_DEVICE_TACTOSYH #define BH_LAYOUT_TACTOSYH_SIZE_X 1 #define BH_LAYOUT_TACTOSYH_SIZE_Y 3 -#define BH_LAYOUT_TACTOSYH_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSYH_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSYH_SIZE_Y - 1)) +#define BH_LAYOUT_TACTOSYH_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSYH_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSYH_SIZE_Y - 1)) #define BH_LAYOUT_TACTOSYH_SIZE (BH_LAYOUT_TACTOSYH_SIZE_X * BH_LAYOUT_TACTOSYH_SIZE_Y) #define BH_LAYOUT_TACTOSYH { BH_LAYOUT_TACTOSYH_MAKE_POINT(0, 0), BH_LAYOUT_TACTOSYH_MAKE_POINT(0, 1), BH_LAYOUT_TACTOSYH_MAKE_POINT(0, 2) } -#endif +#pragma endregion BH_DEVICE_TACTOSYH -#ifdef BH_DEVICE_TACTOSYF +#pragma region BH_DEVICE_TACTOSYF #define BH_LAYOUT_TACTOSYF_SIZE_X 1 #define BH_LAYOUT_TACTOSYF_SIZE_Y 3 -#define BH_LAYOUT_TACTOSYF_MAKE_POINT(x, y) OH::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSYF_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSYF_SIZE_Y - 1)) +#define BH_LAYOUT_TACTOSYF_MAKE_POINT(x, y) OH::PlaneMapper_Margin::mapPoint(x, y, (oh_output_coord_t) (BH_LAYOUT_TACTOSYF_SIZE_X - 1), (oh_output_coord_t) (BH_LAYOUT_TACTOSYF_SIZE_Y - 1)) #define BH_LAYOUT_TACTOSYF_SIZE (BH_LAYOUT_TACTOSYF_SIZE_X * BH_LAYOUT_TACTOSYF_SIZE_Y) #define BH_LAYOUT_TACTOSYF { BH_LAYOUT_TACTOSYF_MAKE_POINT(0, 0), BH_LAYOUT_TACTOSYF_MAKE_POINT(0, 1), BH_LAYOUT_TACTOSYF_MAKE_POINT(0, 2), } -#endif +#pragma endregion BH_DEVICE_TACTOSYF // All below are weird choices of bHaptics engineers... // Why to use unconventional UUIDs, that are reserved for other purposes? diff --git a/lib/bhaptics/connection_bhble.cpp b/lib/bhaptics_ble/connection_bhble.cpp similarity index 100% rename from lib/bhaptics/connection_bhble.cpp rename to lib/bhaptics_ble/connection_bhble.cpp diff --git a/lib/bhaptics/connection_bhble.hpp b/lib/bhaptics_ble/connection_bhble.hpp similarity index 98% rename from lib/bhaptics/connection_bhble.hpp rename to lib/bhaptics_ble/connection_bhble.hpp index f5c92290..7e9978e0 100644 --- a/lib/bhaptics/connection_bhble.hpp +++ b/lib/bhaptics_ble/connection_bhble.hpp @@ -15,7 +15,7 @@ #endif #if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true -#include "abstract_battery.hpp" + #include #endif // typedef void (*bh_motor_handler_t)(std::string&); diff --git a/lib/haptics/haptic_plane.hpp b/lib/haptics/haptic_plane.hpp index 4abcfb60..8fffb099 100644 --- a/lib/haptics/haptic_plane.hpp +++ b/lib/haptics/haptic_plane.hpp @@ -44,38 +44,42 @@ namespace OH { void writeOutput(const oh_output_data_t&) override; }; - /** - * Re-maps a point index to output coordinate. - * @tparam _Tp The type of the point index. - */ - template - inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { - const oh_output_coord_t x_coord = map<_Tp>(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); - const oh_output_coord_t y_coord = map<_Tp>(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); + class PlaneMapper_Margin { + public: + + /** + * Re-maps a point index to output coordinate. + * @tparam _Tp The type of the point index. + */ + template + static inline oh_output_point_t* mapPoint(_Tp x, _Tp y, _Tp x_max, _Tp y_max) { + const oh_output_coord_t x_coord = map<_Tp>(x + 1, 0, x_max + 2, 0, OH_OUTPUT_COORD_MAX); + const oh_output_coord_t y_coord = map<_Tp>(y + 1, 0, y_max + 2, 0, OH_OUTPUT_COORD_MAX); - return new oh_output_point_t(x_coord, y_coord); - } + return new oh_output_point_t(x_coord, y_coord); + } - template - inline std::map mapMatrixCoordinates(std::vector> map2d) { - std::map points{}; + template + static inline std::map mapMatrixCoordinates(std::vector> map2d) { + std::map points{}; - size_t y_size = map2d.size(); - size_t y_max = y_size - 1; + size_t y_size = map2d.size(); + size_t y_max = y_size - 1; - for (size_t y = 0; y < y_size; ++y) { - auto row = map2d.at(y); - size_t x_size = row.size(); - size_t x_max = x_size - 1; + for (size_t y = 0; y < y_size; ++y) { + auto row = map2d.at(y); + size_t x_size = row.size(); + size_t x_max = x_size - 1; - for (size_t x = 0; x < x_size; ++x) { - AbstractActuator* wr = row.at(x); - oh_output_point_t* coord = mapPoint(x, y, x_max, y_max); + for (size_t x = 0; x < x_size; ++x) { + AbstractActuator* wr = row.at(x); + oh_output_point_t* coord = mapPoint(x, y, x_max, y_max); - points[*coord] = wr; - } - } + points[*coord] = wr; + } + } - return points; + return points; + } }; } // namespace OH diff --git a/test/test_haptics_plane/main.cpp b/test/test_haptics_plane/main.cpp index d76da48e..c8080e19 100644 --- a/test/test_haptics_plane/main.cpp +++ b/test/test_haptics_plane/main.cpp @@ -164,18 +164,18 @@ void test_closest_it_updates_state(void) { TEST_ASSERT_EQUAL(65, plane->getOutputStates()->at({64, 64}).intensity); } -void test_map_points(void) { - auto point = mapPoint(0, 0, 0, 0); +void test_plain_mapper_margin_map_points(void) { + auto point = PlaneMapper_Margin::mapPoint(0, 0, 0, 0); TEST_ASSERT_EQUAL(127, point->x); TEST_ASSERT_EQUAL(127, point->y); - point = mapPoint(0, 0, 1, 1); + point = PlaneMapper_Margin::mapPoint(0, 0, 1, 1); TEST_ASSERT_EQUAL(85, point->x); TEST_ASSERT_EQUAL(85, point->y); - point = mapPoint(1, 1, 1, 1); + point = PlaneMapper_Margin::mapPoint(1, 1, 1, 1); TEST_ASSERT_EQUAL(170, point->x); TEST_ASSERT_EQUAL(170, point->y); @@ -190,7 +190,7 @@ int process(void) { RUN_TEST(test_closest_it_writes_to_correct_if_exact); RUN_TEST(test_closest_it_correctly_finds_closest); RUN_TEST(test_closest_it_updates_state); - RUN_TEST(test_map_points); + RUN_TEST(test_plain_mapper_margin_map_points); return UNITY_END(); } From 7988bdfce14a28f64aa40fed4d86295f873bce1f Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 18 Apr 2023 22:31:56 +0400 Subject: [PATCH 33/35] test(bHaptics): add tests for device layouts --- test/test_bhaptics/main.cpp | 357 ++++++++++++++++++++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100644 test/test_bhaptics/main.cpp diff --git a/test/test_bhaptics/main.cpp b/test/test_bhaptics/main.cpp new file mode 100644 index 00000000..a37628db --- /dev/null +++ b/test/test_bhaptics/main.cpp @@ -0,0 +1,357 @@ +#include +#include +#include +#include + +using namespace OH; +using namespace BH; + +class TestActuator : public AbstractActuator { + public: + bool isSetup = false; + oh_output_intensity_t intensity = 0; + + TestActuator() : AbstractActuator() {} + void setup() override { + this->isSetup = true; + } + void writeOutput(oh_output_intensity_t intensity) override { + this->intensity = intensity; + } +}; + +void test_layout_tactsuitx16(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTSUITX16_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTSUITX16; + + static const size_t layoutGroupsSize = BH_LAYOUT_TACTSUITX16_GROUPS_SIZE; + static const uint8_t layoutGroups[layoutGroupsSize] = BH_LAYOUT_TACTSUITX16_GROUPS; + + auto body = new HapticBody(); + + TestActuator* actuator0 = new TestActuator(); + TestActuator* actuator1 = new TestActuator(); + TestActuator* actuator2 = new TestActuator(); + TestActuator* actuator3 = new TestActuator(); + TestActuator* actuator4 = new TestActuator(); + TestActuator* actuator5 = new TestActuator(); + TestActuator* actuator6 = new TestActuator(); + TestActuator* actuator7 = new TestActuator(); + TestActuator* actuator8 = new TestActuator(); + TestActuator* actuator9 = new TestActuator(); + TestActuator* actuator10 = new TestActuator(); + TestActuator* actuator11 = new TestActuator(); + TestActuator* actuator12 = new TestActuator(); + TestActuator* actuator13 = new TestActuator(); + TestActuator* actuator14 = new TestActuator(); + TestActuator* actuator15 = new TestActuator(); + + auto frontOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ + {actuator0, actuator1, actuator2, actuator3}, + {actuator4, actuator5, actuator6, actuator7}, + }); + auto backOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ + {actuator8, actuator9, actuator10, actuator11}, + {actuator12, actuator13, actuator14, actuator15}, + }); + + auto frontPlane = new HapticPlane(frontOutputs); + auto backPlane = new HapticPlane(backOutputs); + + body->addComponent(OUTPUT_PATH_CHEST_FRONT, frontPlane); + body->addComponent(OUTPUT_PATH_CHEST_BACK, backPlane); + body->setup(); + + uint8_t values[] = { + 0x01, 0x00, 0x23, 0x00, 0x00, 0x45, 0x00, 0x67, 0x00, 0x00, + 0x89, 0x00, 0xab, 0x00, 0x00, 0xcd, 0x00, 0xef, 0x00, 0x00, + }; + std::string value = std::string((char*)values, sizeof(values)); + + vestX16OutputTransformer(body, value, bhLayout, bhLayoutSize, layoutGroups, layoutGroupsSize); + TEST_ASSERT_EQUAL_INT(0, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(17, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(204, actuator2->intensity); + TEST_ASSERT_EQUAL_INT(221, actuator3->intensity); + TEST_ASSERT_EQUAL_INT(34, actuator4->intensity); + TEST_ASSERT_EQUAL_INT(51, actuator5->intensity); + TEST_ASSERT_EQUAL_INT(238, actuator6->intensity); + TEST_ASSERT_EQUAL_INT(255, actuator7->intensity); + + TEST_ASSERT_EQUAL_INT(68, actuator8->intensity); + TEST_ASSERT_EQUAL_INT(85, actuator9->intensity); + TEST_ASSERT_EQUAL_INT(136, actuator10->intensity); + TEST_ASSERT_EQUAL_INT(153, actuator11->intensity); + TEST_ASSERT_EQUAL_INT(102, actuator12->intensity); + TEST_ASSERT_EQUAL_INT(119, actuator13->intensity); + TEST_ASSERT_EQUAL_INT(170, actuator14->intensity); + TEST_ASSERT_EQUAL_INT(187, actuator15->intensity); +} + +void test_layout_tactsuitx40(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTSUITX40_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTSUITX40; + + auto body = new HapticBody(); + + std::vector> frontMatrix = { + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + }; + std::vector> backMatrix = { + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + {new TestActuator(), new TestActuator(), new TestActuator(), new TestActuator()}, + }; + + auto frontOutputs = PlaneMapper_Margin::mapMatrixCoordinates(frontMatrix); + auto backOutputs = PlaneMapper_Margin::mapMatrixCoordinates(backMatrix); + + auto frontPlane = new HapticPlane(frontOutputs); + auto backPlane = new HapticPlane(backOutputs); + + body->addComponent(OUTPUT_PATH_CHEST_FRONT, frontPlane); + body->addComponent(OUTPUT_PATH_CHEST_BACK, backPlane); + body->setup(); + + uint8_t values[] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + std::string value = std::string((char*)values, sizeof(values)); + + vestOutputTransformer(body, value, bhLayout, bhLayoutSize); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[0][0])->intensity); + TEST_ASSERT_EQUAL_INT(17, static_cast(frontMatrix[0][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[0][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[0][3])->intensity); + TEST_ASSERT_EQUAL_INT(34, static_cast(frontMatrix[1][0])->intensity); + TEST_ASSERT_EQUAL_INT(51, static_cast(frontMatrix[1][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[1][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[1][3])->intensity); + TEST_ASSERT_EQUAL_INT(68, static_cast(frontMatrix[2][0])->intensity); + TEST_ASSERT_EQUAL_INT(85, static_cast(frontMatrix[2][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[2][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[2][3])->intensity); + TEST_ASSERT_EQUAL_INT(102, static_cast(frontMatrix[3][0])->intensity); + TEST_ASSERT_EQUAL_INT(119, static_cast(frontMatrix[3][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[3][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[3][3])->intensity); + TEST_ASSERT_EQUAL_INT(136, static_cast(frontMatrix[4][0])->intensity); + TEST_ASSERT_EQUAL_INT(153, static_cast(frontMatrix[4][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[4][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(frontMatrix[4][3])->intensity); + + TEST_ASSERT_EQUAL_INT(170, static_cast(backMatrix[0][0])->intensity); + TEST_ASSERT_EQUAL_INT(187, static_cast(backMatrix[0][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[0][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[0][3])->intensity); + TEST_ASSERT_EQUAL_INT(204, static_cast(backMatrix[1][0])->intensity); + TEST_ASSERT_EQUAL_INT(221, static_cast(backMatrix[1][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[1][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[1][3])->intensity); + TEST_ASSERT_EQUAL_INT(238, static_cast(backMatrix[2][0])->intensity); + TEST_ASSERT_EQUAL_INT(255, static_cast(backMatrix[2][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[2][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[2][3])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[3][0])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[3][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[3][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[3][3])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[4][0])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[4][1])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[4][2])->intensity); + TEST_ASSERT_EQUAL_INT(0, static_cast(backMatrix[4][3])->intensity); +} + +void test_layout_tactal(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTAL_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; + + auto body = new HapticBody(); + + TestActuator* actuator0 = new TestActuator(); + TestActuator* actuator1 = new TestActuator(); + TestActuator* actuator2 = new TestActuator(); + TestActuator* actuator3 = new TestActuator(); + TestActuator* actuator4 = new TestActuator(); + TestActuator* actuator5 = new TestActuator(); + + auto outputs = PlaneMapper_Margin::mapMatrixCoordinates({ + { actuator0, actuator1, actuator2, actuator3, actuator4, actuator5 }, + }); + auto plane = new HapticPlane(outputs); + + body->addComponent(OUTPUT_PATH_ACCESSORY, plane); + body->setup(); + + uint8_t values[] = {0x64, 0x00, 0x00, 0x00, 0x00, 0x00}; + std::string value = std::string((char*)values, sizeof(values)); + + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(255, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator2->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator3->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator4->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator5->intensity); + + value = "\x10\x20\x30\x40\x50\x60"; + + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(40, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(81, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(122, actuator2->intensity); + TEST_ASSERT_EQUAL_INT(163, actuator3->intensity); + TEST_ASSERT_EQUAL_INT(204, actuator4->intensity); + TEST_ASSERT_EQUAL_INT(244, actuator5->intensity); +} + +void test_layout_tactosy2(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTOSY2_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSY2; + + auto body = new HapticBody(); + + TestActuator* actuator0 = new TestActuator(); + TestActuator* actuator1 = new TestActuator(); + TestActuator* actuator2 = new TestActuator(); + TestActuator* actuator3 = new TestActuator(); + TestActuator* actuator4 = new TestActuator(); + TestActuator* actuator5 = new TestActuator(); + + auto outputs = PlaneMapper_Margin::mapMatrixCoordinates({ + { actuator0, actuator1, actuator2}, + { actuator3, actuator4, actuator5 }, + }); + auto plane = new HapticPlane(outputs); + + body->addComponent(OUTPUT_PATH_ACCESSORY, plane); + body->setup(); + + uint8_t values[] = {0x64, 0x00, 0x00, 0x00, 0x00, 0x00}; + std::string value = std::string((char*)values, sizeof(values)); + + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(255, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator2->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator3->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator4->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator5->intensity); + + value = "\x10\x20\x30\x40\x50\x60"; + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(40, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(81, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(122, actuator2->intensity); + TEST_ASSERT_EQUAL_INT(163, actuator3->intensity); + TEST_ASSERT_EQUAL_INT(204, actuator4->intensity); + TEST_ASSERT_EQUAL_INT(244, actuator5->intensity); +} + +void test_layout_tactosyh(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYH_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYH; + + auto body = new HapticBody(); + + TestActuator* actuator0 = new TestActuator(); + TestActuator* actuator1 = new TestActuator(); + TestActuator* actuator2 = new TestActuator(); + + auto outputs = PlaneMapper_Margin::mapMatrixCoordinates({ + { actuator0 }, + { actuator1 }, + { actuator2 }, + }); + auto plane = new HapticPlane(outputs); + + body->addComponent(OUTPUT_PATH_ACCESSORY, plane); + body->setup(); + + uint8_t values[] = {0x64, 0x00, 0x00}; + std::string value = std::string((char*)values, sizeof(values)); + + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(255, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator2->intensity); + + value = "\x10\x20\x30"; + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(40, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(81, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(122, actuator2->intensity); +} + +void test_layout_tactosyf(void) { + static const size_t bhLayoutSize = BH_LAYOUT_TACTOSYF_SIZE; + static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTOSYF; + + auto body = new HapticBody(); + + TestActuator* actuator0 = new TestActuator(); + TestActuator* actuator1 = new TestActuator(); + TestActuator* actuator2 = new TestActuator(); + + auto outputs = PlaneMapper_Margin::mapMatrixCoordinates({ + { actuator0 }, + { actuator1 }, + { actuator2 }, + }); + auto plane = new HapticPlane(outputs); + + body->addComponent(OUTPUT_PATH_ACCESSORY, plane); + body->setup(); + + uint8_t values[] = {0x64, 0x00, 0x00}; + std::string value = std::string((char*)values, sizeof(values)); + + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(255, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(0, actuator2->intensity); + + value = "\x10\x20\x30"; + plainOutputTransformer(body, value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + TEST_ASSERT_EQUAL_INT(40, actuator0->intensity); + TEST_ASSERT_EQUAL_INT(81, actuator1->intensity); + TEST_ASSERT_EQUAL_INT(122, actuator2->intensity); +} + +int process(void) { + UNITY_BEGIN(); + + RUN_TEST(test_layout_tactsuitx16); + RUN_TEST(test_layout_tactsuitx40); + RUN_TEST(test_layout_tactal); + RUN_TEST(test_layout_tactosy2); + RUN_TEST(test_layout_tactosyh); + RUN_TEST(test_layout_tactosyf); + + return UNITY_END(); +} + +#ifdef ARDUINO + +#include + +void setup(void) { + process(); +} + +void loop(void) {} + +#else + +int main(int argc, char** argv) { + return process(); +} + +#endif From 9c326eba790216190835693543e1933f2496b34b Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 18 Apr 2023 23:09:29 +0400 Subject: [PATCH 34/35] feat: add Bluetooth Serial example --- .github/workflows/ci.yml | 14 +----- examples/bhaptics-ble-bt-serial.cpp | 70 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 examples/bhaptics-ble-bt-serial.cpp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 841864ac..0298c543 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,24 +50,12 @@ jobs: # battery_flag: BATTERY_ENABLED=true # serial_plotter_flag: SERIAL_PLOTTER=true # nimble_flag: BLUETOOTH_USE_NIMBLE=false - - target: bhaptics_tactsuit_x40 - os: windows-latest + os: ubuntu-latest coverage: false battery_flag: BATTERY_ENABLED=true serial_plotter_flag: SERIAL_PLOTTER=false nimble_flag: BLUETOOTH_USE_NIMBLE=true - - target: bhaptics_tactsuit_x40 - os: windows-latest - coverage: false - battery_flag: BATTERY_ENABLED=true - serial_plotter_flag: SERIAL_PLOTTER=false - nimble_flag: BLUETOOTH_USE_NIMBLE=false - - target: bhaptics_tactsuit_x40 - os: macos-latest - battery_flag: BATTERY_ENABLED=true - serial_plotter_flag: SERIAL_PLOTTER=false - nimble_flag: BLUETOOTH_USE_NIMBLE=false steps: - uses: actions/checkout@v3 diff --git a/examples/bhaptics-ble-bt-serial.cpp b/examples/bhaptics-ble-bt-serial.cpp new file mode 100644 index 00000000..82fe2729 --- /dev/null +++ b/examples/bhaptics-ble-bt-serial.cpp @@ -0,0 +1,70 @@ +// Override you configs in this file (Ctrl+Click) +#include "config/all.h" + +#include +#include + +#include "senseshift.h" + +#include +#include +#include + +#include +#include + +using namespace OH; +using namespace BH; + +extern SenseShift App; +SenseShift* app = &App; + +BluetoothSerial SerialBT; +BluetoothSerial* btSerial = &SerialBT; + +static const size_t bhLayoutSize = BH_LAYOUT_TACTAL_SIZE; +static const oh_output_point_t* bhLayout[bhLayoutSize] = BH_LAYOUT_TACTAL; + +class BLECallbacks : public BHBLEConnectionCallbacks { + public: + void postInit() { + btSerial->begin("SenseShift Serial"); + } +}; + +void setupMode() { + // Configure PWM pins to their positions on the face + auto faceOutputs = PlaneMapper_Margin::mapMatrixCoordinates({ + // clang-format off + {new PWMOutputWriter(32), new PWMOutputWriter(33), new PWMOutputWriter(25), new PWMOutputWriter(26), new PWMOutputWriter(27), new PWMOutputWriter(14)}, + // clang-format on + }); + + auto* face = new HapticPlane_Closest(faceOutputs); + app->getHapticBody()->addComponent(OUTPUT_PATH_ACCESSORY, face); + + app->getHapticBody()->setup(); + + uint8_t serialNumber[BH_SERIAL_NUMBER_LENGTH] = BH_SERIAL_NUMBER; + ConnectionBHBLE_Config config = { + .deviceName = BLUETOOTH_NAME, + .appearance = BH_BLE_APPEARANCE, + .serialNumber = serialNumber, + }; + auto* bhBleConnection = new ConnectionBHBLE(config, [](std::string& value)->void { + plainOutputTransformer(app->getHapticBody(), value, bhLayout, bhLayoutSize, OUTPUT_PATH_ACCESSORY); + }, app); + bhBleConnection->setCallbacks(new BLECallbacks()); + bhBleConnection->begin(); +} + +void loopMode() { + // This way is suboptimal, but hardware interrupts for Serial are not supported by the Arduino framework + if (btSerial->available()) { + Serial.print(btSerial->read()); + } + if (Serial.available()) { + btSerial->write(Serial.read()); + } + sleep(20); +} From 421230ef93bdde428aafd81d3864792d81de136d Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Tue, 18 Apr 2023 23:12:23 +0400 Subject: [PATCH 35/35] chore: remove `devices.json` as preparation for multi-protocol --- devices.json | 203 --------------------------------------------------- 1 file changed, 203 deletions(-) delete mode 100644 devices.json diff --git a/devices.json b/devices.json deleted file mode 100644 index ff7016ae..00000000 --- a/devices.json +++ /dev/null @@ -1,203 +0,0 @@ -[ - { - "Name": "Tactosy", - "NameContains": "tactosy_", - "FirmwareService": "tactosy-v2", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "ForearmL", - "ForearmL" - ], - "WriteSize": 20 - }, - { - "Name": "Tactosy2", - "NameContains": "tactosy2_", - "FirmwareService": "v2-tactosy2", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "ForearmL", - "ForearmR" - ], - "WriteSize": 20 - }, - { - "Name": "Tactot", - "NameContains": "tactot_", - "FirmwareService": "tactot", - "RawValue": 508, - "HasBattery": false, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "Tactot2", - "NameContains": "tactot2_", - "FirmwareService": "tactot2", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "Tactot3", - "NameContains": "tactot3_", - "FirmwareService": "v2-tactot3", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "Tactot3", - "NameContains": "tactot_", - "FirmwareService": "tactot3", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "TactSuitX40", - "NameContains": "tactsuit", - "FirmwareService": "tactsuit_x40", - "RawValue": 509, - "HasBattery": true, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "TactSuitX16", - "NameContains": "tactsuit", - "FirmwareService": "tactsuit_x16", - "RawValue": 510, - "HasBattery": true, - "CandidatePositions": [ - "Vest" - ], - "WriteSize": 20 - }, - { - "Name": "Tactal", - "NameContains": "tactal_", - "FirmwareService": "v2-tactal", - "HasBattery": true, - "CandidatePositions": [ - "Head" - ], - "WriteSize": 20 - }, - { - "Name": "TactGloveL", - "NameContains": "tactglove (l", - "FirmwareService": "tactglove-v1", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "GloveL" - ], - "WriteSize": 20 - }, - { - "Name": "TactGloveR", - "NameContains": "tactglove (r", - "FirmwareService": "tactglove-v1", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "GloveR" - ], - "WriteSize": 20 - }, - { - "Name": "TactFacial", - "NameContains": "tactfacial_", - "FirmwareService": "tactfacial", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "Head" - ], - "WriteSize": 20 - }, - { - "Name": "TactosyH", - "NameContains": "tactosyh_", - "FirmwareService": "v2-tactosyh", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "HandL", - "HandR" - ], - "WriteSize": 20 - }, - { - "Name": "TactosyF", - "NameContains": "tactosyf_", - "FirmwareService": "v2-tactosyf", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "FootL", - "FootR" - ], - "WriteSize": 20 - }, - { - "Name": "TactThermoHL", - "NameContains": "tactthermohl", - "FirmwareService": "", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "HandL" - ], - "WriteSize": 20 - }, - { - "Name": "TactThermoHR", - "NameContains": "tactthermohr", - "FirmwareService": "", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "HandR" - ], - "WriteSize": 20 - }, - { - "Name": "TactThermoFL", - "NameContains": "tactthermofl", - "FirmwareService": "", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "FootL" - ], - "WriteSize": 20 - }, - { - "Name": "TactThermoFR", - "NameContains": "tactthermofr", - "FirmwareService": "", - "RawValue": 508, - "HasBattery": true, - "CandidatePositions": [ - "FootR" - ], - "WriteSize": 20 - } -] \ No newline at end of file