1616#define USE_FASTLED // as ESPLiveScript.h calls hsv ! one of the reserved functions!!
1717#include " ESPLiveScript.h"
1818
19- // extern void _addPin(uint8_t pinNr);
20- // extern void _addPixelsPre();
21- // extern void _addPixel(uint16_t x, uint16_t y, uint16_t z);
22- // extern void _addPixelsPost();
23-
2419PhysicalLayer *glayerP = nullptr ;
2520VirtualLayer *glayerV = nullptr ;
2621
@@ -30,8 +25,8 @@ static void _addPixel(uint16_t x, uint16_t y, uint16_t z) {glayerP->addPixel({x,
3025static void _addPixelsPost () {glayerP->addPixelsPost ();}
3126
3227void _fadeToBlackBy (uint8_t fadeValue) { glayerV->fadeToBlackBy (fadeValue);}
33- static void _sPCLive (uint16_t pixel, CRGB color) {glayerV->setPixelColor (pixel, color);} // setPixelColor with color
34- static void _sCFPLive (uint16_t pixel, uint8_t index, uint8_t brightness) { glayerV->setPixelColor (pixel, ColorFromPalette (PartyColors_p, index, brightness));} // setPixelColor within palette
28+ static void _sPC (uint16_t pixel, CRGB color) {glayerV->setPixelColor (pixel, color);} // setPixelColor with color
29+ static void _sCFP (uint16_t pixel, uint8_t index, uint8_t brightness) { glayerV->setPixelColor (pixel, ColorFromPalette (PartyColors_p, index, brightness));} // setPixelColor within palette
3530
3631static float _triangle (float j) {return 1.0 - fabs (fmod (2 * j, 2.0 ) - 1.0 );}
3732static float _time (float j) {
@@ -45,16 +40,38 @@ void sync() {
4540 delay (1 ); // feed the watchdog
4641}
4742
48- void addExternalVal (string result, string name, void * ptr) {
49- if (findLink (name, externalType::value) == -1 ) // not allready added earlier
50- addExternalVariable (name, result, " " , ptr);
51- }
52-
53- void addExternalFun (string result, string name, string parameters, void * ptr) {
54- if (findLink (name, externalType::function) == -1 ) // not allready added earlier
55- addExternalFunction (name, result, parameters, ptr);
43+ void addExternal (string definition, void * ptr) {
44+ bool success = false ;
45+ size_t firstSpace = definition.find (' ' );
46+ if (firstSpace != std::string::npos) {
47+ string returnType = definition.substr (0 , firstSpace);
48+
49+ string parameters = " " ;
50+ string functionName = " " ;
51+
52+ size_t openParen = definition.find (' (' , firstSpace + 1 );
53+ if (openParen != std::string::npos) { // function
54+ functionName = definition.substr (firstSpace + 1 , openParen - firstSpace - 1 );
55+
56+ size_t closeParen = definition.find (' )' , openParen + 1 );
57+ if (closeParen != std::string::npos) { // function with parameters
58+ parameters = definition.substr (openParen + 1 , closeParen - openParen - 1 );
59+ success = true ;
60+ if (findLink (functionName, externalType::function) == -1 ) // not allready added earlier
61+ addExternalFunction (functionName, returnType, parameters, ptr);
62+ }
63+ } else { // variable
64+ functionName = definition.substr (firstSpace + 1 );
65+ success = true ;
66+ if (findLink (functionName, externalType::value) == -1 ) // not allready added earlier
67+ addExternalVariable (functionName, returnType, " " , ptr);
68+ }
69+ }
70+ if (!success) {
71+ ESP_LOGE (TAG, " Failed to parse function definition: %s" , definition.c_str ());
72+ }
5673}
57-
74+
5875Parser parser = Parser();
5976
6077void LiveScriptNode::setup () {
@@ -65,54 +82,48 @@ void LiveScriptNode::setup() {
6582 return ;
6683 }
6784
68- // send UI spinner
85+ // generic functions
86+ addExternal (" uint32_t millis()" , (void *)millis);
87+ addExternal (" uint32_t now()" , (void *)millis); // todo: synchronized time (sys->now)
88+ addExternal (" uint16_t random16(uint16_t)" , (void *)(uint16_t (*)(uint16_t ))random16);
89+ addExternal ( " void delay(uint8_t)" , (void *)delay);
90+ addExternal ( " void pinMode(int,int)" , (void *)pinMode);
91+ addExternal ( " void digitalWrite(int,int)" , (void *)digitalWrite);
92+
93+ // trigonometric functions
94+ addExternal ( " float sin(float)" , (void *)(float (*)(float ))sin);
95+ addExternal ( " float cos(float)" , (void *)(float (*)(float ))cos);
96+ addExternal ( " uint8_t sin8(uint8_t)" , (void *)sin8);
97+ addExternal ( " uint8_t cos8(uint8_t)" , (void *)cos8);
98+ addExternal ( " float atan2(float,float)" , (void *)(float (*)(float ,float ))atan2);
99+ addExternal ( " uint8_t inoise8(uint16_t,uint16_t,uint16_t)" , (void *)(uint8_t (*)(uint16_t ,uint16_t ,uint16_t ))inoise8);
100+ addExternal ( " float hypot(float,float)" , (void *)(float (*)(float ,float ))hypot);
101+ addExternal ( " float time(float)" , (void *)_time);
102+ addExternal ( " float triangle(float)" , (void *)_triangle);
103+
104+ // MoonLight functions
105+ addExternal ( " void addPin(uint8_t)" , (void *)_addPin);
106+ addExternal ( " void addPixelsPre()" , (void *)_addPixelsPre);
107+ addExternal ( " void addPixel(uint16_t,uint16_t,uint16_t)" , (void *)_addPixel);
108+ addExternal ( " void addPixelsPost()" , (void *)_addPixelsPost);
109+
110+ addExternal ( " void fadeToBlackBy(uint8_t)" , (void *)_fadeToBlackBy);
111+ addExternal ( " CRGB* leds" , (void *)layerV->layerP ->leds );
112+ addExternal ( " void sPC(uint16_t,CRGB)" , (void *)_sPC);
113+ addExternal ( " void sCFP(uint16_t,uint8_t,uint8_t)" , (void *)_sCFP);
114+ addExternal (" uint16_t width" , &layerV->size .x );
115+ addExternal (" uint16_t height" , &layerV->size .y );
116+ addExternal (" uint16_t depth" , &layerV->size .z );
69117
70- addExternalFun (" void" , " addPin" , " uint8_t" , (void *)_addPin);
71- addExternalFun (" void" , " addPixelsPre" , " " , (void *)_addPixelsPre);
72- addExternalFun (" void" , " addPixel" , " uint16_t,uint16_t,uint16_t" , (void *)_addPixel);
73- addExternalFun (" void" , " addPixelsPost" , " " , (void *)_addPixelsPost);
74- addExternalFun (" uint32_t" , " millis" , " " , (void *)millis);
75- float (*_sin)(float ) = sin;
76- addExternalFun (" float" , " sin" , " float" , (void *)_sin);
77- float (*_cos)(float ) = cos;
78- addExternalFun (" float" , " cos" , " float" , (void *)_cos);
79- float (*_atan2)(float , float ) = atan2;
80- addExternalFun (" float" , " atan2" , " float,float" ,(void *)_atan2);
81- float (*_hypot)(float , float ) = hypot;
82- addExternalFun (" float" , " hypot" , " float,float" ,(void *)_hypot);
83- addExternalFun (" float" , " time" , " float" , (void *)_time);
84- addExternalFun (" float" , " triangle" , " float" , (void *)_triangle);
85-
86- addExternalFun (" void" , " pinMode" , " (int a1, int a2)" , (void *)&pinMode);
87- addExternalFun (" void" , " digitalWrite" , " (int a1, int a2)" , (void *)&digitalWrite);
88- addExternalFun (" void" , " delay" , " (int a1)" , (void *)&delay);
89-
90- addExternalFun (" uint8_t" , " cos8" , " uint8_t" , (void *)cos8);
91- addExternalFun (" void" , " delay" , " uint8_t" , (void *)delay);
92- uint8_t (*_inoise8)(uint16_t , uint16_t , uint16_t )=inoise8;
93- addExternalFun (" uint8_t" , " inoise8" , " uint16_t,uint16_t,uint16_t" , (void *)_inoise8);
94- addExternalFun (" uint32_t" , " now" , " " , (void *)millis);
95- uint16_t (*_random16)(uint16_t )=random16; // enforce specific random16 function
96- addExternalFun (" uint16_t" , " random16" , " uint16_t" , (void *)_random16);
97- addExternalFun (" uint8_t" , " sin8" , " uint8_t" , (void *)sin8);
98-
99- addExternalFun (" void" , " fadeToBlackBy" , " uint8_t" , (void *)_fadeToBlackBy);
100- addExternalVal (" CRGB *" , " leds" , (void *)layerV->layerP ->leds );
101- addExternalFun (" void" , " sPC" , " uint16_t,CRGB" , (void *)_sPCLive);
102- addExternalFun (" void" , " sCFP" , " uint16_t,uint8_t,uint8_t" , (void *)_sCFPLive);
103- addExternalVal (" uint16_t" , " width" , &layerV->size .x );
104- addExternalVal (" uint16_t" , " height" , &layerV->size .y );
105- addExternalVal (" uint16_t" , " depth" , &layerV->size .z );
106-
107- // void sPC(uint16_t,CRGB)
108- // CRGB * leds
109118
110119 for (asm_external el: external_links) {
111120 ESP_LOGD (TAG, " elink %s %s %d" , el.shortname .c_str (), el.name .c_str (), el.type );
112121 }
113122
114123 runningPrograms.setFunctionToSync (sync);
115124
125+ // send UI spinner
126+
116127 // run the recompile not in httpd but in main loopTask (otherwise we run out of stack space)
117128 // runInLoopTask.push_back([&, animation, type, error] {
118129 ESP_LOGD (TAG, " compileAndRun %s %s" , animation, type);
0 commit comments