ALpha_R

rcButton.lua

Dec 6th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1.  
  2. -- affichage + gestion des evenement
  3. -- size 5x1 100x10
  4. -- size 2x2 36x24 /0.5/ 18x12 /1.0/
  5. -- boutton = 11*10
  6.  
  7. -------------------------------------------------//Initialisation
  8.  
  9. local rcServer = nil
  10. local monitor = peripheral.wrap("right")
  11. monitor.setTextScale(1)
  12. term.redirect(monitor)
  13. SIZE_X, SIZE_Y = term.getSize()
  14.  
  15. local ptc = "reactorNetwork"
  16. local hostname = "rcButton"
  17.  
  18. rednet.open("top")
  19. rednet.host("reactorNetwork", hostname)
  20.  
  21. local info = {}
  22.  
  23. local on = paintutils.loadImage("On.nfp")
  24. local off = paintutils.loadImage("Off.nfp")
  25. local onTurb = paintutils.loadImage("onTurb.nfp")
  26. local offTurb = paintutils.loadImage("offTurb.nfp")
  27.  
  28. local res
  29. local color = {}
  30.  
  31. -------------------------------------------------//Connection au server
  32.  
  33. print("Connection ...")
  34.  
  35. rcServer = rednet.lookup(ptc, "rcServer")
  36.  
  37. term.clear()
  38.  
  39. -------------------------------------------------//getInfo()
  40.  
  41. function getInfo()
  42.     res, info = rednet.receive(ptc, 10)
  43. end
  44.  
  45. -------------------------------------------------//affichage()
  46.  
  47. function affichage()
  48.     term.setBackgroundColor(colors.black)
  49.     term.clear()
  50.  
  51.     term.setCursorPos(1,2)
  52.     term.write("Reactor " .. (info.rcActive and "[ON]" or "[OFF]"))
  53.     term.setCursorPos(1,5)
  54.     term.write("Turb. 1 " .. (info.tb1Active and "[ON]" or "[OFF]"))
  55.     term.setCursorPos(1,8)
  56.     term.write("Turb. 2 " .. (info.tb2Active and "[ON]" or "[OFF]"))
  57.     term.setCursorPos(1,11)
  58.     term.write("Turb. 3 " .. (info.tb3Active and "[ON]" or "[OFF]"))
  59.  
  60.     paintutils.drawPixel(17, 2, (info.rcActive and colors.green or colors.red))
  61.     paintutils.drawPixel(17, 5, (info.tb1Active and colors.green or colors.red))
  62.     paintutils.drawPixel(17, 8, (info.tb2Active and colors.green or colors.red))
  63.     paintutils.drawPixel(17, 11, (info.tb3Active and colors.green or colors.red))
  64.     paintutils.drawPixel(15, 5, (info.tb1Engaged and colors.green or colors.red))
  65.     paintutils.drawPixel(15, 8, (info.tb2Engaged and colors.green or colors.red))
  66.     paintutils.drawPixel(15, 11, (info.tb3Engaged and colors.green or colors.red)) 
  67. end
  68.  
  69. -------------------------------------------------//shutdownAll()
  70.  
  71. function shutdownAll()
  72.     if(info.rcActive)then
  73.         info.rcActive = false
  74.         info.tb1Active = false
  75.         info.tb2Active = false
  76.         info.tb3Active = false
  77.     else
  78.         info.rcActive = true
  79.         info.tb1Active = true
  80.         info.tb2Active = true
  81.         info.tb3Active = true
  82.     end    
  83. end
  84.  
  85. -------------------------------------------------//button()
  86.  
  87. function button()
  88.     os.startTimer(0.5)
  89.     local event, side, x, y = os.pullEvent()
  90.     os.sleep(0.2)
  91.     if(event == "monitor_touch")then
  92.         if(x == 17 and y == 2)then
  93.           shutdownAll()
  94.         elseif(x ==17)then
  95.             if(y == 5)then
  96.                 info.tb1Active = (not info.tb1Active) and true or false
  97.             end
  98.             if(y == 8)then
  99.                 info.tb2Active = (not info.tb2Active) and true or false    
  100.             end
  101.             if(y == 11)then
  102.                 info.tb3Active = (not info.tb3Active) and true or false
  103.             end
  104.  
  105.             --Button ON/OFF
  106.  
  107.         elseif(x == 15)then
  108.             if(y == 5)then
  109.                 info.tb1Engaged = (not info.tb1Engaged) and true or false
  110.             end
  111.             if(y == 8)then
  112.                 info.tb2Engaged = (not info.tb2Engaged) and true or false
  113.             end
  114.             if(y == 11)then
  115.                 info.tb3Engaged = (not info.tb3Engaged) and true or false              
  116.             end
  117.  
  118.             --Button COIL ENGAGED
  119.  
  120.         end
  121.     end
  122.     if(info.tb1Active == false and info.tb2Active == false and info.tb3Active == false)then
  123.         info.rcActive = false
  124.     end
  125. end
  126.  
  127. -------------------------------------------------//sendInfo()
  128.  
  129. function sendInfo()
  130.     rednet.send(rcServer, info, ptc)
  131. end
  132.  
  133. -------------------------------------------------//Main script
  134.  
  135. while(true)do
  136.     os.sleep(0)
  137.     getInfo()
  138.     affichage()
  139.     button()
  140.     sendInfo()
  141. end
Advertisement
Add Comment
Please, Sign In to add comment