jseely

ReactorMonitor

Mar 16th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. local monitor = peripheral.wrap("bottom")
  2. local reactorController = peripheral.wrap("right")
  3. local reactorCardData = {}
  4. local homeButtons = {}
  5. local backButton = {}
  6. local infoButton = {}
  7. local Page = 0
  8. local backgroundColor = colors.black
  9. local buttonPositions = {}
  10. local yieldTime
  11.  
  12. local function yield()
  13.     if yieldTime then
  14.         if os.clock() - yieldTime > 2 then
  15.             os.queueEvent("fakeEvent")
  16.             os.pullEvent("fakeEvent")
  17.             yieldTime = os.clock()
  18.         end
  19.     else
  20.         yieldTime = os.clock()
  21.     end
  22. end
  23.  
  24. local function processTouch(x, y)
  25.     click = Point.new(x, y)
  26.     if (not (Page == -1)) and infoButton.isClicked(click) then
  27.         Page = -1
  28.         return
  29.     end
  30.     if Page == 0 then
  31.         for i = 1, 9 do
  32.             if not (homeButtons[i] == nil) then
  33.                 if homeButtons[i].isClicked(click) then
  34.                     Page = i
  35.                 end
  36.             end
  37.             yield()
  38.         end
  39.     elseif backButton.isClicked(click) then
  40.         Page = 0
  41.     end
  42.     os.sleep(0.1)
  43.     yield()
  44. end
  45.  
  46. local function clearMonitor()
  47.     monitor.setBackgroundColor(backgroundColor)
  48.     local x, y = monitor.getSize()
  49.     for i = 1, y do
  50.         monitor.setCursorPos(1, i)
  51.         for j = 1, x do
  52.             monitor.write(" ")
  53.             yield()
  54.         end
  55.     end
  56. end
  57.  
  58. local function getreactorCardData()
  59.     for i = 1, 9 do
  60.         if not (reactorController.get(i) == nil) then
  61.             reactorCardData[i] = {}
  62.             local id, state, title, info = reactorController.get(i)
  63.             reactorCardData[i].id = id
  64.             reactorCardData[i].state = state
  65.             reactorCardData[i].title = title
  66.             reactorCardData[i].info = info
  67.         else
  68.             reactorCardData[i] = nil
  69.         end
  70.         yield()
  71.     end
  72. end
  73.  
  74. local function getHomeButtonColor(reactorData)
  75.     if reactorData.info["timeLeft"] == 0 and reactorData.info["heat"] > 0 then
  76.         return colors.blue
  77.     end
  78.     if reactorData.info["timeLeft"] == 0 and reactorData.info["heat"] == 0 then
  79.         return colors.red
  80.     end
  81.     local heat = reactorData.info["heat"]
  82.     local maxHeat = reactorData.info["maxHeat"]
  83.     if heat > 0.9*maxHeat then
  84.         return colors.orange
  85.     end
  86.     return colors.green
  87. end
  88.  
  89. local function createButtons()
  90.     for i = 1, 9 do
  91.         if not (reactorCardData[i] == nil) then
  92.             color = getHomeButtonColor(reactorCardData[i])
  93.             homeButtons[i] = Button.new(buttonPositions[i].pos, buttonPositions[i].size, reactorCardData[i].title .. "|" .. reactorCardData[i].info["timeLeft"], color, function() switchPage(i) end)
  94.         else
  95.             homeButtons[i] = nil
  96.         end
  97.         yield()
  98.     end
  99. end
  100.  
  101. local function refreshMonitor()
  102.     getreactorCardData()
  103.     clearMonitor()
  104.     if Page == 0 then
  105.         createButtons()
  106.         for i = 1, 9 do
  107.             if not (homeButtons[i] == nil) then
  108.                 homeButtons[i].render(monitor)
  109.             end
  110.             yield()
  111.         end
  112.         infoButton.render(monitor)
  113.     elseif Page == -1 then
  114.         monitor.setCursorPos(1,1)
  115.         monitor.write("Tiles on home page change")
  116.         monitor.setCursorPos(1,2)
  117.         monitor.write("color to represent state:")
  118.         monitor.setCursorPos(1,3)
  119.         monitor.write("Red = Off")
  120.         monitor.setCursorPos(1,4)
  121.         monitor.write("Blue = Off(cooling)")
  122.         monitor.setCursorPos(1,5)
  123.         monitor.write("Green = Running")
  124.         monitor.setCursorPos(1,6)
  125.         monitor.write("Orange = Overheating")
  126.         monitor.setCursorPos(1,8)
  127.         monitor.write("Tile text contains the title")
  128.         monitor.setCursorPos(1,9)
  129.         monitor.write("on the first line and then")
  130.         monitor.setCursorPos(1,10)
  131.         monitor.write("time remaining on the second.")
  132.         monitor.setCursorPos(1,12)
  133.         monitor.write("PS. I have it set up to")
  134.         monitor.setCursorPos(1,13)
  135.         monitor.write("autopull updates on restart.")
  136.         backButton.render(monitor)
  137.     else
  138.         local line = 1
  139.         for tag, data in pairs(reactorCardData[Page].info) do
  140.             monitor.setCursorPos(1,line)
  141.             monitor.write(tag .. " : " ..  tostring(data))
  142.             line = line + 1
  143.         end
  144.         backButton.render(monitor)
  145.         infoButton.render(monitor)
  146.     end
  147.     yield()
  148.     os.sleep(1)
  149. end
  150.  
  151. local function startMonitor()
  152.     while true do
  153.         local timeout = os.startTimer(1)
  154.         event = {os.pullEvent()}
  155.         if event[1] == "monitor_touch" then
  156.             processTouch(event[3], event[4])
  157.         end
  158.         refreshMonitor()
  159.         yield()
  160.     end
  161. end
  162.  
  163. buttonPositions[1] = { pos = Point.new(1,1); size = Point.new(9,6) }
  164. buttonPositions[2] = { pos = Point.new(11,1); size = Point.new(9,6) }
  165. buttonPositions[3] = { pos = Point.new(21,1); size = Point.new(9,6) }
  166. buttonPositions[4] = { pos = Point.new(1,8); size = Point.new(9,5) }
  167. buttonPositions[5] = { pos = Point.new(11,8); size = Point.new(9,5) }
  168. buttonPositions[6] = { pos = Point.new(21,8); size = Point.new(9,5) }
  169. buttonPositions[7] = { pos = Point.new(1,14); size = Point.new(9,6) }
  170. buttonPositions[8] = { pos = Point.new(11,14); size = Point.new(9,6) }
  171. buttonPositions[9] = { pos = Point.new(21,14); size = Point.new(9,6) }
  172. backButton = Button.new(buttonPositions[9].pos, buttonPositions[9].size, "Back", colors.blue, function() switchPage(0) end)
  173. infoButton = Button.new(Point.new(29, 19), Point.new(1,1), "i", colors.blue, function() switchPage(0) end)
  174.  
  175. monitor.setCursorBlink(false)
  176. refreshMonitor()
  177. startMonitor()
Advertisement
Add Comment
Please, Sign In to add comment