Skip to content

Commit

Permalink
Add Live scripts test - disabled by default
Browse files Browse the repository at this point in the history
enable: include STARBASE_USERMOD_LIVE in pio.ini

Add octo.sc

LedEffects, LedModEffects
- add LiveScriptEffect

UserModLive
- add LEDs specific code
  • Loading branch information
ewoudwijma committed Jul 29, 2024
1 parent 7540833 commit 1bb19f6
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
56 changes: 56 additions & 0 deletions misc/octo.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//import memset
//save_reg
define PI 3.1415926535
define speed 1
define nb_branches 5
uint8_t C_X ;
uint8_t C_Y;
uint8_t mapp;
uint8_t rMapRadius[NUM_LEDS];
uint8_t rMapAngle[NUM_LEDS];
uint32_t t; //=speed

void setup()
{
C_X = width / 2;
C_Y = height / 2;
mapp = 255 / width;
for (int x = -C_X; x < C_X + (width % 2); x++) {
for (int y = -C_Y; y < C_Y + (height % 2); y++) {

float h=128*(atan2(y, x)/PI);
rMapAngle[(x + C_X) *height+y + C_Y]= (int)(h);
h=hypot(x,y)*mapp;
rMapRadius[(x + C_X)*height +y + C_Y] = (int)(h); //thanks Sutaburosu
}
}
}


void loop() {

// memset(leds, 0, width * height * 3);
for (uint8_t x = 0; x < width; x++) {
for (uint8_t y = 0; y < height; y++) {
uint8_t angle = rMapAngle[x*height+y];
uint8_t radius = rMapRadius[x*height+y];

// leds[y*panel_width+x] = hsv(t + radius, 255, sin8(t*4+sin8(t * 4 - radius)+angle*nb_branches));
// int h=sin8(t*4+sin8(t * 4 - radius)+angle*nb_branches);
// leds[y*panel_width+x] = hsv(t + radius, 255, h);

sPC(y*panel_width+x, hsv(t + radius, 255, sin8(t*4+sin8(t * 4 - radius)+angle*nb_branches)));
}
}
t=t+speed;
//delay(16);
}

void main() {
resetStat();
setup();
while (2>1) {
loop();
show();
}
}
68 changes: 67 additions & 1 deletion src/App/LedEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "../User/UserModMPU6050.h"
#endif

#ifdef STARBASE_USERMOD_LIVE
#include "../User/UserModLive.h"
#endif

//utility function
float distance(float x1, float y1, float z1, float x2, float y2, float z2) {
return sqrtf((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
Expand Down Expand Up @@ -211,7 +215,7 @@ class SinelonEffect: public Effect {
leds.fadeToBlackBy(20);

int pos = beatsin16( bpm, 0, leds.nrOfLeds-1 );
leds[pos] = leds.getPixelColor(pos) + CHSV( sys->now/50, 255, 255);
leds[pos] += CHSV( sys->now/50, 255, 255);
}

void controls(Leds &leds, JsonObject parentVar) {
Expand Down Expand Up @@ -2914,3 +2918,65 @@ class Byte2TestEffect2: public Effect {

}
}; // 2ByteTest2

#ifdef STARBASE_USERMOD_LIVE

class LiveScriptEffect: public Effect {
const char * name() {return "Live Script";}
uint8_t dim() {return _2D;}
const char * tags() {return "💫";}

void loop(Leds &leds) {
}

void controls(Leds &leds, JsonObject parentVar) {
Effect::controls(leds, parentVar);
ui->initSelect(parentVar, "script2", UINT16_MAX, false , [&leds](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"][rowNr];

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

char fileName[32] = "";

if (fileNr > 0 && liveM->scPreBaseScript.length()) { //not None and live setup done
fileNr--; //-1 as none is no file
files->seqNrToName(fileName, fileNr, ".sc");
ppf("script f:%d f:%d s:%s\n", funType, fileNr, fileName);

// in ledleds.h: void setPixelColorLive(unsigned16 indexV, uint32_t color) {setPixelColor(indexV, CRGB::Black);}
// void (Leds::*sPCCached)(unsigned16, uint32_t) = &Leds::setPixelColorLive;
// Leds *leds2 = &leds;
// (leds2->*sPCCached)(0, 0);

gLeds = &leds; //set the leds class for live script

//set the custom defines
liveM->scPreCustomScript = "";
liveM->scPreCustomScript += "define width " + to_string(leds.size.x) + "\n";
liveM->scPreCustomScript += "define height " + to_string(leds.size.y) + "\n";
liveM->scPreCustomScript += "define NUM_LEDS " + to_string(leds.nrOfLeds) + "\n";
liveM->scPreCustomScript += "define panel_width " + to_string(leds.size.x) + "\n"; //isn't panel_width always the same as width?
}

if (strcmp(fileName, "") != 0)
liveM->run(fileName, true); //force a new file to run
else {
liveM->kill();
}

return true; }
default: return false;
}}); //script
}
};

#endif
4 changes: 4 additions & 0 deletions src/App/LedModEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class LedModEffects:public SysModule {
effects.push_back(new Byte2TestEffect); // not 3D but next to pixelMap for testing
effects.push_back(new Byte2TestEffect2); // not 3D but next to pixelMap for testing

#ifdef STARBASE_USERMOD_LIVE
effects.push_back(new LiveScriptEffect);
#endif

//load projections
fixture.projections.push_back(new NoneProjection);
fixture.projections.push_back(new DefaultProjection);
Expand Down
25 changes: 25 additions & 0 deletions src/User/UserModLive.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static void show()

// driver.showPixels(WAIT); // LEDS specific

//show is done in LedModEffects!

long time3 = ESP.getCycleCount();
float k = (float)(time2 - time1) / 240000000;
fps = 1 / k; //StarBase: class variable so it can be shown in UI!!!
Expand Down Expand Up @@ -76,6 +78,19 @@ static float _hypot(float x,float y) {return hypot(x,y);}
static float _atan2(float x,float y) { return atan2(x,y);}
static float _sin(float j) {return sin(j);}

//LEDS specific
static CRGB POSV(uint8_t h, uint8_t s, uint8_t v) {return CHSV(h, s, v);}
static uint8_t _sin8(uint8_t a) {return sin8(a);}
static Leds *gLeds = nullptr;
static void sPCLive(uint16_t pixel, CRGB color) {
// if (pixel == 0) ppf(".");
// if (pixel < 10)
// ppf(" %d <- %d-%d-%d", pixel, color.r, color.g, color.b);
// gLeds->setPixelColor(pixel, CRGB(random8(), random8(), random8()));
if (gLeds) gLeds->setPixelColor(pixel, color);
}
//End LEDS specific

class UserModLive:public SysModule {

public:
Expand Down Expand Up @@ -152,6 +167,16 @@ class UserModLive:public SysModule {
// addExternalFun("delay", [](int ms) {delay(ms);});
// addExternalFun("digitalWrite", [](int pin, int val) {digitalWrite(pin, val);});

//LEDS specific
addExternalFun("CRGB", "hsv", "(int a1, int a2, int a3)", (void *)POSV);
addExternalFun("uint8_t", "sin8","(uint8_t a1)",(void*)_sin8); //using int here causes value must be between 0 and 16 error!!!
addExternalFun("void", "sPC", "(int a1, CRGB a2)", (void *)sPCLive);
//address of overloaded function with no contextual type information: setPixelColorLive
//ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&Leds::setPixelColorLive' [-fpermissive]
//converting from 'void (Leds::*)(uint16_t, uint32_t)' {aka 'void (Leds::*)(short unsigned int, unsigned int)'} to 'void*' [-Wpmf-conversions]

//End LEDS specific

} //setup

void addExternalVal(string result, string name, void * ptr) {
Expand Down

0 comments on commit 1bb19f6

Please sign in to comment.