Initial commit
This commit is contained in:
151
PowerManager.lua
Normal file
151
PowerManager.lua
Normal file
@@ -0,0 +1,151 @@
|
||||
--Programm um an den PC angeschlossene TE und RF Speicherzellen grafisch darzustellen
|
||||
|
||||
local component = require( "component" )
|
||||
local gpu = component.gpu
|
||||
local event = require( "event" )
|
||||
|
||||
local oldW, oldH = gpu.getResolution()
|
||||
local newW = 160
|
||||
local newH = 50
|
||||
gpu.setResolution(newW, newH)
|
||||
|
||||
function clearScreen()
|
||||
local w,h = gpu.getResolution()
|
||||
drawLine(1, 1, w, h, 0x000000)
|
||||
end
|
||||
|
||||
|
||||
function drawLine(startX, startY, stopX, stopY, colorOfLine)
|
||||
local oldColor = gpu.getBackground(false)
|
||||
gpu.setBackground(colorOfLine, false)
|
||||
gpu.fill(startX, startY, stopX, stopY, " ")
|
||||
gpu.setBackground(oldColor, false)
|
||||
end
|
||||
|
||||
|
||||
function powerBar( label, y, x, value, maxVal, colorOfBar, show, unit )
|
||||
local oldColor = gpu.getBackground(false)
|
||||
local border = 3
|
||||
local borderSymbol = " "
|
||||
local barSymbol = " "
|
||||
local percentage = (value * 100 / maxVal)
|
||||
local redGraphValue = 20
|
||||
|
||||
if percentage <= redGraphValue then
|
||||
colorOfBar = 0xf00000
|
||||
end
|
||||
drawLine(border, y, x, 2, 0x000000)
|
||||
w = math.floor( value * (x / maxVal) )
|
||||
p = math.floor( (w / x) * 100 )
|
||||
gpu.set( border, y, label .. ": " .. tostring( p ) .. "%" )
|
||||
drawLine(border, y+1, x, 1, 0x222222)
|
||||
drawLine(border, y+1, w, 1, colorOfBar)
|
||||
gpu.setBackground( oldColor, false )
|
||||
if show then
|
||||
local valStr = formatBig( value ) .. unit
|
||||
local n = string.len( valStr )
|
||||
gpu.set( (x+3) - n, y, valStr )
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function formatBig( value )
|
||||
local output = ""
|
||||
local valRem = 0
|
||||
local valPart = 0
|
||||
while value > 0 do
|
||||
valRem = math.floor( value / 1000 )
|
||||
valPart = value - (valRem * 1000)
|
||||
if output == "" then
|
||||
output = string.format( "%03d", valPart )
|
||||
elseif valRem == 0 then
|
||||
output = valPart .. "," .. output
|
||||
else
|
||||
output = string.format( "%03d", valPart ) .. "," .. output
|
||||
end
|
||||
value = valRem
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
function getCells()
|
||||
local countDcOrb = 0
|
||||
local countTEcell = 0
|
||||
local countRfTCell = 0
|
||||
|
||||
local TEcell = component.list( "energy_device" )
|
||||
local RfTCell = component.list("rftools_powercell")
|
||||
|
||||
local cellsID = {}
|
||||
|
||||
for address, name in pairs(TEcell) do
|
||||
countTEcell = countTEcell + 1
|
||||
if countTEcell > 1 then
|
||||
cellsID[address] = "TE Zelle".." "..countTEcell
|
||||
else
|
||||
cellsID[address] = "TE Zelle"
|
||||
end
|
||||
end
|
||||
|
||||
for address, name in pairs(RfTCell) do
|
||||
countRfTCell = countRfTCell + 1
|
||||
if countRfTCell > 1 then
|
||||
cellsID[address] = "RfT Zelle".." "..countRfTCell
|
||||
else
|
||||
cellsID[address] = "RfT Zelle"
|
||||
end
|
||||
end
|
||||
return cellsID
|
||||
end
|
||||
|
||||
function getTotal()
|
||||
local totalPower = 0
|
||||
local totalMaxPower = 0
|
||||
local cellid = getCells()
|
||||
for address, name in pairs(cellid) do
|
||||
local cell = component.proxy( address )
|
||||
totalPower = totalPower + cell.getEnergyStored()
|
||||
totalMaxPower = totalMaxPower + cell.getMaxEnergyStored()
|
||||
end
|
||||
return totalPower, totalMaxPower
|
||||
|
||||
end
|
||||
|
||||
local splashText = "Supercoole Stromanzeige"
|
||||
local frameTitle = "Power Monitor - Klicken zum Beenden"
|
||||
|
||||
clearScreen()
|
||||
drawLine(1, 1, newW, 1, 0xbbbbbb)
|
||||
gpu.set((newW - #splashText) / 2, 24, splashText)
|
||||
os.sleep(1)
|
||||
|
||||
clearScreen()
|
||||
gpu.set((newW - #frameTitle) / 2, 1, frameTitle)
|
||||
local cellsID = getCells()
|
||||
|
||||
while true do
|
||||
local _,_,x,y = event.pull( 1, "touch" )
|
||||
local count = 0
|
||||
if x and y then goto quit end
|
||||
|
||||
local halfWidth = newW / 2
|
||||
drawLine(halfWidth, 3, 1, 40, 0xffffff)
|
||||
drawLine(1, 43, newW, 1, 0xffffff)
|
||||
gpu.set(1, 42, "Speicherzellen")
|
||||
gpu.set(halfWidth + 1, 42, "Generatoren")
|
||||
|
||||
for address, name in pairs(cellsID) do
|
||||
local cell = component.proxy( address )
|
||||
count = count + 1
|
||||
local t = count * 3
|
||||
powerBar( name, t , halfWidth - 6, cell.getEnergyStored(), cell.getMaxEnergyStored() , 0x00bb00, true, "RF" )
|
||||
end
|
||||
|
||||
local totalPower, totalMaxPower = getTotal()
|
||||
powerBar( "Gesamt", 51 - count, newW - 6, totalPower, totalMaxPower, 0x00bb00, true, "RF" )
|
||||
end
|
||||
|
||||
|
||||
::quit::
|
||||
gpu.setResolution( oldW, oldH )
|
||||
clearScreen()
|
||||
Reference in New Issue
Block a user