Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- configs
- 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 -- how often to display new data in seconds.
- local unitFormatCaps = true -- true = X.X KRF. false = X.XkRF
- local dataCorrection = 2.5 -- value to divide IM data by.
- -- === Smart peripheral discovery (local + wired modem remotes) ===
- local function findMonitorAndInduction()
- local foundMonitor, foundInduction
- local function consider(name, getType)
- local pType = (getType and getType(name)) or peripheral.getType(name)
- local lname = tostring(name):lower()
- -- first monitor we see
- 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
- -- first “induction matrix” we see (by name OR type)
- if not foundInduction then
- local looksLikeIM = (pType == "inductionPort")
- 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 end
- end
- end
- end
- -- 1) local peripherals
- for _, name in ipairs(peripheral.getNames()) do
- local pType = peripheral.getType(name)
- consider(name, peripheral.getType)
- -- 2) if it's a *wired* modem, walk its remote network
- 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 getTypeRemote = function(n)
- if mdm.getTypeRemote then return mdm.getTypeRemote(n) end
- return nil
- end
- consider(rname, getTypeRemote)
- if foundMonitor and foundInduction then return foundMonitor, foundInduction end
- end
- end
- end
- end
- end
- if foundMonitor and foundInduction then break end
- end
- return foundMonitor, foundInduction
- end
- -- setup (auto-discover across local + wired modem)
- local m, im = findMonitorAndInduction()
- local w, h = nil
- -- global values
- 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"}
- }
- 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
- 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
- function drawBar(xmin, xmax, y, r, c)
- for i=1, r, 1 do
- draw(xmin, xmax, y+i-1, 1, c)
- end
- end
- function writeAt(x, y, t, c, b, target)
- target = target or m
- local oc = target.getTextColour()
- local ob = target.getBackgroundColour()
- 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
- function newWindow(target, x, y, w, h, bg)
- local win = window.create(target, x, y, w, h) -- create window
- win.setBackgroundColor(bg or 32768)
- win.clear()
- return win
- end
- -- error screen
- 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()
- -- error
- m.setTextColour(16384)
- m.setCursorPos((x/2) - (string.len(errorText)/2), (y/2)-1)
- m.write(errorText)
- -- msg
- 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) -- remove leftover data
- writeAt((x/2) - (string.len(countdownStr)/2), (y/2)+2, countdownStr, baseTextColour)
- print(countdownStr)
- timer = timer -1
- os.sleep(1)
- end
- os.reboot()
- end
- local function printColored(text, color)
- if term.isColor and term.isColor() and color and type(color) == "number" then
- term.setTextColor(color)
- end
- print(text)
- if term.isColor and term.isColor() then
- term.setTextColor(colors.white)
- end
- end
- -- monitor check
- if m ~= nil then
- pcall(function() m.setTextScale(0.5) end) -- target 100x38 if possible
- local x, y = m.getSize()
- if x ~= 100 or y ~= 38 then
- print("WARNING: Monitor size is "..x.."x"..y..". Designed for 100x38 (5x3 blocks @ 0.5 scale).")
- 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
- -- matrix check
- while not im or not (pcall(function() return im.isFormed() end) and im.isFormed()) do
- if not im then
- displayError(2)
- else
- displayError(3)
- end
- end
- function removeOverflow(tableToEdit, maxDataCount) -- delete old data
- while #tableToEdit >= maxDataCount do
- table.remove(tableToEdit, 1)
- end
- return tableToEdit
- end
- function getRange(input)
- local max = 0
- local min = 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
- 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
- function roundD(exact, quantum)
- local quant,frac = math.modf(exact/quantum)
- return quantum * (quant + (frac > 0.5 and 1 or 0))
- end
- -- format time
- 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}
- }
- 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." -- fallback, if time is 0 or negative
- end
- 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
- function protCall(func, errIndex)
- local success, result = pcall(func)
- if success and result ~= nil then
- return result
- else
- displayError(errIndex)
- end
- end
- function indexOf(array, value)
- for i, v in ipairs(array) do
- if v == value then
- return i
- end
- end
- return nil
- end
- local energy, maxEnergy, installedCells, installedProviders, lastInput, lastOutput, filledPercentage, maxTransfer, lowBatAlarmEnabled = nil
- local inputHistory, outputHistory, flowHistory = {}, {}, {}
- function drawStaticElements()
- local p = 2
- local chartBGColour = 256
- draw(p, w-3, p, 1, borderColour) -- top
- draw(p, w-3, h-1, 1, borderColour) -- bottom
- draw(p, 1, p, h-p, borderColour) -- left
- draw(w-1, 1, p, h-p, borderColour) -- right
- draw(27, 1, p, h-p, borderColour) -- vertical divider
- draw(28, 71, h/2+2, 1, borderColour) -- horizontal divider
- draw(63, 1, h/2+3, 15, borderColour) -- small vertical divider
- draw(2, 25, 28, 1, borderColour) -- small horizontal divider
- 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
- function drawInputGraph()
- table.insert(inputHistory, lastInput)
- removeOverflow(inputHistory, 34)
- local inGraph = newWindow(m, 29, 23, 33, 13, chartBGColour)
- local ox, oy = inGraph.getSize()
- local graph_min = 0
- local graph_max = 13
- local rf_min, rf_max = getRange(inputHistory)
- for i = 0, #inputHistory do
- local value = inputHistory[#inputHistory-i]
- if not value then break end
- local result = math.floor(math.min(math.max((value * (graph_max - graph_min) / rf_max) + graph_min, graph_min), graph_max) + 0.5)
- if result ~= result then result = 0 end -- NaN check
- draw(ox-i, 1, oy-result+1, result, inputChartColour, inGraph)
- end
- local maxRf = conversion(rf_max, true, true) .. "RF/t"
- local txt, fg, bg = inGraph.getLine(1)
- for i = 0, #maxRf do
- local char = maxRf:sub(1+i, 1+i)
- local b = fromBlit(bg:sub(1+i, 1+i)) or chartBGColour
- writeAt(1+i, 1, char, baseTextColour, b, inGraph)
- end
- local maxPercent = tostring(round((rf_max/maxTransfer)*100) .. "%")
- for i = 0, #maxPercent do
- local c = ox-#maxPercent+i+1
- local char = maxPercent:sub(1+i, 1+i)
- local b = fromBlit(inGraph.getLine(1 select)) or chartBGColour -- safety if getLine reused
- 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) -- erase leftover data on title bar
- 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)*100) .. "%) "))
- if lastInput == maxTransfer 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
- function drawOutputGraph()
- table.insert(outputHistory, lastOutput)
- removeOverflow(outputHistory, 34)
- local outGraph = newWindow(m, 65, 23, 33, 13, chartBGColour)
- local ox, oy = outGraph.getSize()
- local graph_min = 0
- local graph_max = 13
- local rf_min, rf_max = getRange(outputHistory)
- for i = 0, #outputHistory do
- local value = outputHistory[#outputHistory-i]
- if not value then break end
- local result = math.floor(math.min(math.max((value * (graph_max - graph_min) / rf_max) + graph_min, graph_min), graph_max) + 0.5)
- if result ~= result then result = 0 end -- NaN check
- draw(ox-i, 1, oy-result+1, result, outputChartColour, outGraph)
- end
- local maxRf = conversion(rf_max*-1, true, true) .. "RF/t"
- local txt, fg, 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)*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) -- erase leftover data on title bar
- 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)*100) .. "%) ")
- if lastOutput == maxTransfer 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
- 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 graph_min = 0
- local graph_max = 8
- local rf_min, rf_max = getRange(flowHistory)
- local scale = math.max(math.abs(rf_min), rf_max) -- scale
- 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 * (graph_max - graph_min) / scale) + graph_min, graph_min), graph_max) + 0.5)
- if result ~= result then result = 0 end -- NaN check
- 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) -- erase leftover data
- 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 txt, fg, 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
- function drawBattery()
- drawBar(4, 22, 4, 23, chartBGColour) -- battery background
- 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)
- local flow = lastInput - lastOutput
- local chargeStatus = "Idle"
- local timeStatus = ""
- local statusColour = 16
- local time = "Inf."
- if (flow < 0) then
- statusColour = 16384
- chargeStatus = "Depleting"
- timeStatus = "Empty In:"
- time = adjustTime(energy / (lastOutput - lastInput) / 20)
- elseif flow > 0 then
- statusColour = 32
- chargeStatus = "Charging"
- timeStatus = "Full In:"
- time = adjustTime((maxEnergy - energy) / (lastInput - lastOutput) / 20)
- end
- local timeString = timeStatus ~= "" and timeStatus .. " " .. time or time
- 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
- function drawStatistics()
- writeAt(4, 30, "Size: " .. imWidth .. " x " .. imHeight .. " x " .. imLength, baseTextColour)
- writeAt(4, 31, "Cells: " .. installedCells, baseTextColour)
- writeAt(4, 32, "Providers: " .. installedProviders, baseTextColour)
- writeAt(4, 33, "Empty Slots: " .. tostring(((imWidth - 2) * (imLength - 2) * (imHeight - 2))) - installedCells - installedProviders, baseTextColour)
- writeAt(4, 34, "MaxTransfer: " .. conversion(maxTransfer, true) .. "/t", baseTextColour)
- writeAt(4, 35, "MaxCapacity: " .. conversion(maxEnergy, true), baseTextColour)
- end
- installedCells = protCall(im.getInstalledCells, 1)
- installedProviders = protCall(im.getInstalledProviders, 1)
- maxEnergy = protCall(im.getMaxEnergy, 1)
- maxEnergy = maxEnergy/dataCorrection
- maxTransfer = protCall(im.getTransferCap, 1)
- maxTransfer = maxTransfer/dataCorrection
- imWidth = protCall(im.getWidth, 1)
- imLength = protCall(im.getLength, 1)
- imHeight = protCall(im.getHeight, 1)
- drawStaticElements()
- drawStatistics()
- local monitor_touched, x, y;
- local function touch()
- local color = false
- while true do
- local event_data = table.pack(os.pullEvent())
- if event_data[1] == "monitor_touch" then
- monitor_touched, x, y = table.unpack(event_data, 2, event_data.n)
- break
- end
- end
- end
- local function display()
- while true do
- energy = protCall(im.getEnergy, 1)
- energy = energy/dataCorrection
- lastInput = protCall(im.getLastInput, 1)
- lastInput = lastInput/dataCorrection
- lastOutput = protCall(im.getLastOutput, 1)
- lastOutput = lastOutput/dataCorrection
- filledPercentage = protCall(im.getEnergyFilledPercentage, 1)
- drawInputGraph()
- drawOutputGraph()
- drawFlowGraph()
- drawBattery()
- local activeTransfer = math.max(lastInput, lastOutput)
- local provUsage = round((activeTransfer/maxTransfer)*100, 2)
- os.sleep(screenRefreshRate)
- end
- end
- function saveConfig(name, value)
- local filename = shell.getRunningProgram()
- local lines = {}
- for line in io.lines(filename) do
- table.insert(lines, line)
- end
- -- Update local setting
- 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
- function dropdownMenu(x, y, options)
- local width = 0
- local optionText = ""
- 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
- function drawSettings(settingsX, settingsY, sWidth, sHeight)
- local sTextColour = 1
- local sBGColour = 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 = data.row
- local config = data.config
- local configText
- 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
- function settings()
- local sWidth = 37
- local sHeight = 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
- -- colour dropdown menus
- local optionIndex = dropdownMenu(settingsX + 32, y, colorText)
- if optionIndex ~= nil then
- saveConfig(colourConfigs[y], colorDec[optionIndex])
- end
- elseif y == settingsY + 9 then
- -- refresh rate
- local optionIndex = dropdownMenu(settingsX + 32, y, refreshRates.display)
- if optionIndex ~= nil then
- saveConfig("screenRefreshRate", refreshRates.values[optionIndex])
- end
- elseif y == settingsY + 10 then
- -- unit style
- 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
- -- exit
- return
- elseif x >= settingsX + 10 and x <= settingsX + 16 and y == settingsY + sHeight - 5 then
- -- reset
- 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
- local redrawRequired = false
- while true do
- if redrawRequired then
- m.clear()
- drawStaticElements()
- drawStatistics()
- redrawRequired = false
- end
- parallel.waitForAny(touch, display)
- if y == 2 and x >= 87 and x <= 96 then
- redrawRequired = true
- m.clear()
- settings()
- end
- end
Add Comment
Please, Sign In to add comment