csmit195

Terminal glasses test

Sep 18th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. local glass = peripheral.wrap('bottom')
  2. local power = peripheral.wrap('left')
  3.  
  4. local text = "POWER: 0.00%"
  5.  
  6. local c = 0
  7.  
  8. function addBox()
  9.     glass.addBox(1,1,textLength(text),10,0xFFFFFF,0.2)
  10. end
  11.  
  12. function draw()
  13.     print(c)
  14.     c = c + 1
  15.     glass.clear()
  16.     addBox()
  17.     local powerUsed = power.getEnergyStored()
  18.     local powerMax = power.getMaxEnergyStored()
  19.     local powerPercentage = ( ( powerUsed / powerMax ) * 100 )
  20.     text = round(powerPercentage, 2) .. '%'
  21.     glass.addText(5,2,"POWER: " .. text, 0xFF0000)
  22.     glass.sync()
  23. end
  24.  
  25. function start()
  26.     drawLoop = os.startTimer(1)
  27.     while true do
  28.         checkEvent()
  29.         sleep(0.1)
  30.     end
  31. end
  32.  
  33. function checkEvent()
  34.     event,action,user,uuid,message = os.pullEvent()
  35.     if event == "glasses_chat_command" then
  36.         if ( message:len() > 0 ) then
  37.             if ( message == 'shutdown' ) then
  38.                 setReactorState(true)
  39.             elseif ( message == 'start' ) then
  40.                 setReactorState(false)
  41.             end
  42.         end
  43.     elseif event == "timer" then
  44.         -- Alarm
  45.         if ( drawLoop and action == drawLoop ) then
  46.             draw()
  47.             drawLoop = os.startTimer(1)
  48.         elseif ( alarmTimer and action == alarmTimer ) then
  49.             redstone.setOutput('top', false)
  50.         end
  51.     end
  52. end
  53.  
  54. function setReactorState(state)
  55.     redstone.setOutput('back', state)
  56.     redstone.setOutput('top', state)
  57.     alarmTimer = os.startTimer(5)
  58. end
  59.  
  60. function textLength(str)
  61.     return ( (str:len() + 1) * 11 ) + 3
  62. end
  63.  
  64. function ReadableNumber(num, places)
  65.     local ret
  66.     local placeValue = ("%%.%df"):format(places or 0)
  67.     if not num then
  68.         return 0
  69.     elseif num >= 1000000000000 then
  70.         ret = placeValue:format(num / 1000000000000) .. "T" -- trillion
  71.     elseif num >= 1000000000 then
  72.         ret = placeValue:format(num / 1000000000) .. "B" -- billion
  73.     elseif num >= 1000000 then
  74.         ret = placeValue:format(num / 1000000) .. "M" -- million
  75.     elseif num >= 1000 then
  76.         ret = placeValue:format(num / 1000) .. "K" -- thousand
  77.     else
  78.         ret = num -- hundreds
  79.     end
  80.     return ret
  81. end
  82.  
  83. function round(num, numDecimalPlaces)
  84.     local mult = 10^(numDecimalPlaces or 0)
  85.     return math.floor(num * mult + 0.5) / mult
  86. end
  87.  
  88. start()
Advertisement
Add Comment
Please, Sign In to add comment