From ce90d6c69c42bb5f220f6100600f21e9576be04a Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Tue, 28 May 2024 00:04:04 +0200 Subject: [PATCH] feat(rs): introduce basic redstone library --- install.lua | 1 + libs/rs.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 libs/rs.lua diff --git a/install.lua b/install.lua index 7d895d8..41a5d25 100644 --- a/install.lua +++ b/install.lua @@ -29,6 +29,7 @@ local LIST_CLIENT_FILES = { local LIST_CLIENT_CONFIG_FILES = {} local LIST_LIBS_FILES = { + 'libs/rs.lua', 'libs/net.lua', 'libs/utils.lua', 'libs/turtle-utils.lua', diff --git a/libs/rs.lua b/libs/rs.lua new file mode 100644 index 0000000..2dcc085 --- /dev/null +++ b/libs/rs.lua @@ -0,0 +1,68 @@ +local rs = {} + +rs.listenInput = function(side, onChangeCallback) + local state = redstone.getInput(side) + + while true do + os.pullEvent('redstone') + local newState = redstone.getInput(side) + + if state ~= newState then + state = newState + onChangeCallback(newState) + end + end +end + +rs.createBundledOutput = function(side, initialColorState) + local bundledOutput = {} + + local _colorState = initialColorState or 0 + + local function getState() + return _colorState + end + + local function setState(newColorState) + + if newColorState == _colorState then + return false + end + + _colorState = newColorState + redstone.setBundledOutpout(side, newColorState) + + return true + end + + bundledOutput.getStateColor = getState + bundledOutput.setColorState = setState + + bundledOutput.test = function(color) + return colors.test(getState(), color) + end + + bundledOutput.setOn = function(color) + local colorState = getState() + local newColorState = color.combine(colorState, color) + return setState(newColorState) + end + + bundledOutput.setOff = function(color) + local colorState = getState() + local newColorState = color.subtract(colorState, color) + return setState(newColorState) + end + + bundledOutput.toggle = function(color) + if colors.test(getState(), color) then + return bundledOutput.setOff(color) + end + + return bundledOutput.setOn(color) + end + + return bundledOutput +end + +return rs \ No newline at end of file