Skip to content

Commit

Permalink
Move espTools functions to SysModSystem and use stack size functions
Browse files Browse the repository at this point in the history
General: remove static

LedLeds: remove rotate (as PanTiltRol takes over)

SysModSystem:
- Move espTools functions to SysModSystem
- stack vars: use sysTools_get_arduino_maxStackUsage and sysTools_get_webserver_maxStackUsage
  • Loading branch information
ewowi committed Mar 31, 2024
1 parent a1d9bf6 commit 5846013
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 347 deletions.
6 changes: 1 addition & 5 deletions src/App/LedLeds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ void fastled_fill_rainbow(struct CRGB * targetArray, int numToFill, unsigned8 in
}

unsigned16 Leds::XYZ(unsigned16 x, unsigned16 y, unsigned16 z) {
if (projectionNr == p_Rotate) {
Coord3D result = spinXY(x, y, size.x, size.y, proRollSpeed);
return result.x + result.y * size.x + result.z * size.x * size.y;
}
else if (projectionNr == p_PanTiltRoll || projectionNr == p_Preset1) {
if (projectionNr == p_PanTiltRoll || projectionNr == p_Preset1) {
Coord3D result = Coord3D{x, y, z};
if (proPanSpeed) result = trigoPanTiltRoll.pan(result, size/2, millis() * 5 / (255 - proPanSpeed));
if (proTiltSpeed) result = trigoPanTiltRoll.tilt(result, size/2, millis() * 5 / (255 - proTiltSpeed));
Expand Down
39 changes: 0 additions & 39 deletions src/App/LedLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ enum Projections
{
p_Default,
p_Multiply,
p_Rotate,
p_PanTiltRoll,
p_DistanceFromPoint,
p_Preset1,
Expand Down Expand Up @@ -163,44 +162,6 @@ class SharedData {

};

static float sinrot = 0.0f;
static float cosrot = 1.0f;
static unsigned long last_millis = UINT_MAX;

constexpr float projScaleMax = 1.0f; // full size
constexpr float projScaleMin = 0.701f; // 1/sqrt(2)
static float projScale = projScaleMax;

static Coord3D spinXY(uint_fast16_t x, uint_fast16_t y, uint_fast16_t width, uint_fast16_t height, unsigned8 speed) {
if ((millis()/12) != last_millis) {
// update sin / cos for rotation - once each 12ms
float now = float(millis()/12) / (255 - speed); // this sets the rotation speed
//float now = float(strip.now) / 2000.0f; // error: 'strip' was not declared in this scope
sinrot = sinf(now);
cosrot = cosf(now);
last_millis = millis()/12;
// scale to fit - comment out the next lines to disable
// float maxProj = max(abs(width/2 * sinrot), abs(height/2 * cosrot));
// int maxdim = max(width/2, height/2);
// float newScaling = maxProj / float(maxdim);
// projScale = max(min(newScaling, projScaleMax), projScaleMin);
}
// center
int x1 = int(x) - width/2;
int y1 = int(y) - height/2;
// matrix mult for rotation
float x2 = float(x1) * cosrot - float(y1) * sinrot;
float y2 = float(x1) * sinrot + float(y1) * cosrot;
// un-center
int x3 = lround(x2 * projScale) + width/2; // projScale adds some down-scaling,
int y3 = lround(y2 * projScale) + height/2; // so everything fits fully into the original matrix. Note to self: this is still sub-optimal.
// check bounds

if ((x3 <0) || (x3 >= width) || (y3 <0) || (y3 >= height)) return Coord3D{0, 0, 0}; // outside of matrix
// deliver fish
else return Coord3D{(unsigned16)x3, (unsigned16)y3, 0};
}

struct PhysMap {
// bool isPhys = false; // 1 byte
// union {
Expand Down
5 changes: 2 additions & 3 deletions src/App/LedModEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class LedModEffects:public SysModule {
JsonArray options = ui->setOptions(var); // see enum Projections in LedFixture.h and keep the same order !
options.add("Default");
options.add("Multiply");
options.add("Rotate");
options.add("PanTiltRoll");
options.add("Distance ⌛");
options.add("Preset 1");
Expand Down Expand Up @@ -204,7 +203,7 @@ class LedModEffects:public SysModule {
default: return false;
}});
}
if (proValue == p_Rotate || proValue == p_Preset1 || proValue == p_PanTiltRoll) {
if (proValue == p_Preset1 || proValue == p_PanTiltRoll) {
ui->initSlider(var, "proRoll", 128, 0, 254, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
ui->setLabel(var, "Roll speed");
Expand Down Expand Up @@ -364,7 +363,7 @@ class LedModEffects:public SysModule {
mdl->getValueRowNr = rowNr++;
effects.loop(*leds);
mdl->getValueRowNr = UINT8_MAX;
if (leds->projectionNr == p_PanTiltRoll || leds->projectionNr == p_Rotate || leds->projectionNr == p_Preset1)
if (leds->projectionNr == p_PanTiltRoll || leds->projectionNr == p_Preset1)
leds->fadeToBlackBy(50);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/Sys/SysModNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class SysModNetwork:public SysModule {
bool isConfirmedConnection = false;
DNSServer dnsServer;
byte stacO = 0; //stationCount

//init static variables (https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm)
};

extern SysModNetwork *net;
6 changes: 3 additions & 3 deletions src/Sys/SysModPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "SysModUI.h"
#include "SysModModel.h"
#include "SysModWeb.h"
#include "esp32Tools.h"
#include "SysModSystem.h"

SysModPrint::SysModPrint() :SysModule("Print") {

Expand Down Expand Up @@ -42,9 +42,9 @@ SysModPrint::SysModPrint() :SysModule("Print") {
delay(3000); // this extra delay avoids repeating disconnects on -s2 "Disconnected (ClearCommError failed"
Serial.println(" **** COMMODORE BASIC V2 **** ");
#endif
if (!sysTools_normal_startup() && Serial) { // only print if Serial is connected, and startup was not normal
if (!sys->sysTools_normal_startup() && Serial) { // only print if Serial is connected, and startup was not normal
Serial.print("\nWARNING - possible crash: ");
Serial.println(sysTools_getRestartReason());
Serial.println(sys->sysTools_getRestartReason());
Serial.println("");
}
Serial.println("Ready.\n");
Expand Down
194 changes: 184 additions & 10 deletions src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@
#include "SysModUI.h"
#include "SysModWeb.h"
#include "SysModModel.h"
#include "esp32Tools.h"
#include "SysModWorkFlow.h"

// #include <Esp.h>
#include <rom/rtc.h>

// get the right RTC.H for each MCU
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
#if CONFIG_IDF_TARGET_ESP32S2
#include <esp32s2/rom/rtc.h>
#elif CONFIG_IDF_TARGET_ESP32C3
#include <esp32c3/rom/rtc.h>
#elif CONFIG_IDF_TARGET_ESP32S3
#include <esp32s3/rom/rtc.h>
#elif CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
#include <esp32/rom/rtc.h>
#endif
#else // ESP32 Before IDF 4.0
#include <rom/rtc.h>
#endif

SysModSystem::SysModSystem() :SysModule("System") {};

Expand Down Expand Up @@ -90,28 +103,28 @@ void SysModSystem::setup() {
}});
}

ui->initProgress(parentVar, "mainStack", UINT16_MAX, 0, getArduinoLoopTaskStackSize(), true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
ui->initProgress(parentVar, "mainStack", UINT16_MAX, 0, getArduinoLoopTaskStackSize(), true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, uxTaskGetStackHighWaterMark(NULL));
mdl->setValue(var, sysTools_get_arduino_maxStackUsage());
return true;
case f_UIFun:
ui->setLabel(var, "Main stack");
return true;
case f_ChangeFun:
web->addResponseV(var["id"], "comment", "%d of %d B", uxTaskGetStackHighWaterMark(NULL), getArduinoLoopTaskStackSize());
web->addResponseV(var["id"], "comment", "%d of %d B", sysTools_get_arduino_maxStackUsage(), getArduinoLoopTaskStackSize());
return true;
default: return false;
}});

ui->initProgress(parentVar, "tcpStack", UINT16_MAX, 0, CONFIG_ASYNC_TCP_TASK_STACK_SIZE, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
ui->initProgress(parentVar, "tcpStack", UINT16_MAX, 0, CONFIG_ASYNC_TCP_TASK_STACK_SIZE, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, uxTaskGetStackHighWaterMark(xTaskGetHandle("async_tcp")));
mdl->setValue(var, sysTools_get_webserver_maxStackUsage());
return true;
case f_UIFun:
ui->setLabel(var, "TCP stack");
return true;
case f_ChangeFun:
web->addResponseV(var["id"], "comment", "%d of %d B", uxTaskGetStackHighWaterMark(xTaskGetHandle("async_tcp")), CONFIG_ASYNC_TCP_TASK_STACK_SIZE);
web->addResponseV(var["id"], "comment", "%d of %d B", sysTools_get_webserver_maxStackUsage(), CONFIG_ASYNC_TCP_TASK_STACK_SIZE);
return true;
default: return false;
}});
Expand Down Expand Up @@ -166,7 +179,7 @@ void SysModSystem::setup() {
default: return false;
}});

// static char msgbuf[32];
// char msgbuf[32];
// snprintf(msgbuf, sizeof(msgbuf)-1, "%s rev.%d", ESP.getChipModel(), ESP.getChipRevision());
// ui->initText(parentVar, "e32model")] = msgbuf;
// ui->initText(parentVar, "e32cores")] = ESP.getChipCores();
Expand Down Expand Up @@ -248,4 +261,165 @@ void SysModSystem::addRestartReasonsSelect(JsonArray options) {
options.add(sysTools_restart2String( 8)); ////!< Reset after exiting deep sleep mode
options.add(sysTools_restart2String( 9)); // //!< Brownout reset (software or hardware)
options.add(sysTools_restart2String(10)); // //!< Reset over SDIO
}
}

//from esptools.h - public

// check if estart was "normal"
bool SysModSystem::sysTools_normal_startup() {
esp_reset_reason_t restartCode = getRestartReason();
if ((restartCode == ESP_RST_POWERON) || (restartCode == ESP_RST_SW)) return true; // poweron or esp_restart()
return false;
}

// RESTART reason as long string
String SysModSystem::sysTools_getRestartReason() {
esp_reset_reason_t restartCode = getRestartReason();
String reasonText = restartCode2InfoLong(restartCode);
String longText = String("(code ") + String((int)restartCode) + String( ") ") + reasonText;

int core0code = getCoreResetReason(0);
int core1code = getCoreResetReason(1);
longText = longText + ". Core#0 (code " + String(core0code) + ") " + resetCode2Info(core0code);
if (core1code > 0)
longText = longText + "; Core#1 (code " + String(core1code) + ") " + resetCode2Info(core1code);

longText = longText + ".";
return longText;
}

// helper for SysModSystem::addRestartReasonsSelect. Returns "(#) ReasonText"
String SysModSystem::sysTools_restart2String(int reasoncode) {
esp_reset_reason_t restartCode = esp_reset_reason_t(reasoncode); // its a trick, not a sony ;-)
String longText = String("(") + String(reasoncode) + String( ") ") + restartCode2Info(restartCode);
return longText;
}

