Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adjust as needed for your setup:
- -- Modem
- local modemSide = "top"
- -- Current Floor Header (used in multiline displays only)
- local multiH = "Current Floor"
- -- Moving Text (Used when elevator is moving between floors on name displays)
- -- Multiline config
- local moveL1 = "Elevator is" -- Line 1
- local moveL2 = "moving" -- Line 2
- -- Single Line config
- local moveSL = "Moving"
- --Code starts here
- local configFileName = "displays.txt"
- function loadConfig()
- if fs.exists(configFileName) then
- local file = fs.open(configFileName, "r")
- local config = textutils.unserialize(file.readAll())
- file.close()
- return config
- else
- error("Configuration file not found. Please run the setup script first.")
- end
- end
- local config = loadConfig()
- -- Prepare a table to hold the last displayed text for each display to minimize updates
- local lastDisplayedText = {}
- rednet.open(modemSide)
- function centerText(text, line)
- local width, height = term.getSize()
- term.setCursorPos((width - string.len(text)) / 2, line)
- print(text)
- end
- function displayUI()
- term.clear()
- centerText("Controlling Displays", 1)
- centerText("Current Floor", 4)
- end
- function clearAndUpdateDisplay(display, text, displayId)
- if lastDisplayedText[displayId] ~= text then
- display.clear()
- display.setCursorPos(1, 1)
- display.write(text)
- display.update()
- lastDisplayedText[displayId] = text
- end
- end
- function updateDisplays(floor)
- for i, dispConfig in ipairs(config.displays) do
- local display = peripheral.wrap(dispConfig.side)
- if display then
- local displayText = dispConfig.type == "name" and floor.name or floor.num
- local h, w = display.getSize()
- local DispType = GetDispType(h, w)
- centerText(floor.num .. " - " .. floor.name, 6)
- if dispConfig.type == "name" then
- if DispType == "multi" then
- if floor.num == "--" then
- centerTextAndUpdate(display, moveL1, i, 1)
- centerTextAndUpdate(display, moveL2, i, 2)
- else
- centerTextAndUpdate(display, multiH, i, 1)
- centerTextAndUpdate(display, displayText, i, 2)
- end
- lastDisplayedText[i] = displayText
- else
- if floor.num == "--" then
- centerTextAndUpdate(display, moveSL, i, 1)
- else
- centerTextAndUpdate(display, displayText, i, 1)
- end
- lastDisplayedText[i] = displayText
- end
- else
- clearAndUpdateDisplay(display, displayText, i, 1)
- end
- else
- print("Display not found on side: " .. dispConfig.side)
- end
- end
- end
- function centerTextAndUpdate(display, text, displayId, line)
- if lastDisplayedText[displayId] ~= text then
- local h, w = display.getSize()
- display.setCursorPos(((w - string.len(text)) / 2) + 1, line)
- display.clearLine()
- display.write(text)
- display.update()
- end
- end
- function GetDispType(disph, dispw)
- local dtype = ""
- if disph == 1 then
- dtype = "single"
- else
- dtype = "multi"
- end
- return dtype
- end
- displayUI()
- while true do
- local senderId, message, protocol = rednet.receive("ElevatorFloor")
- if protocol == "ElevatorFloor" then
- updateDisplays(message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement