Advertisement
MrFoxit

Energy Cell Reader3 : Slave

Mar 27th, 2025 (edited)
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. if not mon then
  3.     print("Aucun monitor détecté")
  4.     return
  5. end
  6.  
  7. rednet.open("right") -- adapte à ton modem
  8.  
  9. mon.setTextScale(0.5)
  10. local w, h = mon.getSize()
  11.  
  12. function getBarColor(percent)
  13.     if percent < 0.3 then return colors.red
  14.     elseif percent < 0.6 then return colors.orange
  15.     else return colors.lime end
  16. end
  17.  
  18. function drawBar(x, y, percent, width)
  19.     local filled = math.floor(percent * width)
  20.     local empty = width - filled
  21.     mon.setCursorPos(x, y)
  22.     mon.setBackgroundColor(getBarColor(percent))
  23.     mon.write(string.rep(" ", filled))
  24.     mon.setBackgroundColor(colors.gray)
  25.     mon.write(string.rep(" ", empty))
  26.     mon.setBackgroundColor(colors.black)
  27. end
  28.  
  29. function clearLine(y)
  30.     mon.setCursorPos(1, y)
  31.     mon.setBackgroundColor(colors.black)
  32.     mon.write(string.rep(" ", w))
  33. end
  34.  
  35. while true do
  36.     local id, data = rednet.receive("energy_display")
  37.  
  38.     if data then
  39.         mon.setBackgroundColor(colors.black)
  40.  
  41.         for i, cell in ipairs(data.cells) do
  42.             local y = i * 2 - 1
  43.             clearLine(y)
  44.  
  45.             local nomCellule = string.format("Cellule %-2d", i)
  46.             mon.setCursorPos(1, y)
  47.             mon.setTextColor(colors.yellow)
  48.             mon.write(string.format("[%s]", nomCellule))
  49.  
  50.             drawBar(10, y, cell.percent, 20)
  51.  
  52.             mon.setCursorPos(32, y)
  53.             mon.setTextColor(colors.white)
  54.             mon.write(string.format("%3d%%", cell.percent * 100))
  55.  
  56.             mon.setCursorPos(40, y)
  57.             if cell.delta > 0 then
  58.                 mon.setTextColor(colors.lime)
  59.             elseif cell.delta < 0 then
  60.                 mon.setTextColor(colors.red)
  61.             else
  62.                 mon.setTextColor(colors.gray)
  63.             end
  64.             mon.write(string.format("%+4d RF/t ", cell.delta))
  65.         end
  66.  
  67.         -- Affichage global
  68.         local globalBarY = h - 1
  69.         clearLine(globalBarY)
  70.         clearLine(h)
  71.  
  72.         drawBar(10, globalBarY, data.globalPercent, 20)
  73.  
  74.         mon.setCursorPos(1, globalBarY)
  75.         mon.setTextColor(colors.cyan)
  76.         mon.write("[GLOBAL]")
  77.  
  78.         mon.setCursorPos(32, globalBarY)
  79.         mon.setTextColor(colors.white)
  80.         mon.write(string.format("%3d%%", data.globalPercent * 100))
  81.  
  82.         mon.setCursorPos(1, h)
  83.         mon.setTextColor(colors.lightGray)
  84.         mon.write("Estimation : " .. data.estTime .. "          ")
  85.     end
  86. end
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement