Vladexus7

Bouton tactile

Nov 19th, 2025 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | Software | 0 0
  1. local state = false
  2. local modemSide = "back"
  3. local modem = peripheral.wrap(modemSide)
  4. local width, height = term.getSize()
  5. local data = {"info","off","Temp",0,"Coolant",0,"Damage",0,"Waste",0)
  6.  
  7. local yh,yb = math.floor(height/2+3),height-1
  8. local xg,xd = 2,width-1
  9.  
  10. local buttonY = math.floor(3 * height / 4)
  11.  
  12. remoteID = 14
  13.  
  14. rednet.open(modemSide)
  15.  
  16. local function sendData(Data)
  17.     rednet.send(remoteID,Data)
  18. end
  19.  
  20. -- Fonction pour dessiner le bouton switch ON/OFF au centre
  21. local function drawButton(state, text)
  22.     --term.clear()
  23.     term.setCursorPos(xg,yh)
  24.  
  25.     local bgColor = colors.red
  26.     local fgColor = colors.white
  27.     if state then
  28.         bgColor = colors.green
  29.         fgColor = colors.black
  30.     end
  31.  
  32.     -- Dessine le fond ecran tout en couleur du switch
  33.     for y = yh, yb do
  34.         term.setCursorPos(xg,y)
  35.         term.setBackgroundColor(bgColor)
  36.         term.write(string.rep(" ",xd-xg+1))
  37.         --term.clear()
  38.     end
  39.  
  40.     -- Calcule la position du texte pour le centrer
  41.     local textLen = #text
  42.     local x = math.floor((width - textLen) / 2) + 1
  43.     local y = math.floor((yh+yb)/2)
  44.  
  45.     term.setCursorPos(x, y)
  46.     term.setTextColor(fgColor)
  47.     term.write(text)
  48. end
  49.  
  50. -- Fonction d attente d un clic tactile, retourne true si sur le bouton
  51. local function waitTouch()
  52.     while true do
  53.         local event, side, x, y = os.pullEvent("monitor_touch")
  54.         if event == "mouse_click" then  
  55.             if y == buttonY then
  56.                 return true
  57.             end
  58.         end
  59.     end
  60. end
  61.  
  62. local function chState()
  63.     while true do
  64.         if state then
  65.             drawButton(true, "ON")
  66.         else
  67.             drawButton(false, "OFF")
  68.         end
  69.         -- Attend un clic (toucher) clavier de l utilisateur
  70.         local event, button, x, y = os.pullEvent("mouse_click")
  71.         if y >= yh and y <= yb and x>=xg and x<=xd then
  72.             state = not state -- Change l etat
  73.               sendData(state)
  74.         end
  75.     end
  76. end
  77.  
  78. local function dataDisplay()
  79.     --TODO : display
  80. end
  81.  
  82. local function dataUpdate()
  83.     data = rednet.receive()
  84. end
  85.  
  86. local function main()
  87.     --TODO : multithread incluant le multithread de dataUpdate et chState
  88. end
Advertisement
Add Comment
Please, Sign In to add comment