From c6e5533e30ce45d706b373c00deba983f254ece7 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 29 Dec 2022 11:08:20 +0100 Subject: [PATCH] Added terminal rendering. --- quantum-beacon.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/quantum-beacon.lua b/quantum-beacon.lua index 4607446..0598e20 100644 --- a/quantum-beacon.lua +++ b/quantum-beacon.lua @@ -10,13 +10,83 @@ local RECEIVE_CHANNEL = 101 local NODE_NAME = "TMP" local TRANSMIT_INTERVAL = 15 +local function drawScreen(online) + local w, h = term.getSize() + term.setBackgroundColor(colors.black) + term.clear() + + term.setCursorPos(1, 1) + term.setBackgroundColor(colors.gray) + term.setTextColor(colors.white) + term.clearLine() + term.write("Quantum Beacon") + + + term.setBackgroundColor(colors.black) + term.setTextColor(colors.lightGray) + term.setCursorPos(1, 3) + term.write("Node:") + term.setCursorPos(1, 5) + term.write("Channel:") + term.setCursorPos(1, 7) + term.write("Transmit Interval:") + term.setCursorPos(1, 9) + term.write("Online Status:") + + term.setTextColor(colors.white) + term.setCursorPos(20, 3) + term.write(NODE_NAME) + term.setCursorPos(20, 5) + term.write(tostring(SEND_CHANNEL)) + term.setCursorPos(20, 7) + term.write(tostring(TRANSMIT_INTERVAL)) + local str = nil + if online then + str = "Online" + term.setTextColor(colors.lime) + else + str = "Offline" + term.setTextColor(colors.red) + end + term.setCursorPos(20, 9) + term.write(str) +end + +local function clearMessageLine() + local _, h = term.getSize() + term.setCursorPos(1, h - 1) + term.setBackgroundColor(colors.gray) + term.clearLine() +end + +local function showError(msg) + clearMessageLine() + local _, h = term.getSize() + term.setCursorPos(2, h - 1) + term.setBackgroundColor(colors.gray) + term.setTextColor(colors.red) + term.write(msg) +end + +local function showMessage(msg) + clearMessageLine() + local _, h = term.getSize() + term.setCursorPos(2, h - 1) + term.setBackgroundColor(colors.gray) + term.setTextColor(colors.lightBlue) + term.write(msg) +end + local function getPeripheralOrWait(name) local p = nil repeat p = peripheral.find(name) if p == nil then - print("Error: Couldn't find an attached peripheral with name \"" .. name .. "\". Attach one please.") + showError("Error: Couldn't find an attached peripheral with name \"" .. name .. "\"") os.pullEvent("peripheral") + if p ~= nil then + showMessage("Peripheral \"" .. name .. "\" connected.") + end end until p ~= nil return p @@ -29,6 +99,8 @@ local function meSystemConnected(meBridge) return craftingCpus ~= nil and #craftingCpus > 0 end +drawScreen(false) + while true do local modem = getPeripheralOrWait("modem") local meBridge = getPeripheralOrWait("meBridge") @@ -38,5 +110,6 @@ while true do online = meSystemConnected(meBridge) } modem.transmit(SEND_CHANNEL, RECEIVE_CHANNEL, packet) + drawScreen(packet.online) os.sleep(TRANSMIT_INTERVAL) end