Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local logo = {
- " ___ ",
- " / _ \\ ",
- " / , _/",
- "/_/|_/"
- }
- -- Configuration
- local configFileName = "elevator_config.txt"
- local computerID = os.getComputerID()
- local config = {channel = "", currentFloor = "", computerID = ""}
- local modem = peripheral.find("modem")
- local monitorLeft = peripheral.wrap("left")
- local monitorRight = peripheral.wrap("right")
- local monitorTop = peripheral.wrap("top")
- monitorTop.setTextScale(1)
- monitorTop.setTextColour(colours.green)
- monitorRight.setTextScale(1)
- monitorRight.setTextColour(colours.purple)
- monitorLeft.setTextScale(1)
- monitorLeft.setTextColour(colours.purple)
- local lastButtonPress = 0
- local cooldown = 2 -- 2-second cooldown
- local lastIdleTime = 0 -- Time when the last "idle" message was sent
- local idleCooldown = 2 -- 1-second cooldown for "idle" message
- -- Save Config to file
- local function saveConfig(config)
- local configData = textutils.serializeJSON(config)
- local file = fs.open(configFileName, "w")
- file.write(configData)
- file.close()
- end
- -- Load Config from file
- local function loadConfig()
- if fs.exists(configFileName) then
- local file = fs.open(configFileName, "r")
- local configData = file.readAll()
- file.close()
- local loadedConfig = textutils.unserializeJSON(configData)
- config = loadedConfig
- end
- end
- local function refreshTopMonitor()
- monitorTop.clear()
- monitorTop.setCursorPos(1, 1)
- end
- local function centerText(monitor, text)
- local width, height = monitor.getSize()
- local x = math.ceil((width - #text) / 2)
- local y = math.ceil(height / 2)
- monitor.setCursorPos(x, y)
- monitor.write(text)
- end
- local function displayLogo(monitor, logo, color)
- refreshTopMonitor()
- monitor.setTextColour(color)
- local _, height = monitor.getSize()
- local yStart = math.ceil(height / 2) - math.ceil(#logo / 2)
- for y, line in ipairs(logo) do
- monitor.setCursorPos(1, yStart + y - 1)
- monitor.write(line)
- end
- end
- centerText(monitorLeft, "Up")
- centerText(monitorRight, "Down")
- local function handleTouch(side, x, y)
- local currentTime = os.epoch("utc")
- if currentTime - lastButtonPress >= cooldown * 1000 then
- if side == "left" then
- modem.transmit(config.channel, config.channel + 1, "up")
- idleSent = false
- elseif side == "right" then
- modem.transmit(config.channel, config.channel + 1, "down")
- idleSent = false
- end
- lastButtonPress = currentTime
- else
- print("Button press ignored due to cooldown.")
- end
- end
- local function mainLoop(config)
- local idleFlag = true -- Add this flag to control when to send "idle"
- while true do
- local event, side, x, y, senderChannel, replyChannel, message, senderDistance = os.pullEventRaw()
- local currentTime = os.epoch("utc") / 1000 -- Get current time in seconds
- if event == "redstone" then
- if idleFlag and (currentTime - lastIdleTime >= idleCooldown) then
- modem.transmit(config.channel, config.channel + 1, "idle:" .. config.currentFloor)
- displayLogo(monitorTop, logo, colours.green)
- print("Elevator floor: " .. config.currentFloor)
- lastIdleTime = currentTime -- Update the last idle time
- end
- else
- displayLogo(monitorTop, logo, colours.purple)
- end
- if event == "monitor_touch" then
- idleFlag = false -- Reset the idle flag upon touch
- handleTouch(side, x, y)
- lastIdleTime = currentTime -- Update the last idle time here as well
- else
- idleFlag = true -- Set the idle flag for other events
- end
- end
- end
- -- Initialization
- local function initializeClient()
- loadConfig()
- if not config.channel or config.channel == "" then
- print("Please enter the channel to use:")
- config.channel = tonumber(read())
- end
- if not config.currentFloor or config.currentFloor == "" then
- print("Please enter the current floor:")
- config.currentFloor = tonumber(read())
- end
- if not config.computerID then
- config.computerID = computerID
- end
- saveConfig(config)
- mainLoop(config)
- end
- -- Entry Point
- initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement