refactor(light-server): add generic applyCommand and createLoop
This commit is contained in:
parent
1d882ada6d
commit
1784bfb93e
@ -11,7 +11,7 @@ local MAIN_OUTPUT_SIDE = 'left'
|
||||
local SECONDARY_INPUT_SIDE = 'back'
|
||||
local SECONDARY_OUTPUT_SIDE = 'front'
|
||||
|
||||
local VERSION = '1.1.0'
|
||||
local VERSION = '1.1.1'
|
||||
|
||||
local function getMainColorsOrder()
|
||||
return {
|
||||
@ -66,86 +66,65 @@ local function switchOffLight(bundledOutput, colorsOrder, sleepTime)
|
||||
end
|
||||
end
|
||||
|
||||
local mainLoopQueue = {}
|
||||
local secondaryLoopQueue = {}
|
||||
|
||||
local function applyMainCommand(bundledOutput)
|
||||
local nextState = table.remove(mainLoopQueue, 1)
|
||||
local function applyCommand(bundledOutput, loopQueue, colorsOrder, delayOn, delayOff)
|
||||
local nextState = table.remove(loopQueue, 1)
|
||||
|
||||
if nextState == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local mainColorsOrder = getMainColorsOrder()
|
||||
|
||||
if nextState then
|
||||
switchOnLight(bundledOutput, mainColorsOrder, MAIN_DELAY_ON)
|
||||
switchOnLight(bundledOutput, colorsOrder, delayOn)
|
||||
else
|
||||
switchOffLight(bundledOutput, mainColorsOrder, MAIN_DELAY_OFF)
|
||||
switchOffLight(bundledOutput, colorsOrder, delayOff)
|
||||
end
|
||||
|
||||
return applyMainCommand(bundledOutput)
|
||||
return applyCommand(bundledOutput, loopQueue, colorsOrder, delayOn, delayOff)
|
||||
end
|
||||
|
||||
local function applySecondaryCommand(bundledOutput)
|
||||
local nextState = table.remove(secondaryLoopQueue, 1)
|
||||
local function createLoop(side, eventName, loopQueue, colorsOrder, delayOn, delayOff)
|
||||
local bundledOutput = rs.createBundledOutput(side)
|
||||
|
||||
if nextState == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local secondaryColorsOrder = getSecondaryColorsOrder()
|
||||
|
||||
if nextState then
|
||||
switchOnLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_ON)
|
||||
else
|
||||
switchOffLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF)
|
||||
end
|
||||
|
||||
return applySecondaryCommand(bundledOutput)
|
||||
end
|
||||
|
||||
local function mainLoop()
|
||||
local bundledOutput = rs.createBundledOutput(MAIN_OUTPUT_SIDE)
|
||||
|
||||
while true do
|
||||
os.pullEvent('light_command_main')
|
||||
applyMainCommand(bundledOutput)
|
||||
return function()
|
||||
while true do
|
||||
os.pullEvent(eventName)
|
||||
applyCommand(bundledOutput, loopQueue, colorsOrder, delayOn, delayOff)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function secondaryLoop()
|
||||
local bundledOutput = rs.createBundledOutput(SECONDARY_OUTPUT_SIDE)
|
||||
|
||||
while true do
|
||||
os.pullEvent('light_command_secondary')
|
||||
applySecondaryCommand(bundledOutput)
|
||||
end
|
||||
end
|
||||
|
||||
local function redstoneLoop()
|
||||
local function createRedstoneLoop(mainLoopQueue, secondaryLoopQueue)
|
||||
local mainState = nil
|
||||
local secondaryState = nil
|
||||
|
||||
while true do
|
||||
local newMainState = redstone.getInput(MAIN_INPUT_SIDE)
|
||||
local newSecondaryState = redstone.getInput(SECONDARY_INPUT_SIDE)
|
||||
return function()
|
||||
while true do
|
||||
local newMainState = redstone.getInput(MAIN_INPUT_SIDE)
|
||||
local newSecondaryState = redstone.getInput(SECONDARY_INPUT_SIDE)
|
||||
|
||||
if mainState ~= newMainState then
|
||||
mainState = newMainState
|
||||
table.insert(mainLoopQueue, newMainState)
|
||||
os.queueEvent('light_command_main', newMainState)
|
||||
if mainState ~= newMainState then
|
||||
mainState = newMainState
|
||||
table.insert(mainLoopQueue, newMainState)
|
||||
os.queueEvent('light_command_main', newMainState)
|
||||
end
|
||||
|
||||
if secondaryState ~= newSecondaryState then
|
||||
secondaryState = newSecondaryState
|
||||
table.insert(secondaryLoopQueue, newSecondaryState)
|
||||
os.queueEvent('light_command_secondary', newSecondaryState)
|
||||
end
|
||||
|
||||
os.pullEvent('redstone')
|
||||
end
|
||||
|
||||
if secondaryState ~= newSecondaryState then
|
||||
secondaryState = newSecondaryState
|
||||
table.insert(secondaryLoopQueue, newSecondaryState)
|
||||
os.queueEvent('light_command_secondary', newSecondaryState)
|
||||
end
|
||||
|
||||
os.pullEvent('redstone')
|
||||
end
|
||||
end
|
||||
|
||||
local mainLoopQueue = {}
|
||||
local secondaryLoopQueue = {}
|
||||
|
||||
local mainLoop = createLoop(MAIN_OUTPUT_SIDE, 'light_command_main', mainLoopQueue, getMainColorsOrder(), MAIN_DELAY_ON, MAIN_DELAY_OFF)
|
||||
local secondaryLoop = createLoop(SECONDARY_OUTPUT_SIDE, 'light_command_secondary', secondaryLoopQueue, getSecondaryColorsOrder(), SECONDARY_DELAY_ON, SECONDARY_DELAY_OFF)
|
||||
local redstoneLoop = createRedstoneLoop(mainLoopQueue, secondaryLoopQueue)
|
||||
|
||||
print('> Starting light server v' .. VERSION)
|
||||
parallel.waitForAny(mainLoop, secondaryLoop, redstoneLoop)
|
||||
Loading…
Reference in New Issue
Block a user