From e9ca08e0243c340e9a15992cc758d7a2936e1dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Sun, 15 Sep 2024 13:59:29 +0200 Subject: [PATCH] added optimized code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- MA3 OSC FEEDBACK.lua | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/MA3 OSC FEEDBACK.lua b/MA3 OSC FEEDBACK.lua index 95f0377..46a6a4b 100644 --- a/MA3 OSC FEEDBACK.lua +++ b/MA3 OSC FEEDBACK.lua @@ -38,6 +38,21 @@ local function send_osc(etype, exec_no, value) Cmd(cmd_str) end +-- Function to reset all button LEDs to off (set RGB to 0) +local function reset_all_leds() + for _, exec_no in ipairs(executor_table) do + send_osc("ExecCol_R", exec_no, 0) + send_osc("ExecCol_G", exec_no, 0) + send_osc("ExecCol_B", exec_no, 0) + + -- If the executor is in the xKey range, clear the xKey label + if (exec_no >= 291 and exec_no <= 298) or (exec_no >= 191 and exec_no <= 198) then + Cmd(osc_str_template:format(osc_config, "xKey", exec_no, "")) + end + end + debug_print("All LEDs have been reset to off.") +end + -- Reuse function to handle empty playback local function handle_empty_playback(exec_no) debug_print('Empty playback found on:'..tostring(exec_no)) @@ -60,14 +75,20 @@ local function poll(exec_no) local execAssObj = execAssObjNoClass:GetClass() local colors = color_map[execAssObj] or {0, 0, 0} -- fallback colors - -- Check if assObjApp exists before using gsub + -- Check if assObjApp exists and is valid local assObjApp = execAssObjNoClass:Get("Appearance") if assObjApp then - -- Make sure assObjApp is valid and then remove "Appearance " prefix - local app_id = tonumber(tostring(assObjApp):gsub("Appearance ", "")) + -- Make sure assObjApp is valid and is a string, then remove "Appearance " prefix + assObjApp = tostring(assObjApp):gsub("Appearance ", "") + local app_id = tonumber(assObjApp) -- Convert the string to a number after cleanup + + -- Ensure app_id is valid and the appearance exists in ShowData if app_id and ShowData().Appearances[app_id] then local app_data = ShowData().Appearances[app_id] colors = {tonumber(app_data.BackR), tonumber(app_data.BackG), tonumber(app_data.BackB)} + else + -- If app_id is invalid, fallback to default colors + colors = {0, 0, 0} end end @@ -84,6 +105,7 @@ local function poll(exec_no) end end + local function mainloop() while enabled do for _, exec_no in ipairs(executor_table) do @@ -94,8 +116,12 @@ local function mainloop() end local function maintoggle() - enabled = not enabled if enabled then + enabled = false + else + enabled = true + -- Reset all LEDs to off before starting the main loop + reset_all_leds() mainloop() end end