Skip to content

Commit

Permalink
Live scripts: bugfixes and improvements, add Live Fixture examples
Browse files Browse the repository at this point in the history
Live Scripts
- balls: use random16 instead of rand (crashed with div0)
- F_scripts: add ledSize and shape
- add F_Ring and F_XMasTree

pio.ini: ELS 2.9.1.1 to 2.9.1.2

LedLayer: move set/getBitValue here

LedModEffects:
- addPixelsPre: add ledSize and shape
- Live Fixture: add default externals (sin etc) and fixture.domap is false (otherwise runs twice restart)

UserModLive
- add cos
- Kill switch only if still exists
  • Loading branch information
ewoudwijma committed Oct 21, 2024
1 parent b08f59e commit 720d21b
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion misc/LiveScripts/F_Cube888.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define depth 8

void main()
{
addPixelsPre(width,height,depth,width * height * depth);
addPixelsPre(width,height,depth,width * height * depth, 5, 0);
for (int z=0; z<depth;z++) {
for (int y=0; y<height; y++) {
for (int x=0; x<width;x++) {
Expand Down
17 changes: 17 additions & 0 deletions misc/LiveScripts/F_Ring.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//F_Ring.sc

define PI 3.141592654
define radius 200
define ledCount 100

void main()
{
addPixelsPre(radius * 2 / 10, radius * 2 / 10, 1, ledCount, 5, 0);
for (int i=0; i<ledCount; i++) {
int x = radius + sin(i/ledCount * 2 * PI) * radius;
int y = radius + cos(i/ledCount * 2 * PI) * radius;
printf("x:%d y:%d\n", x, y);
addPixel(x,y,0);
}
addPixelsPost();
}
17 changes: 17 additions & 0 deletions misc/LiveScripts/F_XMasTree.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//F_XMasTree.sc

define PI 3.141592654
define ledCount 100

void main()
{
addPixelsPre(20, 20, 20, ledCount*20, 2, 0);
for (int radius = 0; radius < 200; radius = radius + 10) {
for (int i=0; i<radius; i++) {
int x = radius + sin(i/radius * 2 * PI) * radius;
int y = radius + cos(i/radius * 2 * PI) * radius;
addPixel(x/2,y/2,radius);
}
}
addPixelsPost();
}
2 changes: 1 addition & 1 deletion misc/LiveScripts/F_panel2040.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define depth 1

void main()
{
addPixelsPre(width,height,depth,width * height * depth);
addPixelsPre(width,height,depth,width * height * depth, 5, 0);
for (int z=0; z<depth;z++) {
for (int y=0; y<height; y++) {
for (int x=0; x<width;x++) {
Expand Down
2 changes: 1 addition & 1 deletion misc/LiveScripts/F_panel4020.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define depth 1

void main()
{
addPixelsPre(width,height,depth,width * height * depth);
addPixelsPre(width,height,depth,width * height * depth, 5, 0);
for (int z=0; z<depth;z++) {
for (int y=0; y<height; y++) {
for (int x=0; x<width;x++) {
Expand Down
13 changes: 6 additions & 7 deletions misc/LiveScripts/ballsSL.sc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//ballsSL.sc

import rand
define max_nb_balls 20
define rmax 4
define rmin 3
Expand Down Expand Up @@ -98,13 +97,13 @@ void setup()
{
for(int i=0;i<max_nb_balls;i++)
{
vx[i] = rand(280)/255+0.7;
vy[i] = rand(280)/255+0.5;
r[i] = (rmax-rmin)*(rand(280)/180) +rmin;
xc[i] = width/2*(rand(280)/255+0.3)+15;
yc[i] = height/2*(rand(280)/255+0.3)+15;
vx[i] = random16(280)/255+0.7;
vy[i] = random16(280)/255+0.5;
r[i] = (rmax-rmin)*(random16(280)/180) +rmin;
xc[i] = width/2*(random16(280)/255+0.3)+15;
yc[i] = height/2*(random16(280)/255+0.3)+15;

color[i] = rand(255);
color[i] = random8();
}

h=0;
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ lib_deps =
build_flags =
-D STARBASE_USERMOD_LIVE
lib_deps =
https://github.com/hpwit/ESPLiveScript.git#02b9051 ; #v2.9.1.1 of 20241019 12:07
; https://github.com/hpwit/ESPLiveScript.git#02b9051 ; #v2.9.1.1 of 20241019 12:07
https://github.com/hpwit/ESPLiveScript.git#8192abea ; #v2.9.1.2 of 20241020 08:01

[STARLIGHT_CLOCKLESS_LED_DRIVER]
build_flags =
Expand Down
18 changes: 2 additions & 16 deletions src/App/LedEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
static uint8_t _beatSin8(uint8_t bpm, uint8_t lowest, uint8_t highest) {return beatsin8(bpm, lowest, highest);}
static uint8_t _inoise8(uint16_t x, uint16_t y, uint16_t z) {return inoise8(x, y, z);}
static uint8_t _random8() {return random8();}
static uint8_t _random16(uint8_t r) {return random16(r);}

//StarLight functions
static LedsLayer *gLeds = nullptr;
Expand Down Expand Up @@ -1363,22 +1364,6 @@ class Noise2DEffect: public Effect {
}
}; //Noise2D

//utility function
static bool getBitValue(const byte* byteArray, size_t n) {
size_t byteIndex = n / 8;
size_t bitIndex = n % 8;
uint8_t byte = byteArray[byteIndex];
return (byte >> bitIndex) & 1;
}
//utility function
static void setBitValue(byte* byteArray, size_t n, bool value) {
size_t byteIndex = n / 8;
size_t bitIndex = n % 8;
if (value)
byteArray[byteIndex] |= (1 << bitIndex);
else
byteArray[byteIndex] &= ~(1 << bitIndex);
}
//utiltity function?
uint16_t crc16(const unsigned char* data_p, size_t length) {
uint8_t x;
Expand Down Expand Up @@ -2931,6 +2916,7 @@ class LiveEffect: public Effect {
liveM->addExternalFun("uint8_t", "beatSin8", "(uint8_t a1, uint8_t a2, uint8_t a3)", (void *)_beatSin8);
liveM->addExternalFun("uint8_t", "inoise8", "(uint16_t a1, uint16_t a2, uint16_t a3)", (void *)_inoise8);
liveM->addExternalFun("uint8_t", "random8", "()", (void *)_random8);
liveM->addExternalFun("uint16_t", "random16", "(uint16_t a1)", (void *)_random16);
liveM->addExternalFun("uint8_t", "sin8", "(uint8_t a1)",(void*)_sin8); //using int here causes value must be between 0 and 16 error!!!
liveM->addExternalFun("uint8_t", "cos8", "(uint8_t a1)",(void*)_cos8); //using int here causes value must be between 0 and 16 error!!!
liveM->addExternalFun("void", "sPC", "(uint16_t a1, CRGB a2)", (void *)sPCLive);
Expand Down
19 changes: 18 additions & 1 deletion src/App/LedLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,29 @@ class SharedData {

class LedsLayer; //forward

//utility function
static bool getBitValue(const byte* byteArray, size_t n) {
size_t byteIndex = n / 8;
size_t bitIndex = n % 8;
uint8_t byte = byteArray[byteIndex];
return (byte >> bitIndex) & 1;
}
//utility function
static void setBitValue(byte* byteArray, size_t n, bool value) {
size_t byteIndex = n / 8;
size_t bitIndex = n % 8;
if (value)
byteArray[byteIndex] |= (1 << bitIndex);
else
byteArray[byteIndex] &= ~(1 << bitIndex);
}

class Fixture {

public:

CRGB ledsP[NUM_LEDS_Max];
std::vector<bool> pixelsToBlend; //this is a 1-bit vector !!! overlapping effects will blend
byte pixelsToBlend[NUM_LEDS_Max/8]; //this is a 1-bit vector !!! overlapping effects will blend

// CRGB *leds = nullptr;
// if (!leds)
Expand Down
12 changes: 8 additions & 4 deletions src/App/LedModEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

#ifdef STARBASE_USERMOD_LIVE
Fixture *gFixture = nullptr;
static void _addPixelsPre(uint16_t a1, uint16_t a2, uint16_t a3, uint16_t a4) {
static void _addPixelsPre(uint16_t a1, uint16_t a2, uint16_t a3, uint16_t a4, uint8_t a5, uint8_t a6) {
if (gFixture) {
gFixture->fixSize.x = a1;
gFixture->fixSize.y = a2;
gFixture->fixSize.z = a3;
gFixture->nrOfLeds = a4;
gFixture->ledSize = 5;
gFixture->shape = 0;
gFixture->ledSize = a5;
gFixture->shape = a6;
gFixture->projectAndMapPre(gFixture->fixSize, gFixture->nrOfLeds, gFixture->ledSize, gFixture->shape);
}
}
Expand Down Expand Up @@ -609,8 +609,10 @@ class LedModEffects:public SysModule {

liveM->scPreBaseScript = ""; //externals etc generated (would prefer String for esp32...)

liveM->addExternals();

//adding to scPreBaseScript
liveM->addExternalFun("void", "addPixelsPre", "(uint16_t a1, uint16_t a2, uint16_t a3, uint16_t a4)", (void *)_addPixelsPre);
liveM->addExternalFun("void", "addPixelsPre", "(uint16_t a1, uint16_t a2, uint16_t a3, uint16_t a4, uint8_t a5, uint8_t a6)", (void *)_addPixelsPre);
liveM->addExternalFun("void", "addPixel", "(uint16_t a1, uint16_t a2, uint16_t a3)", (void *)_addPixel);
liveM->addExternalFun("void", "addPin", "(uint16_t a1)", (void *)_addPin);
liveM->addExternalFun("void", "addPixelsPost", "()", (void *)_addPixelsPost);
Expand All @@ -619,6 +621,8 @@ class LedModEffects:public SysModule {

liveM->run(fileName);

fixture.doMap = false; //so don't run it twice

} else
#endif
{
Expand Down
5 changes: 4 additions & 1 deletion src/User/UserModLive.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static void resetShowStats()
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);}
static float _cos(float j) {return cos(j);}
static float _triangle(float j) {return 1.0 - fabs(fmod(2 * j, 2.0) - 1.0);}
static float _time(float j) {
float myVal = sys->now;
Expand Down Expand Up @@ -227,7 +228,8 @@ class UserModLive:public SysModule {
}});
ui->initButton(tableVar, "Kill", false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case onChange:
kill(runningPrograms.execPtr[rowNr]->name.c_str());
if (runningPrograms.execPtr[rowNr])
kill(runningPrograms.execPtr[rowNr]->name.c_str());
return true;
default: return false;
}});
Expand All @@ -248,6 +250,7 @@ class UserModLive:public SysModule {
addExternalFun("float", "atan2", "(float a1, float a2)",(void*)_atan2);
addExternalFun("float", "hypot", "(float a1, float a2)",(void*)_hypot);
addExternalFun("float", "sin", "(float a1)", (void *)_sin);
addExternalFun("float", "cos", "(float a1)", (void *)_cos);
addExternalFun("float", "time", "(float a1)", (void *)_time);
addExternalFun("float", "triangle", "(float a1)", (void *)_triangle);
addExternalFun("uint32_t", "millis", "()", (void *)millis);
Expand Down

0 comments on commit 720d21b

Please sign in to comment.