Guest User

startup

a guest
Sep 21st, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.62 KB | None | 0 0
  1. -- Displays Info Regarding House
  2. -- Generic Variables
  3. local monitorSide = "right"
  4. local rednetSide = "bottom"
  5. local monitor = peripheral.wrap(monitorSide)
  6.  
  7. --Display Variables
  8. local defaultDisplay = "Power"
  9. local currentDisplay = defaultDisplay
  10. local cTabColor = colors.black
  11. local oTabColor = colors.cyan
  12. local cTextColor = colors.white
  13. local oTextColor = colors.white
  14. local tabOrder = {"Status",
  15.                   "Storage",
  16.                   "Power",
  17.                   "Other",
  18.                   "Games"}
  19. local width, height = monitor.getSize()
  20. local percentBarLength = width - 4 -- Max length of percentage bars
  21.  
  22. -- Rednet Variables
  23. local wireless = true
  24. local timeout = 5 -- Number of seconds to wait
  25.                   -- when receiving messages before
  26.                   -- timing out
  27. local interval = 1 -- Number of seconds between
  28.                    -- updates
  29.  
  30. if wireless == true then
  31.   rednet.open(rednetSide)
  32. end
  33.  
  34. -- Computer ID's and pFilter's
  35. comps = {}
  36. comps["meComp"] = {id = 4,
  37.                    pFilter = "Storage_Monitor"}
  38. comps["reactorComp"] = {id = 7,
  39.                    pFilter = "Reactor_Monitor"}
  40.  
  41. -- Ping computers
  42. function ping (comp)
  43.   local ID = comps[comp]["id"]
  44.   local pFilter = comps[comp]["pFilter"]
  45.   rednet.send(ID, "ping", pFilter)
  46.   local senderID, message, protocol = rednet.receive(timeout)
  47.   if not message then
  48.     return false
  49.   elseif message == "pong" and senderID == ID and protocol == pFilter then
  50.     return true
  51.   else
  52.     return false
  53.   end  
  54. end
  55.  
  56. -- Check computers
  57. function check (comp)
  58.   local ID = comps[comp]["id"]
  59.   local pFilter = comps[comp]["pFilter"]
  60.   rednet.send(ID, "check", pFilter)
  61.   local senderID, message, protocol = rednet.receive(timeout)
  62.   if not message then
  63.     return false
  64.   elseif message == "found" and senderID == ID and protocol == pFilter then
  65.     return true
  66.   else
  67.     return false
  68.   end
  69. end  
  70.  
  71. -- Get info
  72. function getInfo (comp, msg)
  73.   print(comp.." "..msg)
  74.   local ID = comps[comp]["id"]
  75.   local pFilter = comps[comp]["pFilter"]
  76.   rednet.send(ID, msg, pFilter)
  77.   local senderID, message, protocol = rednet.receive(timeout)
  78.   if not message then
  79.     return false
  80.   elseif senderID == ID and protocol == pFilter then
  81.     return message
  82.   else
  83.     return false
  84.   end
  85. end
  86.  
  87. ----- Generic display functions -----
  88. function updateDisplay ()
  89.   -- Disable current buttons
  90.   for name, temp in pairs(buttons) do
  91.     --print(name.." "..tostring(buttons[name]))
  92.     if buttons[name]["active"] == true then
  93.       buttons[name]["active"] = false
  94.     end
  95.   end
  96.  
  97.   if currentDisplay == "Storage" then
  98.     displayStorage()
  99.   elseif currentDisplay == "Power" then
  100.     displayPower()
  101.   end
  102. end
  103.  
  104. function displayTop ()
  105.   local spaceForEach = math.floor((width - 2 - 2 * (#tabOrder - 1)) / #tabOrder)
  106.   monitor.setBackgroundColor(oTabColor)
  107.   mPrint(string.rep(" ", width))
  108.   mWrite(" ")
  109.   for index, tabs in pairs(tabOrder) do
  110.     if tabs == currentDisplay then
  111.       monitor.setBackgroundColor(cTabColor)
  112.       monitor.setTextColor(cTextColor)
  113.     else
  114.       monitor.setBackgroundColor(oTabColor)
  115.       monitor.setTextColor(oTextColor)
  116.     end
  117.     mWrite(tabs..string.rep(" ", spaceForEach - string.len(tabs)))
  118.     monitor.setBackgroundColor(oTabColor)
  119.     mWrite("  ")
  120.   end
  121.   monitor.setCursorPos(1,3)
  122.   monitor.setBackgroundColor(colors.black)
  123. end
  124.  
  125. function selectDisplay(x)
  126.   local spaceForEach = math.floor((width - 2 - 2 * (#tabOrder - 1)) / #tabOrder)
  127.   for i = 1, #tabOrder do
  128.     if x > (1 + (spaceForEach + 2) * (i - 1)) and x < (spaceForEach * i + 2 * i) then
  129.       currentDisplay = tabOrder[i]
  130.     end
  131.   end
  132.   return false    
  133. end
  134.  
  135. function checkButtons(x, y)
  136.   for name, temp in pairs(buttons) do
  137.     local button = buttons[name]
  138.     if button["active"] == true and y == button["yPos"] then
  139.       if x >= button["xPos"] and x < button["xPos"] + math.floor(button["width"]/2) then
  140.         buttons[name]["funcF"]()
  141.       elseif x > (button["xPos"] + math.floor(button["width"]/2)) and x <= (button["xPos"] + button["width"]) then
  142.         buttons[name]["funcT"]()
  143.       end
  144.     end
  145.   end
  146. end
  147.  
  148. function percentBar (percent, vCurrent, vMax, title, fgColor, bgColor, textColor, x, y, length)
  149.   monitor.setCursorPos(x, y)
  150.   mWrite(title..string.rep(" ", length - string.len(title) - string.len(percent) - 1)..percent.."%")
  151.   monitor.setCursorPos(x, y + 1)
  152.   monitor.setBackgroundColor(fgColor)
  153.  
  154.   local numBarsColored = math.ceil(percent/100 * length)
  155.   local preText = math.floor(length / 2) - (string.len(vCurrent) + 1)
  156.   if preText > numBarsColored then
  157.     mWrite(string.rep(" ", numBarsColored), textColor)
  158.     monitor.setBackgroundColor(bgColor)
  159.     mWrite(string.rep(" ", preText - numBarsColored), textColor)
  160.   else
  161.     mWrite(string.rep(" ", preText), textColor)
  162.   end
  163.  
  164.   local text = tostring(vCurrent).." / "..tostring(vMax)
  165.   local i = 1
  166.   for c in text:gmatch"." do
  167.     if preText + i > numBarsColored then
  168.       monitor.setBackgroundColor(bgColor)
  169.     end
  170.     mWrite(c, textColor)
  171.     i = i + 1
  172.   end
  173.   local postText = length - (preText + string.len(text))
  174.   if monitor.getBackgroundColor() == fgColor then
  175.     mWrite(string.rep(" ", numBarsColored - string.len(text) - preText), textColor)
  176.     monitor.setBackgroundColor(bgColor)
  177.     mWrite(string.rep(" ", length - numBarsColored), textColor)
  178.   else
  179.     mWrite(string.rep(" ", postText), textColor)    
  180.   end
  181.   monitor.setBackgroundColor(colors.black)
  182.   monitor.setCursorPos(1, y + 2)
  183. end
  184.  
  185. -- Buttons
  186. buttons = {}
  187. buttons["reactorToggle"] = {funcF = function() getInfo("reactorComp", "turnOff") end,
  188.                             funcT = function() getInfo("reactorComp", "turnOn") end}
  189.  
  190. function displayButton(name, textT, textF, state --[[T/F]], x, y, width)
  191.   buttons[name] = {active = true, xPos = x, yPos = y, width = width}
  192.   if state == true then
  193.     monitor.setBackgroundColor(colors.black)
  194.     monitor.setTextColor(colors.white)
  195.   else
  196.     monitor.setBackgroundColor(colors.red)
  197.     monitor.setTextColor(colors.white)
  198.   end
  199.   monitor.setCursorPos(x, y)
  200.   mWrite(textF..string.rep(" ", math.floor(width/2) - string.len(textF)))
  201.  
  202.   monitor.setBackgroundColor(colors.black)
  203.   mWrite(" ")
  204.   if state == true then
  205.     monitor.setBackgroundColor(colors.green)
  206.     monitor.setTextColor(colors.white)
  207.   else
  208.     monitor.setBackgroundColor(colors.black)
  209.     monitor.setTextColor(colors.white)
  210.   end
  211.   mWrite(textT..string.rep(" ", width/2 - string.len(textT)))  
  212. end
  213.  
  214. ----- Convenience functions -----
  215. function mWrite (text, color)
  216.   monitor.setTextColor(color or colors.white)
  217.   monitor.write(text)
  218.   monitor.setTextColor(colors.white)
  219. end
  220.  
  221. function mPrint (text, color)
  222.   monitor.setTextColor(color or colors.white)
  223.   monitor.write(text)
  224.   local x, y = monitor.getCursorPos()
  225.   monitor.setCursorPos(1, y + 1)
  226.   monitor.setTextColor(colors.white)
  227. end
  228.  
  229. function mClear ()
  230.   monitor.setCursorPos(1, 1)
  231.   monitor.setTextColor(colors.white)
  232.   monitor.setBackgroundColor(colors.black)
  233.   monitor.clear()
  234. end
  235.  
  236. function round (num, dp)
  237.   local multi = 10^(dp or 0)
  238.   return math.floor(num * multi + 0.5) / multi
  239. end
  240.  
  241. function mError(text)
  242.   mPrint(text, colors.red)
  243. end
  244.  
  245. function mSleep(time)
  246.   sleep(interval)
  247. end
  248.  
  249. ----- Displays -----
  250. -- Storage
  251. function displayStorage ()
  252.   local info = getInfo("meComp", "checkStorage")
  253.   local items = getInfo("meComp", "checkItems")
  254.  
  255.   mClear()
  256.   displayTop()
  257.   if not info or not items then
  258.     mError("ERR: Could not connect to 'meComp'")
  259.   else
  260.     percent = round(info[1]*100, 2)
  261.     currentBytes = info[2]
  262.     maxBytes = info[3]
  263.     percentBar(percent, currentBytes, maxBytes, "Storage", colors.cyan, colors.white, colors.black, 3, 4, percentBarLength)  
  264.    
  265.     monitor.setCursorPos(3, 7)
  266.     mPrint("There are "..items[1].." total items in the ME System")
  267.     monitor.setCursorPos(3, 8)
  268.     mPrint("Comprised of "..items[3].." unique items")
  269.   end
  270. end
  271.  
  272. -- Power
  273. function displayPower()
  274.   local info = getInfo("reactorComp", "checkPower")
  275.  
  276.   mClear()
  277.   displayTop()
  278.   if not info then
  279.     mError("ERR: Could not connect to 'reactorComp'")
  280.   else
  281.     percent = round((info[1]/info[2])*100, 2)
  282.     currentPower = info[1]
  283.     maxPower = info[2]
  284.     percentBar(percent, currentPower, maxPower, "Reactor Power", colors.red, colors.white, colors.black, 3, 4, 3*percentBarLength/4 - 2)
  285.  
  286.     displayButton("reactorToggle", "On", "Off", getInfo("reactorComp", "checkPower")[3], 3 + 3*percentBarLength/4 + 3, 5, 7)
  287.     monitor.setCursorPos(1, 10)
  288.   end
  289. end
  290.  
  291. -- Click Event
  292. function click()
  293.   local event, side, x, y = os.pullEvent("monitor_touch")
  294.   if y == 2 then
  295.     if selectDisplay(x) == true then
  296.       updateDisplay()
  297.       return
  298.     end
  299.   end
  300.  
  301.   checkButtons(x, y)
  302. end
  303.  
  304. -- Initialise
  305. monitor.setCursorBlink(true)
  306. mClear()
  307. monitor.setTextScale(1)
  308. mWrite("Initialising...   ")
  309. sleep(1)
  310. mPrint("Done")
  311. mPrint(" ")
  312.  
  313. -- Check computers
  314. for name, id in pairs(comps) do
  315.   mWrite("Pinging '"..name.."'...   ")
  316.   sleep(1)
  317.   if ping(name) == true then
  318.     mPrint("PONG")
  319.     sleep(1)
  320.     mWrite("Checking attached system...   ")
  321.     sleep(1)
  322.     if check(name) == true then
  323.       mPrint("FOUND")
  324.     else
  325.       mError("ERR: No system found")
  326.     end
  327.   else
  328.     mError("ERR: No computer found")
  329.   end
  330.   mPrint(" ")
  331.   sleep(1)
  332. end
  333.  
  334. -- Goto default display
  335. mPrint("Default display: "..defaultDisplay)
  336. sleep(1)
  337. mPrint("Getting info...")
  338. sleep(1)
  339. monitor.setCursorBlink(false)
  340.  
  341. print("Now in main loop")
  342. -- Main loop
  343. while wireless == true do
  344.   -- Update the current display
  345.   updateDisplay()
  346.   parallel.waitForAny(mSleep, click)
  347. end
Advertisement
Add Comment
Please, Sign In to add comment