Add led_clear_range lua function so leds can be cleared without clearing them all for certain effects

This commit is contained in:
Paul Dino Jones 2025-08-26 18:05:09 -04:00
parent 811ce77da8
commit 88f3e0ebb9
4 changed files with 54 additions and 6 deletions

View File

@ -58,11 +58,11 @@ while(i <= proxcars and leftsideset == false) do
i = i + 1 i = i + 1
end end
rpm_leds = 6
-- rpm stuff -- rpm stuff
if simdata.rpm > 0 and simdata.maxrpm > 0 then if simdata.rpm > 0 and simdata.maxrpm > 0 then
rpmmargin = .05*simdata.maxrpm; rpmmargin = .05*simdata.maxrpm;
rpminterval = (simdata.maxrpm-rpmmargin) / 6; rpminterval = (simdata.maxrpm-rpmmargin) / rpm_leds;
litleds = 0 litleds = 0
for i = 1,6 do for i = 1,6 do
@ -72,18 +72,18 @@ if simdata.rpm > 0 and simdata.maxrpm > 0 then
end end
color = GREEN color = GREEN
if litleds > 3 and litleds < 6 then if litleds > rpm_leds/2 and litleds < rpm_leds then
color = YELLOW color = YELLOW
end end
if litleds >= 6 then if litleds >= rpm_leds then
color = RED color = RED
end end
if litleds >= 6 then if litleds >= rpm_leds then
if simdata.mtick % 2 == 0 then if simdata.mtick % 2 == 0 then
set_led_range_to_color(1, litleds, color) set_led_range_to_color(1, litleds, color)
else else
led_clear_all() led_clear_range(1, rpm_leds)
end end
else else
set_led_range_to_color(1, litleds, color) set_led_range_to_color(1, litleds, color)

View File

@ -313,6 +313,7 @@ int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata)
lua_register(L, "set_led_to_rgb_color", set_led_to_rgb_color); lua_register(L, "set_led_to_rgb_color", set_led_to_rgb_color);
lua_register(L, "set_led_range_to_rgb_color", set_led_range_to_rgb_color); lua_register(L, "set_led_range_to_rgb_color", set_led_range_to_rgb_color);
lua_register(L, "led_clear_all", led_clear_all); lua_register(L, "led_clear_all", led_clear_all);
lua_register(L, "led_clear_range", led_clear_all);
lua_pushinteger(L, LUALEDCOLOR_RED); lua_pushinteger(L, LUALEDCOLOR_RED);
lua_setglobal(L, "RED"); lua_setglobal(L, "RED");

View File

@ -340,3 +340,49 @@ int led_clear_all(lua_State *L)
} }
} }
int led_clear_range(lua_State *L)
{
slogt("lua called c function led_clear_range");
int range_start = lua_tonumber(L, 1);
int range_end = lua_tonumber(L, 2);
slogd("lua range start is %i", range_start);
slogd("lua range end is %i", range_end);
range_start = range_start - 1;
lua_getglobal(L, "TotalLeds");
int numlights = 0;
if (lua_isnumber(L, -1))
{
numlights = lua_tonumber(L, -1);
}
slogd("num leds is %i", numlights);
if(range_end > numlights)
{
range_end = numlights;
}
if(range_end == 0)
{
return 1;
}
lua_pushstring(L, "buff");
lua_gettable(L, LUA_REGISTRYINDEX);
char* bytes = lua_touserdata(L, -1);
slogt("first byte of buff is x%02x", bytes[0]);
for( int i = 0; i < numlights; i++)
{
bytes[(i * 3) + 0] = 0x00;
bytes[(i * 3) + 1] = 0x00;
bytes[(i * 3) + 2] = 0x00;
}
}

View File

@ -21,5 +21,6 @@ int set_led_to_color(lua_State *L);
int set_led_range_to_rgb_color(lua_State *L); int set_led_range_to_rgb_color(lua_State *L);
int set_led_to_rgb_color(lua_State *L); int set_led_to_rgb_color(lua_State *L);
int led_clear_all(lua_State *L); int led_clear_all(lua_State *L);
int led_clear_range(lua_State *L);
#endif #endif