// helper for SysModSystem::addResetReasonsSelect. Returns "CoreResetReasonText (#)"
String SysModSystem::sysTools_reset2String(int resetCode) {
String longText = resetCode2Info(resetCode) + String(" (") + String(resetCode) + String(")");
return longText;
}

int SysModSystem::sysTools_get_arduino_maxStackUsage(void) {
char * loop_taskname = pcTaskGetTaskName(loop_taskHandle); // ask for name of the known task (to make sure we are still looking at the right one)

if ((loop_taskHandle == NULL) || (loop_taskname == NULL) || (strncmp(loop_taskname, "loopTask", 8) != 0)) {
loop_taskHandle = xTaskGetHandle("loopTask"); // need to look for the task by name. FreeRTOS docs say this is very slow, so we store the result for next time
}

if (loop_taskHandle != NULL) return uxTaskGetStackHighWaterMark(loop_taskHandle); // got it !!
else return -1;
}

int SysModSystem::sysTools_get_webserver_maxStackUsage(void) {
char * tcp_taskname = pcTaskGetTaskName(tcp_taskHandle); // ask for name of the known task (to make sure we are still looking at the right one)

if ((tcp_taskHandle == NULL) || (tcp_taskname == NULL) || (strncmp(tcp_taskname, "async_tcp", 9) != 0)) {
tcp_taskHandle = xTaskGetHandle("async_tcp"); // need to look for the task by name. FreeRTOS docs say this is very slow, so we store the result for next time
}

if (tcp_taskHandle != NULL) return uxTaskGetStackHighWaterMark(tcp_taskHandle); // got it !!
else return -1;
}


//from esptools.h - private

// helper fuctions
int SysModSystem::getCoreResetReason(int core) {
if (core >= ESP.getChipCores()) return 0;
return((int)rtc_get_reset_reason(core));
}

String SysModSystem::resetCode2Info(int reason) {
switch(reason) {

case 1 : // 1 = Vbat power on reset
return "power-on"; break;
case 2 : // 2 = this code is not defined on ESP32
return "exception"; break;
case 3 : // 3 = Software reset digital core
return "SW reset"; break;
case 12: //12 = Software reset CPU
return "SW restart"; break;
case 5 : // 5 = Deep Sleep wakeup reset digital core
return "wakeup"; break;
case 14: //14 = for APP CPU, reset by PRO CPU
return "restart"; break;
case 15: //15 = Reset when the vdd voltage is not stable (brownout)
return "brown-out"; break;

// watchdog resets
case 4 : // 4 = Legacy watch dog reset digital core
case 6 : // 6 = Reset by SLC module, reset digital core
case 7 : // 7 = Timer Group0 Watch dog reset digital core
case 8 : // 8 = Timer Group1 Watch dog reset digital core
case 9 : // 9 = RTC Watch dog Reset digital core
case 11: //11 = Time Group watchdog reset CPU
case 13: //13 = RTC Watch dog Reset CPU
case 16: //16 = RTC Watch dog reset digital core and rtc module
case 17: //17 = Time Group1 reset CPU
return "watchdog"; break;
case 18: //18 = super watchdog reset digital core and rtc module
return "super watchdog"; break;

// misc
case 10: // 10 = Instrusion tested to reset CPU
return "intrusion"; break;
case 19: //19 = glitch reset digital core and rtc module
return "glitch"; break;
case 20: //20 = efuse reset digital core
return "EFUSE reset"; break;
case 21: //21 = usb uart reset digital core
return "USB UART reset"; break;
case 22: //22 = usb jtag reset digital core
return "JTAG reset"; break;
case 23: //23 = power glitch reset digital core and rtc module
return "power glitch"; break;

// unknown reason code
case 0:
return "none"; break;
default:
return "unknown"; break;
}
}

esp_reset_reason_t SysModSystem::getRestartReason() {
return(esp_reset_reason());
}
String SysModSystem::restartCode2InfoLong(esp_reset_reason_t reason) {
switch (reason) {
case ESP_RST_UNKNOWN: return("Reset reason can not be determined"); break;
case ESP_RST_POWERON: return("Restart due to power-on event"); break;
case ESP_RST_EXT: return("Reset by external pin (not applicable for ESP32)"); break;
case ESP_RST_SW: return("Software restart via esp_restart()"); break;
case ESP_RST_PANIC: return("Software reset due to panic or unhandled exception (SW error)"); break;
case ESP_RST_INT_WDT: return("Reset (software or hardware) due to interrupt watchdog"); break;
case ESP_RST_TASK_WDT: return("Reset due to task watchdog"); break;
case ESP_RST_WDT: return("Reset due to other watchdogs"); break;
case ESP_RST_DEEPSLEEP:return("Restart after exiting deep sleep mode"); break;
case ESP_RST_BROWNOUT: return("Brownout Reset (software or hardware)"); break;
case ESP_RST_SDIO: return("Reset over SDIO"); break;
}
return("unknown");
}

String SysModSystem::restartCode2Info(esp_reset_reason_t reason) {
switch (reason) {
case ESP_RST_UNKNOWN: return("unknown reason"); break;
case ESP_RST_POWERON: return("power-on event"); break;
case ESP_RST_EXT: return("external pin reset"); break;
case ESP_RST_SW: return("SW restart by esp_restart()"); break;
case ESP_RST_PANIC: return("SW error - panic or exception"); break;
case ESP_RST_INT_WDT: return("interrupt watchdog"); break;
case ESP_RST_TASK_WDT: return("task watchdog"); break;
case ESP_RST_WDT: return("other watchdog"); break;
case ESP_RST_DEEPSLEEP:return("exit from deep sleep"); break;
case ESP_RST_BROWNOUT: return("Brownout Reset"); break;
case ESP_RST_SDIO: return("Reset over SDIO"); break;
}
return("unknown");
}

17 changes: 17 additions & 0 deletions src/Sys/SysModSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@ class SysModSystem:public SysModule {
void loop1s();
void loop10s();

//from esp32Tools
bool sysTools_normal_startup(void); // FALSE if unusual startup code --> use next function to get more info
String sysTools_getRestartReason(void); // long string including restart codes from system, Core#0 and Core#1 (if availeable)
String sysTools_restart2String(int reasoncode); // helper for SysModSystem::addRestartReasonsSelect. Returns "(#) ReasonText"
String sysTools_reset2String(int resetCode); // helper for SysModSystem::addResetReasonsSelect. Returns "shortResetReasonText (#)"
int sysTools_get_arduino_maxStackUsage(void); // to query max used stack of the arduino task. returns "-1" if unknown
int sysTools_get_webserver_maxStackUsage(void); // to query max used stack of the webserver task. returns "-1" if unknown

private:
unsigned long loopCounter = 0;

void addResetReasonsSelect(JsonArray select);
void addRestartReasonsSelect(JsonArray select);

// from esp32Tools: helper fuctions
int getCoreResetReason(int core);
String resetCode2Info(int reason);
esp_reset_reason_t getRestartReason();
String restartCode2InfoLong(esp_reset_reason_t reason);
String restartCode2Info(esp_reset_reason_t reason);

TaskHandle_t loop_taskHandle = NULL; // to store the task handle for later calls
TaskHandle_t tcp_taskHandle = NULL; // to store the task handle for later calls

};

extern SysModSystem *sys;
Loading

0 comments on commit 5846013

Please sign in to comment.