Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Author: Jack Dell
- -- Website: jackdell.ca
- -- Date: 2025-01-27
- -- Config
- local LISTEN_CHANNEL = 2
- local RELAY_CHANNEL = 1
- local TEXT_SCALE = 0.75
- -- Connecting to peripherals
- local monitor = peripheral.wrap("back") or error("No monitor found")
- local modem = peripheral.wrap("top") or error("No modem found")
- -- Getting the monitor width and height
- local w, h = monitor.getSize()
- -- Button definitions
- local buttons = {
- {text = "Auto", x = 2, y = h-4, width = 6, height = 3, color = colors.blue},
- {text = "On", x = 10, y = h-4, width = 4, height = 3, color = colors.blue},
- {text = "Off", x = 15, y = h-4, width = 5, height = 3, color = colors.blue}
- }
- function drawButton(button)
- monitor.setBackgroundColor(button.color)
- -- Draw the button shape
- for y = button.y, button.y + button.height - 1 do
- monitor.setCursorPos(button.x, y)
- monitor.write(string.rep(" ", button.width))
- end
- -- Draw text in middle row
- monitor.setCursorPos(button.x + math.floor((button.width - #button.text) / 2), button.y + 1)
- monitor.write(button.text)
- monitor.setBackgroundColor(colors.black)
- end
- function drawButtons(stats)
- -- Set the active button to green, otherwise blue
- buttons[1].color = stats.dynamic and colors.green or colors.blue
- buttons[2].color = (not stats.dynamic and stats.active) and colors.green or colors.blue
- buttons[3].color = (not stats.dynamic and not stats.active) and colors.green or colors.blue
- -- Draw each button to the monitor
- for _, button in pairs(buttons) do
- drawButton(button)
- end
- end
- function formatRF(rf)
- if rf >= 1000000 then
- return string.format("%.2fM", rf/1000000)
- else
- return string.format("%.2fK", rf/1000)
- end
- end
- function drawStats(stats)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write(string.format("Power: %s RF/t", formatRF(stats.rf_output)))
- monitor.setCursorPos(1,2)
- monitor.write(string.format("Stored: %s/%s RF", formatRF(stats.rf_stored), formatRF(stats.rf_capacity)))
- monitor.setCursorPos(1,3)
- monitor.write(string.format("Temp: %.1f C", stats.temperature))
- monitor.setCursorPos(1,4)
- monitor.write(string.format("Fuel: %.1f%%", (stats.fuel_ammount/stats.fuel_capacity)*100))
- monitor.setCursorPos(1,5)
- monitor.write(string.format("Mode: %s", stats.dynamic and "Auto" or "Manual"))
- end
- function draw(stats)
- drawStats(stats)
- drawButtons(stats)
- end
- function handleMessage()
- modem.open(LISTEN_CHANNEL)
- while true do
- local _, _, channel, _, message = os.pullEvent("modem_message")
- if channel == LISTEN_CHANNEL
- and type(message) == "table"
- then
- draw(message)
- end
- end
- end
- function handleClick()
- while true do
- local _, _, x, y = os.pullEvent("monitor_touch")
- for i, button in ipairs(buttons) do
- if y >= button.y and y < button.y + button.height and
- x >= button.x and x < button.x + button.width then
- local message = {
- override = button.text ~= "Auto",
- state = button.text == "On"
- }
- modem.transmit(RELAY_CHANNEL, LISTEN_CHANNEL, message)
- end
- end
- end
- end
- function init()
- monitor.clear()
- monitor.setTextScale(TEXT_SCALE)
- parallel.waitForAll(handleMessage, handleClick)
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement