Fix led lua code so the buffer do not include device specific magic values

This commit is contained in:
Paul Dino Jones 2025-03-23 18:05:58 -04:00
parent 1e6bde9da1
commit 8c764358b7
2 changed files with 37 additions and 34 deletions

View File

@ -216,6 +216,7 @@ int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata)
int total_leds = serialdevice->numleds;
size_t bufsize = (total_leds * 3) + 14;
char ledbytes[total_leds * 3];
char bytes[bufsize];
for(int j = 0; j < bufsize; j++)
@ -241,7 +242,7 @@ int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata)
lua_State* L = serialdevice->m.L;
lua_pushstring(L, "buff");
lua_pushlightuserdata(L, &bytes);
lua_pushlightuserdata(L, &ledbytes);
lua_settable(L, LUA_REGISTRYINDEX);
simdata_to_lua(L, simdata);
@ -273,14 +274,16 @@ int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata)
fprintf(stderr, "Error calling Lua script: %s\n", lua_tostring(L, -1));
}
for(int i = 0; i < total_leds; i++)
{
bytes[11 + (i * 3) + 0] = ledbytes[(i * 3) + 0];
bytes[11 + (i * 3) + 1] = ledbytes[(i * 3) + 1];
bytes[11 + (i * 3) + 2] = ledbytes[(i * 3) + 2];
}
size_t size = sizeof(bytes);
arduino_update(serialdevice, &bytes, size);
// move to free
return result;
}

View File

@ -18,7 +18,7 @@ int simdata_to_lua(lua_State *L, SimData* simdata) {
lua_pushinteger(L, simdata->rpms);
lua_setfield(L, -2, "rpm");
lua_pushinteger(L, simdata->maxrpm);
lua_setfield(L, -2, "maxrpm");
@ -151,12 +151,12 @@ int set_led_range_to_color(lua_State *L)
numlights = lua_tonumber(L, -1);
}
slogd("num leds is %i", numlights);
if(range_end > numlights)
{
range_end = numlights;
}
if(range_end == 0)
{
slogt("Invalid range, doing nothing");
@ -168,16 +168,16 @@ int set_led_range_to_color(lua_State *L)
char* bytes = lua_touserdata(L, -1);
slogt("tenth byte of buff is x%02x", bytes[10]);
uint8_t color0 = get_color_rgb_value(color, 0);
uint8_t color1 = get_color_rgb_value(color, 1);
uint8_t color2 = get_color_rgb_value(color, 2);
for( int i = range_start; i < range_end; i++)
{
bytes[(i * 3) + 11 + 0] = color0;
bytes[(i * 3) + 11 + 1] = color1;
bytes[(i * 3) + 11 + 2] = color2;
bytes[(i * 3) + 0] = color0;
bytes[(i * 3) + 1] = color1;
bytes[(i * 3) + 2] = color2;
}
}
@ -213,7 +213,7 @@ int set_led_range_to_rgb_color(lua_State *L)
numlights = lua_tonumber(L, -1);
}
slogd("num leds is %i", numlights);
if(range_end > numlights)
{
range_end = numlights;
@ -229,12 +229,12 @@ int set_led_range_to_rgb_color(lua_State *L)
char* bytes = lua_touserdata(L, -1);
slogt("tenth byte of buff is x%02x", bytes[10]);
for( int i = range_start; i < range_end; i++)
{
bytes[(i * 3) + 11 + 0] = color0;
bytes[(i * 3) + 11 + 1] = color1;
bytes[(i * 3) + 11 + 2] = color2;
bytes[(i * 3) + 0] = color0;
bytes[(i * 3) + 1] = color1;
bytes[(i * 3) + 2] = color2;
}
}
@ -248,7 +248,7 @@ int set_led_to_color(lua_State *L)
slogd("lua led is %i", led);
slogd("lua color is %i", color);
led = led - 1;
lua_getglobal(L, "TotalLeds");
@ -269,14 +269,14 @@ int set_led_to_color(lua_State *L)
char* bytes = lua_touserdata(L, -1);
slogt("tenth byte of buff is x%02x", bytes[10]);
uint8_t color0 = get_color_rgb_value(color, 0);
uint8_t color1 = get_color_rgb_value(color, 1);
uint8_t color2 = get_color_rgb_value(color, 2);
bytes[(led * 3) + 11 + 0] = color0;
bytes[(led * 3) + 11 + 1] = color1;
bytes[(led * 3) + 11 + 2] = color2;
bytes[(led * 3) + 0] = color0;
bytes[(led * 3) + 1] = color1;
bytes[(led * 3) + 2] = color2;
}
@ -296,7 +296,7 @@ int set_led_to_rgb_color(lua_State *L)
slogd("lua color0 is %i", color0);
slogd("lua color1 is %i", color1);
slogd("lua color2 is %i", color2);
led = led - 1;
lua_getglobal(L, "TotalLeds");
@ -317,10 +317,10 @@ int set_led_to_rgb_color(lua_State *L)
char* bytes = lua_touserdata(L, -1);
slogt("tenth byte of buff is x%02x", bytes[10]);
bytes[(led * 3) + 11 + 0] = color0;
bytes[(led * 3) + 11 + 1] = color1;
bytes[(led * 3) + 11 + 2] = color2;
bytes[(led * 3) + 0] = color0;
bytes[(led * 3) + 1] = color1;
bytes[(led * 3) + 2] = color2;
}
@ -337,18 +337,18 @@ int led_clear_all(lua_State *L)
numlights = lua_tonumber(L, -1);
}
slogd("num leds is %i", numlights);
lua_pushstring(L, "buff");
lua_gettable(L, LUA_REGISTRYINDEX);
char* bytes = lua_touserdata(L, -1);
slogt("tenth byte of buff is x%02x", bytes[10]);
for( int i = 0; i < numlights; i++)
{
bytes[(i * 3) + 11 + 0] = 0x00;
bytes[(i * 3) + 11 + 1] = 0x00;
bytes[(i * 3) + 11 + 2] = 0x00;
bytes[(i * 3) + 0] = 0x00;
bytes[(i * 3) + 1] = 0x00;
bytes[(i * 3) + 2] = 0x00;
}
}