Advertisement
Guest User

startup

a guest
Sep 18th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. -- Displays Info Regarding House
  2.  
  3. -- Setup Variables
  4. local monitorSide = "right"
  5. local rednetSide = "bottom"
  6. local monitor = peripheral.wrap(monitorSide)
  7. local defaultDisplay = "storage"
  8. local currentDisplay = defaultDisplay
  9.  
  10. -- Rednet Variables
  11. local wireless = true
  12. local timeout = 5 -- Number of seconds to wait
  13.                   -- when receiving messages before
  14.                   -- timing out
  15. local interval = 1 -- Number of seconds between
  16.                    -- updates
  17.  
  18. if wireless == true then
  19.   rednet.open(rednetSide)
  20. end
  21.  
  22. -- Computer ID's and pFilter's
  23. comps = {}
  24. comps["meComp"] = {id = 4, pFilter = "Storage_Monitor"}
  25.  
  26. -- Ping computers
  27. function ping (ID, pFilter)
  28.   rednet.send(ID, "ping", pFilter)
  29.   local senderID, message, protocol = rednet.receive(timeout)
  30.   if not message then
  31.     return false
  32.   elseif message == "pong" and senderID == ID and protocol == pFilter then
  33.     return true
  34.   else
  35.     return false
  36.   end  
  37. end
  38.  
  39. -- Check computers
  40. function check (ID, pFilter)
  41.   rednet.send(ID, "check", pFilter)
  42.   local senderID, message, protocol = rednet.receive(timeout)
  43.   if not message then
  44.     return false
  45.   elseif message == "found" and senderID == ID and protocol == pFilter then
  46.     return true
  47.   else
  48.     return false
  49.   end
  50. end  
  51.  
  52. -- Get info
  53. function getInfo (ID, msg, pFilter)
  54.   rednet.send(ID, msg, pFilter)
  55.   local senderID, message, protocol = rednet.receive(timeout)
  56.   if not message then
  57.     return false
  58.   elseif senderID == ID and protocol == pFilter then
  59.     return message
  60.   else
  61.     return false
  62.   end
  63. end
  64.  
  65. -- Switch
  66. function updateDisplay ()
  67.   if currentDisplay == "storage" then
  68.     displayStorage()
  69.   end
  70. end
  71.  
  72. ----- Convenience functions -----
  73. function mWrite (text, color)
  74.   write(text)
  75.   monitor.setTextColor(color or colors.white)
  76.   monitor.write(text)
  77.   monitor.setTextColor(colors.white)
  78. end
  79.  
  80. function mPrint (text, color)
  81.   print(text)
  82.   monitor.setTextColor(color or colors.white)
  83.   monitor.write(text)
  84.   local x, y = monitor.getCursorPos()
  85.   monitor.setCursorPos(1, y + 1)
  86.   monitor.setTextColor(colors.white)
  87. end
  88.  
  89. function mClear ()
  90.   monitor.setBackgroundColor(colors.black)
  91.   monitor.clear()
  92.   monitor.setCursorPos(1, 1)
  93.   monitor.setTextColor(colors.white)
  94. end
  95.  
  96. function round (num, dp)
  97.   local multi = 10^(dp or 0)
  98.   return math.floor(num * multi + 0.5) / multi
  99. end
  100.  
  101. function mError(text)
  102.   mPrint(text, colors.red)
  103. end
  104.  
  105. ----- Displays -----
  106. -- Storage
  107. function displayStorage ()
  108.   local storage = getInfo(comps["meComp"]["id"], "checkStorage", comps["meComp"]["pFilter"])
  109.   mClear()
  110.   if not storage then
  111.     mError("ERR: Could not connect to 'meComp'")
  112.   else
  113.     mPrint(round(storage[1]*100, 2).."%") -- storagePercentage
  114.     mPrint(storage[2].." / "..storage[3]) -- currentItems / maxItems
  115.   end
  116. end
  117.  
  118. -- Initialise
  119. monitor.setCursorBlink(true)
  120. mClear()
  121. mWrite("Initialising...   ")
  122. sleep(1.5)
  123. mPrint("Done")
  124. mPrint(" ")
  125.  
  126.  
  127. -- Check computers
  128. for name, info in pairs(comps) do
  129.   mWrite("Pinging '"..name.."'...   ")
  130.   sleep(1)
  131.   if ping(info["id"], info["pFilter"]) == true then
  132.     mPrint("PONG")
  133.     sleep(1)
  134.     mWrite("Checking attached system...   ")
  135.     sleep(1)
  136.     if check(info["id"], info["pFilter"]) == true then
  137.       mPrint("FOUND")
  138.     else
  139.       mError("ERR: No system found")
  140.     end
  141.   else
  142.     mError("ERR: No computer found")
  143.   end
  144.   mPrint(" ")
  145. end
  146.  
  147. -- Goto default display
  148. sleep(1)
  149. mPrint("Default display: "..defaultDisplay)
  150. sleep(1)
  151. mPrint("Getting info...")
  152. sleep(1)
  153. updateDisplay()
  154.  
  155.  
  156. print("Now in main loop")
  157. -- Main loop
  158. while wireless == true do
  159.   -- Update the current display
  160.   updateDisplay()
  161.   sleep(interval)
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement