From 0d8db945ef9de3b9254edc09da2e0b9cc117fa74 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Thu, 5 Oct 2023 21:01:47 +0200 Subject: [PATCH] Bentuino rework --- .../Bentuino/.vscode/c_cpp_properties.json | 96 +++++++----- ...screatorclub.json => flowtoyscreator.json} | 8 +- .../Bentuino/boards/flowtoyscreatortab.json | 2 +- Firmware/Bentuino/platformio.ini | 32 +++- Firmware/Bentuino/src/Common/BoardDefines.h | 36 +++-- Firmware/Bentuino/src/Common/Helpers.h | 37 +++-- Firmware/Bentuino/src/Common/Parameter.cpp | 8 +- Firmware/Bentuino/src/Common/Parameter.h | 8 +- Firmware/Bentuino/src/Common/Settings.cpp | 10 +- Firmware/Bentuino/src/Common/Settings.h | 1 + .../Bentuino/src/Common/script/Script.cpp | 5 +- Firmware/Bentuino/src/Common/script/Script.h | 2 +- Firmware/Bentuino/src/Component/Component.cpp | 29 +++- Firmware/Bentuino/src/Component/Component.h | 8 +- .../components/battery/BatteryComponent.cpp | 2 +- .../components/battery/BatteryComponent.h | 2 +- .../communication/CommunicationComponent.cpp | 20 +-- .../communication/CommunicationComponent.h | 4 +- .../communication/osc/OSCComponent.cpp | 33 +++-- .../communication/osc/OSCComponent.h | 46 +++--- .../communication/serial/SerialComponent.cpp | 4 +- .../communication/serial/SerialComponent.h | 2 +- .../components/files/FilesComponent.cpp | 50 +++---- .../components/files/FilesComponent.h | 14 +- .../Component/components/imu/IMUComponent.cpp | 33 +++-- .../Component/components/imu/IMUComponent.h | 12 +- .../Component/components/io/IOComponent.cpp | 57 ++++--- .../src/Component/components/io/IOComponent.h | 21 +-- .../components/io/button/ButtonComponent.cpp | 8 +- .../components/io/button/ButtonComponent.h | 4 +- .../ledstrip/Layer/LedStripLayer.cpp | 18 +-- .../components/ledstrip/Layer/LedStripLayer.h | 6 +- .../Layer/layers/bake/LedStripBakeLayer.cpp | 12 +- .../Layer/layers/bake/LedStripBakeLayer.h | 12 +- .../layers/script/LedStripScriptLayer.cpp | 22 +-- .../Layer/layers/script/LedStripScriptLayer.h | 4 +- .../layers/stream/LedStripStreamLayer.cpp | 46 +++--- .../Layer/layers/stream/LedStripStreamLayer.h | 8 +- .../layers/system/LedStripSystemLayer.cpp | 7 +- .../Layer/layers/system/LedStripSystemLayer.h | 2 +- .../components/ledstrip/LedStripComponent.cpp | 140 +++++++++--------- .../components/ledstrip/LedStripComponent.h | 130 ++++++++++------ .../components/server/WebServerComponent.cpp | 4 +- .../components/servo/ServoComponent.cpp | 18 +-- .../components/servo/ServoComponent.h | 6 +- .../components/stepper/StepperComponent.cpp | 31 ++-- .../components/stepper/StepperComponent.h | 16 +- .../components/wifi/WifiComponent.cpp | 9 +- .../Component/components/wifi/WifiComponent.h | 71 +++++---- Firmware/Bentuino/src/RootComponent.cpp | 96 +++++++----- Firmware/Bentuino/src/RootComponent.h | 48 +++--- Firmware/Bentuino/src/main.cpp | 19 ++- 52 files changed, 736 insertions(+), 583 deletions(-) rename Firmware/Bentuino/boards/{flowtoyscreatorclub.json => flowtoyscreator.json} (75%) diff --git a/Firmware/Bentuino/.vscode/c_cpp_properties.json b/Firmware/Bentuino/.vscode/c_cpp_properties.json index 1a5f1c06..9c86cdac 100644 --- a/Firmware/Bentuino/.vscode/c_cpp_properties.json +++ b/Firmware/Bentuino/.vscode/c_cpp_properties.json @@ -8,31 +8,31 @@ { "name": "PlatformIO", "includePath": [ - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/include", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/lib/SD/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/FastAccelStepper/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArtnetWifi/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ESP32Servo/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/include", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/lib/SD/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/FastAccelStepper/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArtnetWifi/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ESP32Servo/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/WebServer/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/SimplexNoise", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Wasm3/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/arduino-timer/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit DotStar", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/SimplexNoise", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Wasm3/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/arduino-timer/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit DotStar", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/OSC", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/OSC", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArduinoJson/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArduinoJson/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/Preferences/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055/utility", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit Unified Sensor", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BusIO", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055/utility", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit Unified Sensor", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BusIO", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit NeoPixel", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit NeoPixel", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/config", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/app_trace", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/app_update", @@ -121,31 +121,31 @@ "browse": { "limitSymbolsToIncludedHeaders": true, "path": [ - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/include", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/lib/SD/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/FastAccelStepper/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArtnetWifi/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ESP32Servo/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/include", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/lib/SD/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/FastAccelStepper/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArtnetWifi/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ESP32Servo/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/WebServer/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/SimplexNoise", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Wasm3/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/arduino-timer/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit DotStar", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/SimplexNoise", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Wasm3/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/arduino-timer/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit DotStar", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/OSC", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/OSC", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArduinoJson/src", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/ArduinoJson/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/Preferences/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055/utility", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit Unified Sensor", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BusIO", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BNO055/utility", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit Unified Sensor", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit BusIO", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src", - "d:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit NeoPixel", + "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/bentuino/Adafruit NeoPixel", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/config", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/app_trace", "C:/Users/bkupe/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/app_update", @@ -233,11 +233,25 @@ ] }, "defines": [ - "PLATFORMIO=50205", - "ARDUINO_FLOWTOYS_CREATOR_TAB", + "PLATFORMIO=60111", + "ARDUINO_ESP32_DEV", + "ARDUINO_BENTUINO", "POWER_KEEP_PIN=12", - "WAKEUP_BUTTON=36", - "WAKEUP_BUTTON_STATE=HIGH", + "POWER_WAKEUP_BUTTON=32", + "POWER_WAKEUP_BUTTON_STATE=HIGH", + "LED_DEFAULT_EN_PIN=27", + "LED_DEFAULT_DATA_PIN=32", + "LED_DEFAULT_CLK_PIN=33", + "LED_DEFAULT_COUNT=32", + "LED_DEFAULT_TYPE=SK9822", + "LED_DEFAULT_COLOR_ORDER=BGR", + "LED_DEFAULT_INVERT_DIRECTION=1", + "LED_DEFAULT_BRIGHTNESS=0.5f", + "LED_MAX_BRIGHTNESS=0.5f", + "LED_MAX_COUNT=1000", + "BUTTON_DEFAULT_PIN=34", + "IMU_DEFAULT_SDA=23", + "IMU_DEFAULT_SCL=22", "ESP32", "ESP_PLATFORM", "F_CPU=240000000L", @@ -246,11 +260,11 @@ "ARDUINO=10805", "ARDUINO_ARCH_ESP32", "ARDUINO_VARIANT=\"esp32\"", - "ARDUINO_BOARD=\"Flowtoys Creator Tab\"", + "ARDUINO_BOARD=\"Espressif ESP32 Dev Module\"", "" ], - "cStandard": "c99", - "cppStandard": "c++11", + "cStandard": "gnu99", + "cppStandard": "gnu++11", "compilerPath": "C:/Users/bkupe/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-gcc.exe", "compilerArgs": [ "-mlongcalls", diff --git a/Firmware/Bentuino/boards/flowtoyscreatorclub.json b/Firmware/Bentuino/boards/flowtoyscreator.json similarity index 75% rename from Firmware/Bentuino/boards/flowtoyscreatorclub.json rename to Firmware/Bentuino/boards/flowtoyscreator.json index b668373b..8274d62c 100644 --- a/Firmware/Bentuino/boards/flowtoyscreatorclub.json +++ b/Firmware/Bentuino/boards/flowtoyscreator.json @@ -4,13 +4,13 @@ "ldscript": "esp32_out.ld" }, "core": "esp32", - "extra_flags": "-DARDUINO_FLOWTOYS_CREATOR_CLUB -DPOWER_KEEP_PIN=12 -DWAKEUP_BUTTON=32 -DWAKEUP_BUTTON_STATE=HIGH", + "extra_flags": "", "f_cpu": "240000000L", "f_flash": "40000000L", "flash_mode": "dio", "mcu": "esp32", "variant": "esp32", - "partitions":"default.csv" + "partitions":"no_ota.csv" }, "connectivity": [ "wifi", @@ -25,13 +25,13 @@ "arduino", "espidf" ], - "name": "Flowtoys Creator Club", + "name": "Flowtoys Creator", "upload": { "flash_size": "4MB", "maximum_ram_size": 327680, "maximum_size": 4194304, "require_upload_port": true, - "speed": 460800 + "speed": 921600 }, "url": "https://github.com/benkuper/Bento", "vendor": "Flowtoys" diff --git a/Firmware/Bentuino/boards/flowtoyscreatortab.json b/Firmware/Bentuino/boards/flowtoyscreatortab.json index 989a0d9c..3528bac8 100644 --- a/Firmware/Bentuino/boards/flowtoyscreatortab.json +++ b/Firmware/Bentuino/boards/flowtoyscreatortab.json @@ -31,7 +31,7 @@ "maximum_ram_size": 327680, "maximum_size": 4194304, "require_upload_port": true, - "speed": 460800 + "speed": 921600 }, "url": "https://github.com/benkuper/Bento", "vendor": "Flowtoys" diff --git a/Firmware/Bentuino/platformio.ini b/Firmware/Bentuino/platformio.ini index 9b913f41..2474ad6f 100644 --- a/Firmware/Bentuino/platformio.ini +++ b/Firmware/Bentuino/platformio.ini @@ -9,8 +9,8 @@ ; https://docs.platformio.org/page/projectconf.html [env:bentuino] +board = esp32dev platform = espressif32 -board = flowtoyscreatortab framework = arduino lib_deps = adafruit/Adafruit NeoPixel@^1.10.0 @@ -33,8 +33,36 @@ lib_deps = madhephaestus/ESP32Servo @ ^0.11.0 rstephan/ArtnetWifi @ ^1.4.0 gin66/FastAccelStepper @ ^0.27.4 -src_filter = + + + +build_src_filter = + + upload_speed = 921600 monitor_speed = 115200 lib_compat_mode = strict lib_ldf_mode = deep +board_build.f_cpu = 240000000L +board_build.f_flash = 40000000L +board_build.flash_mode = dio +board_build.mcu = esp32 +board_build.variant = esp32 +board_build.partitions = no_ota.csv + +build_flags = + -D ARDUINO_BENTUINO + -D POWER_KEEP_PIN=12 + -D POWER_WAKEUP_BUTTON=32 + -D POWER_WAKEUP_BUTTON_STATE=HIGH + -D LED_DEFAULT_EN_PIN=27 + -D LED_DEFAULT_DATA_PIN=32 + -D LED_DEFAULT_CLK_PIN=33 + -D LED_DEFAULT_COUNT=32 + -D LED_DEFAULT_TYPE=SK9822 + -D LED_DEFAULT_COLOR_ORDER=BGR + -D LED_DEFAULT_INVERT_DIRECTION=1 + -D LED_DEFAULT_BRIGHTNESS=0.5f + -D LED_MAX_BRIGHTNESS=0.5f + -D LED_MAX_COUNT=1000 + -D BUTTON_DEFAULT_PIN=34 + -D IMU_DEFAULT_SDA=23 + -D IMU_DEFAULT_SCL=22 + + diff --git a/Firmware/Bentuino/src/Common/BoardDefines.h b/Firmware/Bentuino/src/Common/BoardDefines.h index 1400857a..49ef49ba 100644 --- a/Firmware/Bentuino/src/Common/BoardDefines.h +++ b/Firmware/Bentuino/src/Common/BoardDefines.h @@ -1,31 +1,29 @@ #pragma once +#ifndef MAIN_INCLUDE // Avoid showing twice -#ifndef MAIN_INCLUDE //Avoid showing twice -#pragma message ("Compiling For " ARDUINO_BOARD) -#endif +#pragma message("Compiling For " ARDUINO_BOARD) #ifdef POWER_KEEP_PIN +#pragma message("Using Power PIN " XSTR(POWER_KEEP_PIN)) +#define BOARD_INIT_POWER \ +pinMode(POWER_KEEP_PIN, OUTPUT); \ +digitalWrite(POWER_KEEP_PIN, HIGH); // keep pin12 high to keep board awake -#ifndef MAIN_INCLUDE //Avoid showing twice -#pragma message ("Using Power PIN " XSTR(POWER_KEEP_PIN)) -#endif +#define BOARD_SHUTDOWN_POWER \ +digitalWrite(POWER_KEEP_PIN, LOW); +#else -//keep pin12 high to keep board awake -#define BoardInit \ - pinMode(POWER_KEEP_PIN, OUTPUT);\ - digitalWrite(POWER_KEEP_PIN, HIGH); +#define BOARD_INIT_POWER +#define BOARD_INIT_SHUTDOWN -#define BoardShutdown \ - digitalWrite(POWER_KEEP_PIN, LOW); -#endif +#endif //POWER_KEEP_PIN +#endif // MAIN_INCLUDE -#ifndef BoardInit -#define BoardInit -#endif +#define BoardInit \ + BOARD_INIT_POWER -#ifndef BoardShutdown -#define BoardShutdown -#endif \ No newline at end of file +#define BoardShutdown \ + BOARD_SHUTDOWN_POWER diff --git a/Firmware/Bentuino/src/Common/Helpers.h b/Firmware/Bentuino/src/Common/Helpers.h index 8702432f..aa7bd5a3 100644 --- a/Firmware/Bentuino/src/Common/Helpers.h +++ b/Firmware/Bentuino/src/Common/Helpers.h @@ -24,6 +24,7 @@ // Component Helpers #define AddComponent(name, comp, Type, enabled) comp = addComponent(name, enabled, o["components"][name]); +#define AddOwnedComponent(comp) addComponent(comp, o["components"][(comp)->name]); #define AddDefaultComponentListener(comp) comp->addListener(std::bind(&Component::onChildComponentEvent, this, std::placeholders::_1)); // > Component Class definition @@ -34,17 +35,17 @@ { \ public: -#define DeclareSubComponent(ParentClass, ClassPrefix, Type, Derives) \ - DeclareComponentClass(ParentClass, ClassPrefix, Derives) \ - ClassPrefix##Component(const String &name, bool enabled = true) : ParentClass(name, enabled) {} \ - ~ClassPrefix##Component() {} \ +#define DeclareSubComponent(ParentClass, ClassPrefix, Type, Derives) \ + DeclareComponentClass(ParentClass, ClassPrefix, Derives) \ + ClassPrefix##Component(const String &name = Type, bool enabled = true) : ParentClass(name, enabled) {} \ + ~ClassPrefix##Component() {} \ virtual String getTypeString() const override { return Type; } -#define DeclareComponentSingleton(ClassPrefix, Type, Derives) \ - DeclareComponentClass(Component, ClassPrefix, Derives) \ - DeclareSingleton(ClassPrefix##Component) \ - ClassPrefix##Component(const String &name, bool enabled = true) : Component(name, enabled) { InitSingleton() } \ - ~ClassPrefix##Component() { DeleteSingleton() } \ +#define DeclareComponentSingleton(ClassPrefix, Type, Derives) \ + DeclareComponentClass(Component, ClassPrefix, Derives) \ + DeclareSingleton(ClassPrefix##Component) \ + ClassPrefix##Component(const String &name = Type, bool enabled = true) : Component(name, enabled) { InitSingleton() } \ + ~ClassPrefix##Component() { DeleteSingleton() } \ virtual String getTypeString() const override { return Type; } #define DeclareComponent(ClassPrefix, Type, Derives) DeclareSubComponent(Component, ClassPrefix, Type, Derives) @@ -84,15 +85,23 @@ #define AddDefaultParameterListener(Class, param) param->addListener(std::bind(&Class::onParameterEvent, this, std::placeholders::_1)); #define SendParameterFeedback(param) CommunicationComponent::instance->sendParameterFeedback(this, param); -#define AddParameter(name, val) addParameter(name, val) -#define AddRangeParameter(name, val, minVal, maxVal, isConfig) addParameter(name, val, minVal, maxVal, false) +#define DeclareParameter(param, val, isConfig) Parameter param {#param, val, var(), var(),isConfig} +#define DeclareRangeParameter(param, val, min, max, isConfig) Parameter param {#param, val, min, max,isConfig} +#define DeclareConfigParameter(param, val) DeclareParameter(param, val, true) +#define DeclareRangeConfigParameter(param, val) DeclareRangeParameter(param, val, min, max, true) + +#define AddParameter(param) addParameter(¶m) +#define AddAndSetParameter(param) { addParameter(¶m); param.set(Settings::getVal(o, #param , param.val)); } + +// #define AddParameter(name, val) addParameter(name, val) +// #define AddRangeParameter(name, val, minVal, maxVal, isConfig) addParameter(name, val, minVal, maxVal, false) // Only config parameters check the settings -#define AddConfigParameter(name, val) addParameter(name, Settings::getVal(o, name, val), var(), var(), true) -#define AddRangeConfigParameter(name, val, minVal, maxVal) addParameter(name, Settings::getVal(o, name, val), minVal, maxVal, true) +// #define AddConfigParameter(name, val) addParameter(name, Settings::getVal(o, name, val), var(), var(), true) +// #define AddRangeConfigParameter(name, val, minVal, maxVal) addParameter(name, Settings::getVal(o, name, val), minVal, maxVal, true) // Script -#define LinkScriptFunctionsStart \ +#define LinkScriptFunctionsStart \ virtual void linkScriptFunctionsInternal(IM3Module module, const char *tName) override \ { #define LinkScriptFunctionsEnd } diff --git a/Firmware/Bentuino/src/Common/Parameter.cpp b/Firmware/Bentuino/src/Common/Parameter.cpp index 5e3c2620..57669c70 100644 --- a/Firmware/Bentuino/src/Common/Parameter.cpp +++ b/Firmware/Bentuino/src/Common/Parameter.cpp @@ -13,10 +13,10 @@ Parameter::Parameter(const String &name, var val, var _minVal, var _maxVal, bool Parameter::~Parameter() {} -bool Parameter::boolValue() { return val.boolValue(); } -int Parameter::intValue() { return val.intValue(); } -float Parameter::floatValue() { return val.floatValue(); } -String Parameter::stringValue() { return val.stringValue(); } +bool Parameter::boolValue() const { return val.boolValue(); } +int Parameter::intValue() const { return val.intValue(); } +float Parameter::floatValue() const { return val.floatValue(); } +String Parameter::stringValue() const { return val.stringValue(); } bool Parameter::hasRange() { diff --git a/Firmware/Bentuino/src/Common/Parameter.h b/Firmware/Bentuino/src/Common/Parameter.h index ab9b4771..af6e9393 100644 --- a/Firmware/Bentuino/src/Common/Parameter.h +++ b/Firmware/Bentuino/src/Common/Parameter.h @@ -37,10 +37,10 @@ class Parameter : public EventBroadcaster bool isConfig; bool readOnly; - bool boolValue(); - int intValue(); - float floatValue(); - String stringValue(); + bool boolValue() const; + int intValue() const; + float floatValue() const; + String stringValue() const; bool hasRange(); diff --git a/Firmware/Bentuino/src/Common/Settings.cpp b/Firmware/Bentuino/src/Common/Settings.cpp index f89b3d02..89dc8b47 100644 --- a/Firmware/Bentuino/src/Common/Settings.cpp +++ b/Firmware/Bentuino/src/Common/Settings.cpp @@ -30,7 +30,7 @@ bool Settings::saveSettings() serializeJson(settings, test); DBG("SETTINGS Json serialized and packed : " + String(settingsSize)); DBG(test); - + prefs.clear(); if (s == 0) { @@ -45,6 +45,14 @@ bool Settings::saveSettings() return true; } +bool Settings::clearSettings() +{ + prefs.clear(); + settings.clear(); + DBG("Settings cleared."); + return true; +} + var Settings::getVal(JsonObject o, const String &name, var defaultVal) { // DBG("Get val " + name + "/" + String((int)o.containsKey(name))); diff --git a/Firmware/Bentuino/src/Common/Settings.h b/Firmware/Bentuino/src/Common/Settings.h index c7afa752..a9652046 100644 --- a/Firmware/Bentuino/src/Common/Settings.h +++ b/Firmware/Bentuino/src/Common/Settings.h @@ -10,6 +10,7 @@ class Settings static bool loadSettings(); static bool saveSettings(); + static bool clearSettings(); static var getVal(JsonObject o, const String& name, var defaultVal); }; \ No newline at end of file diff --git a/Firmware/Bentuino/src/Common/script/Script.cpp b/Firmware/Bentuino/src/Common/script/Script.cpp index 66a86b6b..e4aa83b5 100644 --- a/Firmware/Bentuino/src/Common/script/Script.cpp +++ b/Firmware/Bentuino/src/Common/script/Script.cpp @@ -7,12 +7,13 @@ Script::Script(Component *localComponent) : isRunning(false), localComponent(localComponent), - // runtime(NULL), + runtime(NULL), // env(NULL), initFunc(NULL), updateFunc(NULL), stopFunc(NULL) { + DBG("Script Init here"); } void Script::init() @@ -75,7 +76,7 @@ void Script::launchWasm() stop(); #if WASM_ASYNC - xTaskCreate(&Script::launchWasmTaskStatic, "wasm3", NATIVE_STACK_SIZE, this, 5, NULL); + xTaskCreate(&Script::launchWasmTaskStatic, "wasm3", SCRIPT_NATIVE_STACK_SIZE, this, 5, NULL); DBG("Wasm task launched"); #else launchWasmTask(); diff --git a/Firmware/Bentuino/src/Common/script/Script.h b/Firmware/Bentuino/src/Common/script/Script.h index afb05c1e..e872b6cd 100644 --- a/Firmware/Bentuino/src/Common/script/Script.h +++ b/Firmware/Bentuino/src/Common/script/Script.h @@ -2,7 +2,7 @@ #define SCRIPT_MAX_SIZE 32000 #define WASM_STACK_SLOTS 2000 -#define NATIVE_STACK_SIZE (32 * 1024) +#define SCRIPT_NATIVE_STACK_SIZE (32 * 1024) #define WASM_MEMORY_LIMIT 8192 #define WASM_ASYNC 0 diff --git a/Firmware/Bentuino/src/Component/Component.cpp b/Firmware/Bentuino/src/Component/Component.cpp index 71bc6172..d40cacb7 100644 --- a/Firmware/Bentuino/src/Component/Component.cpp +++ b/Firmware/Bentuino/src/Component/Component.cpp @@ -1,18 +1,31 @@ bool Component::init(JsonObject o) { - enabled->set(Settings::getVal(o, "enabled", enabled->boolValue())); + AddAndSetParameter(enabled); isInit = initInternal(o); + + //init all parameters + // for (int i = 0; i < numParameters; i++) + // { + // parameters[i]->set(Settings::getVal(o, parameters[i]->name, parameters[i]->val)); + // } + if (isInit) NDBG(F("Init OK")); else NDBG(F("Init Error.")); + + return isInit; } void Component::update() { + if (!enabled.boolValue()) + return; + + // NDBG("Update"); for (int i = 0; i < numComponents; i++) components[i]->update(); @@ -36,12 +49,18 @@ void Component::clear() numParameters = 0; } -Parameter *Component::addParameter(const String &name, var val, var minVal, var maxVal, bool isConfig) +void Component::addParameter(Parameter *p) { - Parameter *p = new Parameter(name, val, minVal, maxVal, isConfig); parameters[numParameters] = p; numParameters++; - AddDefaultParameterListener(Component, p) return p; + AddDefaultParameterListener(Component, p); +} + +Parameter *Component::addParameter(const String &name, var val, var minVal, var maxVal, bool isConfig) +{ + Parameter *p = new Parameter(name, val, minVal, maxVal, isConfig); + addParameter(p); + return p; } Parameter *Component::addConfigParameter(const String &name, var val, var minVal, var maxVal) @@ -51,7 +70,7 @@ Parameter *Component::addConfigParameter(const String &name, var val, var minVal void Component::onParameterEvent(const ParameterEvent &e) { - if (e.parameter == enabled) + if (e.parameter == &enabled) onEnabledChanged(); onParameterEventInternal(e); diff --git a/Firmware/Bentuino/src/Component/Component.h b/Firmware/Bentuino/src/Component/Component.h index 2cbb9eb7..ce408989 100644 --- a/Firmware/Bentuino/src/Component/Component.h +++ b/Firmware/Bentuino/src/Component/Component.h @@ -1,7 +1,7 @@ #pragma once #define MAX_CHILD_COMPONENTS 16 -#define MAX_CHILD_CONTROLLABLES 16 +#define MAX_CHILD_PARAMETERS 16 //#define MAX_EVENT_TYPES 16 class Component : public EventBroadcaster @@ -13,7 +13,6 @@ class Component : public EventBroadcaster numComponents(0), numParameters(0) { - enabled = addParameter("enabled", _enabled, var(), var(), true); } virtual ~Component() {} @@ -22,14 +21,14 @@ class Component : public EventBroadcaster String name; bool isInit; - Parameter *enabled; + Parameter enabled{"enabled", true,var(), var(),true}; Component *parentComponent; Component *components[MAX_CHILD_COMPONENTS]; uint8_t numComponents; - Parameter *parameters[MAX_CHILD_COMPONENTS]; + Parameter *parameters[MAX_CHILD_PARAMETERS]; uint8_t numParameters; virtual String getComponentEventName(uint8_t type) const { return "[noname]"; } @@ -65,6 +64,7 @@ class Component : public EventBroadcaster Component *getComponentWithName(const String &name); + void addParameter(Parameter* param); Parameter *addParameter(const String &name, var val, var minVal = var(), var maxVal = var(), bool isConfig = false); Parameter *addConfigParameter(const String &name, var val, var minVal = var(), var maxVal = var()); // helpers for non ranged config param declaration simplification Parameter * getParameterWithName(const String &name); diff --git a/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.cpp b/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.cpp index f875bfb6..cd701a03 100644 --- a/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.cpp @@ -2,7 +2,7 @@ ImplementSingleton(BatteryComponent); bool BatteryComponent::initInternal(JsonObject o) { - sendFeedback = AddParameter("sendFeedback", true); + AddAndSetParameter(sendFeedback); return true; } diff --git a/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.h b/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.h index 0202328e..a8cdbfcb 100644 --- a/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.h +++ b/Firmware/Bentuino/src/Component/components/battery/BatteryComponent.h @@ -1,7 +1,7 @@ #pragma once DeclareComponentSingleton(Battery, "battery", ) - Parameter *sendFeedback; +DeclareConfigParameter(sendFeedback, true); bool initInternal(JsonObject o) override; void updateInternal() override; diff --git a/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.cpp b/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.cpp index 7a3de93c..e97ce258 100644 --- a/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.cpp @@ -2,19 +2,19 @@ ImplementSingleton(CommunicationComponent); bool CommunicationComponent::initInternal(JsonObject o) { - AddComponent("serial", serial, Serial, true); - AddComponent("osc", osc, OSC, true); + AddOwnedComponent(&serial); + AddOwnedComponent(&osc); return true; } void CommunicationComponent::onChildComponentEvent(const ComponentEvent &e) { - if (e.component == serial && e.type == SerialComponent::MessageReceived) + if (e.component == &serial && e.type == SerialComponent::MessageReceived) { sendEvent(MessageReceived, e.data, e.numData); } - else if (e.component == osc && e.type == OSCComponent::MessageReceived) + else if (e.component == &osc && e.type == OSCComponent::MessageReceived) { sendEvent(MessageReceived, e.data, e.numData); } @@ -23,19 +23,19 @@ void CommunicationComponent::onChildComponentEvent(const ComponentEvent &e) void CommunicationComponent::sendParameterFeedback(Component *c, Parameter *param) { var data[1]{param->val}; - serial->sendMessage(c->name, param->name, data, 1); - osc->sendMessage(c->name, param->name, data, 1); + serial.sendMessage(c->name, param->name, data, 1); + osc.sendMessage(c->name, param->name, data, 1); } void CommunicationComponent::sendEventFeedback(const ComponentEvent &e) { - serial->sendMessage(e.component->name, e.getName(), e.data, e.numData); - osc->sendMessage(e.component->name, e.getName(), e.data, e.numData); + serial.sendMessage(e.component->name, e.getName(), e.data, e.numData); + osc.sendMessage(e.component->name, e.getName(), e.data, e.numData); } void CommunicationComponent::sendMessage(Component *c, const String &mName, const String &val) { var data[1]{val}; - serial->sendMessage(c->name, mName, data, 1); - osc->sendMessage(c->name, mName, data, 1); + serial.sendMessage(c->name, mName, data, 1); + osc.sendMessage(c->name, mName, data, 1); } diff --git a/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.h b/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.h index 2f5a7ec3..5a8a5483 100644 --- a/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.h +++ b/Firmware/Bentuino/src/Component/components/communication/CommunicationComponent.h @@ -1,8 +1,8 @@ #pragma once DeclareComponentSingleton(Communication, "comm",) - SerialComponent * serial; - OSCComponent * osc; + SerialComponent serial; + OSCComponent osc; bool initInternal(JsonObject o) override; diff --git a/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.cpp b/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.cpp index 8b9c517e..bb7c0551 100644 --- a/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.cpp @@ -1,14 +1,14 @@ ImplementSingleton(OSCComponent) - bool OSCComponent::initInternal(JsonObject o) +bool OSCComponent::initInternal(JsonObject o) { udpIsInit = false; pingEnabled = false; timeSinceLastReceivedPing = 0; - remoteHost = AddConfigParameter("remoteHost", (char *)""); - isAlive = AddParameter("isAlive", false); + AddAndSetParameter(remoteHost); + AddParameter(isAlive); return true; } @@ -16,7 +16,8 @@ ImplementSingleton(OSCComponent) void OSCComponent::updateInternal() { if (pingEnabled && millis() > timeSinceLastReceivedPing + OSC_PING_TIMEOUT) - isAlive->set(false); + isAlive.set(false); + receiveOSC(); } @@ -31,7 +32,7 @@ void OSCComponent::onEnabledChanged() void OSCComponent::setupConnection() { - bool shouldConnect = enabled->boolValue() && WifiComponent::instance->state == WifiComponent::Connected; + bool shouldConnect = enabled.boolValue() && WifiComponent::instance->state == WifiComponent::Connected; udpIsInit = false; if (shouldConnect) @@ -39,7 +40,7 @@ void OSCComponent::setupConnection() NDBG("Start OSC Receiver on " + String(OSC_LOCAL_PORT)); udp.begin(OSC_LOCAL_PORT); udp.flush(); - isAlive->set(true); + isAlive.set(true); timeSinceLastReceivedPing = millis(); if (MDNS.begin((DeviceID).c_str())) @@ -61,7 +62,7 @@ void OSCComponent::setupConnection() NDBG("Stopping Receiver"); udp.flush(); udp.stop(); - isAlive->set(false); + isAlive.set(false); MDNS.end(); } @@ -69,7 +70,7 @@ void OSCComponent::setupConnection() void OSCComponent::receiveOSC() { - if (!enabled->boolValue() || !udpIsInit) + if (!udpIsInit) return; OSCMessage msg; @@ -93,7 +94,7 @@ void OSCComponent::processMessage(OSCMessage &msg) NDBG("Yo received from : " + String(hostData)); - remoteHost->set(hostData); + remoteHost.set(hostData); OSCMessage msg("/wassup"); @@ -106,7 +107,7 @@ void OSCComponent::processMessage(OSCMessage &msg) else if (msg.match("/ping")) { // NDBG("Received ping"); - isAlive->set(true); + isAlive.set(true); pingEnabled = true; timeSinceLastReceivedPing = millis(); @@ -114,7 +115,7 @@ void OSCComponent::processMessage(OSCMessage &msg) { char hostData[32]; msg.getString(0, hostData, 32); - remoteHost->set(hostData); + remoteHost.set(hostData); } OSCMessage msg("/pong"); @@ -148,15 +149,15 @@ void OSCComponent::processMessage(OSCMessage &msg) void OSCComponent::sendMessage(OSCMessage &msg) { - if (!enabled->boolValue()) + if (!enabled.boolValue()) return; - if (remoteHost->stringValue().length() == 0) + if (remoteHost.stringValue().length() == 0) return; char addr[32]; msg.getAddress(addr); - udp.beginPacket((char *)remoteHost->stringValue().c_str(), OSC_REMOTE_PORT); + udp.beginPacket((char *)remoteHost.stringValue().c_str(), OSC_REMOTE_PORT); msg.send(udp); udp.endPacket(); msg.empty(); @@ -164,7 +165,7 @@ void OSCComponent::sendMessage(OSCMessage &msg) void OSCComponent::sendMessage(String address) { - if (!enabled->boolValue()) + if (!enabled.boolValue()) return; OSCMessage m(address.c_str()); @@ -173,7 +174,7 @@ void OSCComponent::sendMessage(String address) void OSCComponent::sendMessage(const String &source, const String &command, var *data, int numData) { - if (!enabled->boolValue()) + if (!enabled.boolValue()) return; OSCMessage msg(("/" + source + "/" + command).c_str()); diff --git a/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.h b/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.h index 9d42661b..898d1222 100644 --- a/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.h +++ b/Firmware/Bentuino/src/Component/components/communication/osc/OSCComponent.h @@ -4,38 +4,38 @@ #define OSC_REMOTE_PORT 10000 #define OSC_PING_TIMEOUT 5000 -DeclareComponentSingleton(OSC, "osc",) +DeclareComponentSingleton(OSC, "osc", ) WiFiUDP udp; - bool udpIsInit; - Parameter * remoteHost; +bool udpIsInit; +Parameter remoteHost{"remoteHost", "", var(), var(), true}; - //Ping - bool pingEnabled; //only activate ping check if received a first ping - long timeSinceLastReceivedPing; - Parameter * isAlive; +// Ping +bool pingEnabled; // only activate ping check if received a first ping +long timeSinceLastReceivedPing; +Parameter isAlive{"isAlive", false}; - bool initInternal(JsonObject o) override; - void updateInternal() override; - void clearInternal() override; +bool initInternal(JsonObject o) override; +void updateInternal() override; +void clearInternal() override; - void onEnabledChanged() override; +void onEnabledChanged() override; - void setupConnection(); +void setupConnection(); - void receiveOSC(); - void processMessage(OSCMessage &m); +void receiveOSC(); +void processMessage(OSCMessage &m); - void saveRemoteHost(String ip); +void saveRemoteHost(String ip); - void sendMessage(OSCMessage &m); - void sendMessage(String address); - void sendMessage(const String &source, const String &command, var *data, int numData); +void sendMessage(OSCMessage &m); +void sendMessage(String address); +void sendMessage(const String &source, const String &command, var *data, int numData); - //Helper - var OSCArgumentToVar(OSCMessage &m, int index); +// Helper +var OSCArgumentToVar(OSCMessage &m, int index); + +DeclareComponentEventTypes(MessageReceived); +DeclareComponentEventNames("MessageReceived"); - DeclareComponentEventTypes(MessageReceived); - DeclareComponentEventNames("MessageReceived"); - EndDeclareComponent \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.cpp b/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.cpp index 159d427d..7dc406fc 100644 --- a/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.cpp @@ -5,7 +5,7 @@ bool SerialComponent::initInternal(JsonObject o) bufferIndex = 0; memset(buffer, 0, 512); Serial.begin(115200); - feedbackEnabled = AddParameter("sendFeedback", false); + AddAndSetParameter(sendFeedback); return true; } @@ -102,7 +102,7 @@ void SerialComponent::processMessage(String buffer) void SerialComponent::sendMessage(String source, String command, var *data, int numData) { - if (!feedbackEnabled->boolValue()) + if (!sendFeedback.boolValue()) return; String msg = source + "." + command; diff --git a/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.h b/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.h index 85341dfe..86ea70d9 100644 --- a/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.h +++ b/Firmware/Bentuino/src/Component/components/communication/serial/SerialComponent.h @@ -5,7 +5,7 @@ DeclareComponentSingleton(Serial, "serial",) char buffer[512]; byte bufferIndex; - Parameter * feedbackEnabled; + Parameter sendFeedback{"sendFeedback", false, var(), var(), true}; bool initInternal(JsonObject o) override; void updateInternal() override; diff --git a/Firmware/Bentuino/src/Component/components/files/FilesComponent.cpp b/Firmware/Bentuino/src/Component/components/files/FilesComponent.cpp index 8a2910dd..0d4e0793 100644 --- a/Firmware/Bentuino/src/Component/components/files/FilesComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/files/FilesComponent.cpp @@ -4,47 +4,47 @@ bool FilesComponent::initInternal(JsonObject o) { useInternalMemory = false; - sdEnPin = AddConfigParameter("sdEnPin", 0); - sdEnVal = AddConfigParameter("sdEnVal", 0); - sdSCK = AddConfigParameter("sdSCK", 0); - sdMiso = AddConfigParameter("sdMiso", 0); - sdMosi = AddConfigParameter("sdMosi", 0); - sdCS = AddConfigParameter("sdCS", 0); - sdSpeed = AddConfigParameter("sdSpeed", 27000000); - - if (sdEnPin->intValue() > 0) + AddAndSetParameter(sdEnPin); + AddAndSetParameter(sdEnVal); + AddAndSetParameter(sdSCK); + AddAndSetParameter(sdMiso); + AddAndSetParameter(sdMosi); + AddAndSetParameter(sdCS); + AddAndSetParameter(sdSpeed); + + if (sdEnPin.intValue() > 0) { - NDBG("Setting SD En Pin " + String(sdEnPin->intValue()) + " to " + String(sdEnVal->intValue())); - pinMode(sdEnPin->intValue(), OUTPUT); - digitalWrite(sdEnPin->intValue(), sdEnVal->intValue()); + NDBG("Setting SD En Pin " + String(sdEnPin.intValue()) + " to " + String(sdEnVal.intValue())); + pinMode(sdEnPin.intValue(), OUTPUT); + digitalWrite(sdEnPin.intValue(), sdEnVal.intValue()); } - if (sdSCK->intValue() == 0 || sdMiso->intValue() == 0 || sdMosi->intValue() == 0 || sdCS->intValue() == 0) + if (sdSCK.intValue() == 0 || sdMiso.intValue() == 0 || sdMosi.intValue() == 0 || sdCS.intValue() == 0) { String npin; - if (sdSCK->intValue() == 0) + if (sdSCK.intValue() == 0) npin += "SCK,"; - if (sdMiso->intValue() == 0) + if (sdMiso.intValue() == 0) npin += "MISO,"; - if (sdMosi->intValue() == 0) + if (sdMosi.intValue() == 0) npin += "MOSI,"; - if (sdCS->intValue() == 0) + if (sdCS.intValue() == 0) npin += "CS"; NDBG(npin + " pins not defined, using internal memory"); return initInternalMemory(); } - NDBG("initilializing SD with pins SCK,MISO,MOSI,CS,Speed : " + sdSCK->stringValue() + "," + sdMiso->stringValue() + "," + sdMosi->stringValue() + "," + sdCS->stringValue() + "," + sdSpeed->stringValue()); - pinMode(sdSCK->intValue(), INPUT_PULLUP); - pinMode(sdMiso->intValue(), INPUT_PULLUP); - pinMode(sdMosi->intValue(), INPUT_PULLUP); - pinMode(sdCS->intValue(), OUTPUT); - digitalWrite(sdCS->intValue(), LOW); + NDBG("initilializing SD with pins SCK,MISO,MOSI,CS,Speed : " + sdSCK.stringValue() + "," + sdMiso.stringValue() + "," + sdMosi.stringValue() + "," + sdCS.stringValue() + "," + sdSpeed.stringValue()); + pinMode(sdSCK.intValue(), INPUT_PULLUP); + pinMode(sdMiso.intValue(), INPUT_PULLUP); + pinMode(sdMosi.intValue(), INPUT_PULLUP); + pinMode(sdCS.intValue(), OUTPUT); + digitalWrite(sdCS.intValue(), LOW); - spiSD.begin((int8_t)sdSCK->intValue(), (int8_t)sdMiso->intValue(), (int8_t)sdMosi->intValue(), (int8_t)sdCS->intValue()); // SCK,MISO,MOSI,ss + spiSD.begin((int8_t)sdSCK.intValue(), (int8_t)sdMiso.intValue(), (int8_t)sdMosi.intValue(), (int8_t)sdCS.intValue()); // SCK,MISO,MOSI,ss - if (SD.begin((uint8_t)sdCS->intValue(), spiSD)) + if (SD.begin((uint8_t)sdCS.intValue(), spiSD)) { NDBG("SD Card initialized."); diff --git a/Firmware/Bentuino/src/Component/components/files/FilesComponent.h b/Firmware/Bentuino/src/Component/components/files/FilesComponent.h index f1f1951a..560d3d4e 100644 --- a/Firmware/Bentuino/src/Component/components/files/FilesComponent.h +++ b/Firmware/Bentuino/src/Component/components/files/FilesComponent.h @@ -7,13 +7,13 @@ bool initInternalMemory(); SPIClass spiSD; bool useInternalMemory; -Parameter * sdEnPin; -Parameter * sdEnVal; -Parameter * sdSCK; -Parameter * sdMiso; -Parameter * sdMosi; -Parameter * sdCS; -Parameter * sdSpeed; +Parameter sdEnPin{ "sdEnPin", 0, var(), var(), true}; +Parameter sdEnVal{ "sdEnVal", 0, var(), var(), true}; +Parameter sdSCK{ "sdSCK", 0, var(), var(), true}; +Parameter sdMiso{ "sdMiso", 0, var(), var(), true}; +Parameter sdMosi{ "sdMosi", 0, var(), var(), true}; +Parameter sdCS{ "sdCS", 0, var(), var(), true}; +Parameter sdSpeed{ "sdSpeed", 27000000, var(), var(), true}; File openFile(String fileName, bool forWriting = false, bool deleteIfExists = true); void deleteFileIfExists(String path); diff --git a/Firmware/Bentuino/src/Component/components/imu/IMUComponent.cpp b/Firmware/Bentuino/src/Component/components/imu/IMUComponent.cpp index 8c7fe3a5..e733f929 100644 --- a/Firmware/Bentuino/src/Component/components/imu/IMUComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/imu/IMUComponent.cpp @@ -1,27 +1,27 @@ bool IMUComponent::initInternal(JsonObject o) { - isConnected = AddParameter("connected", false); - isConnected->readOnly = true; + AddParameter(isConnected); + isConnected.readOnly = true; - sendLevel = AddParameter("sendLevel", 0); + AddAndSetParameter(sendLevel); - orientationSendRate = AddConfigParameter("orientationSendRate", 50); - sdaPin = AddConfigParameter("sdaPin", 0); - sclPin = AddConfigParameter("sclPin", 0); + AddAndSetParameter(orientationSendRate); + AddAndSetParameter(sdaPin); + AddAndSetParameter(sclPin); - if (sdaPin->intValue() == 0 || sclPin->intValue() == 0) + if (sdaPin.intValue() == 0 || sclPin.intValue() == 0) { String npin; - if (sdaPin->intValue() == 0) + if (sdaPin.intValue() == 0) npin += "SDA,"; - if (sclPin->intValue() == 0) + if (sclPin.intValue() == 0) npin += "SCL"; NDBG(npin + " pins not defined, not using IMU"); return false; } - Wire.begin(sdaPin->intValue(), sclPin->intValue()); + Wire.begin(sdaPin.intValue(), sclPin.intValue()); // Init values accelThresholds[0] = .8f; @@ -59,16 +59,17 @@ void IMUComponent::updateInternal() imuLock = true; long curTime = millis(); - int orientationSendMS = 1000 / orientationSendRate->intValue(); + int orientationSendMS = 1000 / orientationSendRate.intValue(); if (curTime > timeSinceOrientationLastSent + orientationSendMS) { - if ((int)sendLevel->intValue() >= 1) + int sLevel = (int)sendLevel.intValue(); + if (sLevel >= 1) { // NDBG("Orientation send "+ var oData[3] { orientation[0],orientation[1],orientation[2] }; sendEvent(OrientationUpdate, oData, 3); - if ((int)sendLevel->val >= 2) + if (sLevel >= 2) { var aData[3] { accel[0],accel[1],accel[2] }; sendEvent(AccelUpdate, aData, 3); @@ -98,7 +99,7 @@ void IMUComponent::clearInternal() void IMUComponent::onEnabledChanged() { - if (enabled->boolValue()) + if (enabled.boolValue()) startIMUTask(); else shouldStopRead = true; @@ -109,7 +110,7 @@ void IMUComponent::startIMUTask() hasNewData = false, shouldStopRead = false; imuLock = false; - xTaskCreate(&IMUComponent::readIMUStatic, "imu", NATIVE_STACK_SIZE, this, 1, NULL); + xTaskCreate(&IMUComponent::readIMUStatic, "imu", IMU_NATIVE_STACK_SIZE, this, 1, NULL); } void IMUComponent::readIMUStatic(void *_imu) @@ -165,7 +166,7 @@ bool IMUComponent::setupBNO() void IMUComponent::readIMU() { - if (!enabled->boolValue()) + if (!enabled.boolValue()) return; if (imuLock) diff --git a/Firmware/Bentuino/src/Component/components/imu/IMUComponent.h b/Firmware/Bentuino/src/Component/components/imu/IMUComponent.h index 9f7f6753..dce6db12 100644 --- a/Firmware/Bentuino/src/Component/components/imu/IMUComponent.h +++ b/Firmware/Bentuino/src/Component/components/imu/IMUComponent.h @@ -1,16 +1,16 @@ #pragma once #define TRAIL_MAX 20 -#define NATIVE_STACK_SIZE (32 * 1024) +#define IMU_NATIVE_STACK_SIZE (32 * 1024) DeclareComponent(IMU, "imu", ) Adafruit_BNO055 bno; -Parameter *isConnected; -Parameter *sendLevel; -Parameter* orientationSendRate; -Parameter *sdaPin; -Parameter *sclPin; +Parameter isConnected { "isConnected", false }; +Parameter sendLevel { "sendLevel", 0, 0, 3, true}; +Parameter orientationSendRate {"orientationSendRate", 50, var(), var(), true}; +Parameter sdaPin {"sdaPin", IMU_DEFAULT_SDA, var(), var(), true}; +Parameter sclPin {"sclPin", IMU_DEFAULT_SCL, var(), var(), true}; long timeSinceOrientationLastSent; diff --git a/Firmware/Bentuino/src/Component/components/io/IOComponent.cpp b/Firmware/Bentuino/src/Component/components/io/IOComponent.cpp index 65de308c..c8cd7082 100644 --- a/Firmware/Bentuino/src/Component/components/io/IOComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/io/IOComponent.cpp @@ -4,20 +4,16 @@ bool IOComponent::initInternal(JsonObject o) { pwmChannel = -1; - pin = AddConfigParameter("pin", 0); - mode = AddConfigParameter("mode", D_INPUT); - inverted = AddConfigParameter("inverted", false); + AddAndSetParameter(pin); + AddAndSetParameter(mode); + AddAndSetParameter(inverted); - int m = mode->intValue(); + int m = mode.intValue(); - if (m == D_INPUT || m == D_INPUT_PULLUP || m == D_OUTPUT) - value = AddParameter("value", false); - else - value = AddRangeParameter("value", 0.0f, 0.0f, 1.0f, false); + AddParameter(value); + value.readOnly = m == D_INPUT || m == D_INPUT_PULLUP || m == D_OUTPUT; - value->readOnly = m == D_INPUT || m == D_INPUT_PULLUP || m == D_OUTPUT; - - prevValue = value->floatValue(); + prevValue = value.floatValue(); setupPin(); updatePin(); @@ -27,8 +23,7 @@ bool IOComponent::initInternal(JsonObject o) void IOComponent::updateInternal() { - if (enabled->boolValue()) - updatePin(); + updatePin(); } void IOComponent::clearInternal() @@ -37,9 +32,9 @@ void IOComponent::clearInternal() void IOComponent::setupPin() { - if (pin->intValue() != -1) + if (pin.intValue() != -1) { - int m = mode->intValue(); + int m = mode.intValue(); int pinm = -1; switch (m) @@ -63,7 +58,7 @@ void IOComponent::setupPin() if (pinm != -1) { - pinMode(pin->intValue(), pinm); + pinMode(pin.intValue(), pinm); } else if (m == A_OUTPUT) { @@ -73,9 +68,9 @@ void IOComponent::setupPin() { pwmChannel = pwmChannelCount; ledcSetup(pwmChannel, 5000, 10); // 0-1024 at a 5khz resolution - ledcAttachPin(pin->intValue(), pwmChannel); + ledcAttachPin(pin.intValue(), pwmChannel); pwmChannelCount++; - NDBG("Attach pin "+pin->stringValue()+" to "+String(pwmChannel)); + NDBG("Attach pin " + pin.stringValue() + " to " + String(pwmChannel)); } else { @@ -88,52 +83,52 @@ void IOComponent::setupPin() void IOComponent::updatePin() { - if (pin->intValue() == -1) + if (pin.intValue() == -1) return; - int m = mode->intValue(); + int m = mode.intValue(); switch (m) { case D_INPUT: case D_INPUT_PULLUP: { - bool val = digitalRead(pin->intValue()); - if (inverted) + bool val = digitalRead(pin.intValue()); + if (inverted.boolValue()) val = !val; - value->set(val); + value.set(val); } break; case D_OUTPUT: case A_OUTPUT: { - if (prevValue != value->floatValue()) + if (prevValue != value.floatValue()) { if (m == D_OUTPUT) { - digitalWrite(pin->intValue(), inverted ? !value->boolValue() : value->boolValue()); + digitalWrite(pin.intValue(), inverted.boolValue() ? !value.boolValue() : value.boolValue()); } else { if (pwmChannel != -1) { - uint32_t v = value->floatValue() * 1024; - NDBG("Set PWM with value "+String(v)); + uint32_t v = value.floatValue() * 1024; + NDBG("Set PWM with value " + String(v)); ledcWrite(pwmChannel, v); } } - prevValue = value->floatValue(); + prevValue = value.floatValue(); } } break; case A_INPUT: { - float v = analogRead(pin->intValue()) / 4095.0f; - if (inverted) + float v = analogRead(pin.intValue()) / 4095.0f; + if (inverted.boolValue()) v = 1 - v; - value->set(v); + value.set(v); } break; } diff --git a/Firmware/Bentuino/src/Component/components/io/IOComponent.h b/Firmware/Bentuino/src/Component/components/io/IOComponent.h index e93fa243..423462b1 100644 --- a/Firmware/Bentuino/src/Component/components/io/IOComponent.h +++ b/Firmware/Bentuino/src/Component/components/io/IOComponent.h @@ -1,5 +1,5 @@ #pragma once -DeclareComponent(IO,"io",) +DeclareComponent(IO, "io", ) enum PinMode { D_INPUT, D_INPUT_PULLUP, @@ -7,13 +7,13 @@ DeclareComponent(IO,"io",) D_OUTPUT, A_OUTPUT }; -Parameter * pin; -Parameter * mode; -Parameter * inverted; +Parameter pin{"pin", 0, var(), var(), true}; +Parameter mode{"mode", D_INPUT, var(), var(), true}; +Parameter inverted{"inverted", false, var(), var(), true}; int pwmChannel; static int pwmChannelCount; -Parameter *value; +Parameter value{"value", 0.f, 0.f, 1.f}; float prevValue; virtual bool initInternal(JsonObject o) override; @@ -24,11 +24,14 @@ virtual void setupPin(); void updatePin(); LinkScriptFunctionsStart -LinkScriptFunction(IOComponent, get,f,); -LinkScriptFunction(IOComponent, set,,f); + LinkScriptFunction(IOComponent, get, f, ); +LinkScriptFunction(IOComponent, set, , f); LinkScriptFunctionsEnd -DeclareScriptFunctionReturn0(IOComponent, get,float) { return value->floatValue(); } -DeclareScriptFunctionVoid1(IOComponent, set,float) { return value->set(arg1); } +DeclareScriptFunctionReturn0(IOComponent, get, float) +{ + return value.floatValue(); +} +DeclareScriptFunctionVoid1(IOComponent, set, float) { return value.set(arg1); } EndDeclareComponent \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.cpp b/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.cpp index 0954382e..6b99055c 100644 --- a/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.cpp @@ -2,7 +2,7 @@ bool ButtonComponent::initInternal(JsonObject o) { bool result = IOComponent::initInternal(o); - isSystem = addParameter("isSystem", false); + AddAndSetParameter(isSystem); debounceCount = 0; multiPressCount = 0; @@ -17,7 +17,7 @@ void ButtonComponent::updateInternal() { IOComponent::updateInternal(); - if (value->boolValue()) + if (value.boolValue()) { if (!isLongPressed && millis() > timeAtPress + LONGPRESS_TIME) { @@ -47,12 +47,12 @@ void ButtonComponent::onParameterEventInternal(const ParameterEvent &e) { IOComponent::onParameterEventInternal(e); - if (e.parameter == value) + if (e.parameter == &value) { isLongPressed = false; isVeryLongPressed = false; - if (value->boolValue()) + if (value.boolValue()) { timeAtPress = millis(); multiPressCount++; diff --git a/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.h b/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.h index b6bc6403..78725b18 100644 --- a/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.h +++ b/Firmware/Bentuino/src/Component/components/io/button/ButtonComponent.h @@ -11,7 +11,7 @@ class ButtonComponent : public IOComponent public: ButtonComponent(const String &name, bool _enabled) : IOComponent(name, _enabled) {} - Parameter *isSystem; + DeclareConfigParameter(isSystem, false); int debounceCount; long timeAtPress; @@ -31,6 +31,6 @@ class ButtonComponent : public IOComponent LinkScriptFunction(ButtonComponent, getMultipress, i, ); LinkScriptFunctionsEnd - DeclareScriptFunctionReturn0(ButtonComponent, getState, uint32_t) { return isVeryLongPressed?3:isLongPressed?2:value->intValue(); } + DeclareScriptFunctionReturn0(ButtonComponent, getState, uint32_t) { return isVeryLongPressed?3:isLongPressed?2:value.intValue(); } DeclareScriptFunctionReturn0(ButtonComponent, getMultipress, uint32_t) { return multiPressCount; } }; \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.cpp index edaaa0c4..17b2ce66 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.cpp @@ -1,26 +1,24 @@ LedStripLayer::LedStripLayer(const String &name, Type t, LedStripComponent *strip) : Component(name), strip(strip), - type(t), - colors(nullptr) + type(t) { - blendMode = addParameter("blendMode", Add); - initColors(); } LedStripLayer::~LedStripLayer() { - free(colors); } -void LedStripLayer::initColors() +bool LedStripLayer::initInternal(JsonObject o) { - if (colors != nullptr) - free(colors); + AddAndSetParameter(blendMode); - numLeds = strip->count->intValue(); - colors = (Color *)malloc(numLeds * sizeof(Color)); + memset(colors, 0, LED_MAX_COUNT * sizeof(Color)); + + numLeds = strip->count.intValue(); for (int i = 0; i < numLeds; i++) colors[i] = Color(0, 0, 0, 0); + + return true; } // Helpers diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.h b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.h index 7a2e9ec3..d68e1f26 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/LedStripLayer.h @@ -24,14 +24,14 @@ class LedStripLayer : LedStripLayer(const String &name, Type t, LedStripComponent * strip); ~LedStripLayer(); - void initColors(); + virtual bool initInternal(JsonObject o) override; LedStripComponent * strip; int numLeds; Type type; - Parameter * blendMode; + Parameter blendMode {"blendMode", Add, var(), var(), true}; - Color * colors; + Color colors[LED_MAX_COUNT]; //Helper functions void clearColors(); diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.cpp index f6e0baea..b6418028 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.cpp @@ -1,5 +1,7 @@ bool LedStripBakeLayer::initInternal(JsonObject o) { + LedStripLayer::initInternal(o); + frameSize = numLeds * 4; fps = 0; @@ -7,8 +9,8 @@ bool LedStripBakeLayer::initInternal(JsonObject o) totalFrames = 0; groupID = -1; localID = -1; - idMode = AddParameter("idMode",false); - loopShow = AddParameter("loop", false); + AddAndSetParameter(idMode); + AddAndSetParameter(loop); isPlaying = false; curTimeMs = 0; prevTimeMs = 0; @@ -23,7 +25,7 @@ void LedStripBakeLayer::updateInternal() if (!curFile) return; - if (idMode->boolValue()) + if (idMode.boolValue()) { showIdFrame(); return; @@ -54,7 +56,7 @@ bool LedStripBakeLayer::playFrame() if (curFile.available() < frameSize) { NDBG("End of show"); - if (loopShow) + if (loop.boolValue()) { sendEvent(Looped); play(0); @@ -236,7 +238,7 @@ void LedStripBakeLayer::togglePlayPause() play(); } -bool LedStripBakeLayer::handleCommandInternal(const String& command, var *data, int numData) +bool LedStripBakeLayer::handleCommandInternal(const String &command, var *data, int numData) { if (checkCommand(command, "load", numData, 1)) { diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.h b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.h index d8730a94..b13880d5 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/bake/LedStripBakeLayer.h @@ -3,7 +3,7 @@ class LedStripBakeLayer : public LedStripLayer { public: - LedStripBakeLayer(const String &name, LedStripComponent *strip) : LedStripLayer(name, LedStripLayer::Bake, strip) {} + LedStripBakeLayer(LedStripComponent *strip) : LedStripLayer("bakeLayer", LedStripLayer::Bake, strip) {} ~LedStripBakeLayer() {} File curFile; @@ -11,8 +11,8 @@ class LedStripBakeLayer : public LedStripLayer StaticJsonDocument<200> metaData; int frameSize; - - // info + + // info int fps; float totalTime; long totalFrames; @@ -21,8 +21,8 @@ class LedStripBakeLayer : public LedStripLayer Color groupColor; // control - Parameter * idMode; - Parameter * loopShow; + Parameter idMode{"idMode", false, var(), var(), true}; + Parameter loop{"loop", false, var(), var(), true}; // playing bool isPlaying; @@ -48,7 +48,7 @@ class LedStripBakeLayer : public LedStripLayer void stop(); void togglePlayPause(); - bool handleCommandInternal(const String& command, var *data, int numData) override; + bool handleCommandInternal(const String &command, var *data, int numData) override; // Time computation helpers long msToBytePos(long t) const { return msToFrame(t) * frameSize; } // rgba diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.cpp index 035d1072..eee79f92 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.cpp @@ -1,5 +1,7 @@ bool LedStripScriptLayer::initInternal(JsonObject o) { + LedStripLayer::initInternal(o); + script = new Script(this); return true; } @@ -14,16 +16,16 @@ void LedStripScriptLayer::clearInternal() bool LedStripScriptLayer::handleCommandInternal(const String &command, var *data, int numData) { - if (CheckCommand("load", 1)) - { - script.load(data->stringValue()); - return true; - } - else if (CheckCommand("stop", 0)) - { - script.stop(); - return true; - } + // if (CheckCommand("load", 1)) + // { + // script.load(data->stringValue()); + // return true; + // } + // else if (CheckCommand("stop", 0)) + // { + // script.stop(); + // return true; + // } return false; } \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.h b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.h index cf15b4aa..37753545 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/script/LedStripScriptLayer.h @@ -3,10 +3,10 @@ class LedStripScriptLayer : public LedStripLayer { public: - LedStripScriptLayer(const String &name, LedStripComponent *strip) : LedStripLayer(name, LedStripLayer::ScriptType, strip), script(this) {} + LedStripScriptLayer(LedStripComponent *strip) : LedStripLayer("scriptLayer", LedStripLayer::ScriptType, strip) {} ~LedStripScriptLayer() {} - Script script; + Script* script; bool initInternal(JsonObject o) override; void updateInternal() override; diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.cpp index be5d4611..495d2bd2 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.cpp @@ -2,6 +2,9 @@ ImplementSingleton(LedStreamReceiverComponent); bool LedStripStreamLayer::initInternal(JsonObject o) { + LedStripLayer::initInternal(o); + + delay(100); LedStreamReceiverComponent::instance->registerLayer(this); return true; @@ -9,8 +12,6 @@ bool LedStripStreamLayer::initInternal(JsonObject o) void LedStripStreamLayer::updateInternal() { - if (!enabled->boolValue()) - return; } void LedStripStreamLayer::clearInternal() @@ -23,31 +24,30 @@ void LedStripStreamLayer::clearInternal() bool LedStreamReceiverComponent::initInternal(JsonObject o) { - udpIsInit = false; - receiveRate = addParameter("receiveRate", 50, 1, 200); + serverIsInit = false; + AddAndSetParameter(receiveRate); byteIndex = 0; - useArtnet = addParameter("useArtnet", false); - + AddAndSetParameter(useArtnet); artnet.setArtDmxCallback(&LedStreamReceiverComponent::onDmxFrame); return true; } void LedStreamReceiverComponent::updateInternal() { + if(!serverIsInit) return; - if (!enabled->boolValue() || !udpIsInit) - return; - - if (useArtnet->boolValue()) + if (useArtnet.boolValue()) { + // DBG("Artnet read"); artnet.read(); } else { + // DBG("UDP Read"); long curTime = millis(); - if (curTime > lastReceiveTime + (1000 / receiveRate->intValue())) + if (curTime > lastReceiveTime + (1000 / receiveRate.intValue())) { lastReceiveTime = curTime; receiveUDP(); @@ -107,29 +107,30 @@ void LedStreamReceiverComponent::onEnabledChanged() void LedStreamReceiverComponent::setupConnection() { - bool shouldConnect = enabled->boolValue() && WifiComponent::instance->state == WifiComponent::Connected; + bool shouldConnect = enabled.boolValue() && WifiComponent::instance->state == WifiComponent::Connected; - udpIsInit = false; + serverIsInit = false; if (shouldConnect) { - NDBG("Start Receive Led Stream on UDP " + String(LEDSTREAM_RECEIVE_PORT)); - if (useArtnet->boolValue()) + if (useArtnet.boolValue()) { + NDBG("Start Receive Led Stream on Artnet"); artnet.begin(); } else { - udp.begin(8888); + NDBG("Start Receive Led Stream on UDP " + String(LEDSTREAM_RECEIVE_PORT)); + udp.begin(LEDSTREAM_RECEIVE_PORT); udp.flush(); } - udpIsInit = true; + serverIsInit = true; } else { - NDBG("Stop receiving UDP Led Stream"); + NDBG("Stop receiving"); udp.flush(); udp.stop(); // artnet.stop(); @@ -138,13 +139,13 @@ void LedStreamReceiverComponent::setupConnection() void LedStreamReceiverComponent::onParameterEventInternal(const ParameterEvent &e) { - if (e.parameter == useArtnet) + if (e.parameter == &useArtnet) { - if (enabled->boolValue()) + if (enabled.boolValue()) { NDBG("Start Receive Led Stream on UDP " + String(LEDSTREAM_RECEIVE_PORT)); - if (useArtnet->boolValue()) + if (useArtnet.boolValue()) { udp.flush(); udp.stop(); @@ -179,7 +180,8 @@ void LedStreamReceiverComponent::unregisterLayer(LedStripStreamLayer *layer) void LedStreamReceiverComponent::onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t *data) { - int stripIndex = universe; + // DBG("Received Artnet "+String(universe)); + int stripIndex = universe - 1; if (stripIndex < instance->layers.size()) { diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.h b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.h index 40ecd96e..7200a59a 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/stream/LedStripStreamLayer.h @@ -9,7 +9,7 @@ class LedStripStreamLayer : public LedStripLayer { public: - LedStripStreamLayer(const String &name, LedStripComponent *strip) : LedStripLayer(name, LedStripLayer::Stream, strip) {} + LedStripStreamLayer(LedStripComponent *strip) : LedStripLayer("streamLayer", LedStripLayer::Stream, strip) {} ~LedStripStreamLayer() {} bool initInternal(JsonObject o) override; @@ -20,11 +20,11 @@ class LedStripStreamLayer : public LedStripLayer DeclareComponentSingleton(LedStreamReceiver, "streamReceiver", ) WiFiUDP udp; - bool udpIsInit; -Parameter *receiveRate; +bool serverIsInit; +Parameter receiveRate { "receiveRate", 50, var(), var(), true}; ArtnetWifi artnet; -Parameter *useArtnet; +Parameter useArtnet{"useArtNet", true, var(), var(), true}; bool initInternal(JsonObject o) override; void updateInternal() override; void clearInternal() override; diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.cpp index 4a4f135d..93376899 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.cpp @@ -1,11 +1,12 @@ bool LedStripSystemLayer::initInternal(JsonObject o) { - // blendMode = Alpha; + LedStripLayer::initInternal(o); return true; } void LedStripSystemLayer::updateInternal() { + clearColors(); updateWifiStatus(); updateShutdown(); @@ -34,7 +35,7 @@ void LedStripSystemLayer::updateWifiStatus() float t = (millis() - WifiComponent::instance->timeAtConnect) / 1000.0f; float pos = cos((t + PI) * 5) * .5f + .5f; - if (strip->invertStrip) + if (strip->invertStrip.boolValue()) pos = 1 - pos; float radius = .3 - (cos(pos * PI * 2) * .5f + .5f) * .25f; @@ -90,7 +91,7 @@ void LedStripSystemLayer::updateShutdown() c = c.withMultipliedAlpha(min(t * 2, 1.f)); float end = constrain((1 - t) * 2, 0, 1); - if (strip->invertStrip) + if (strip->invertStrip.boolValue()) fillRange(c, 1-end, 1); else fillRange(c, 0, end); diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.h b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.h index f5f694d8..7118e876 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/Layer/layers/system/LedStripSystemLayer.h @@ -3,7 +3,7 @@ class LedStripSystemLayer : public LedStripLayer { public: - LedStripSystemLayer(const String &name, LedStripComponent *strip) : LedStripLayer(name, LedStripLayer::System, strip) {} + LedStripSystemLayer(LedStripComponent *strip) : LedStripLayer("systemLayer", LedStripLayer::System, strip) {} ~LedStripSystemLayer() {} bool initInternal(JsonObject o) override; diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.cpp b/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.cpp index b9b0e861..c89cddac 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.cpp @@ -6,60 +6,68 @@ bool LedStripComponent::initInternal(JsonObject o) for (int i = 0; i < LEDSTRIP_NUM_USER_LAYERS; i++) userLayers[i] = NULL; - - systemLayer = NULL; - - dataPin = AddConfigParameter("dataPin", 0); - count = AddConfigParameter("count", 0); - enPin = AddConfigParameter("enPin", 0); - clkPin = AddConfigParameter("clkPin", 0); - invertStrip = AddConfigParameter("invertStrip", false); - - if (enPin->intValue() != 0) + +#if USE_BAKELAYER + addComponent(&bakeLayer, o[bakeLayer.name]); + userLayers[0] = &bakeLayer; +#endif +#if USE_STREAMLAYER + addComponent(&streamLayer, o[streamLayer.name]); + userLayers[1] = &streamLayer; +#endif +#if USE_SCRIPTLAYER + addComponent(&scriptLayer, o[scriptLayer.name]); + userLayers[2] = &scriptLayer; +#endif +#if USE_SYSTEMLAYER + addComponent(&systemLayer, o[systemLayer.name]); +#endif + + + AddAndSetParameter(count); + AddAndSetParameter(dataPin); + AddAndSetParameter(enPin); + AddAndSetParameter(clkPin); + AddAndSetParameter(brightness); + AddAndSetParameter(invertStrip); + + if (enPin.intValue() != 0) { - NDBG("Setting Led Enable pin : " + String(enPin->intValue())); - pinMode(enPin->intValue(), OUTPUT); - digitalWrite(enPin->intValue(), HIGH); // enable LEDs + NDBG("Setting Led Enable pin : " + String(enPin.intValue())); + pinMode(enPin.intValue(), OUTPUT); + digitalWrite(enPin.intValue(), HIGH); // enable LEDs } - brightness = addParameter("brightness", .5f, 0, 1); - colors = (Color *)malloc(count->intValue() * sizeof(Color)); - for (int i = 0; i < count->intValue(); i++) - colors[i] = Color(0, 0, 0, 0); - - if (count->intValue() == 0 || dataPin->intValue() == 0) + if (count.intValue() == 0 || dataPin.intValue() == 0) { NDBG("Ledstrip pin and count have not been set"); return false; } - bakeLayer = addComponent(new LedStripBakeLayer("bake", this)); - streamLayer = addComponent(new LedStripStreamLayer("stream", this)); - scriptLayer = addComponent(new LedStripScriptLayer("script", this)); - systemLayer = addComponent(new LedStripSystemLayer("system", this)); - - userLayers[0] = bakeLayer; - userLayers[1] = streamLayer; - userLayers[2] = scriptLayer; + // colors = (Color *)malloc(count.intValue() * sizeof(Color)); + memset(colors, 0, LED_MAX_COUNT * sizeof(Color)); + for (int i = 0; i < count.intValue(); i++) + colors[i] = Color(0, 0, 0, 0); - if (clkPin->intValue() > 0) + if (clkPin.intValue() > 0) { - dotStarStrip = new Adafruit_DotStar(count->intValue(), dataPin->intValue(), clkPin->intValue(), DOTSTAR_BGR); + NDBG("Setup dotstar"); + dotStarStrip = new Adafruit_DotStar(count.intValue(), dataPin.intValue(), clkPin.intValue(), DOTSTAR_BGR); dotStarStrip->begin(); - dotStarStrip->setBrightness(brightness->floatValue() * 255); - // dotStarStrip->fill(dotStarStrip->Color(255, 0, 0)); - // dotStarStrip->show(); - // delay(300); + dotStarStrip->setBrightness(brightness.floatValue() * LED_MAX_BRIGHTNESS * 255); + dotStarStrip->fill(dotStarStrip->Color(50, 100, 150)); + dotStarStrip->show(); + delay(300); } else { - neoPixelStrip = new Adafruit_NeoPixel(count->intValue(), dataPin->intValue(), NEO_GRB + NEO_KHZ800); + neoPixelStrip = new Adafruit_NeoPixel(count.intValue(), dataPin.intValue(), NEO_GRB + NEO_KHZ800); neoPixelStrip->begin(); - neoPixelStrip->setBrightness(brightness->floatValue() * 255); - // neoPixelStrip->fill(neoPixelStrip->Color(255, 0, 0)); - // neoPixelStrip->show(); - // delay(300); + neoPixelStrip->setBrightness(brightness.floatValue() * LED_MAX_BRIGHTNESS * 255); + neoPixelStrip->fill(neoPixelStrip->Color(50, 100, 150)); + neoPixelStrip->show(); + delay(300); } return true; @@ -68,9 +76,9 @@ bool LedStripComponent::initInternal(JsonObject o) void LedStripComponent::updateInternal() { - if(dotStarStrip == NULL && neoPixelStrip == NULL) + if (dotStarStrip == NULL && neoPixelStrip == NULL) { - return; //not active + return; // not active } // all layer's internal colors are updated in Component's update() function @@ -79,7 +87,9 @@ void LedStripComponent::updateInternal() for (int i = 0; i < LEDSTRIP_NUM_USER_LAYERS; i++) processLayer(userLayers[i]); - processLayer(systemLayer); +#if USE_SYSTEMLAYER + processLayer(&systemLayer); +#endif showLeds(); } @@ -100,45 +110,33 @@ void LedStripComponent::clearInternal() void LedStripComponent::onParameterEventInternal(const ParameterEvent &e) { - if (e.parameter == brightness) + if (e.parameter == &brightness) { if (neoPixelStrip != NULL) - neoPixelStrip->setBrightness(brightness->floatValue() * 255); + neoPixelStrip->setBrightness(brightness.floatValue() * 255); else if (dotStarStrip != NULL) - dotStarStrip->setBrightness(brightness->floatValue() * 255); + dotStarStrip->setBrightness(brightness.floatValue() * 255); } - else if (e.parameter == count) + else if (e.parameter == &count) { if (neoPixelStrip != NULL) - neoPixelStrip->updateLength(count->intValue()); + neoPixelStrip->updateLength(count.intValue()); else if (dotStarStrip != NULL) - dotStarStrip->setBrightness(count->intValue()); - - // free(colors); - // colors = (Color *)malloc(count->intValue() * sizeof(Color)); - // for (int i = 0; i < count->intValue(); i++) - // colors[i] = Color(0, 0, 0, 0); - - - // for (int i = 0; i < LEDSTRIP_NUM_USER_LAYERS; i++) - // if (userLayers[i] != NULL) - // userLayers[i]->initColors(); - - // systemLayer->initColors(); + dotStarStrip->setBrightness(count.intValue()); } } void LedStripComponent::onEnabledChanged() { - setStripPower(enabled->boolValue()); + setStripPower(enabled.boolValue()); } void LedStripComponent::setStripPower(bool value) { - if (enPin->intValue() != 0) - digitalWrite(enPin->intValue(), value); // enable LEDs + if (enPin.intValue() != 0) + digitalWrite(enPin.intValue(), value); // enable LEDs - pinMode(dataPin->intValue(), value ? OUTPUT : OPEN_DRAIN); + pinMode(dataPin.intValue(), value ? OUTPUT : OPEN_DRAIN); } // Layer functions @@ -147,14 +145,14 @@ void LedStripComponent::processLayer(LedStripLayer *layer) if (layer == NULL) return; - if (!layer->enabled->boolValue()) + if (!layer->enabled.boolValue()) return; - for (int i = 0; i < count->intValue(); i++) + for (int i = 0; i < count.intValue(); i++) { Color c = layer->colors[i]; - LedStripLayer::BlendMode bm = (LedStripLayer::BlendMode)layer->blendMode->intValue(); + LedStripLayer::BlendMode bm = (LedStripLayer::BlendMode)layer->blendMode.intValue(); switch (bm) { @@ -198,15 +196,15 @@ void LedStripComponent::processLayer(LedStripLayer *layer) void LedStripComponent::clearColors() { - // for(int i=0;iintValue();i++) colors[i] = Color(0,0,0,0); - memset(colors, 0, sizeof(Color) * count->intValue()); + // for(int i=0;iintValue(); i++) + for (int i = 0; i < count.intValue(); i++) { float a = colors[i].a / 255.0f; neoPixelStrip->setPixelColor(ledMap(i), @@ -218,7 +216,7 @@ void LedStripComponent::showLeds() } else if (dotStarStrip != NULL) { - for (int i = 0; i < count->intValue(); i++) + for (int i = 0; i < count.intValue(); i++) { float a = colors[i].a / 255.0f; dotStarStrip->setPixelColor(ledMap(i), @@ -232,5 +230,5 @@ void LedStripComponent::showLeds() int LedStripComponent::ledMap(int index) const { - return invertStrip->intValue() ? count->intValue() - 1 - index : index; + return (invertStrip.intValue() ? count.intValue() - 1 - index : index); } \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.h b/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.h index 34e92ad8..f7fcfa13 100644 --- a/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.h +++ b/Firmware/Bentuino/src/Component/components/ledstrip/LedStripComponent.h @@ -1,50 +1,86 @@ #pragma once #define LEDSTRIP_NUM_USER_LAYERS 3 - -DeclareComponent(LedStrip, "ledstrip", ) - -Parameter * dataPin; -Parameter * count; -Parameter * clkPin; -Parameter * enPin; - -Parameter * brightness; - -//mapping -Parameter * invertStrip; - -Color *colors; - -// user layers, may be more than one later -LedStripBakeLayer *bakeLayer; -LedStripStreamLayer *streamLayer; -LedStripScriptLayer *scriptLayer; - -// on top of all -LedStripSystemLayer *systemLayer; - -LedStripLayer *userLayers[LEDSTRIP_NUM_USER_LAYERS]; - -Adafruit_NeoPixel *neoPixelStrip; -Adafruit_DotStar * dotStarStrip; - -bool initInternal(JsonObject o) override; -void updateInternal() override; -void clearInternal() override; - -void onParameterEventInternal(const ParameterEvent &e) override; -void onEnabledChanged() override; - -void setStripPower(bool value); - -// Layer functions -void processLayer(LedStripLayer *layer); - -// Color functions -void clearColors(); -void showLeds(); - -int ledMap(int index) const; - -EndDeclareComponent \ No newline at end of file +#define USE_BAKELAYER 0 +#define USE_STREAMLAYER 1 +#define USE_SCRIPTLAYER 0 +#define USE_SYSTEMLAYER 1 + +// DeclareComponent(LedStrip, "ledstrip", ) +class LedStripComponent : public Component +{ +public: + LedStripComponent(const String &name = "ledstrip", bool enabled = true) : Component(getTypeString(), enabled) +#if USE_BAKELAYER + , + bakeLayer(this) +#endif +#if USE_STREAMLAYER + , + streamLayer(this) +#endif +#if USE_SCRIPTLAYER + , + scriptLayer(this) +#endif +#if USE_SYSTEMLAYER + , + systemLayer(this) +#endif + { + } + + ~LedStripComponent() {} + virtual String getTypeString() const override { return "ledstrip"; } + + Parameter count{"count", LED_DEFAULT_COUNT, var(), var(), true}; + Parameter dataPin{"dataPin", LED_DEFAULT_DATA_PIN, var(), var(), true}; + Parameter clkPin{"clkPin", LED_DEFAULT_CLK_PIN, var(), var(), true}; + Parameter enPin{"enPin", LED_DEFAULT_EN_PIN, var(), var(), true}; + + Parameter brightness{"brightness", LED_DEFAULT_BRIGHTNESS, 0, 1, true}; + + // mapping + Parameter invertStrip{"invertStrip", LED_DEFAULT_INVERT_DIRECTION, var(), var(), true}; + + + Color colors[LED_MAX_COUNT]; + + // user layers, may be more than one later +#if USE_BAKELAYER + LedStripBakeLayer bakeLayer; +#endif +#if USE_STREAMLAYER + LedStripStreamLayer streamLayer; +#endif +#if USE_SCRIPTLAYER + LedStripScriptLayer scriptLayer; +#endif +#if USE_SYSTEMLAYER + LedStripSystemLayer systemLayer; +#endif + + LedStripLayer *userLayers[LEDSTRIP_NUM_USER_LAYERS]; + + Adafruit_NeoPixel *neoPixelStrip; + Adafruit_DotStar *dotStarStrip; + + bool initInternal(JsonObject o) override; + void updateInternal() override; + void clearInternal() override; + + void onParameterEventInternal(const ParameterEvent &e) override; + void onEnabledChanged() override; + + void setStripPower(bool value); + + // Layer functions + void processLayer(LedStripLayer *layer); + + // Color functions + void clearColors(); + void showLeds(); + + int ledMap(int index) const; + + EndDeclareComponent \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/server/WebServerComponent.cpp b/Firmware/Bentuino/src/Component/components/server/WebServerComponent.cpp index 08c40cd9..7338003c 100644 --- a/Firmware/Bentuino/src/Component/components/server/WebServerComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/server/WebServerComponent.cpp @@ -14,8 +14,6 @@ ImplementSingleton(WebServerComponent) void WebServerComponent::updateInternal() { - if (!enabled->boolValue()) - return; server.handleClient(); } @@ -31,7 +29,7 @@ void WebServerComponent::onEnabledChanged() void WebServerComponent::setupConnection() { - bool shouldConnect = enabled->boolValue() && WifiComponent::instance->state == WifiComponent::Connected; + bool shouldConnect = enabled.boolValue() && WifiComponent::instance->state == WifiComponent::Connected; if (shouldConnect) { diff --git a/Firmware/Bentuino/src/Component/components/servo/ServoComponent.cpp b/Firmware/Bentuino/src/Component/components/servo/ServoComponent.cpp index 6db324a3..57d45c46 100644 --- a/Firmware/Bentuino/src/Component/components/servo/ServoComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/servo/ServoComponent.cpp @@ -1,10 +1,10 @@ bool ServoComponent::initInternal(JsonObject o) { - pin = AddConfigParameter("pin", 0); - if (pin > 0) - servo.attach(pin->intValue()); + AddAndSetParameter(pin); + if (pin.intValue() > 0) + servo.attach(pin.intValue()); - position = AddParameter("position", 0); + AddParameter(position); return true; } @@ -19,14 +19,14 @@ void ServoComponent::clearInternal() void ServoComponent::onParameterEventInternal(const ParameterEvent &e) { - if (e.parameter == pin) + if (e.parameter == &pin) { servo.detach(); - if (pin > 0) - servo.attach(pin->intValue()); + if (pin.intValue() > 0) + servo.attach(pin.intValue()); } - if (e.parameter == position) + if (e.parameter == &position) { - servo.write(position->intValue()); + servo.write(position.intValue()); } } \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/servo/ServoComponent.h b/Firmware/Bentuino/src/Component/components/servo/ServoComponent.h index 2fe468bb..c0aab451 100644 --- a/Firmware/Bentuino/src/Component/components/servo/ServoComponent.h +++ b/Firmware/Bentuino/src/Component/components/servo/ServoComponent.h @@ -1,9 +1,9 @@ #pragma once -DeclareComponent(Servo, "servo",) +DeclareComponent(Servo, "servo", ) -Parameter * pin; -Parameter * position; +Parameter pin{"pin", 0, var(), var(), true}; +Parameter position{"position", 0.f, 0.f, 1.f}; Servo servo; diff --git a/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.cpp b/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.cpp index 5eac6f87..d0c35439 100644 --- a/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.cpp @@ -9,32 +9,31 @@ bool StepperComponent::initInternal(JsonObject o) engine.init(); } - stepPin = AddConfigParameter("stepPin", 0); - dirPin = AddConfigParameter("dirPin", 0); - enPin = AddConfigParameter("enPin", 0); + AddAndSetParameter(stepPin); + AddAndSetParameter(dirPin); + AddAndSetParameter(enPin); - speed = AddConfigParameter("speed", 0); - accel = AddConfigParameter("accel", 0); + AddAndSetParameter(accel); - position = AddParameter("position", 0); - speed = AddParameter("speed", 0); + AddParameter(position); + AddParameter(speed); - if (stepPin->intValue() > 0) + if (stepPin.intValue() > 0) { - stepper = engine.stepperConnectToPin(stepPin->intValue()); + stepper = engine.stepperConnectToPin(stepPin.intValue()); if (stepper) { - stepper->setDirectionPin(dirPin->intValue()); - stepper->setEnablePin(enPin->intValue()); + stepper->setDirectionPin(dirPin.intValue()); + stepper->setEnablePin(enPin.intValue()); stepper->setAutoEnable(true); - stepper->setSpeedInHz(speed->intValue()); // 500 steps/s - stepper->setAcceleration(accel->intValue()); // 100 steps/s² + stepper->setSpeedInHz(speed.intValue()); // 500 steps/s + stepper->setAcceleration(accel.intValue()); // 100 steps/s² stepper->move(1000); } else { - NDBG("Could not connect stepper on stepPin " + stepPin->stringValue()); + NDBG("Could not connect stepper on stepPin " + stepPin.stringValue()); return false; } } @@ -57,8 +56,8 @@ void StepperComponent::clearInternal() void StepperComponent::onParameterEventInternal(const ParameterEvent &e) { - if(e.parameter == position) + if (e.parameter == &position) { - stepper->moveTo(position->intValue()); + stepper->moveTo(position.intValue()); } } \ No newline at end of file diff --git a/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.h b/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.h index a00de80c..2d9c320b 100644 --- a/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.h +++ b/Firmware/Bentuino/src/Component/components/stepper/StepperComponent.h @@ -1,17 +1,17 @@ #pragma once -DeclareComponent(Stepper, "stepper",) +DeclareComponent(Stepper, "stepper", ) -static FastAccelStepperEngine engine; + static FastAccelStepperEngine engine; static bool engineIsInit; FastAccelStepper *stepper = NULL; -Parameter * stepPin; -Parameter * dirPin; -Parameter * enPin; -Parameter * speed; -Parameter * accel; +Parameter stepPin{"stepPin", 0, var(), var(), true}; +Parameter dirPin{"dirPin", 0, var(), var(), true}; +Parameter enPin{"enPin", 0, var(), var(), true}; +Parameter accel{"accel", 0, var(), var(), true}; -Parameter * position; +Parameter speed{"speed", 0.f}; +Parameter position{"position",0.f}; bool initInternal(JsonObject o) override; void updateInternal() override; diff --git a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp index 95ee195d..44780d72 100644 --- a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp @@ -4,8 +4,9 @@ ImplementSingleton(WifiComponent) { state = Off; - ssid = AddConfigParameter("ssid", ""); - pass = AddConfigParameter("pass", ""); + AddAndSetParameter(ssid); + AddAndSetParameter(pass); + AddAndSetParameter(apOnNoWifi); connect(); @@ -92,8 +93,8 @@ void WifiComponent::connect() WiFi.setTxPower(WIFI_POWER_19dBm); #endif - NDBG("Connecting to " + ssid->stringValue() + " : " + pass->stringValue() + "..."); - WiFi.begin(ssid->stringValue().c_str(), pass->stringValue().c_str()); + NDBG("Connecting to " + ssid.stringValue() + " : " + pass.stringValue() + "..."); + WiFi.begin(ssid.stringValue().c_str(), pass.stringValue().c_str()); setState(Connecting); } diff --git a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h index d067ba7c..611bb734 100644 --- a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h +++ b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h @@ -6,35 +6,46 @@ #include #endif -DeclareComponentSingleton(Wifi, "wifi",) - - enum ConnectionState { Off, Connecting, Connected, ConnectionError, Disabled, Hotspot, PingAlive, PingDead, CONNECTION_STATES_MAX }; - const String connectionStateNames[CONNECTION_STATES_MAX] {"Off", "Connecting", "Connected", "ConnectionError", "Disabled", "Hotspot", "PingAlive","PingDead" }; - - const long timeBetweenTries = 500; //ms - const long connectionTimeout = 10000; //ms - long timeAtConnect; - long lastConnectTime; - long timeAtStateChange; - - ConnectionState state; - - Parameter * ssid; - Parameter * pass; - - bool initInternal(JsonObject o) override; - void updateInternal() override; - void clearInternal() override; - - void connect(); - void disconnect(); - - void disable(); - void setState(ConnectionState s); - - String getIP() const; - - DeclareComponentEventTypes(ConnectionStateChanged); - DeclareComponentEventNames("ConnectionStateChanged"); +DeclareComponentSingleton(Wifi, "wifi", ) + + enum ConnectionState { Off, + Connecting, + Connected, + ConnectionError, + Disabled, + Hotspot, + PingAlive, + PingDead, + CONNECTION_STATES_MAX }; + +const String connectionStateNames[CONNECTION_STATES_MAX]{"Off", "Connecting", "Connected", "ConnectionError", "Disabled", "Hotspot", "PingAlive", "PingDead"}; + +const long timeBetweenTries = 500; // ms +const long connectionTimeout = 10000; // ms +long timeAtConnect; +long lastConnectTime; +long timeAtStateChange; + +ConnectionState state; + +Parameter ssid{"ssid", "", var(), var(), true}; +Parameter pass{"pass", "", var(), var(), true}; +Parameter apOnNoWifi{"apOnNoWifi", true, var(), var(), true}; + +bool initInternal(JsonObject o) override; +void updateInternal() override; +void clearInternal() override; + +void connect(); +void disconnect(); +void setAP(); + +void disable(); +void setState(ConnectionState s); + +String getIP() const; + +DeclareComponentEventTypes(ConnectionStateChanged); +DeclareComponentEventNames("ConnectionStateChanged"); EndDeclareComponent \ No newline at end of file diff --git a/Firmware/Bentuino/src/RootComponent.cpp b/Firmware/Bentuino/src/RootComponent.cpp index d2e88501..3a478d15 100644 --- a/Firmware/Bentuino/src/RootComponent.cpp +++ b/Firmware/Bentuino/src/RootComponent.cpp @@ -1,41 +1,35 @@ ImplementSingleton(RootComponent); - bool RootComponent::initInternal(JsonObject) { BoardInit; timeAtShutdown = 0; - // Generate deviceID - byte mac[6]; - WiFi.macAddress(mac); - - deviceID = ""; - for (int i = 0; i < 6; i++) - deviceID += (i > 0 ? "-" : "") + String(mac[i], HEX); - - deviceID.toUpperCase(); - Settings::loadSettings(); JsonObject o = Settings::settings.as(); + + String test; + serializeJson(o, test); + DBG("init settings"); + DBG(test); - AddComponent("comm", comm, Communication, true); - - AddComponent("streamReceiver", streamReceiver, LedStreamReceiver, true); - AddComponent("strip", strip, LedStrip, true); + AddParameter(deviceName); - AddComponent("battery", battery, Battery, true); - AddComponent("sequence", sequence, Sequence, true); + AddOwnedComponent(&comm); - AddComponent("imu", imu, IMU, false); + AddOwnedComponent(&battery); + AddOwnedComponent(&sequence); - AddComponent("wifi", wifi, Wifi, true); - AddComponent("files", files, Files, true); - AddComponent("script", script, Script, true); - AddComponent("server", server, WebServer, true); + AddOwnedComponent(&wifi); + AddOwnedComponent(&files); + AddOwnedComponent(&script); + AddOwnedComponent(&server); + AddOwnedComponent(&streamReceiver); - AddComponent("button", button, Button, false); + AddComponent("strip", strip, LedStrip, false); + // AddComponent("button", button, Button, false); + AddComponent("imu", imu, IMU, false); // for(int i=0;i<16;i++) // { @@ -45,7 +39,6 @@ bool RootComponent::initInternal(JsonObject) // AddComponent("servo", servo, Servo, true); // AddComponent("stepper", stepper, Stepper, true); - return true; } @@ -56,14 +49,14 @@ void RootComponent::updateInternal() void RootComponent::restart() { - // saveSettings(); + // saveSettings(); ESP.restart(); } void RootComponent::shutdown() { - // saveSettings(); + // saveSettings(); timeAtShutdown = millis(); timer.in(1000, [](void *) -> bool @@ -74,7 +67,7 @@ void RootComponent::powerdown() { clear(); - //NDBG("Sleep now, baby."); + // NDBG("Sleep now, baby."); #ifdef WAKEUP_BUTTON esp_sleep_enable_ext0_wakeup((gpio_num_t)WAKEUP_BUTTON, WAKEUP_BUTTON_STATE); @@ -98,12 +91,20 @@ void RootComponent::saveSettings() Settings::saveSettings(); } +void RootComponent::clearSettings() +{ + Settings::clearSettings(); + NDBG("Settings cleared, will reboot now."); + delay(500); + restart(); +} + void RootComponent::onChildComponentEvent(const ComponentEvent &e) { if (isShuttingDown()) return; - if (e.component == comm) + if (e.component == &comm) { if (e.type == CommunicationComponent::MessageReceived) { @@ -121,25 +122,25 @@ void RootComponent::onChildComponentEvent(const ComponentEvent &e) return; } } - else if (e.component == wifi) + else if (e.component == &wifi) { if (e.type == WifiComponent::ConnectionStateChanged) { - comm->osc->setupConnection(); - server->setupConnection(); - // streamReceiver->setupConnection(); + comm.osc.setupConnection(); + server.setupConnection(); + streamReceiver.setupConnection(); } } else if (e.component == button) { - if (button->isSystem->boolValue()) + if (button->isSystem.boolValue()) { switch (e.type) { case ButtonComponent::ShortPress: { - if (wifi->state == WifiComponent::Connecting) - wifi->disable(); + if (wifi.state == WifiComponent::Connecting) + wifi.disable(); } break; @@ -150,7 +151,7 @@ void RootComponent::onChildComponentEvent(const ComponentEvent &e) } } - comm->sendEventFeedback(e); + comm.sendEventFeedback(e); } bool RootComponent::handleCommandInternal(const String &command, var *data, int numData) @@ -161,14 +162,18 @@ bool RootComponent::handleCommandInternal(const String &command, var *data, int restart(); else if (command == "stats") { - DBG("Heap "+String(ESP.getFreeHeap())+" free / "+String(ESP.getHeapSize())+" total"); - DBG("Free Stack size "+String((int)uxTaskGetStackHighWaterMark(NULL))+" free"); - //comm->sendMessage(this, "freeHeap", String(ESP.getFreeHeap()) + " bytes"); + DBG("Heap " + String(ESP.getFreeHeap()) + " free / " + String(ESP.getHeapSize()) + " total"); + DBG("Free Stack size " + String((int)uxTaskGetStackHighWaterMark(NULL)) + " free"); + // comm->sendMessage(this, "freeHeap", String(ESP.getFreeHeap()) + " bytes"); } else if (command == "saveSettings") { saveSettings(); } + else if (command == "factoryReset") + { + clearSettings(); + } else { return false; @@ -176,3 +181,16 @@ bool RootComponent::handleCommandInternal(const String &command, var *data, int return true; } + +String RootComponent::getDeviceID() const +{ + byte mac[6]; + WiFi.macAddress(mac); + + String d = ""; + for (int i = 0; i < 6; i++) + d += (i > 0 ? "-" : "") + String(mac[i], HEX); + + d.toUpperCase(); + return d; +} \ No newline at end of file diff --git a/Firmware/Bentuino/src/RootComponent.h b/Firmware/Bentuino/src/RootComponent.h index 6401e5e6..8f9263d2 100644 --- a/Firmware/Bentuino/src/RootComponent.h +++ b/Firmware/Bentuino/src/RootComponent.h @@ -1,32 +1,34 @@ -//#define NUM_SYSTEM_COMPONENTS 6 +#pragma once DeclareComponentSingleton(Root, "root", ) -String deviceID; +const String deviceID = getDeviceID(); +Parameter deviceName{"deviceName", deviceID, var(), var(), true}; // system -CommunicationComponent * comm; -WifiComponent * wifi; -FilesComponent * files; -WebServerComponent * server; -ScriptComponent *script; - -BatteryComponent * battery; -SequenceComponent * sequence; - -//users -ButtonComponent * button; -IMUComponent * imu; -LedStreamReceiverComponent * streamReceiver; +CommunicationComponent comm; +WifiComponent wifi; +FilesComponent files; +WebServerComponent server; +ScriptComponent script; + +BatteryComponent battery; +SequenceComponent sequence; + +LedStreamReceiverComponent streamReceiver; + +// users LedStripComponent *strip; -ServoComponent * servo; -StepperComponent * stepper; +ButtonComponent *button; +IMUComponent *imu; +ServoComponent *servo; +StepperComponent *stepper; // IOComponent * ioComponents[16]; -//Needs a single structure +// Needs a single structure -//Behaviour +// Behaviour Timer<5> timer; long timeAtShutdown; @@ -39,12 +41,16 @@ void restart(); void powerdown(); void saveSettings(); +void clearSettings(); void onChildComponentEvent(const ComponentEvent &e) override; -bool handleCommandInternal(const String & command, var * data, int numData) override; +bool handleCommandInternal(const String &command, var *data, int numData) override; bool isShuttingDown() const { return timeAtShutdown > 0; } -//const Component *systemComponents[NUM_SYSTEM_COMPONENTS]{comm, wifi, files, server, sequence, battery}; + +String getDeviceID() const; + +// const Component *systemComponents[NUM_SYSTEM_COMPONENTS]{comm, wifi, files, server, sequence, battery}; EndDeclareComponent \ No newline at end of file diff --git a/Firmware/Bentuino/src/main.cpp b/Firmware/Bentuino/src/main.cpp index 9637e661..d65ec3b5 100644 --- a/Firmware/Bentuino/src/main.cpp +++ b/Firmware/Bentuino/src/main.cpp @@ -9,7 +9,10 @@ RootComponent root("root"); void setup() { root.init(); - delay(1000); + + DBG("Device is init, free heap " + String(ESP.getFreeHeap()) + " / " + String(ESP.getMinFreeHeap())); + delay(500); + //s.load("test"); } @@ -19,15 +22,15 @@ void loop() } -#ifndef LED_PIN -#define LED_PIN 13 -#endif +// #ifndef LED_PIN +// #define LED_PIN 13 +// #endif -#define WASM_STACK_SLOTS 1024 -#define NATIVE_STACK_SIZE (32*1024) +// #define WASM_STACK_SLOTS 1024 +// #define NATIVE_STACK_SIZE (32*1024) -// For (most) devices that cannot allocate a 64KiB wasm page -#define WASM_MEMORY_LIMIT 4096 +// // For (most) devices that cannot allocate a 64KiB wasm page +// #define WASM_MEMORY_LIMIT 4096 // unsigned char app_wasm[] = {