Skip to content

Commit

Permalink
ELS latest, add pub sub, add presets, StarString, cpuTime
Browse files Browse the repository at this point in the history
pio.ini
- ESPLiveScript v3.1

SysModInstances
- dash vars: use publish

SysModModel
- Coord3D constructors
- add publish and subscribe
- add VarFunction and VarEvent(s)PS (for pubsub)
- add presets JSON
- triggerEvent: add publish
- constructor: read presets.json
- UI show eventsVar and eventsPS (devMode)
- doWriteModel: save presets

SysModNetwork
- add presets (WIP)

SysModUI
- add VCR (WIP)

SysModule
- add StarString
- add cpuTime
- addPresets function (preset, assign preset , clear preset

SysModules
- show cpuTime using cycleCount (WIP)

UserModLive
- sync/preKill/postKill as static class Vars (sync <- show(M))
- add syncWithSync (using waitingOnLiveScript), to be used by other modules to sync in
- cleanup show->sync
- remove resetStat
- rename fps1/2 to fpsCycles and fpsSync (to be removed)
- add error (WIP)
- remove show and resetStat as external funs
  • Loading branch information
ewowi committed Dec 8, 2024
1 parent 7d96011 commit 8823da5
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 126 deletions.
2 changes: 1 addition & 1 deletion misc/misc.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Release steps
- update dates in files
- check all lib_deps on right version
- clean and build to check latest versions (before github actions fail on this)
- clean and build to check latest versions (before github actions fail on this) - (remove .pio and rebuild)

WebHook to MM discord
https://discord.com/api/webhooks/1229821142479536179/UeO3ryPqUyHABYTAuGtHZK6t7yghM0ZETN0LUYpg32KBleGhm-zvaYDzkyYjiqaVqt0T/github
Expand Down
4 changes: 2 additions & 2 deletions misc/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@
"fun": 74
},
{
"id": "fps1",
"id": "fpsCycles",
"type": "text",
"pid": "LiveScripts",
"ro": true,
Expand All @@ -961,7 +961,7 @@
"value": "0 /s"
},
{
"id": "fps2",
"id": "fpsFrame",
"type": "text",
"pid": "LiveScripts",
"ro": true,
Expand Down
7 changes: 4 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ build_flags =
-D STARBASE_USERMOD_LIVE
-D EXTPRINTF=ppf ;redirect Live Script prints to StarBase print
lib_deps =
https://github.com/ewowi/ESPLiveScript.git#main ;1.2.0. ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates
https://github.com/ewowi/ESPLiveScript.git#d62bf25 ; v3.1 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates

[STARBASE]
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24112412 ; Date and time (GMT!), update at every commit!!
-D VERSION=24120809 ; Date and time (GMT!), update at every commit!!
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
-mtext-section-literals ;otherwise [UserModLive::setup()]+0xa17): dangerous relocation: l32r: literal target out of range (try using text-section-literals)
Expand Down Expand Up @@ -156,7 +156,8 @@ lib_deps =

