Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Displays Info Regarding House
- -- Setup Variables
- local monitorSide = "right"
- local rednetSide = "bottom"
- local monitor = peripheral.wrap(monitorSide)
- local defaultDisplay = "storage"
- local currentDisplay = defaultDisplay
- -- Rednet Variables
- local wireless = true
- local timeout = 5 -- Number of seconds to wait
- -- when receiving messages before
- -- timing out
- local interval = 1 -- Number of seconds between
- -- updates
- if wireless == true then
- rednet.open(rednetSide)
- end
- -- Computer ID's and pFilter's
- comps = {}
- comps["meComp"] = {id = 4, pFilter = "Storage_Monitor"}
- -- Ping computers
- function ping (ID, pFilter)
- rednet.send(ID, "ping", pFilter)
- local senderID, message, protocol = rednet.receive(timeout)
- if not message then
- return false
- elseif message == "pong" and senderID == ID and protocol == pFilter then
- return true
- else
- return false
- end
- end
- -- Check computers
- function check (ID, pFilter)
- rednet.send(ID, "check", pFilter)
- local senderID, message, protocol = rednet.receive(timeout)
- if not message then
- return false
- elseif message == "found" and senderID == ID and protocol == pFilter then
- return true
- else
- return false
- end
- end
- -- Get info
- function getInfo (ID, msg, pFilter)
- rednet.send(ID, msg, pFilter)
- local senderID, message, protocol = rednet.receive(timeout)
- if not message then
- return false
- elseif senderID == ID and protocol == pFilter then
- return message
- else
- return false
- end
- end
- -- Switch
- function updateDisplay ()
- if currentDisplay == "storage" then
- displayStorage()
- end
- end
- ----- Convenience functions -----
- function mWrite (text, color)
- write(text)
- monitor.setTextColor(color or colors.white)
- monitor.write(text)
- monitor.setTextColor(colors.white)
- end
- function mPrint (text, color)
- print(text)
- monitor.setTextColor(color or colors.white)
- monitor.write(text)
- local x, y = monitor.getCursorPos()
- monitor.setCursorPos(1, y + 1)
- monitor.setTextColor(colors.white)
- end
- function mClear ()
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colors.white)
- end
- function round (num, dp)
- local multi = 10^(dp or 0)
- return math.floor(num * multi + 0.5) / multi
- end
- function mError(text)
- mPrint(text, colors.red)
- end
- ----- Displays -----
- -- Storage
- function displayStorage ()
- local storage = getInfo(comps["meComp"]["id"], "checkStorage", comps["meComp"]["pFilter"])
- mClear()
- if not storage then
- mError("ERR: Could not connect to 'meComp'")
- else
- mPrint(round(storage[1]*100, 2).."%") -- storagePercentage
- mPrint(storage[2].." / "..storage[3]) -- currentItems / maxItems
- end
- end
- -- Initialise
- monitor.setCursorBlink(true)
- mClear()
- mWrite("Initialising... ")
- sleep(1.5)
- mPrint("Done")
- mPrint(" ")
- -- Check computers
- for name, info in pairs(comps) do
- mWrite("Pinging '"..name.."'... ")
- sleep(1)
- if ping(info["id"], info["pFilter"]) == true then
- mPrint("PONG")
- sleep(1)
- mWrite("Checking attached system... ")
- sleep(1)
- if check(info["id"], info["pFilter"]) == true then
- mPrint("FOUND")
- else
- mError("ERR: No system found")
- end
- else
- mError("ERR: No computer found")
- end
- mPrint(" ")
- end
- -- Goto default display
- sleep(1)
- mPrint("Default display: "..defaultDisplay)
- sleep(1)
- mPrint("Getting info...")
- sleep(1)
- updateDisplay()
- print("Now in main loop")
- -- Main loop
- while wireless == true do
- -- Update the current display
- updateDisplay()
- sleep(interval)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement