SHOW:
|
|
- or go back to the newest paste.
| 1 | local tArgs = {...}
| |
| 2 | ||
| 3 | if os.getComputerID() ~= 37 then | |
| 4 | error("Not computer #37")
| |
| 5 | exit() | |
| 6 | end | |
| 7 | ||
| 8 | recordLogs = (#tArgs >= 1 and tArgs[1] == "log") | |
| 9 | logFile = "/disk/energy.log" | |
| 10 | ||
| 11 | local function echo(txt,x,y) | |
| 12 | term.setCursorPos(x,y) | |
| 13 | term.write(txt) | |
| 14 | end | |
| 15 | ||
| 16 | local function diff(_old,_new,x,y) | |
| 17 | local c | |
| 18 | if _old > _new then | |
| 19 | term.setTextColor(colors.red) | |
| 20 | elseif _old < _new then | |
| 21 | term.setTextColor(colors.green) | |
| 22 | end | |
| 23 | local val = math.floor((_new - _old)*1000+.5)/1000 | |
| 24 | echo(val,x,y) | |
| 25 | term.setTextColor(colors.white) | |
| 26 | end | |
| 27 | ||
| 28 | side = "back" | |
| 29 | cell = peripheral.wrap(side) | |
| 30 | capacityMax = cell.getMaxEnergyStored(side) | |
| 31 | ||
| 32 | function capacity(n) | |
| 33 | return math.floor(n/capacityMax*1000+.5)/10 | |
| 34 | end | |
| 35 | ||
| 36 | oLog = {
| |
| 37 | nTime = 0, | |
| 38 | energyStored = 0, | |
| 39 | capacity = 0, | |
| 40 | } | |
| 41 | ||
| 42 | w,h = term.getSize() | |
| 43 | blink = false | |
| 44 | _nTime = os.time() | |
| 45 | _energyStored = cell.getEnergyStored("back")
| |
| 46 | _capacity = capacity(_energyStored) | |
| 47 | ||
| 48 | function display(line) | |
| 49 | local all = line == nil | |
| 50 | if all then | |
| 51 | term.clear() | |
| 52 | line = false | |
| 53 | else | |
| 54 | term.setCursorPos(1,line) | |
| 55 | term.clearLine() | |
| 56 | end | |
| 57 | local sBlink = blink and "-- " or " -- " | |
| 58 | if all or line == 1 then | |
| 59 | echo("Energy Watch "..sBlink.." (delay:"..delay..")",1,1)
| |
| 60 | end | |
| 61 | ||
| 62 | if all or line == 2 then | |
| 63 | echo("time: ".._nTime,1,2)
| |
| 64 | end | |
| 65 | if all or line == 3 then | |
| 66 | diff(oLog.nTime,_nTime, 3,3) | |
| 67 | end | |
| 68 | ||
| 69 | if all or line == 4 then | |
| 70 | echo("energyStored: ".._energyStored,1,4)
| |
| 71 | term.write(" RF")
| |
| 72 | end | |
| 73 | if all or line == 5 then | |
| 74 | diff(oLog.energyStored,_energyStored,3,5) | |
| 75 | term.write(" RF")
| |
| 76 | end | |
| 77 | if all or line == 6 then | |
| 78 | echo("Capacity: ".._capacity,1,6)
| |
| 79 | term.write("%")
| |
| 80 | end | |
| 81 | if all or line == 7 then | |
| 82 | diff(oLog.capacity,_capacity,3,7) | |
| 83 | term.write("%")
| |
| 84 | end | |
| 85 | end | |
| 86 | ||
| 87 | init = true | |
| 88 | delay = .8 | |
| 89 | terminate = false | |
| 90 | refresh = os.startTimer(.2) | |
| 91 | while not terminate do | |
| 92 | e,p = os.pullEvent() | |
| 93 | if e == "key" and p ~= keys.rightCtrl and p ~= keys.leftCtrl then | |
| 94 | if p == keys.t then | |
| 95 | terminate = true | |
| 96 | term.setCursorPos(1,h-1) | |
| 97 | term.clearLine() | |
| 98 | term.setTextColor(colors.white) | |
| 99 | print("Terminate")
| |
| 100 | sleep(1) | |
| 101 | elseif p == keys.s then | |
| 102 | delay = math.min(25.6, math.floor(delay*20+.5)/10) | |
| 103 | display(1) | |
| 104 | -- refresh = os.startTimer(delay) | |
| 105 | elseif p == keys.f then | |
| 106 | delay = math.max(.2, math.floor(delay*5+.5)/10) | |
| 107 | display(1) | |
| 108 | -- refresh = os.startTimer(delay) | |
| 109 | elseif p == keys.l then | |
| 110 | recordLogs = not recordLogs | |
| 111 | if recordLogs then | |
| 112 | term.setTextColor(colors.yellow) | |
| 113 | echo(" Record: init",w-16,3)
| |
| 114 | term.setTextColor(colors.white) | |
| 115 | else | |
| 116 | term.setTextColor(colors.gray) | |
| 117 | echo("Record: disabled",w-16,3)
| |
| 118 | term.setTextColor(colors.white) | |
| 119 | end | |
| 120 | else | |
| 121 | term.setTextColor(colors.gray) | |
| 122 | echo("[S]Slow / [F]Fast / [L]record Logs / [T]Terminate",1,h)
| |
| 123 | term.setTextColor(colors.white) | |
| 124 | end | |
| 125 | elseif e == "timer" then | |
| 126 | blink = not blink | |
| 127 | _nTime = os.time() | |
| 128 | _energyStored = cell.getEnergyStored("back")
| |
| 129 | _capacity = capacity(_energyStored) | |
| 130 | ||
| 131 | display() | |
| 132 | ||
| 133 | oLog.nTime = _nTime | |
| 134 | oLog.energyStored = _energyStored | |
| 135 | oLog.capacity = _capacity | |
| 136 | ||
| 137 | if recordLogs then | |
| 138 | if not init then | |
| 139 | term.setTextColor(colors.orange) | |
| 140 | echo("Record: saved",w-13,3)
| |
| 141 | term.setTextColor(colors.white) | |
| 142 | local f = fs.open(logFile,"a") | |
| 143 | f.write(textutils.serialize(oLog).."\n") | |
| 144 | f.close() | |
| 145 | else | |
| 146 | term.setTextColor(colors.yellow) | |
| 147 | echo("Record: init",w-12,3)
| |
| 148 | term.setTextColor(colors.white) | |
| 149 | shell.run("rm "..logFile)
| |
| 150 | local f = fs.open(logFile,"w") | |
| 151 | f.write("")
| |
| 152 | f.close() | |
| 153 | init = false | |
| 154 | end | |
| 155 | else | |
| 156 | term.setTextColor(colors.gray) | |
| 157 | echo("Record: disabled",w-16,3)
| |
| 158 | term.setTextColor(colors.white) | |
| 159 | end | |
| 160 | ||
| 161 | refresh = os.startTimer(delay) | |
| 162 | end | |
| 163 | end |