Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Displays Info Regarding House
- -- Generic Variables
- local monitorSide = "right"
- local rednetSide = "bottom"
- local monitor = peripheral.wrap(monitorSide)
- --Display Variables
- local defaultDisplay = "Power"
- local currentDisplay = defaultDisplay
- local cTabColor = colors.black
- local oTabColor = colors.cyan
- local cTextColor = colors.white
- local oTextColor = colors.white
- local tabOrder = {"Status",
- "Storage",
- "Power",
- "Other",
- "Games"}
- local width, height = monitor.getSize()
- local percentBarLength = width - 4 -- Max length of percentage bars
- -- 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"}
- comps["reactorComp"] = {id = 7,
- pFilter = "Reactor_Monitor"}
- -- Ping computers
- function ping (comp)
- local ID = comps[comp]["id"]
- local pFilter = comps[comp]["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 (comp)
- local ID = comps[comp]["id"]
- local pFilter = comps[comp]["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 (comp, msg)
- print(comp.." "..msg)
- local ID = comps[comp]["id"]
- local pFilter = comps[comp]["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
- ----- Generic display functions -----
- function updateDisplay ()
- -- Disable current buttons
- for name, temp in pairs(buttons) do
- --print(name.." "..tostring(buttons[name]))
- if buttons[name]["active"] == true then
- buttons[name]["active"] = false
- end
- end
- if currentDisplay == "Storage" then
- displayStorage()
- elseif currentDisplay == "Power" then
- displayPower()
- end
- end
- function displayTop ()
- local spaceForEach = math.floor((width - 2 - 2 * (#tabOrder - 1)) / #tabOrder)
- monitor.setBackgroundColor(oTabColor)
- mPrint(string.rep(" ", width))
- mWrite(" ")
- for index, tabs in pairs(tabOrder) do
- if tabs == currentDisplay then
- monitor.setBackgroundColor(cTabColor)
- monitor.setTextColor(cTextColor)
- else
- monitor.setBackgroundColor(oTabColor)
- monitor.setTextColor(oTextColor)
- end
- mWrite(tabs..string.rep(" ", spaceForEach - string.len(tabs)))
- monitor.setBackgroundColor(oTabColor)
- mWrite(" ")
- end
- monitor.setCursorPos(1,3)
- monitor.setBackgroundColor(colors.black)
- end
- function selectDisplay(x)
- local spaceForEach = math.floor((width - 2 - 2 * (#tabOrder - 1)) / #tabOrder)
- for i = 1, #tabOrder do
- if x > (1 + (spaceForEach + 2) * (i - 1)) and x < (spaceForEach * i + 2 * i) then
- currentDisplay = tabOrder[i]
- end
- end
- return false
- end
- function checkButtons(x, y)
- for name, temp in pairs(buttons) do
- local button = buttons[name]
- if button["active"] == true and y == button["yPos"] then
- if x >= button["xPos"] and x < button["xPos"] + math.floor(button["width"]/2) then
- buttons[name]["funcF"]()
- elseif x > (button["xPos"] + math.floor(button["width"]/2)) and x <= (button["xPos"] + button["width"]) then
- buttons[name]["funcT"]()
- end
- end
- end
- end
- function percentBar (percent, vCurrent, vMax, title, fgColor, bgColor, textColor, x, y, length)
- monitor.setCursorPos(x, y)
- mWrite(title..string.rep(" ", length - string.len(title) - string.len(percent) - 1)..percent.."%")
- monitor.setCursorPos(x, y + 1)
- monitor.setBackgroundColor(fgColor)
- local numBarsColored = math.ceil(percent/100 * length)
- local preText = math.floor(length / 2) - (string.len(vCurrent) + 1)
- if preText > numBarsColored then
- mWrite(string.rep(" ", numBarsColored), textColor)
- monitor.setBackgroundColor(bgColor)
- mWrite(string.rep(" ", preText - numBarsColored), textColor)
- else
- mWrite(string.rep(" ", preText), textColor)
- end
- local text = tostring(vCurrent).." / "..tostring(vMax)
- local i = 1
- for c in text:gmatch"." do
- if preText + i > numBarsColored then
- monitor.setBackgroundColor(bgColor)
- end
- mWrite(c, textColor)
- i = i + 1
- end
- local postText = length - (preText + string.len(text))
- if monitor.getBackgroundColor() == fgColor then
- mWrite(string.rep(" ", numBarsColored - string.len(text) - preText), textColor)
- monitor.setBackgroundColor(bgColor)
- mWrite(string.rep(" ", length - numBarsColored), textColor)
- else
- mWrite(string.rep(" ", postText), textColor)
- end
- monitor.setBackgroundColor(colors.black)
- monitor.setCursorPos(1, y + 2)
- end
- -- Buttons
- buttons = {}
- buttons["reactorToggle"] = {funcF = function() getInfo("reactorComp", "turnOff") end,
- funcT = function() getInfo("reactorComp", "turnOn") end}
- function displayButton(name, textT, textF, state --[[T/F]], x, y, width)
- buttons[name] = {active = true, xPos = x, yPos = y, width = width}
- if state == true then
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- else
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.white)
- end
- monitor.setCursorPos(x, y)
- mWrite(textF..string.rep(" ", math.floor(width/2) - string.len(textF)))
- monitor.setBackgroundColor(colors.black)
- mWrite(" ")
- if state == true then
- monitor.setBackgroundColor(colors.green)
- monitor.setTextColor(colors.white)
- else
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- end
- mWrite(textT..string.rep(" ", width/2 - string.len(textT)))
- end
- ----- Convenience functions -----
- function mWrite (text, color)
- monitor.setTextColor(color or colors.white)
- monitor.write(text)
- monitor.setTextColor(colors.white)
- end
- function mPrint (text, color)
- 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.setCursorPos(1, 1)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- 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
- function mSleep(time)
- sleep(interval)
- end
- ----- Displays -----
- -- Storage
- function displayStorage ()
- local info = getInfo("meComp", "checkStorage")
- local items = getInfo("meComp", "checkItems")
- mClear()
- displayTop()
- if not info or not items then
- mError("ERR: Could not connect to 'meComp'")
- else
- percent = round(info[1]*100, 2)
- currentBytes = info[2]
- maxBytes = info[3]
- percentBar(percent, currentBytes, maxBytes, "Storage", colors.cyan, colors.white, colors.black, 3, 4, percentBarLength)
- monitor.setCursorPos(3, 7)
- mPrint("There are "..items[1].." total items in the ME System")
- monitor.setCursorPos(3, 8)
- mPrint("Comprised of "..items[3].." unique items")
- end
- end
- -- Power
- function displayPower()
- local info = getInfo("reactorComp", "checkPower")
- mClear()
- displayTop()
- if not info then
- mError("ERR: Could not connect to 'reactorComp'")
- else
- percent = round((info[1]/info[2])*100, 2)
- currentPower = info[1]
- maxPower = info[2]
- percentBar(percent, currentPower, maxPower, "Reactor Power", colors.red, colors.white, colors.black, 3, 4, 3*percentBarLength/4 - 2)
- displayButton("reactorToggle", "On", "Off", getInfo("reactorComp", "checkPower")[3], 3 + 3*percentBarLength/4 + 3, 5, 7)
- monitor.setCursorPos(1, 10)
- end
- end
- -- Click Event
- function click()
- local event, side, x, y = os.pullEvent("monitor_touch")
- if y == 2 then
- if selectDisplay(x) == true then
- updateDisplay()
- return
- end
- end
- checkButtons(x, y)
- end
- -- Initialise
- monitor.setCursorBlink(true)
- mClear()
- monitor.setTextScale(1)
- mWrite("Initialising... ")
- sleep(1)
- mPrint("Done")
- mPrint(" ")
- -- Check computers
- for name, id in pairs(comps) do
- mWrite("Pinging '"..name.."'... ")
- sleep(1)
- if ping(name) == true then
- mPrint("PONG")
- sleep(1)
- mWrite("Checking attached system... ")
- sleep(1)
- if check(name) == true then
- mPrint("FOUND")
- else
- mError("ERR: No system found")
- end
- else
- mError("ERR: No computer found")
- end
- mPrint(" ")
- sleep(1)
- end
- -- Goto default display
- mPrint("Default display: "..defaultDisplay)
- sleep(1)
- mPrint("Getting info...")
- sleep(1)
- monitor.setCursorBlink(false)
- print("Now in main loop")
- -- Main loop
- while wireless == true do
- -- Update the current display
- updateDisplay()
- parallel.waitForAny(mSleep, click)
- end
Advertisement
Add Comment
Please, Sign In to add comment