Advertisement
Guest User

gratest

a guest
Mar 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. local gpuutils = require("screen_utils")
  2. local component = require("component")
  3. local term = require("term")
  4. local event = require("event")
  5. local serialization = require("serialization")
  6.  
  7. local modem = component.modem
  8. local running = true
  9.  
  10. local charges = {}
  11. local currentChargeX = 30
  12. local currentChargeY = 10
  13.  
  14. function DrawBattery(x,y,charge,name)
  15.  
  16.   fullH = 4
  17.  
  18.   DrawRectangle(x,y,3,fullH,0x990000)
  19.   height = fullH * charge
  20.   y = y+(fullH-height)
  21.   DrawRectangle(x,y,3,height,0x009900)
  22.  
  23. end
  24.  
  25. function GetDataTable()
  26.  
  27. end
  28.  
  29. local eventHandler = setmetatable({}, { __index = function() return unknownEvent end })
  30.  
  31. function unknownEvent()
  32.  
  33. end
  34.  
  35. function eventHandler.touch(id, x, y)
  36.   print("Touch "..x..","..y)
  37.   running = false;
  38. end
  39.  
  40. function eventHandler.modem_message(_,_,from,port,_,message)
  41.     if (_) then
  42.       value = serialization.unserialize(_)
  43.       if (value["id"] == "charge_report") then
  44.         if (not charges[value["address"]]) then
  45.           newCharge = {}
  46.           newCharge["x"] = currentChargeX;
  47.           newCharge["y"] = currentChargeY;
  48.           currentChargeX = currentChargeX + 15
  49.           charges[value["address"]] = newCharge  
  50.         end
  51.         bat = charges[value["address"]]
  52.         DrawBattery(bat["x"], bat["y"], value["charge"], "Charge")
  53.       end
  54.     end
  55. end
  56.  
  57. function HandleEvent(eventId, ...)
  58.  
  59. --  print("event id:"..eventId)
  60.   if (eventId) then
  61.     eventHandler[eventId](...)
  62.   end
  63. end
  64.  
  65. modem.open(20)
  66. print(modem.isOpen(20))
  67.  
  68. while(running) do
  69.   HandleEvent(event.pull())
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement