Advertisement
ramdor

BatteryDisplay

Jan 14th, 2013
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. -- Battery LCD display - Ramdor 2013 - v1.1
  2. -- Handles any number of tier1 or tier2 nuclear readers
  3. -- Handles any number of cards
  4. -- Outputs redstone signals on all unused faces (ie those without nuclear readers)
  5. --
  6. -- v1.1 - fix to redstone.setBundledOutput(side,nRed) derp
  7. -- v1.0 - release
  8. --
  9. -- You may edit/copy/redistribute/whatever this
  10. -- code in any way.  Just a credit listing this
  11. -- pastebin link (pastebin.com/WhYwt813) will be fine :)
  12. --
  13. -- seems like the id for energy sensor location card
  14. -- changed to LSB 2.  Swap the two following lines if cards are
  15. -- not found
  16. local sEnergySensorLocCardID = "00000000-0000-0000-0000-000000000002"
  17. -- local sEnergySensorLocCardID = "00000000-0000-0000-0000-000000000000"
  18.  
  19. -- The number of leds to use
  20. -- maximum 16.
  21. local nTotalLeds = 12
  22.  
  23. term.clear()
  24. term.setCursorPos(1,1)
  25. print("Battery Display - Ramdor 2013 - v1.0")
  26. print("CTRL-T to terminate")
  27.  
  28. local nReaders = 0
  29. local nWait = 5
  30. local nrs = {}
  31. local nrSides = {}
  32.  
  33. for i, side in pairs(rs.getSides()) do
  34.   if peripheral.getType(side) == "nuclearReader" then
  35.     table.insert(nrs, peripheral.wrap(side))
  36.     nrSides[side] = true
  37.     nReaders = nReaders + 1
  38.   else
  39.     nrSides[side] = false
  40.   end
  41. end
  42.  
  43. if (nReaders==0) then
  44.   print("No readers found")
  45.   do return end
  46. end
  47.  
  48. local nr
  49. local nSlots
  50. local data
  51. local nMax
  52. local nEnergy
  53. local fPerc
  54. local nLeds
  55. local nRed
  56. local nCards
  57. local bRunning = true
  58.  
  59. while(bRunning) do
  60.   nMax = 0
  61.   nEnergy = 0
  62.   nCards = 0
  63.  
  64.   for i,nr in pairs(nrs) do
  65.     nSlots = nr.getSlots()
  66.     for n=1,nSlots do
  67.       id,state,title,data = nr.get(n)
  68.  
  69.       if(id == sEnergySensorLocCardID) then
  70.         nMax = nMax + tonumber(data["maxStorageL"])
  71.         nEnergy = nEnergy + tonumber(data["energyL"])
  72.  
  73.         nCards = nCards + 1
  74.       end
  75.     end
  76.   end
  77.  
  78.   if(nCards>0) then
  79.     -- we found some cards
  80.     fPerc = (nEnergy / nMax) * 100.0
  81.     nLeds = math.floor((nEnergy / nMax) * nTotalLeds)
  82.  
  83.     nRed = (2 ^ nLeds) - 1
  84.  
  85.     -- output to all sides that do not have a nuclear reader
  86.     for i, side in pairs(rs.getSides()) do
  87.       if(not nrSides[side]) then
  88.         redstone.setBundledOutput(side,nRed)
  89.       end
  90.     end
  91.  
  92.     term.setCursorPos(1,4)
  93.     term.write(nEnergy.." eu of "..nMax.." eu            ")
  94.     term.setCursorPos(1,5)
  95.     term.write(string.format("%06.2f ",fPerc).."%             ")
  96.     term.setCursorPos(1,6)
  97.     term.write(nLeds.." leds    ")
  98.  
  99.     os.sleep(nWait)
  100.   else
  101.     term.setCursorPos(1,4)
  102.     term.write("No cards found                                ")
  103.     term.setCursorPos(1,5)
  104.     term.write("                                              ")
  105.     term.setCursorPos(1,6)
  106.     term.write("                                              ")
  107.  
  108.     os.sleep(1)
  109.   end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement