Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ============================================
- -- CONFIGURATION - Edit these values as needed
- -- ============================================
- local onText = "ACTIVE"
- local offText = "INACTIVE"
- local onForeground = colors.white
- local onBackground = colors.green
- local offForeground = colors.white
- local offBackground = colors.red
- -- Input behavior flags
- local inputIsLatch = false -- true: toggles state on each pulse (for buttons)
- -- false: follows redstone state directly (for levers)
- local isInverted = false -- true: redstone ON = off display, redstone OFF = on display
- -- false: redstone ON = on display, redstone OFF = off display
- -- Persistence settings
- local persistenceFile = ".latch_state" -- File to store latch state (hidden file)
- -- ============================================
- -- PROGRAM CODE - No need to edit below
- -- ============================================
- -- Wrap the monitor on the back
- local monitor = peripheral.wrap("back")
- if not monitor then
- error("No monitor found on back of computer!")
- end
- -- Track latch state
- local latchState = false
- -- Save latch state to file
- local function saveState()
- if not inputIsLatch then return end
- local file = fs.open(persistenceFile, "w")
- if file then
- file.write(latchState and "true" or "false")
- file.close()
- end
- end
- -- Load latch state from file
- local function loadState()
- if not inputIsLatch then return end
- if fs.exists(persistenceFile) then
- local file = fs.open(persistenceFile, "r")
- if file then
- local content = file.readAll()
- file.close()
- latchState = (content == "true")
- end
- end
- end
- -- Center text horizontally and vertically on monitor
- local function centerText(mon, text)
- local w, h = mon.getSize()
- local x = math.floor((w - #text) / 2) + 1
- local y = math.floor(h / 2) + 1
- mon.setCursorPos(x, y)
- mon.write(text)
- end
- -- Update the monitor display based on state
- local function updateDisplay(isOn)
- if isOn then
- monitor.setBackgroundColor(onBackground)
- monitor.setTextColor(onForeground)
- else
- monitor.setBackgroundColor(offBackground)
- monitor.setTextColor(offForeground)
- end
- monitor.clear()
- centerText(monitor, isOn and onText or offText)
- end
- -- Get the current display state based on input and flags
- local function getDisplayState()
- if inputIsLatch then
- return isInverted and not latchState or latchState
- else
- local rsInput = redstone.getInput("front")
- return isInverted and not rsInput or rsInput
- end
- end
- -- Load previous state and set initial display
- loadState()
- updateDisplay(getDisplayState())
- -- Main loop - listen for redstone changes
- while true do
- os.pullEvent("redstone")
- if inputIsLatch then
- -- Toggle latch on rising edge only
- if redstone.getInput("front") then
- latchState = not latchState
- saveState() -- Persist the new state
- updateDisplay(getDisplayState())
- end
- else
- -- Direct mode - follow redstone state
- updateDisplay(getDisplayState())
- end
- end
Advertisement