sshikamaru

reception.lua

Dec 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. --Liste des require
  2. local computer = require("computer")
  3. local component = require("component")
  4. local term = require("term")
  5. local math = require("math")
  6. local event = require("event")
  7. local string = require("string")
  8. local unicode = require("unicode")
  9. local colors = require("colors")
  10. local sides = require("sides")
  11. local serialization = require("serialization")
  12.  
  13. --Récupération du matériel
  14. local screen = component.get("d191","screen")
  15. local gpu = component.gpu
  16. local modem = component.modem
  17.  
  18. --Ouverture des ports
  19. modem.open(1001)
  20.  
  21. --Résolution
  22. gpu.bind(screen)
  23. gpu.setResolution(63,35)
  24.  
  25. --Déclaration de variables
  26. local value_receive
  27.  
  28. --Fonctions
  29. function loadbar(x,y,width,cur,text,bg)
  30.   local raw = " " .. text ..string.rep(" ", width - unicode.len(text) - 2) .. " "
  31.   local oldbg = gpu.setBackground(bg)
  32.   local oldfg = gpu.setForeground(0xFFFFFF - bg)
  33.   gpu.set(x,y,unicode.sub(raw,1,cur))
  34.   gpu.setBackground(oldbg)
  35.   gpu.setForeground(oldfg)
  36.   gpu.set(x+cur,y,unicode.sub(raw,cur+1,width))
  37. end
  38.  
  39. local function afflevel(x,y,length,tableau)
  40.   local amount = tableau[1]
  41.   local capacity = tableau[2]
  42.   local pct = tableau[3]
  43.   local color = 0x00FFFF
  44.   local cur = math.floor(pct * length)
  45.   local textfrac = string.format("%s / %s", amount, capacity)
  46.   local textpct = string.format("%.02f%%", pct*100)
  47.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  48.   local dir = ""
  49.   loadbar(x,y,length,cur,dir,color)
  50.   loadbar(x,y+1,length,cur,dir,color)
  51.   loadbar(x,y+2,length,cur,text,color)
  52. end  
  53.  
  54.  
  55.  
  56. --Trame de fond
  57.  gpu.set(1,1,"╔═════════════════════════════════════════════════════════════╗")
  58.  gpu.set(1,2,"║                                                             ║")
  59.  gpu.set(1,3,"║                                                             ║")
  60.  gpu.set(1,4,"║                                                             ║")
  61.  gpu.set(1,5,"║                                                             ║")
  62.  gpu.set(1,6,"║                                                             ║")
  63.  gpu.set(1,7,"║                                                             ║")
  64.  gpu.set(1,8,"║                                                             ║")
  65.  gpu.set(1,9,"║                                                             ║")
  66. gpu.set(1,10,"║                                                             ║")
  67. gpu.set(1,11,"║                                                             ║")
  68. gpu.set(1,12,"║                                                             ║")
  69. gpu.set(1,13,"║                                                             ║")
  70. gpu.set(1,14,"║                                                             ║")
  71. gpu.set(1,15,"║                                                             ║")
  72. gpu.set(1,16,"║                                                             ║")
  73. gpu.set(1,17,"║                                                             ║")
  74. gpu.set(1,18,"║                                                             ║")
  75. gpu.set(1,19,"║                                                             ║")
  76. gpu.set(1,20,"║                                                             ║")
  77. gpu.set(1,21,"║                                                             ║")
  78. gpu.set(1,22,"║                                                             ║")
  79. gpu.set(1,23,"║                                                             ║")
  80. gpu.set(1,24,"║                                                             ║")
  81. gpu.set(1,25,"║                                                             ║")
  82. gpu.set(1,26,"║                                                             ║")
  83. gpu.set(1,27,"║                                                             ║")
  84. gpu.set(1,28,"║                                                             ║")
  85. gpu.set(1,29,"║                                                             ║")
  86. gpu.set(1,30,"║                                                             ║")
  87. gpu.set(1,31,"║                                                             ║")
  88. gpu.set(1,32,"║                                                             ║")
  89. gpu.set(1,33,"║                                                             ║")
  90. gpu.set(1,34,"║                                                             ║")
  91. gpu.set(1,35,"╚═════════════════════════════════════════════════════════════╝")
  92.  
  93.  
  94. local function drawbars()
  95.   afflevel(2,7,61,value_receive)
  96. end
  97.  
  98. local function onModem(_,_,from,port,_,message)
  99.   value_receive = serialization.unserialize(message)
  100.   gpu.set(2,15,string.format("%.02f°C", value_receive))
  101. end
  102.  
  103. local function onTouch(_,address,x,y,_,pseudo)
  104.   gpu.set(2,2,"X      : "..string.format("% 3s",x))
  105.   gpu.set(2,3,"Y      : "..string.format("% 3s",y))
  106.   gpu.set(2,4,"Pseudo : "..pseudo)
  107.  
  108.   if x~=1 and y~=1 then
  109.  
  110.   elseif x==1 and y==1 then
  111.     computer.pushSignal("quit")
  112.     gpu.fill(1,1,63,35," ")
  113.     term.setCursor(1,1)
  114.     return false
  115.   end
  116. end
  117.  
  118. local function onTimer(_,timer)
  119.   drawbars()
  120.   return true
  121. end
  122.  
  123. event.listen("touch",onTouch)
  124. event.listen("modem_message",onModem)
  125. local timer = event.timer(1,onTimer,math.huge)
  126. event.pull("quit")
  127. event.cancel(timer)
  128. event.ignore("touch",onTouch)
  129. event.ignore("modem_message",onModem)
  130. gpu.fill(1,1,63,35," ")
  131. gpu.setResolution(160,50)
Add Comment
Please, Sign In to add comment