From ee8a5da86875df8920b6780e4adfe3a7cd24908a Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Tue, 28 May 2024 01:22:44 +0200 Subject: [PATCH] fix(light-server): add mainLoopQueue and try to process events after the animation execution --- light-server.lua | 50 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/light-server.lua b/light-server.lua index 1536a48..5a92824 100644 --- a/light-server.lua +++ b/light-server.lua @@ -11,7 +11,7 @@ local MAIN_OUTPUT_SIDE = 'left' local SECONDARY_INPUT_SIDE = 'back' local SECONDARY_OUTPUT_SIDE = 'front' -local VERSION = '1.0.0' +local VERSION = '1.0.1' local function getMainColorsOrder() return { @@ -66,31 +66,51 @@ local function switchOffLight(bundledOutput, colorsOrder, sleepTime) end end +local mainLoopQueue = {} + +local function applyMainCommand(bundledOutput, lightIsOn) + local mainColorsOrder = getMainColorsOrder() + + if lightIsOn then + switchOnLight(bundledOutput, mainColorsOrder, MAIN_DELAY_ON) + else + switchOffLight(bundledOutput, mainColorsOrder, MAIN_DELAY_OFF) + end +end + +local function applySecondaryCommand(bundledOutput, lightIsOn) + local secondaryColorsOrder = getSecondaryColorsOrder() + + if lightIsOn then + switchOnLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_ON) + else + switchOffLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF) + end +end + local function mainLoop() local mainOutput = rs.createBundledOutput(MAIN_OUTPUT_SIDE) local secondaryOutput = rs.createBundledOutput(SECONDARY_OUTPUT_SIDE) - local mainColorsOrder = getMainColorsOrder() - local secondaryColorsOrder = getSecondaryColorsOrder() - while true do local _, typeOfRoom, lightIsOn = os.pullEvent('light_command') + table.remove(mainLoopQueue, 1) if typeOfRoom == 'main' then - if lightIsOn then - switchOnLight(mainOutput, mainColorsOrder, MAIN_DELAY_ON) - else - switchOffLight(mainOutput, mainColorsOrder, MAIN_DELAY_OFF) - end + applyMainCommand(mainOutput, lightIsOn) elseif typeOfRoom == 'secondary' then - if lightIsOn then - switchOnLight(secondaryOutput, secondaryColorsOrder, SECONDARY_DELAY_ON) - else - switchOffLight(secondaryOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF) - end + applySecondaryCommand(mainOutput, lightIsOn) else error('unknown type of room "' .. tostring(typeOfRoom) .. '"', 0) end + + local event = table.remove(mainLoopQueue, 1) + + if event and event.typeOfRoom == 'main' then + applyMainCommand(mainOutput, event.lightIsOn) + elseif event and event.typeOfRoom == 'secondary' then + applySecondaryCommand(secondaryOutput, event.lightIsOn) + end end end @@ -104,11 +124,13 @@ local function redstoneLoop() if mainState ~= newMainState then mainState = newMainState + table.insert(mainLoopQueue, { typeOfRoom = 'main', lightIsOn = newMainState }) os.queueEvent('light_command', 'main', newMainState) end if secondaryState ~= newSecondaryState then secondaryState = newSecondaryState + table.insert(mainLoopQueue, { typeOfRoom = 'secondary', lightIsOn = newSecondaryState }) os.queueEvent('light_command', 'secondary', newSecondaryState) end