Skip to content

Commit

Permalink
Add Live module
Browse files Browse the repository at this point in the history
Move misc files to misc
- add helloworld1/2.sc

pio / main.cpp: add STARBASE_USERMOD_LIVE (default not included in StarBase (WIP)

SysModFiles: seqNrToName: support filter

UserModLive: new!

UserModMPU6050: disabled by default
  • Loading branch information
ewoudwijma committed Jun 30, 2024
1 parent 374d498 commit 67e4ee8
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 11 deletions.
14 changes: 14 additions & 0 deletions misc/helloworld1.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
external void show1();

void main()
{
int h=1;
while(h>0)
{
for(int i=0;i<10;i++)
{
}
show1();
h++;
}
}
14 changes: 14 additions & 0 deletions misc/helloworld2.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
external void show2();

void main()
{
int h=1;
while(h>0)
{
for(int i=0;i<10;i++)
{
}
show2();
h++;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ build_flags =
lib_deps =
ElectronicCats/MPU6050 @ 1.3.0

;asmParser © https://github.com/hpwit/ASMParser
[STARBASE_USERMOD_LIVE]
build_flags =
-D STARBASE_USERMOD_LIVE
lib_deps =
https://github.com/hpwit/ASMParser.git
; about 1% / 19KB flash

[STARBASE]
build_flags =
Expand All @@ -60,13 +67,15 @@ build_flags =
${STARBASE_USERMOD_E131.build_flags}
${STARBASE_USERMOD_MPU6050.build_flags}
; ${STARBASE_USERMOD_HA.build_flags}
; ${STARBASE_USERMOD_LIVE.build_flags}
lib_deps =
${ESPAsyncWebServer.lib_deps}
https://github.com/bblanchon/ArduinoJson.git#v7.0.3
; https://github.com/Jason2866/ESP32_Show_Info.git
${STARBASE_USERMOD_E131.lib_deps}
${STARBASE_USERMOD_MPU6050.lib_deps}
; ${STARBASE_USERMOD_HA.lib_deps}
; ${STARBASE_USERMOD_LIVE.lib_deps}



Expand Down
20 changes: 11 additions & 9 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,27 @@ void SysModFiles::dirToJson(JsonArray array, bool nameOnly, const char * filter)
root.close();
}

bool SysModFiles::seqNrToName(char * fileName, size_t seqNr) {
bool SysModFiles::seqNrToName(char * fileName, size_t seqNr, const char * filter) {

File root = LittleFS.open("/");
File file = root.openNextFile();

size_t counter = 0;
while (file) {
if (counter == seqNr) {
// ppf("seqNrToName: %d %s %d\n", seqNr, file.name(), file.size());
root.close();
strncat(fileName, "/", 31); //add root prefix, fileName is 32 bytes but sizeof doesn't know so cheating
strncat(fileName, file.name(), 31);
file.close();
return true;
if (filter == nullptr || strstr(file.name(), filter) != nullptr) {
if (counter == seqNr) {
// ppf("seqNrToName: %d %s %d\n", seqNr, file.name(), file.size());
root.close();
strncat(fileName, "/", 31); //add root prefix, fileName is 32 bytes but sizeof doesn't know so cheating
strncat(fileName, file.name(), 31);
file.close();
return true;
}
counter++;
}

file.close();
file = root.openNextFile();
counter++;
}

root.close();
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SysModFiles: public SysModule {
void dirToJson(JsonArray array, bool nameOnly = false, const char * filter = nullptr);

//get back the name of a file based on the sequence
bool seqNrToName(char * fileName, size_t seqNr);
bool seqNrToName(char * fileName, size_t seqNr, const char * filter = nullptr);

//reads file and load it in json
//name is copied from WLED but better to call it readJsonFrom file
Expand Down
131 changes: 131 additions & 0 deletions src/User/UserModLive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
@title StarBase
@file UserModLive.h
@date 20240411
@repo https://github.com/ewowi/StarBase, submit changes to this file as PRs to ewowi/StarBase
@Authors https://github.com/ewowi/StarBase/commits/main
@Copyright © 2024 Github StarBase Commit Authors, asmParser © https://github.com/hpwit/ASMParser
@license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
@license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]
*/

// #define __RUN_CORE 0
#include "parser.h"

long time1;
static float _min = 9999;
static float _max = 0;
static uint32_t _nb_stat = 0;
static float _totfps;

static void show1()
{
// SKIPPED: check nargs (must be 3 because arg[0] is self)
long time2 = ESP.getCycleCount();
// driver.showPixels(WAIT);

float k = (float)(time2 - time1) / 240000000;
float fps = 1 / k;
_nb_stat++;
if (_min > fps && fps > 10 && _nb_stat > 10)
_min = fps;
if (_max < fps && fps < 200 && _nb_stat > 10)
_max = fps;
if (_nb_stat > 10)
_totfps += fps;
ppf("current fps:%.2f average:%.2f min:%.2f max:%.2f\r\n", fps, _totfps / (_nb_stat - 10), _min, _max);
time1 = ESP.getCycleCount();

// SKIPPED: check that both v1 and v2 are int numbers
// RETURN_VALUE(VALUE_FROM_INT(0), rindex);
}

static void show2() {
// ppf("show 2\n"); //test without print - works
}

class UserModLive:public SysModule {

public:

Parser p = Parser();

UserModLive() :SysModule("Live") {
isEnabled = false; //need to enable after fresh setup
};

void setup() override {
SysModule::setup();

parentVar = ui->initUserMod(parentVar, name, 6310);

ui->initSelect(parentVar, "script", UINT16_MAX, false ,[this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI: {
// ui->setComment(var, "Fixture to display effect on");
JsonArray options = ui->setOptions(var);
options.add("None");
files->dirToJson(options, true, ".sc"); //only files containing F(ixture), alphabetically

return true; }
case onChange: {
//set script
uint8_t fileNr = var["value"];

SCExecutable._kill(); //kill any old tasks

ppf("%s script f:%d f:%d\n", name, funType, fileNr);

if (fileNr > 0) { //not None

fileNr--; //-1 as none is no file

char fileName[32] = "";

files->seqNrToName(fileName, fileNr, ".sc");

// ppf("%s script f:%d f:%d\n", name, funType, fileNr);

if (strcmp(fileName, "") != 0) {

File f = files->open(fileName, "r");
if (!f)
ppf("UserModLive setup script open %s for %s failed", fileName, "r");
else {

string script = string(f.readString().c_str());

ppf("%s\n", script);

if (p.parse_c(&script))
{
SCExecutable.executeAsTask("main");
}
f.close();
}
}
else
ppf("UserModLive setup file for %d not found", fileNr);
}
return true; }
default: return false;
}}); //fixture

// ui->initButton

addExternal("show1", externalType::function, (void *)&show1);
addExternal("show2", externalType::function, (void *)&show2);

// addExternal("test", externalType::function, (void *)&test); //compile error ...

}

void test() {
ppf("hello test world\n");
}

};

extern UserModLive *live;


//asm_parser.h:325:1: warning: control reaches end of non-void function
2 changes: 1 addition & 1 deletion src/User/UserModMPU6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserModMPU6050: public SysModule {
VectorFloat gravityVector;

UserModMPU6050() :SysModule("Motion Tracking") {
// isEnabled = false;
isEnabled = false; //need to enable after fresh setup
};

void setup() {
Expand Down
10 changes: 10 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ AppModDemo *appModDemo;
#include "User/UserModMPU6050.h"
UserModMPU6050 *mpu6050;
#endif
#ifdef STARBASE_USERMOD_LIVE
#include "User/UserModLive.h"
UserModLive *live;
#endif

//setup all modules
void setup() {
Expand All @@ -73,6 +77,9 @@ void setup() {
#ifdef STARBASE_USERMOD_MPU6050
mpu6050 = new UserModMPU6050();
#endif
#ifdef STARBASE_USERMOD_LIVE
live = new UserModLive();
#endif

//Reorder with care! this is the order in which setup and loop is executed
//If changed make sure mdlEnabled.chFun executes var["value"].to<JsonArray>(); and saveModel!
Expand All @@ -99,6 +106,9 @@ void setup() {
mdls->add(ui);
mdls->add(mdns); //no ui
mdls->add(instances);
#ifdef STARBASE_USERMOD_LIVE
mdls->add(live);
#endif

//do not add mdls itself as it does setup and loop for itself!!! (it is the orchestrator)
mdls->setup();
Expand Down

0 comments on commit 67e4ee8

Please sign in to comment.