Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- if os.getComputerID() ~= 37 then
- error("Not computer #37")
- exit()
- end
- recordLogs = (#tArgs >= 1 and tArgs[1] == "log")
- logFile = "/disk/energy.log"
- local function echo(txt,x,y)
- term.setCursorPos(x,y)
- term.write(txt)
- end
- local function diff(_old,_new,x,y)
- local c
- if _old > _new then
- term.setTextColor(colors.red)
- elseif _old < _new then
- term.setTextColor(colors.green)
- end
- local val = math.floor((_new - _old)*1000+.5)/1000
- echo(val,x,y)
- term.setTextColor(colors.white)
- end
- side = "back"
- cell = peripheral.wrap(side)
- capacityMax = cell.getMaxEnergyStored(side)
- function capacity(n)
- return math.floor(n/capacityMax*1000+.5)/10
- end
- oLog = {
- nTime = 0,
- energyStored = 0,
- capacity = 0,
- }
- w,h = term.getSize()
- blink = false
- _nTime = os.time()
- _energyStored = cell.getEnergyStored("back")
- _capacity = capacity(_energyStored)
- function display(line)
- local all = line == nil
- if all then
- term.clear()
- line = false
- else
- term.setCursorPos(1,line)
- term.clearLine()
- end
- local sBlink = blink and "-- " or " -- "
- if all or line == 1 then
- echo("Energy Watch "..sBlink.." (delay:"..delay..")",1,1)
- end
- if all or line == 2 then
- echo("time: ".._nTime,1,2)
- end
- if all or line == 3 then
- diff(oLog.nTime,_nTime, 3,3)
- end
- if all or line == 4 then
- echo("energyStored: ".._energyStored,1,4)
- term.write(" RF")
- end
- if all or line == 5 then
- diff(oLog.energyStored,_energyStored,3,5)
- term.write(" RF")
- end
- if all or line == 6 then
- echo("Capacity: ".._capacity,1,6)
- term.write("%")
- end
- if all or line == 7 then
- diff(oLog.capacity,_capacity,3,7)
- term.write("%")
- end
- end
- init = true
- delay = .8
- terminate = false
- refresh = os.startTimer(.2)
- while not terminate do
- e,p = os.pullEvent()
- if e == "key" and p ~= keys.rightCtrl and p ~= keys.leftCtrl then
- if p == keys.t then
- terminate = true
- term.setCursorPos(1,h-1)
- term.clearLine()
- term.setTextColor(colors.white)
- print("Terminate")
- sleep(1)
- elseif p == keys.s then
- delay = math.min(25.6, math.floor(delay*20+.5)/10)
- display(1)
- -- refresh = os.startTimer(delay)
- elseif p == keys.f then
- delay = math.max(.2, math.floor(delay*5+.5)/10)
- display(1)
- -- refresh = os.startTimer(delay)
- elseif p == keys.l then
- recordLogs = not recordLogs
- if recordLogs then
- term.setTextColor(colors.yellow)
- echo(" Record: init",w-16,3)
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.gray)
- echo("Record: disabled",w-16,3)
- term.setTextColor(colors.white)
- end
- else
- term.setTextColor(colors.gray)
- echo("[S]Slow / [F]Fast / [L]record Logs / [T]Terminate",1,h)
- term.setTextColor(colors.white)
- end
- elseif e == "timer" then
- blink = not blink
- _nTime = os.time()
- _energyStored = cell.getEnergyStored("back")
- _capacity = capacity(_energyStored)
- display()
- oLog.nTime = _nTime
- oLog.energyStored = _energyStored
- oLog.capacity = _capacity
- if recordLogs then
- if not init then
- term.setTextColor(colors.orange)
- echo("Record: saved",w-13,3)
- term.setTextColor(colors.white)
- local f = fs.open(logFile,"a")
- f.write(textutils.serialize(oLog).."\n")
- f.close()
- else
- term.setTextColor(colors.yellow)
- echo("Record: init",w-12,3)
- term.setTextColor(colors.white)
- shell.run("rm "..logFile)
- local f = fs.open(logFile,"w")
- f.write("")
- f.close()
- init = false
- end
- else
- term.setTextColor(colors.gray)
- echo("Record: disabled",w-16,3)
- term.setTextColor(colors.white)
- end
- refresh = os.startTimer(delay)
- end
- end
Add Comment
Please, Sign In to add comment