From 1d2c5d3b672e3e549d2b1c0a055ff7df32d606e0 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 17 Aug 2021 22:06:13 +0200 Subject: [PATCH] Implemented basic ledstrip --- .gitmodules | 3 ++ components/FastLED-idf | 1 + config_comred.cmake | 2 ++ config_feedc0de.cmake | 3 ++ main/CMakeLists.txt | 5 +-- main/ledstrip.h | 51 +++++++++++++++++++++++++++++ main/main.cpp | 73 +++++++++++++++++++++++++++--------------- sdkconfig_feedc0de | 7 ++++ 8 files changed, 117 insertions(+), 28 deletions(-) create mode 160000 components/FastLED-idf create mode 100644 main/ledstrip.h diff --git a/.gitmodules b/.gitmodules index ece0971..79a6457 100644 --- a/.gitmodules +++ b/.gitmodules @@ -52,3 +52,6 @@ [submodule "components/espasynchttpreq"] path = components/espasynchttpreq url = git@github.com:0xFEEDC0DE64/espasynchttpreq.git +[submodule "components/FastLED-idf"] + path = components/FastLED-idf + url = git@github.com:0xFEEDC0DE64/FastLED-idf.git diff --git a/components/FastLED-idf b/components/FastLED-idf new file mode 160000 index 0000000..151565b --- /dev/null +++ b/components/FastLED-idf @@ -0,0 +1 @@ +Subproject commit 151565bee41b9fb6472e552d9089c3b6428a67e6 diff --git a/config_comred.cmake b/config_comred.cmake index ecec0de..356ea46 100644 --- a/config_comred.cmake +++ b/config_comred.cmake @@ -84,4 +84,6 @@ set(BOBBYCAR_BUILDFLAGS # -DFEATURE_GARAGE # -DFEATURE_NTP -DFEATURE_WIRELESS_CONFIG +# -DFEATURE_LEDSTRIP +# -DPINS_LEDSTRIP=26 ) diff --git a/config_feedc0de.cmake b/config_feedc0de.cmake index 78e7524..2a8c0e6 100644 --- a/config_feedc0de.cmake +++ b/config_feedc0de.cmake @@ -84,4 +84,7 @@ set(BOBBYCAR_BUILDFLAGS -DFEATURE_GARAGE -DFEATURE_NTP # -DFEATURE_WIRELESS_CONFIG + -DFEATURE_LEDSTRIP + -DPINS_LEDSTRIP=26 + -DLEDSTRIP_LENGTH=95 ) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 824cb1a..91f4c3a 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -126,6 +126,7 @@ set(headers icons/unchecked.h icons/update.h icons/wifi.h + ledstrip.h menudisplay.h menuitem.h modes/defaultmode.h @@ -185,9 +186,9 @@ set(sources set(dependencies libsodium freertos nvs_flash esp_http_server esp_https_ota mdns app_update esp_system esp_websocket_client driver - arduino-esp32 ArduinoJson esp-nimble-cpp + arduino-esp32 ArduinoJson esp-nimble-cpp FastLED-idf TFT_eSPI bobbycar-protocol cpputils cxx-ring-buffer date - espasynchttpreq espasyncota espchrono espcpputils esphttpdutils espwifistack expected fmt TFT_eSPI + espasynchttpreq espasyncota espchrono espcpputils esphttpdutils espwifistack expected fmt ) idf_component_register( diff --git a/main/ledstrip.h b/main/ledstrip.h new file mode 100644 index 0000000..791f3ce --- /dev/null +++ b/main/ledstrip.h @@ -0,0 +1,51 @@ +#pragma once + +#ifdef FEATURE_LEDSTRIP +// 3rdparty lib includes +#include + +// local includes +#include "globals.h" + +namespace { +std::array leds; +uint8_t gHue = 0; + +void initLedStrip() +{ + FastLED.addLeds(std::begin(leds), leds.size()) + .setCorrection(TypicalSMD5050); +} + +void updateLedStrip() +{ + EVERY_N_MILLISECONDS( 20 ) { gHue++; } + + if (brems && *brems > 50.f) + { + auto color = avgSpeedKmh < -0.1f ? CRGB{255, 255, 255} : CRGB{255, 0, 0}; + constexpr auto kleinerOffset = 4; + constexpr auto grosserOffset = 10; + + const auto center = std::begin(leds) + (leds.size() / 2) + 1; + + std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); + std::fill(center - grosserOffset, center - kleinerOffset, color); + std::fill(center + kleinerOffset, center + grosserOffset, color); + } + else + { + fadeToBlackBy(std::begin(leds), leds.size(), 20); + + uint8_t dothue = 0; + for (int i = 0; i < 8; i++) + { + leds[beatsin16(i + 7, 0, leds.size())] |= CHSV(dothue, 200, 255); + dothue += 32; + } + } + + FastLED.show(); +} +} // namespace +#endif diff --git a/main/main.cpp b/main/main.cpp index 770a915..2868f92 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -134,6 +134,9 @@ using namespace std::chrono_literals; #endif #include "wifi_bobbycar.h" #include "time_bobbycar.h" +#ifdef FEATURE_LEDSTRIP +#include "ledstrip.h" +#endif namespace { std::optional lastWifiUpdate; @@ -154,6 +157,9 @@ std::optional lastCloudUpdate; #ifdef FEATURE_NTP std::optional lastNtpUpdate; #endif +#ifdef FEATURE_LEDSTRIP +std::optional lastLedstripUpdate; +#endif } extern "C" void app_main() @@ -173,6 +179,32 @@ extern "C" void app_main() initScreen(); + bootLabel.redraw("settings"); + settings = presets::defaultSettings; + stringSettings = presets::makeDefaultStringSettings(); + + if (settingsPersister.init()) + { + if (!settingsPersister.openCommon()) + ESP_LOGE("BOBBY", "openCommon() failed"); + + if (!settingsPersister.openProfile(0)) + ESP_LOGE("BOBBY", "openProfile(0) failed"); + + loadSettings(); + } + else + ESP_LOGE("BOBBY", "init() failed"); + + bootLabel.redraw("deviceName"); + if (const auto result = wifi_stack::get_default_mac_addr()) + std::sprintf(deviceName, STRING(DEVICE_PREFIX) "_%02hhx%02hhx%02hhx", result->at(3), result->at(4), result->at(5)); + else + ESP_LOGE("MAIN", "get_default_mac_addr() failed: %.*s", result.error().size(), result.error().data()); + + bootLabel.redraw("wifi"); + wifi_begin(); + #ifdef FEATURE_DPAD bootLabel.redraw("dpad"); dpad::init(); @@ -204,32 +236,6 @@ extern "C" void app_main() digitalWrite(PINS_MOSFET2, LOW); #endif - bootLabel.redraw("settings"); - settings = presets::defaultSettings; - stringSettings = presets::makeDefaultStringSettings(); - - if (settingsPersister.init()) - { - if (!settingsPersister.openCommon()) - ESP_LOGE("BOBBY", "openCommon() failed"); - - if (!settingsPersister.openProfile(0)) - ESP_LOGE("BOBBY", "openProfile(0) failed"); - - loadSettings(); - } - else - ESP_LOGE("BOBBY", "init() failed"); - - bootLabel.redraw("deviceName"); - if (const auto result = wifi_stack::get_default_mac_addr()) - std::sprintf(deviceName, STRING(DEVICE_PREFIX) "_%02hhx%02hhx%02hhx", result->at(3), result->at(4), result->at(5)); - else - ESP_LOGE("MAIN", "get_default_mac_addr() failed: %.*s", result.error().size(), result.error().data()); - - bootLabel.redraw("wifi"); - wifi_begin(); - #ifdef FEATURE_SERIAL bootLabel.redraw("swap front back"); updateSwapFrontBack(); @@ -256,6 +262,7 @@ extern "C" void app_main() #endif #ifdef FEATURE_CAN + bootLabel.redraw("can"); can::initCan(); #endif @@ -267,6 +274,11 @@ extern "C" void app_main() controllers.back.serial.get().begin(38400, SERIAL_8N1, PINS_RX2, PINS_TX2); #endif +#ifdef FEATURE_LEDSTRIP + bootLabel.redraw("LED strip"); + initLedStrip(); +#endif + raw_gas = std::nullopt; raw_brems = std::nullopt; gas = std::nullopt; @@ -451,5 +463,14 @@ extern "C" void app_main() #ifdef FEATURE_BMS bms::update(); #endif + +#ifdef FEATURE_LEDSTRIP + if (!lastLedstripUpdate || now - *lastLedstripUpdate >= 1000ms / 60) + { + updateLedStrip(); + + lastLedstripUpdate = now; + } +#endif } } diff --git a/sdkconfig_feedc0de b/sdkconfig_feedc0de index df0e790..bb5cb92 100644 --- a/sdkconfig_feedc0de +++ b/sdkconfig_feedc0de @@ -127,6 +127,13 @@ CONFIG_PARTITION_TABLE_OFFSET=0xf000 CONFIG_PARTITION_TABLE_MD5=y # end of Partition Table +# +# FastLED +# +# CONFIG_FASTLED_METHOD_I2S is not set +CONFIG_FASTLED_METHOD_RMT=y +# end of FastLED + # # Arduino Configuration #