Ajustor

monitor

Apr 7th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local serialization = require("serialization")
  4.  
  5. local gpu = component.gpu -- get primary gpu component
  6. local computer = component.computer
  7. local modem = component.modem
  8. local w, h = gpu.getResolution()
  9. local energy = {}
  10. local arraySize=0
  11. local maxEnergy=0
  12. local currEnergy=0
  13.  
  14. modem.open(17)
  15.  
  16. if not modem.isOpen(17) then
  17.   print("Modem failed to open on port 17. Exiting...")
  18.   os.exit()
  19. end
  20.  
  21. local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
  22.   black = 0x000000, white = 0xFFFFFF, grey = 0x47494C, lightGrey = 0xBBBBBB, orange = 0xFFA500, alert = 0xFF0000}
  23.  
  24.  
  25. function parseMsg(psMsgRaw)
  26. local loS = require("serialization")
  27.  
  28.   -- Parse the raw message into a destination and a command
  29.  
  30.   local ltMsg = loS.unserialize(tostring(psMsgRaw))
  31.   currEnergy = ltMsg.curr
  32.   maxEnergy = ltMsg.max
  33.  
  34. end
  35.  
  36. function show()
  37. gpu.set( w/2-7, 1, "Current energy")
  38. gpu.setBackground(colors.grey)
  39.   gpu.fill( w*(currEnergy/maxEnergy), 2, w, 4, " ")
  40. if currEnergy > maxEnergy*66/100 then
  41.     gpu.setBackground(colors.green)
  42.   elseif currEnergy > maxEnergy*33/100 then
  43.     gpu.setBackground(colors.orange)
  44.   elseif currEnergy > maxEnergy*11/100 then
  45.     gpu.setBackground(colors.red)
  46.   else
  47.     gpu.setBackground(colors.alert)
  48.     computer.beep()
  49.   end
  50.   gpu.fill( 1, 2, w*(currEnergy/maxEnergy), 4, " ")
  51.  
  52.   --gpu.set( 1, 4, currEnergy .. "/" .. maxEnergy)
  53.   gpu.setBackground(colors.black)
  54.  
  55. end
  56.  
  57. gpu.fill(1, 1, w, h, " ")
  58. while true do
  59.     _, _, _, _, _, msg = event.pull("modem_message")
  60.     parseMsg(msg)
  61.     show()
  62. end
Advertisement
Add Comment
Please, Sign In to add comment