; https://github.com/platformio/platform-espressif32/issues/1360
; https://community.platformio.org/t/support-esp32-wrover-module/17717
; note: flasghing to new board goes wrong, try first without ICVD then with and without etc until it works (witchcraft)
; note: flashing to new board goes wrong, try first without ICVD then with and without etc until it works (witchcraft)
; Guru Meditation Error: Core 0 panic'ed (Cache disabled but cached memory region accessed).
[env:esp-wrover-kit]
board = esp-wrover-kit ; esp-wrover-kit ;https://github.com/platformio/platform-espressif32/blob/develop/boards/esp-wrover-kit.json
; recommended to pin to a platform version, see https://github.com/platformio/platform-espressif32/releases
Expand Down
5 changes: 4 additions & 1 deletion src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ class SysModInstances:public SysModule {
return true;
case onUI:
// call onUI of the base variable for the new variable
mdl->varEvents[variable.var["fun"]](insVariable, rowNr, onUI);
if (variable.var["fun"].as<uint8_t>() != UINT8_MAX)
mdl->varEvents[variable.var["fun"]](insVariable, rowNr, onUI);
else
insVariable.publish(onUI, rowNr); //is insVariable subscribed ???
return true;
case onChange: {
//do not set this initially!!!
Expand Down
56 changes: 50 additions & 6 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
{
if (rowNr != UINT8_MAX) {
if (childVariable.order() < 0) { //if not updated
valArray[rowNr] = (char*)0; // set element in valArray to 0
valArray[rowNr] = (char*)0; // set element in valArray to 0 (is content deleted from memory?)

ppf("varPostDetails %s.%s[%d] <- null\n", id(), childVariable.id(), rowNr);
// setValue(var, -99, rowNr); //set value -99
Expand Down Expand Up @@ -247,10 +247,13 @@
bool result = false;

//call varEvent if exists
if (!var["fun"].isNull()) {//isNull needed here!
if (!var["fun"].isNull()) { //isNull needed here!
size_t funNr = var["fun"];
if (funNr < mdl->varEvents.size()) {
// ppf("voor v1 call %s.%s[%d] %d %d %d\n", pid(), id(), rowNr, funNr, eventType, mdl->varEvents.size());
result = mdl->varEvents[funNr](*this, rowNr, eventType);

//all ppf here:
if (result && !readOnly()) { //send rowNr = 0 if no rowNr
//only print vars with a value and not onSetValue as that changes a lot due to instances clients etc (tbd)
//don't print if onSetValue or oldValue is null
Expand All @@ -269,10 +272,13 @@
}
} //varEvent exists
}
else
else if (funNr == UINT8_MAX)
result = publish(eventType, rowNr);
else
ppf("dev triggerEvent function nr %s.%s outside bounds %d >= %d\n", pid(), id(), funNr, mdl->varEvents.size());
} //varEvent exists


//delete pointers after calling var.onDelete as var.onDelete might need the values
if (eventType == onAdd || eventType == onDelete) {

Expand Down Expand Up @@ -486,7 +492,7 @@
//sets the default values, by varEvent if exists, otherwise manually (by returning true)
if (doSetValue) {
bool onSetValueExists = false;
if (!var["fun"].isNull()) {
if (!var["fun"].isNull()) { // && var["fun"] != UINT8_MAX
onSetValueExists = triggerEvent(onSetValue, mdl->setValueRowNr);
}
if (!onSetValueExists) { //setValue provided (if not null)
Expand Down Expand Up @@ -516,6 +522,7 @@

SysModModel::SysModModel() :SysModule("Model") {
model = new JsonDocument(&allocator);
presets = new JsonDocument(&allocator);

JsonArray root = model->to<JsonArray>(); //create

Expand All @@ -526,6 +533,9 @@ SysModModel::SysModModel() :SysModule("Model") {
} else {
root = model->to<JsonArray>(); //re create the model as it is corrupted by readFromFile
}

files->readObjectFromFile("/presets.json", presets); //do not create if not exists

}

void SysModModel::setup() {
Expand Down Expand Up @@ -565,6 +575,16 @@ void SysModModel::setup() {
default: return false;
}});

Variable currentVar;
currentVar = ui->initText(parentVar, "eventsVar", nullptr, 16, true);
currentVar.subscribe(onLoop1s, [this](EventArguments) {
variable.setValueF("%d x %d = %d", varEvents.size(), sizeof(VarEvent), varEvents.size() * sizeof(VarEvent));
});
currentVar = ui->initText(parentVar, "eventsPS", nullptr, 16, true);
currentVar.subscribe(onLoop1s, [this](EventArguments) {
variable.setValueF("%d x %d = %d (%d + %d + %d)", varEventsPS.size(), sizeof(VarEventPS), varEventsPS.size() * sizeof(VarEventPS), sizeof(Variable), sizeof(VarFunction), sizeof(uint8_t));
});

#endif //STARBASE_DEVMODE
}

Expand Down Expand Up @@ -593,6 +613,10 @@ void SysModModel::loop20ms() {

// print->printJson("Write model", *model); //this shows the model before exclusion

if (!presets->isNull())
files->writeObjectToFile("/presets.json", presets);


doWriteModel = false;
}
}
Expand Down Expand Up @@ -707,8 +731,8 @@ Variable SysModModel::initVar(Variable parent, const char * id, const char * typ
// if (itr!=ucFunctions.end()) //found
// var["varEvent"] = distance(ucFunctions.begin(), itr); //assign found function
// else { //not found
mdl->varEvents.push_back(varEvent); //add new function
var["fun"] = mdl->varEvents.size()-1;
varEvents.push_back(varEvent); //add new function
var["fun"] = varEvents.size()-1;
// }

if (varEvent(variable, UINT8_MAX, onLoop)) { //test run if it supports loop
Expand All @@ -729,6 +753,26 @@ Variable SysModModel::initVar(Variable parent, const char * id, const char * typ
return variable;
}

void Variable::subscribe(uint8_t eventType, const VarFunction &varFunction) {
ppf("subscribe %d %s.%s\n", eventType, pid(), id());
mdl->varEventsPS.push_back({*this, eventType, varFunction}); //add new function
var["fun"] = UINT8_MAX; //to trigger response from ui
}

bool Variable::publish(uint8_t eventType, uint8_t rowNr) {
bool found = false;
for (VarEventPS &varEventPS: mdl->varEventsPS) {
if (eventType == varEventPS.eventType && strncmp(pid(), varEventPS.variable.pid(), 32) == 0 && strncmp(id(), varEventPS.variable.id(), 32) == 0) {
if (strcmp(id(), "effect") == 0 && eventType!= onLoop1s)
ppf("publish %s.%s[%d] %d=%d %s.%s\n", pid(), id(), rowNr, eventType, varEventPS.eventType , varEventPS.variable.pid(), varEventPS.variable.id());
varEventPS.varFunction(*this, rowNr, eventType);
found = true;
}
}
return found;
}


JsonObject SysModModel::walkThroughModel(std::function<JsonObject(JsonObject, JsonObject)> fun, JsonObject parentVar) {
for (JsonObject var : parentVar.isNull()?model->as<JsonArray>(): parentVar["n"]) {
// ppf(" %s", var["id"].as<String>());
Expand Down
37 changes: 32 additions & 5 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ struct Coord3D {
// this->z = y;
// }

Coord3D() {
}

Coord3D(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}

//comparisons
bool operator!=(Coord3D rhs) {
// ppf("Coord3D compare%d %d %d %d %d %d\n", x, y, z, rhs.x, rhs.y, rhs.z);
Expand Down Expand Up @@ -202,6 +211,16 @@ enum eventTypes
f_count
};

class Variable; //forward

typedef std::function<void(Variable)> FindFun;

#define EventArguments Variable variable, uint8_t rowNr, uint8_t eventType
// #define EventArguments2 Variable variable, uint8_t rowNr

// https://stackoverflow.com/questions/59111610/how-do-you-declare-a-lambda-function-using-typedef-and-then-use-it-by-passing-to
typedef std::function<uint8_t(EventArguments)> VarEvent;
typedef std::function<void(EventArguments)> VarFunction; // void: no return

class Variable {
public:
Expand All @@ -216,6 +235,7 @@ class Variable {
//core methods
const char *pid() const {return var["pid"];}
const char *id() const {return var["id"];}
const char *type() const {return var["type"];}

JsonVariant value() const {return var["value"];}
JsonVariant value(uint8_t rowNr) const {return var["value"][rowNr];}
Expand All @@ -235,6 +255,7 @@ class Variable {
//recursively remove all value[rowNr] from children of var
void removeValuesForRow(uint8_t rowNr);

bool valIsArray() const {return var["value"].is<JsonArray>();}
JsonArray valArray() const {if (var["value"].is<JsonArray>()) return var["value"]; else return JsonArray(); }

//if variable is a table, loop through its rows
Expand Down Expand Up @@ -334,21 +355,26 @@ class Variable {
//gives a variable an initital value returns true if setValue must be called
bool initValue(int min = 0, int max = 255, int pointer = 0);

}; //class Variable
void subscribe(uint8_t eventType, const VarFunction &varFunction = nullptr);
bool publish(uint8_t eventType, uint8_t rowNr = UINT8_MAX);

typedef std::function<void(Variable)> FindFun;
}; //class Variable

#define EventArguments Variable variable, uint8_t rowNr, uint8_t eventType
//For Publish and Subscribe events
struct VarEventPS {
Variable variable; //8 bytes: cannot be a pointer as Variable is volatile, the var inside variable is not volatile
uint8_t eventType; //1 byte but rounded to 4 bytes (room for more variables, e.g. rowNr?)
VarFunction varFunction; //function: 16 bytes
}; //total 28 bytes

// https://stackoverflow.com/questions/59111610/how-do-you-declare-a-lambda-function-using-typedef-and-then-use-it-by-passing-to
typedef std::function<uint8_t(EventArguments)> VarEvent;

class SysModModel: public SysModule {

public:

RAM_Allocator allocator;
JsonDocument *model = nullptr;
JsonDocument *presets = nullptr;

bool doWriteModel = false;

Expand All @@ -357,6 +383,7 @@ class SysModModel: public SysModule {
int varCounter = 1; //start with 1 so it can be negative, see var["o"]

std::vector<VarEvent> varEvents;
std::vector<VarEventPS> varEventsPS;

SysModModel();
void setup() override;
Expand Down
2 changes: 2 additions & 0 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void SysModNetwork::setup() {
default: return false;
}});

addPresets(parentVar.var);

}

void SysModNetwork::loop1s() {
Expand Down
4 changes: 4 additions & 0 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class SysModUI: public SysModule {
return initVarAndValue<const char *>(parent, id, "url", value, 0, 0, readOnly, varEvent);
}

Variable initVCR(Variable parent, const char * id, bool readOnly = false, const VarEvent &varEvent = nullptr) {
return initVarAndValue<bool>(parent, id, "vcr", false, 0, 0, readOnly, varEvent);
}

//initVarAndValue using basic value
template <typename Type>
Variable initVarAndValue(Variable parent, const char * id, const char * type, Type value, int min = 0, int max = 255, bool readOnly = true, const VarEvent &varEvent = nullptr) {
Expand Down
Loading

0 comments on commit 8823da5

Please sign in to comment.