Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- === Config ===
- local borderColour = 128
- local chartBGColour = 128
- local inputChartColour = 32
- local outputChartColour = 16384
- local batteryChargeColour = 8
- local idleColour = 16
- local sectionTitleColour = 1
- local baseTextColour = 1
- local screenRefreshRate = 0.1
- local unitFormatCaps = true
- local dataCorrection = 2.5
- -- === Discover monitor + Induction (handles local + wired modem) ===
- local function findMonitorAndInduction()
- local foundMonitor, foundInduction, legacy
- local function consider(name, getType)
- local pType = (getType and getType(name)) or peripheral.getType(name)
- local lname = tostring(name):lower()
- if (not foundMonitor) and (pType == "monitor" or lname:find("monitor", 1, true)) then
- local ok, p = pcall(peripheral.wrap, name); if ok and p then foundMonitor = p end
- end
- if not foundInduction then
- -- “Port” indicates modern; plain “matrix/induction” indicates legacy block name
- local isPort = (pType == "inductionPort")
- or (pType and pType:lower():find("induction", 1, true) and pType:lower():find("port", 1, true))
- or (lname:find("inductionport", 1, true) ~= nil)
- local looksLikeIM = isPort
- or (pType and pType:lower():find("induction", 1, true))
- or lname:find("induction", 1, true)
- or lname:find("matrix", 1, true)
- if looksLikeIM then
- local ok, p = pcall(peripheral.wrap, name)
- if ok and p then
- foundInduction = p
- legacy = (not isPort) and (lname:find("matrix", 1, true) or (pType and pType:lower():find("matrix", 1, true))) or false
- end
- end
- end
- end
- for _, name in ipairs(peripheral.getNames()) do
- local pType = peripheral.getType(name)
- consider(name, peripheral.getType)
- if pType == "modem" then
- local mdm = peripheral.wrap(name)
- if mdm and mdm.isWireless and mdm.getNamesRemote then
- local isWireless = false; pcall(function() isWireless = mdm.isWireless() end)
- if not isWireless then
- local ok, remotes = pcall(mdm.getNamesRemote)
- if ok and type(remotes) == "table" then
- for _, rname in ipairs(remotes) do
- local function getTypeRemote(n) if mdm.getTypeRemote then return mdm.getTypeRemote(n) end return nil end
- consider(rname, getTypeRemote)
- if foundMonitor and foundInduction then return foundMonitor, foundInduction, legacy end
- end
- end
- end
- end
- end
- if foundMonitor and foundInduction then break end
- end
- return foundMonitor, foundInduction, legacy
- end
- local m, im, imLegacy = findMonitorAndInduction()
- local w, h
- -- === Peripheral helpers & legacy shims ===
- local function hasMethod(p, name) return p and type(p[name]) == "function" end
- local function getLastInput()
- if hasMethod(im, "getLastInput") then return im.getLastInput() end
- if hasMethod(im, "getInput") then return im.getInput() end
- return 0
- end
- local function getLastOutput()
- if hasMethod(im, "getLastOutput") then return im.getLastOutput() end
- if hasMethod(im, "getOutput") then return im.getOutput() end
- return 0
- end
- local function getFilledPct()
- if hasMethod(im, "getEnergyFilledPercentage") then return im.getEnergyFilledPercentage() end
- local e = im.getEnergy and im.getEnergy() or 0
- local mE = im.getMaxEnergy and im.getMaxEnergy() or 0
- if mE > 0 then return e / mE end
- return 0
- end
- -- === UI helpers ===
- local colorText = {"White","Orange","Magenta","Light blue","Yellow","Lime","Pink","Gray","Light gray","Cyan","Purple","Blue","Brown","Green","Red","Black"}
- local colorDec = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
- local refreshRates = { values = {0.1,0.3,0.5,1.0,1.0,2.0}, display = {"Fastest","Fast","Normal","Slow","Very Slow","Slowest"} }
- local unitStyle = { values = {true,false}, display = {"X.X KRF","X.XkRF"} }
- local function fromBlit(hex)
- if #hex ~= 1 then return nil end
- local value = tonumber(hex, 16); if not value then return nil end
- return 2 ^ value
- end
- local function draw(xStart, xLen, yStart, yLen, c, target)
- target = target or m
- local ob = target.getBackgroundColour()
- target.setBackgroundColor(c)
- target.setCursorPos(xStart, yStart)
- if xLen > 1 then
- for i = 1, xLen do
- target.write(" "); target.setCursorPos(xStart+i, yStart)
- end
- elseif xLen ~= 0 and yLen ~= 0 then
- target.write(" ")
- end
- if yLen > 1 then
- for k = 1, yLen-1 do
- target.setCursorPos(xStart, yStart+k); target.write(" ")
- end
- end
- target.setBackgroundColor(ob)
- end
- local function drawBar(xmin, xmax, y, r, c)
- for i=1, r do draw(xmin, xmax, y+i-1, 1, c) end
- end
- local function writeAt(x, y, t, c, b, target)
- target = target or m
- local oc = target.getTextColour and target.getTextColour() or colors.white
- local ob = target.getBackgroundColour and target.getBackgroundColour() or colors.black
- if target.setTextColour then target.setTextColour(c or oc) end
- if target.setBackgroundColour then target.setBackgroundColour(b or ob) end
- target.setCursorPos(x, y); target.write(t)
- if target.setTextColour then target.setTextColour(oc) end
- if target.setBackgroundColour then target.setBackgroundColour(ob) end
- end
- local function newWindow(target, x, y, w_, h_, bg)
- local win = window.create(target, x, y, w_, h_)
- win.setBackgroundColor(bg or 32768); win.clear()
- return win
- end
- local function displayError(index)
- local x, y = m.getSize()
- local errorText = "ERROR"
- local errors = {
- "The Induction Matrix has been tampered with.",
- "Induction Port not connected.",
- "The Induction Matrix is incomplete."
- }
- m.clear()
- m.setTextColour(16384)
- m.setCursorPos((x/2) - (string.len(errorText)/2), (y/2)-1)
- m.write(errorText)
- writeAt((x/2) - (string.len(errors[index])/2), y/2, errors[index], baseTextColour)
- printError("ERROR: " .. errors[index])
- local timer = 10
- while timer > -1 do
- local countdownStr = "Next attempt in " .. timer
- draw((x/2) - (string.len(countdownStr)/2), string.len(countdownStr) + 1, (y/2)+2, 1, 32768)
- writeAt((x/2) - (string.len(countdownStr)/2), (y/2)+2, countdownStr, baseTextColour)
- print(countdownStr)
- timer = timer -1
- os.sleep(1)
- end
- os.reboot()
- end
- -- === Init monitor ===
- if m ~= nil then
- pcall(function() m.setTextScale(0.5) end)
- local x, y = m.getSize()
- if x ~= 100 or y ~= 38 then print("WARNING: Monitor size is "..x.."x"..y..". Designed for 100x38.") end
- w, h = m.getSize()
- m.setBackgroundColor(32768); m.clear(); m.setTextColour(1)
- else
- print("No monitor found (local or via wired modem)."); return
- end
- -- === Validate matrix/port ===
- if not im then
- displayError(2)
- else
- if (not imLegacy) and hasMethod(im, "isFormed") then
- while not im.isFormed() do displayError(3) end
- end
- end
- -- === Misc helpers ===
- local function removeOverflow(tab, maxDataCount)
- while #tab >= maxDataCount do table.remove(tab, 1) end
- return tab
- end
- local function getRange(input)
- local max, min = 0, 0
- for i = 1, #input do
- if i == 1 then max = input[i]; min = input[i] end
- if input[i] > max then max = input[i] end
- if input[i] < min then min = input[i] end
- end
- return min, max
- end
- local function round(x, decimal)
- local m_ = 10^(decimal or 0)
- local result = math.floor(x * m_ + 0.5) / m_
- return string.format("%." .. (decimal or 0) .. "f", result)
- end
- local function roundD(exact, quantum)
- local quant,frac = math.modf(exact/quantum)
- return quantum * (quant + (frac > 0.5 and 1 or 0))
- end
- local timeUnits = {
- {threshold = 8553600, unit = "99+ days"},
- {threshold = 86400, unit = "days", divisor = 86400},
- {threshold = 3600, unit = "hours", divisor = 3600},
- {threshold = 60, unit = "min", divisor = 60},
- {threshold = 1, unit = "sec", divisor = 1}
- }
- local function adjustTime(time)
- for _, unitData in ipairs(timeUnits) do
- if time >= unitData.threshold then
- local adjustedTime
- if unitData.divisor and unitData.divisor ~= 1 then
- adjustedTime = string.format("%.1f", time / unitData.divisor)
- elseif unitData.divisor then
- adjustedTime = math.floor(time / unitData.divisor)
- else
- adjustedTime = ""
- end
- return string.format("%s %s", adjustedTime, unitData.unit)
- end
- end
- return "0 sec."
- end
- local function conversion(exact, text, addPlus, afterDecimalPoint)
- afterDecimalPoint = afterDecimalPoint or 0.1
- addPlus = addPlus or false
- local units = unitFormatCaps and {"", "K", "M", "G", "T", "P", "E", "Z", "Y"} or {"", "k", "m", "g", "t", "p", "e", "z", "y"}
- local pot = 1
- local absExact = math.abs(exact)
- while absExact >= (1000^pot) do pot = pot + 1 end
- local value = roundD(exact / (1000^(pot - 1)), afterDecimalPoint)
- local out = tostring(value)
- if addPlus and value > 0 then out = "+" .. out end
- if text then out = out .. (unitFormatCaps and " " .. units[pot] or units[pot]) end
- return out
- end
- local function protCall(func, errIndex)
- local success, result = pcall(func)
- if success and result ~= nil then return result else displayError(errIndex) end
- end
- local function indexOf(array, value)
- for i, v in ipairs(array) do if v == value then return i end end
- return nil
- end
- -- === Globals ===
- local energy, maxEnergy, installedCells, installedProviders, lastInput, lastOutput, filledPercentage, maxTransfer
- local inputHistory, outputHistory, flowHistory = {}, {}, {}
- local imWidth, imLength, imHeight
- -- ---------- Stable predictor (linear regression on energy vs time) ----------
- local PRED_WINDOW = 200 -- ~20s at 0.1s refresh
- local ENERGY_TREND_CHECK = 20 -- require recent direction agreement
- local FLOW_EPS = 1 -- ignore tiny changes (<1 RF/s)
- local energyHist = {} -- { {t=seconds, e=RF}, ... }
- local t_elapsed = 0 -- seconds since start
- local dEdt_sec = 0 -- slope RF/s (used by drawBattery)
- local function pushEnergySample(t, e)
- table.insert(energyHist, { t = t, e = e })
- removeOverflow(energyHist, PRED_WINDOW)
- end
- local function energySlope()
- local n = #energyHist
- if n < 2 then return 0 end
- local sumt, sume, sumtt, sumte = 0, 0, 0, 0
- for i = 1, n do
- local t = energyHist[i].t
- local e = energyHist[i].e
- sumt = sumt + t
- sume = sume + e
- sumtt = sumtt + t*t
- sumte = sumte + t*e
- end
- local denom = n * sumtt - sumt * sumt
- if denom == 0 then return 0 end
- return (n * sumte - sumt * sume) / denom
- end
- local function stableSlope()
- local s = energySlope() -- RF/s
- local n = #energyHist
- if n < 2 then return 0 end
- local checks = math.min(ENERGY_TREND_CHECK, n - 1)
- local agree = 0
- for i = n - checks + 1, n do
- if i > 1 then
- local d = energyHist[i].e - energyHist[i-1].e
- if d * s > 0 then agree = agree + 1 end
- end
- end
- local ok = (checks == 0) or (agree / checks >= 0.7)
- if not ok then return 0 end
- if math.abs(s) < FLOW_EPS then return 0 end
- return s
- end
- -- ---------------------------------------------------------------------------
- -- === Drawing ===
- local function drawStaticElements()
- local p = 2
- draw(p, w-3, p, 1, borderColour)
- draw(p, w-3, h-1, 1, borderColour)
- draw(p, 1, p, h-p, borderColour)
- draw(w-1, 1, p, h-p, borderColour)
- draw(27, 1, p, h-p, borderColour)
- draw(28, 71, h/2+2, 1, borderColour)
- draw(63, 1, h/2+3, 15, borderColour)
- draw(2, 25, 28, 1, borderColour)
- writeAt(5, 2, " BATTERY ", sectionTitleColour)
- writeAt(30, 2, " ENERGY FLOW: ", sectionTitleColour)
- writeAt(5, 28, " STATS: ", sectionTitleColour)
- local configButtonText = "SETTINGS " .. string.char(26)
- local configButtonSpaceMaker = " "
- local buttonX = w - #configButtonText - 3
- local buttonY = 2
- for i = 1, #configButtonText do configButtonSpaceMaker = configButtonSpaceMaker .. " " end
- writeAt(buttonX - 1, buttonY, configButtonSpaceMaker)
- writeAt(buttonX, buttonY, configButtonText, colors.white, colors.gray)
- writeAt(30, 21, " INPUT: ", sectionTitleColour)
- writeAt(66, 21, " OUTPUT: ", sectionTitleColour)
- end
- local function drawInputGraph()
- table.insert(inputHistory, lastInput); removeOverflow(inputHistory, 34)
- local inGraph = newWindow(m, 29, 23, 33, 13, chartBGColour)
- local ox, oy = inGraph.getSize()
- local _, rf_max = getRange(inputHistory)
- for i = 0, #inputHistory do
- local value = inputHistory[#inputHistory-i]; if not value then break end
- local denom = (rf_max == 0 and 1 or rf_max)
- local result = math.floor(math.min(math.max((value * (13 / denom)), 0), 13) + 0.5)
- if result ~= result then result = 0 end
- draw(ox-i, 1, oy-result+1, result, inputChartColour, inGraph)
- end
- local maxRf = conversion(rf_max, true, true) .. "RF/t"
- local _, _, bgtop = inGraph.getLine(1)
- for i = 0, #maxRf do
- local char = maxRf:sub(1+i, 1+i)
- local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 1, char, baseTextColour, b, inGraph)
- end
- local maxPercent = tostring(round((rf_max/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%")
- for i = 0, #maxPercent do
- local c = ox-#maxPercent+i+1
- local char = maxPercent:sub(1+i, 1+i)
- local b = fromBlit(bgtop:sub(c, c)) or chartBGColour
- writeAt(c, 1, char, baseTextColour, b, inGraph)
- end
- local minRf = "0RF/t"
- local _, _, bglow = inGraph.getLine(oy)
- for i = 0, #minRf do
- local char = minRf:sub(1+i, 1+i)
- local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, oy, char, baseTextColour, b, inGraph)
- end
- local minPercent = "0%"
- for i = 0, #minPercent do
- local c = ox-#minPercent+i+1
- local char = minPercent:sub(1+i, 1+i)
- local b = fromBlit(bglow:sub(c, c)) or chartBGColour
- writeAt(c, oy, char, baseTextColour, b, inGraph)
- end
- draw(39, 22, 21, 1, borderColour)
- local rf_tick = conversion(lastInput, true, true) .. "RF/t "
- writeAt(39, 21, rf_tick, (lastInput > 0 and inputChartColour or idleColour))
- writeAt(39+#rf_tick, 21, "(" .. tostring(round((lastInput/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%) "))
- if lastInput == maxTransfer and maxTransfer > 0 then
- local maxStrings = {"MAX INPUT!","Add / Upgrade","Providers."}
- drawBar(46-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
- writeAt(46-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
- writeAt(46-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
- writeAt(46-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
- end
- end
- local function drawOutputGraph()
- table.insert(outputHistory, lastOutput); removeOverflow(outputHistory, 34)
- local outGraph = newWindow(m, 65, 23, 33, 13, chartBGColour)
- local ox, oy = outGraph.getSize()
- local _, rf_max = getRange(outputHistory)
- for i = 0, #outputHistory do
- local value = outputHistory[#outputHistory-i]; if not value then break end
- local denom = (rf_max == 0 and 1 or rf_max)
- local result = math.floor(math.min(math.max((value * (13 / denom)), 0), 13) + 0.5)
- if result ~= result then result = 0 end
- draw(ox-i, 1, oy-result+1, result, outputChartColour, outGraph)
- end
- local maxRf = conversion(rf_max*-1, true, true) .. "RF/t"
- local _, _, bgtop = outGraph.getLine(1)
- for i = 0, #maxRf do
- local char = maxRf:sub(1+i, 1+i)
- local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 1, char, baseTextColour, b, outGraph)
- end
- local maxPercent = tostring(round((rf_max/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%")
- for i = 0, #maxPercent do
- local c = ox-#maxPercent+i+1
- local char = maxPercent:sub(1+i, 1+i)
- local b = fromBlit(bgtop:sub(c, c)) or chartBGColour
- writeAt(c, 1, char, baseTextColour, b, outGraph)
- end
- local minRf = "0RF/t"
- local _, _, bglow = outGraph.getLine(oy)
- for i = 0, #minRf do
- local char = minRf:sub(1+i, 1+i)
- local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, oy, char, baseTextColour, b, outGraph)
- end
- local minPercent = "0%"
- for i = 0, #minPercent do
- local c = ox-#minPercent+i+1
- local char = minPercent:sub(1+i, 1+i)
- local b = fromBlit(bglow:sub(c, c)) or chartBGColour
- writeAt(c, oy, char, baseTextColour, b, outGraph)
- end
- draw(76, 22, 21, 1, borderColour)
- local rf_tick = conversion(lastOutput*-1, true) .. "RF/t "
- writeAt(76, 21, rf_tick, (lastOutput > 0 and outputChartColour or idleColour))
- writeAt(76+#rf_tick, 21, "(" .. tostring(round((lastOutput/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%) "))
- if lastOutput == maxTransfer and maxTransfer > 0 then
- local maxStrings = {"MAX OUTPUT!","Add / Upgrade","Providers."}
- drawBar(82-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
- writeAt(82-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
- writeAt(82-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
- writeAt(82-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
- end
- end
- local function drawFlowGraph()
- table.insert(flowHistory, lastInput-lastOutput); removeOverflow(flowHistory, 70)
- local flowGraph = newWindow(m, 29, 4, 69, 16, chartBGColour)
- local fx, fy = flowGraph.getSize()
- local rf_min, rf_max = getRange(flowHistory)
- local scale = math.max(math.abs(rf_min), rf_max); if scale == 0 then scale = 1 end
- for i = 0, #flowHistory do
- local value = flowHistory[#flowHistory-i]; if not value then break end
- value = math.abs(value)
- local result = math.floor(math.min(math.max((value * (8 / scale)), 0), 8) + 0.5)
- if result ~= result then result = 0 end
- if result == 0 then
- writeAt(fx-i, 8, "_", 32768, chartBGColour, flowGraph)
- elseif flowHistory[#flowHistory-i] < 0 then
- draw(fx-i, 1, 9, result, outputChartColour, flowGraph)
- else
- draw(fx-i, 1, 9-result, result, inputChartColour, flowGraph)
- end
- end
- local lastFlow = flowHistory[#flowHistory]
- draw(45, 15, 2, 1, borderColour)
- writeAt(45, 2, conversion(lastFlow, true, true) .. "RF/t ", (lastFlow == 0 and idleColour or (lastFlow > 0 and inputChartColour or outputChartColour)))
- local positiveRf = conversion(scale, true, true) .. "RF/t"
- local _, _, bgtop = flowGraph.getLine(1)
- for i = 0, #positiveRf do
- local char = positiveRf:sub(1+i, 1+i)
- local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 1, char, baseTextColour, b, flowGraph)
- end
- local noRF = "0 RF/t"
- local _, _, bgn = flowGraph.getLine(8)
- for i = 0, #noRF do
- local char = noRF:sub(1+i, 1+i)
- local b = fromBlit(bgn:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 8, char, baseTextColour, b, flowGraph)
- end
- local negativeRf = conversion(scale*-1, true, true) .. "RF/t"
- local _, _, bgbot = flowGraph.getLine(16)
- for i = 0, #negativeRf do
- local char = negativeRf:sub(1+i, 1+i)
- local b = fromBlit(bgbot:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 16, char, baseTextColour, b, flowGraph)
- end
- end
- local function drawBattery()
- drawBar(4, 22, 4, 23, chartBGColour)
- local chargeHeight = round(filledPercentage*23)
- drawBar(4, 22, 27-chargeHeight, chargeHeight, batteryChargeColour)
- local percent = tostring(round((energy/maxEnergy)*100, 2)) .. "%"
- local percentPos = (22/2) - (string.len(percent)/2) + 4
- writeAt(percentPos, 15, percent, baseTextColour, chartBGColour)
- local stored = "STORED: " .. conversion(energy, true, false, 0.01) .. "RF"
- local storedPos = (22/2) - (string.len(stored)/2) + 4
- writeAt(storedPos, 5, stored, baseTextColour, chartBGColour)
- -- Use regression slope (dE/dt in RF/s) for steady ETA
- local chargeStatus, timeStatus = "Idle", ""
- local timeStr = "Inf."
- if dEdt_sec < -FLOW_EPS then
- chargeStatus = "Depleting"
- timeStatus = "Empty In:"
- timeStr = adjustTime(energy / (-dEdt_sec))
- elseif dEdt_sec > FLOW_EPS then
- chargeStatus = "Charging"
- timeStatus = "Full In:"
- timeStr = adjustTime((maxEnergy - energy) / dEdt_sec)
- end
- local timeString = (timeStatus ~= "" and (timeStatus .. " " .. timeStr) or timeStr)
- local timePos = (22/2) - (string.len(timeString)/2) + 4
- writeAt((22/2) - (string.len(chargeStatus)/2) + 4, 24, chargeStatus, baseTextColour, chartBGColour)
- writeAt(timePos, 25, timeString, baseTextColour, chartBGColour)
- end
- local function drawStatistics()
- local row = 30
- if imWidth and imHeight and imLength then
- writeAt(4, row, "Size: " .. imWidth .. " x " .. imHeight .. " x " .. imLength, baseTextColour); row = row + 1
- end
- if installedCells ~= nil then writeAt(4, row, "Cells: " .. installedCells, baseTextColour); row = row + 1 end
- if installedProviders ~= nil then writeAt(4, row, "Providers: " .. installedProviders, baseTextColour); row = row + 1 end
- if imWidth and imHeight and imLength and installedCells and installedProviders then
- local empty = ((imWidth - 2) * (imLength - 2) * (imHeight - 2)) - installedCells - installedProviders
- writeAt(4, row, "Empty Slots: " .. tostring(empty), baseTextColour); row = row + 1
- end
- writeAt(4, row, "MaxTransfer: " .. conversion(maxTransfer, true) .. "/t", baseTextColour); row = row + 1
- writeAt(4, row, "MaxCapacity: " .. conversion(maxEnergy, true), baseTextColour)
- end
- -- === Read static capabilities (with legacy tolerance) ===
- installedCells = hasMethod(im, "getInstalledCells") and protCall(im.getInstalledCells, 1) or nil
- installedProviders = hasMethod(im, "getInstalledProviders") and protCall(im.getInstalledProviders, 1) or nil
- maxEnergy = (protCall(im.getMaxEnergy, 1) or 0) / dataCorrection
- maxTransfer = (protCall(im.getTransferCap, 1) or 0) / dataCorrection
- imWidth = hasMethod(im, "getWidth") and protCall(im.getWidth, 1) or nil
- imLength = hasMethod(im, "getLength") and protCall(im.getLength, 1) or nil
- imHeight = hasMethod(im, "getHeight") and protCall(im.getHeight, 1) or nil
- drawStaticElements()
- drawStatistics()
- -- === Input handling ===
- local monitor_touched, x_touch, y_touch
- local function touch()
- while true do
- local event_data = table.pack(os.pullEvent())
- if event_data[1] == "monitor_touch" then
- monitor_touched, x_touch, y_touch = table.unpack(event_data, 2, event_data.n)
- break
- end
- end
- end
- -- === Main display loop ===
- local function display()
- while true do
- energy = protCall(im.getEnergy, 1) / dataCorrection
- lastInput = (getLastInput() or 0) / dataCorrection
- lastOutput = (getLastOutput() or 0) / dataCorrection
- filledPercentage = getFilledPct()
- -- feed predictor with energy vs time (seconds)
- t_elapsed = t_elapsed + screenRefreshRate
- pushEnergySample(t_elapsed, energy)
- dEdt_sec = stableSlope() -- RF / second (smoothed)
- -- draw
- drawInputGraph()
- drawOutputGraph()
- drawFlowGraph()
- drawBattery()
- os.sleep(screenRefreshRate)
- end
- end
- -- === Settings persistence ===
- function saveConfig(name, value)
- local filename = shell.getRunningProgram()
- local lines = {}
- for line in io.lines(filename) do table.insert(lines, line) end
- local settingsMap = {
- borderColour = { var = "borderColour", line = 2 },
- chartBGColour = { var = "chartBGColour", line = 3 },
- inputChartColour = { var = "inputChartColour", line = 4 },
- outputChartColour = { var = "outputChartColour", line = 5 },
- batteryChargeColour= { var = "batteryChargeColour",line = 6 },
- idleColour = { var = "idleColour", line = 7 },
- sectionTitleColour = { var = "sectionTitleColour", line = 8 },
- baseTextColour = { var = "baseTextColour", line = 9 },
- screenRefreshRate = { var = "screenRefreshRate", line = 11 },
- unitFormatCaps = { var = "unitFormatCaps", line = 12 },
- }
- if settingsMap[name] then
- if name == "borderColour" then borderColour = value
- elseif name == "chartBGColour" then chartBGColour = value
- elseif name == "inputChartColour" then inputChartColour = value
- elseif name == "outputChartColour" then outputChartColour = value
- elseif name == "batteryChargeColour" then batteryChargeColour = value
- elseif name == "idleColour" then idleColour = value
- elseif name == "sectionTitleColour" then sectionTitleColour = value
- elseif name == "baseTextColour" then baseTextColour = value
- elseif name == "screenRefreshRate" then screenRefreshRate = value
- elseif name == "unitFormatCaps" then unitFormatCaps = value
- end
- lines[settingsMap[name].line] = "local " .. settingsMap[name].var .. " = " .. tostring(value)
- end
- local h_ = io.open(filename, 'w'); for _, line in ipairs(lines) do h_:write(line .. "\n") end; h_:close()
- end
- local function dropdownMenu(x, y, options)
- local width, optionText = 0, ""
- for i = 1, #options do options[i] = tostring(options[i]); if #options[i] > width then width = #options[i] end end
- for i = 1, #options do
- optionText = " " .. options[i] .. " "
- for j = 1, width - #options[i] do optionText = optionText .. " " end
- writeAt(x - 2 - width, y + i, optionText, 1, 256)
- end
- while true do
- local event, side, ox, oy = os.pullEvent("monitor_touch")
- if (ox >= x - 2 - width and ox <= x and oy >= y + 1 and oy <= y + #options) then
- return oy-y
- else
- return nil
- end
- end
- end
- local function drawSettings(settingsX, settingsY, sWidth, sHeight)
- local sTextColour, sBGColour = 1, 128
- drawBar(settingsX - 2, sWidth, settingsY - 2, 1, sBGColour)
- drawBar(settingsX - 2, sWidth, settingsY + sHeight - 3, 1, sBGColour)
- drawBar(settingsX - 2, 1, settingsY - 2, sHeight, sBGColour)
- drawBar(settingsX - 2 + sWidth, 1, settingsY - 2, sHeight, sBGColour)
- local labels = {"Borders: ","Chart Background: ","Input Chart: ","Output Chart: ","Battery: ","Idle: ","Section Titles: ","Base Text: "}
- for i, label in ipairs(labels) do writeAt(settingsX, i + settingsY - 1, label) end
- writeAt(settingsX, settingsY + 9, "Refresh Rate: ")
- writeAt(settingsX, settingsY + 10, "Unit Style: ")
- local configs = {
- {row = 0, config = borderColour},
- {row = 1, config = chartBGColour},
- {row = 2, config = inputChartColour},
- {row = 3, config = outputChartColour},
- {row = 4, config = batteryChargeColour},
- {row = 5, config = idleColour},
- {row = 6, config = sectionTitleColour},
- {row = 7, config = baseTextColour},
- {row = 9, config = screenRefreshRate},
- {row = 10, config = unitFormatCaps},
- }
- for i, data in ipairs(configs) do
- local row, config, configText = data.row, data.config
- if row <= 7 then
- configText = tostring(colorText[indexOf(colorDec, config)]) .. " "
- writeAt(settingsX + 18, settingsY + row, " ", 1, config)
- writeAt(settingsX + 20, settingsY + row, " ", 1, sBGColour)
- elseif row == 9 then
- configText = tostring(refreshRates.display[indexOf(refreshRates.values, config)]) .. " "
- writeAt(settingsX + 18, settingsY + row, " ", 1, sBGColour)
- elseif row == 10 then
- configText = tostring(unitStyle.display[indexOf(unitStyle.values, config)]) .. " "
- writeAt(settingsX + 18, settingsY + row, " ", 1, sBGColour)
- end
- writeAt(settingsX + 21, settingsY + row, configText, sTextColour, sBGColour)
- writeAt(settingsX + 32, settingsY + row, string.char(31), sTextColour, sBGColour)
- end
- writeAt(settingsX, settingsY - 2, " SETTINGS ", sTextColour, 32768)
- writeAt(settingsX, settingsY + sHeight - 5, " " .. string.char(27) .. " BACK ", sTextColour, 16384)
- writeAt(settingsX + 10, settingsY + sHeight - 5, " RESET ", sTextColour, 512)
- end
- local function settings()
- local sWidth, sHeight = 37, 17
- local settingsX = (w / 2) - math.floor(sWidth / 2) + 2
- local settingsY = (h / 2) - math.floor(sHeight / 2) + 2
- local colourConfigs = {
- [settingsY + 0] = "borderColour",
- [settingsY + 1] = "chartBGColour",
- [settingsY + 2] = "inputChartColour",
- [settingsY + 3] = "outputChartColour",
- [settingsY + 4] = "batteryChargeColour",
- [settingsY + 5] = "idleColour",
- [settingsY + 6] = "sectionTitleColour",
- [settingsY + 7] = "baseTextColour",
- }
- local numberConfigs = {
- [settingsY + 9] = "screenRefreshRate",
- [settingsY + 10] = "unitFormatCaps",
- }
- while true do
- m.clear()
- drawSettings(settingsX, settingsY, sWidth, sHeight)
- local event, side, x_, y_ = os.pullEvent("monitor_touch")
- if x_ >= settingsX + 18 and x_ <= settingsX + 32 then
- if colourConfigs[y_] then
- local optionIndex = dropdownMenu(settingsX + 32, y_, colorText)
- if optionIndex ~= nil then saveConfig(colourConfigs[y_], colorDec[optionIndex]) end
- elseif y_ == settingsY + 9 then
- local optionIndex = dropdownMenu(settingsX + 32, y_, refreshRates.display)
- if optionIndex ~= nil then saveConfig("screenRefreshRate", refreshRates.values[optionIndex]) end
- elseif y_ == settingsY + 10 then
- local optionIndex = dropdownMenu(settingsX + 32, y_, unitStyle.display)
- if optionIndex ~= nil then saveConfig("unitFormatCaps", unitStyle.values[optionIndex]) end
- end
- elseif x_ >= settingsX and x_ <= settingsX + 7 and y_ == settingsY + sHeight - 5 then
- return
- elseif x_ >= settingsX + 10 and x_ <= settingsX + 16 and y_ == settingsY + sHeight - 5 then
- local defaults = {
- borderColour = 128, chartBGColour = 128, inputChartColour = 32,
- outputChartColour = 16384, batteryChargeColour = 8, idleColour = 16,
- sectionTitleColour = 1, baseTextColour = 1, screenRefreshRate = 0.1,
- unitFormatCaps = true
- }
- for configName, value in pairs(defaults) do saveConfig(tostring(configName), value) end
- end
- end
- end
- -- === Main loop ===
- local redrawRequired = false
- while true do
- if redrawRequired then
- m.clear(); drawStaticElements(); drawStatistics(); redrawRequired = false
- end
- parallel.waitForAny(touch, display)
- if y_touch == 2 and x_touch >= 87 and x_touch <= 96 then
- redrawRequired = true; m.clear(); settings()
- end
- end
Add Comment
Please, Sign In to add comment