From 46635141da1b72701562016dba2de17eb45cdb74 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Tue, 28 May 2024 00:41:41 +0200 Subject: [PATCH] feat: introduce basic light-server program --- install.lua | 1 + light-server.lua | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 light-server.lua diff --git a/install.lua b/install.lua index 41a5d25..e657347 100644 --- a/install.lua +++ b/install.lua @@ -14,6 +14,7 @@ local LIST_TURTLE_CONFIG_FILES = { } local LIST_SERVER_FILES = { + 'light-server.lua', 'inferium-server.lua', 'notify-server.lua' } diff --git a/light-server.lua b/light-server.lua new file mode 100644 index 0000000..c7826b4 --- /dev/null +++ b/light-server.lua @@ -0,0 +1,115 @@ +local MAIN_DELAY_ON = 0.25 +local MAIN_DELAY_OFF = 0 +local SECONDARY_DELAY_ON = 0.75 +local SECONDARY_DELAY_OFF = 0.25 + +local MAIN_INPUT_SIDE = 'top' +local MAIN_OUTPUT_SIDE = 'left' + +local SECONDARY_INPUT_SIDE = 'back' +local SECONDARY_OUTPUT_SIDE = 'front' + +local function getMainColorsOrder() + return { + colors.white, + colors.orange, + colors.magenta, + colors.lightBlue, + colors.yellow, + colors.lime, + colors.pink, + colors.gray, + colors.lightGray, + colors.cyan, + colors.purple, + colors.blue, + colors.brown, + colors.green, + colors.red, + colors.black + } +end + +local function getSecondaryColorsOrder() + return { + colors.white, + colors.orange, + colors.magenta + } +end + +local function switchOnLight(bundledOutput, colorsOrder, sleepTime) + if sleepTime <= 0 then + bundledOutput.setColorState(colors.combine(table.unpack(colorsOrder))) + return + end + + for _, color in pairs(colorsOrder) do + bundledOutput.setOn(color) + os.sleep(sleepTime) + end +end + +local function switchOffLight(bundledOutput, colorsOrder, sleepTime) + if sleepTime <= 0 then + bundledOutput.setColorState(0) + return + end + + for _, color in pairs(colorsOrder) do + bundledOutput.setOff(color) + os.sleep(sleepTime) + 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 _, lightIsOn, typeOfRoom = os.pullEvent('light_command') + + if typeOfRoom == 'main' then + if lightIsOn then + switchOnLight(mainOutput, mainColorsOrder, MAIN_DELAY_ON) + else + switchOffLight(mainOutput, mainColorsOrder, MAIN_DELAY_OFF) + end + elseif typeOfRoom == 'secondary' then + if lightIsOn then + switchOnLight(secondaryOutput, secondaryColorsOrder, SECONDARY_DELAY_ON) + else + switchOffLight(secondaryOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF) + end + else + error('unknown type of room "' .. tostring(typeOfRoom) .. '"', 0) + end + end +end + +local function redstoneLoop() + local mainState = nil + local secondaryState = nil + + while true do + local newMainState = redstone.getInput(MAIN_INPUT_SIDE) + local newSecondaryState = redstone.getInput(SECONDARY_INPUT_SIDE) + + if mainState ~= newMainState then + mainState = newMainState + os.queueEvent('main', newMainState) + end + + if secondaryState ~= newSecondaryState then + secondaryState = newSecondaryState + os.queueEvent('secondary', newSecondaryState) + end + + os.pullEvent('redstone') + end +end + +parallel.waitForAny(mainLoop, redstoneLoop) \ No newline at end of file