Skip to content

Commit

Permalink
UserModLive: generate external code
Browse files Browse the repository at this point in the history
.sc files: remove external

UserModLive
- add scPreScript to store externals etc
- addExternalFun with parameters to generate externals
- add Kill function to do all the kill stuff
  • Loading branch information
ewoudwijma committed Jul 11, 2024
1 parent e0cdeb1 commit c68faf3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
6 changes: 1 addition & 5 deletions misc/blink.sc
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
external void show();
external void pinMode(uint8_t pin, uint8_t mode);
external void digitalWrite(uint8_t pin, uint8_t val);
external void delay(uint32_t ms);
//external void pinsM->allocatePin(unsigned8 pinNr, const char * owner, const char * details); //classes supported?
//externals generated

//how to deal with external defines?
define OUTPUT 0x03
Expand Down
3 changes: 2 additions & 1 deletion misc/showtest1.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
external void show();
//externals generated
//E (23663) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:

void main()
{
Expand Down
2 changes: 1 addition & 1 deletion misc/showtest2.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
external void showM();
//externals generated

void main()
{
Expand Down
61 changes: 42 additions & 19 deletions src/User/UserModLive.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class UserModLive:public SysModule {
public:

Parser p = Parser();
char fileName[32] = "";
char fileName[32] = ""; //running sc file
string scPreScript = ""; //externals etc generated (would prefer String for esp32...)

UserModLive() :SysModule("Live") {
isEnabled = false; //need to enable after fresh setup
Expand All @@ -90,7 +91,7 @@ class UserModLive:public SysModule {

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

ui->initSelect(parentVar, "script", UINT16_MAX, false ,[this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
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);
Expand Down Expand Up @@ -129,26 +130,42 @@ class UserModLive:public SysModule {
// ui->initButton

//Live scripts defaults
addExternal("show", externalType::function, (void *)&show);
addExternal("showM", externalType::function, (void *)&UserModLive::showM); // warning: converting from 'void (UserModLive::*)()' to 'void*' [-Wpmf-conversions]
addExternal("resetStat", externalType::function, (void *)&resetShowStats);
addExternalFun("show", "()", (void *)&show);
addExternalFun("showM", "()", (void *)&UserModLive::showM); // warning: converting from 'void (UserModLive::*)()' to 'void*' [-Wpmf-conversions]
addExternalFun("resetStat", "()", (void *)&resetShowStats);

addExternal("display", externalType::function, (void *)&dispshit);
addExternal("dp", externalType::function, (void *)displayfloat);
addExternal("error", externalType::function, (void *)&showError);
addExternal("print", externalType::function, (void *)__print);
addExternalFun("display", "(int a1)", (void *)&dispshit);
addExternalFun("dp", "(float a1)", (void *)displayfloat);
addExternalFun("error", "(int a1, int a2, int a3)", (void *)&showError);
addExternalFun("print", "(char * a1)", (void *)__print);

addExternal("atan2",externalType::function,(void*)_atan2);
addExternal("hypot",externalType::function,(void*)_hypot);
addExternal("sin", externalType::function, (void *)_sin);
addExternalFun("atan2","(float a1, float a2)",(void*)_atan2);
addExternalFun("hypot","(float a1, float a2)",(void*)_hypot);
addExternalFun("sin", "(float a1)", (void *)_sin);

//added by StarBase
addExternal("pinMode", externalType::function, (void *)&pinMode);
addExternal("digitalWrite", externalType::function, (void *)&digitalWrite);
addExternal("delay", externalType::function, (void *)&delay);
addExternalFun("pinMode", "(int a1, int a2)", (void *)&pinMode);
addExternalFun("digitalWrite", "(int a1, int a2)", (void *)&digitalWrite);
addExternalFun("delay", "(int a1)", (void *)&delay);

// addExternalFun("delay", [](int ms) {delay(ms);});
// addExternalFun("digitalWrite", [](int pin, int val) {digitalWrite(pin, val);});
}

void addExternalFun(string name, string parameters,void * ptr) {
addExternal(name, externalType::function, ptr);
scPreScript += "external void " + name + parameters + ";\n";
}

// void addExternalFun(string name, std::function<void(int)> fun) {
// addExternal(name, externalType::function, (void *)&fun)); //unfortionately InstructionFetchError, why does it work in initText etc?
// ppf("external %s(int arg1);\n", name.c_str()); //add to string
// }
// void addExternalFun(string name, std::function<void(int, int)> fun) {
// addExternal(name, externalType::function, (void *)&fun); //unfortionately InstructionFetchError
// ppf("external %s(int arg1, int arg2);\n", name.c_str()); //add to string
// }

//testing class functions instead of static
void showM() {
long time2 = ESP.getCycleCount();
Expand Down Expand Up @@ -180,8 +197,7 @@ class UserModLive:public SysModule {
if (!force && strcmp(fileName, this->fileName) != 0) // if another fileName then force should be true;
return;

SCExecutable._kill(); //kill any old tasks
fps = 0;
kill();

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

Expand All @@ -190,9 +206,9 @@ class UserModLive:public SysModule {
ppf("UserModLive setup script open %s for %s failed\n", fileName, "r");
else {

string scScript = string(f.readString().c_str());
string scScript = scPreScript + string(f.readString().c_str());

ppf("%s\n", scScript);
ppf("%s\n", scScript.c_str());

if (p.parse_c(&scScript))
{
Expand All @@ -205,6 +221,13 @@ class UserModLive:public SysModule {
else
ppf("UserModLive setup file for %s not found\n", fileName);
}

void kill() {
SCExecutable._kill(); //kill any old tasks
fps = 0;
strcpy(fileName, "");
}

};

extern UserModLive *liveM;
Expand Down

0 comments on commit c68faf3

Please sign in to comment.