April_The_Sergal

induc

Sep 28th, 2025
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.26 KB | None | 0 0
  1. -- configs
  2. local borderColour = 128
  3. local chartBGColour = 128
  4. local inputChartColour = 32
  5. local outputChartColour = 16384
  6. local batteryChargeColour = 8
  7. local idleColour = 16
  8. local sectionTitleColour = 1
  9. local baseTextColour = 1
  10.  
  11. local screenRefreshRate = 0.1 -- how often to display new data in seconds.
  12. local unitFormatCaps = true -- true = X.X KRF. false = X.XkRF
  13. local dataCorrection = 2.5 -- value to divide IM data by.
  14.  
  15. -- === Smart peripheral discovery (local + wired modem remotes) ===
  16. local function findMonitorAndInduction()
  17.   local foundMonitor, foundInduction
  18.  
  19.   local function consider(name, getType)
  20.     local pType = (getType and getType(name)) or peripheral.getType(name)
  21.     local lname = tostring(name):lower()
  22.  
  23.     -- first monitor we see
  24.     if (not foundMonitor) and (pType == "monitor" or lname:find("monitor", 1, true)) then
  25.       local ok, p = pcall(peripheral.wrap, name)
  26.       if ok and p then foundMonitor = p end
  27.     end
  28.  
  29.     -- first “induction matrix” we see (by name OR type)
  30.     if not foundInduction then
  31.       local looksLikeIM = (pType == "inductionPort")
  32.         or (pType and pType:lower():find("induction", 1, true))
  33.         or lname:find("induction", 1, true)
  34.         or lname:find("matrix", 1, true)
  35.       if looksLikeIM then
  36.         local ok, p = pcall(peripheral.wrap, name)
  37.         if ok and p then foundInduction = p end
  38.       end
  39.     end
  40.   end
  41.  
  42.   -- 1) local peripherals
  43.   for _, name in ipairs(peripheral.getNames()) do
  44.     local pType = peripheral.getType(name)
  45.     consider(name, peripheral.getType)
  46.  
  47.     -- 2) if it's a *wired* modem, walk its remote network
  48.     if pType == "modem" then
  49.       local mdm = peripheral.wrap(name)
  50.       if mdm and mdm.isWireless and mdm.getNamesRemote then
  51.         local isWireless = false
  52.         pcall(function() isWireless = mdm.isWireless() end)
  53.         if not isWireless then
  54.           local ok, remotes = pcall(mdm.getNamesRemote)
  55.           if ok and type(remotes) == "table" then
  56.             for _, rname in ipairs(remotes) do
  57.               local getTypeRemote = function(n)
  58.                 if mdm.getTypeRemote then return mdm.getTypeRemote(n) end
  59.                 return nil
  60.               end
  61.               consider(rname, getTypeRemote)
  62.               if foundMonitor and foundInduction then return foundMonitor, foundInduction end
  63.             end
  64.           end
  65.         end
  66.       end
  67.     end
  68.  
  69.     if foundMonitor and foundInduction then break end
  70.   end
  71.  
  72.   return foundMonitor, foundInduction
  73. end
  74.  
  75. -- setup (auto-discover across local + wired modem)
  76. local m, im = findMonitorAndInduction()
  77. local w, h = nil
  78.  
  79. -- global values
  80. local colorText = {"White","Orange","Magenta","Light blue","Yellow","Lime","Pink","Gray","Light gray","Cyan","Purple","Blue","Brown","Green","Red","Black"}
  81. local colorDec = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}
  82. local refreshRates = {
  83.     values = {  0.1, 0.3, 0.5, 1.0, 1.0, 2.0 },
  84.     display = { "Fastest", "Fast", "Normal", "Slow", "Very Slow", "Slowest"}
  85. }
  86. local unitStyle = {
  87.     values = { true, false},
  88.     display = { "X.X KRF", "X.XkRF"}
  89. }
  90.  
  91. function fromBlit(hex)
  92.     if #hex ~= 1 then return nil end
  93.     local value = tonumber(hex, 16)
  94.     if not value then return nil end
  95.  
  96.     return 2 ^ value
  97. end
  98.  
  99. function draw(xStart, xLen, yStart, yLen, c, target)
  100.     target = target or m
  101.     local ob = target.getBackgroundColour()
  102.     target.setBackgroundColor(c)
  103.     target.setCursorPos(xStart, yStart)
  104.     if xLen > 1 then
  105.         for i = 1, xLen do
  106.             target.write(" ")
  107.             target.setCursorPos(xStart+i, yStart)
  108.         end
  109.     elseif xLen ~= 0 and yLen ~= 0 then
  110.         target.write(" ")
  111.     end
  112.  
  113.     if yLen > 1 then
  114.         for k = 1, yLen-1 do
  115.             target.setCursorPos(xStart, yStart+k)
  116.             target.write(" ")
  117.         end
  118.     end
  119.    
  120.     target.setBackgroundColor(ob)
  121. end
  122.  
  123. function drawBar(xmin, xmax, y, r, c)
  124.     for i=1, r, 1 do    
  125.         draw(xmin, xmax, y+i-1, 1, c)
  126.     end
  127. end
  128.  
  129. function writeAt(x, y, t, c, b, target)
  130.     target = target or m
  131.     local oc = target.getTextColour()
  132.     local ob = target.getBackgroundColour()
  133.     if target.setTextColour then target.setTextColour(c or oc) end
  134.     if target.setBackgroundColour then target.setBackgroundColour(b or ob) end
  135.     target.setCursorPos(x, y)
  136.     target.write(t)
  137.     if target.setTextColour then target.setTextColour(oc) end
  138.     if target.setBackgroundColour then target.setBackgroundColour(ob) end
  139. end
  140.  
  141. function newWindow(target, x, y, w, h, bg)
  142.     local win = window.create(target, x, y, w, h) -- create window
  143.     win.setBackgroundColor(bg or 32768)
  144.     win.clear()
  145.     return win
  146. end
  147.  
  148. -- error screen
  149. function displayError(index)
  150.     local x, y = m.getSize()
  151.     local errorText = "ERROR"
  152.     local errors = {
  153.         "The Induction Matrix has been tampered with.",
  154.         "Induction Port not connected.",
  155.         "The Induction Matrix is incomplete."
  156.     }
  157.  
  158.     m.clear()
  159.     -- error
  160.     m.setTextColour(16384)
  161.     m.setCursorPos((x/2) - (string.len(errorText)/2), (y/2)-1)
  162.     m.write(errorText)
  163.     -- msg
  164.     writeAt((x/2) - (string.len(errors[index])/2), y/2, errors[index], baseTextColour)
  165.     printError("ERROR: " .. errors[index])
  166.  
  167.     local timer = 10
  168.     while timer > -1 do
  169.         local countdownStr = "Next attempt in " .. timer
  170.         draw((x/2) - (string.len(countdownStr)/2), string.len(countdownStr) + 1, (y/2)+2, 1, 32768) -- remove leftover data
  171.         writeAt((x/2) - (string.len(countdownStr)/2), (y/2)+2, countdownStr, baseTextColour)
  172.         print(countdownStr)
  173.  
  174.         timer = timer -1
  175.         os.sleep(1)
  176.     end
  177.  
  178.     os.reboot()
  179. end
  180.  
  181. local function printColored(text, color)
  182.     if term.isColor and term.isColor() and color and type(color) == "number" then
  183.         term.setTextColor(color)
  184.     end
  185.     print(text)
  186.     if term.isColor and term.isColor() then
  187.         term.setTextColor(colors.white)
  188.     end
  189. end
  190.  
  191. -- monitor check
  192. if m ~= nil then
  193.     pcall(function() m.setTextScale(0.5) end) -- target 100x38 if possible
  194.     local x, y = m.getSize()
  195.     if x ~= 100 or y ~= 38 then
  196.         print("WARNING: Monitor size is "..x.."x"..y..". Designed for 100x38 (5x3 blocks @ 0.5 scale).")
  197.     end
  198.  
  199.     w, h = m.getSize()
  200.     m.setBackgroundColor(32768)
  201.     m.clear()
  202.     m.setTextColour(1)
  203. else
  204.     print("No monitor found (local or via wired modem).")
  205.     return
  206. end
  207.  
  208. -- matrix check
  209. while not im or not (pcall(function() return im.isFormed() end) and im.isFormed()) do
  210.     if not im then
  211.         displayError(2)
  212.     else
  213.         displayError(3)
  214.     end
  215. end
  216.  
  217. function removeOverflow(tableToEdit, maxDataCount)  -- delete old data
  218.     while #tableToEdit >= maxDataCount do
  219.         table.remove(tableToEdit, 1)
  220.     end
  221.     return tableToEdit
  222. end
  223.  
  224. function getRange(input)
  225.     local max = 0
  226.     local min = 0
  227.     for i = 1, #input do
  228.         if i == 1 then
  229.             max = input[i]
  230.             min = input[i]
  231.         end
  232.         if(input[i] > max) then max = input[i] end
  233.         if(input[i] < min) then min = input[i] end
  234.     end
  235.     return min, max
  236. end
  237.  
  238. function round(x, decimal)
  239.     local m = 10^(decimal or 0)
  240.     local result = math.floor(x * m + 0.5) / m
  241.     return string.format("%." .. (decimal or 0) .. "f", result)
  242. end
  243.  
  244. function roundD(exact, quantum)
  245.     local quant,frac = math.modf(exact/quantum)
  246.     return quantum * (quant + (frac > 0.5 and 1 or 0))
  247. end
  248.  
  249. -- format time
  250. local timeUnits = {
  251.     {threshold = 8553600, unit = "99+ days"},
  252.     {threshold = 86400, unit = "days", divisor = 86400},
  253.     {threshold = 3600, unit = "hours", divisor = 3600},
  254.     {threshold = 60, unit = "min", divisor = 60},
  255.     {threshold = 1, unit = "sec", divisor = 1}
  256. }
  257. function adjustTime(time)
  258.     for _, unitData in ipairs(timeUnits) do
  259.         if time >= unitData.threshold then
  260.             local adjustedTime
  261.             if unitData.divisor and unitData.divisor ~= 1 then
  262.                 adjustedTime = string.format("%.1f", time / unitData.divisor)
  263.             elseif unitData.divisor then
  264.                 adjustedTime = math.floor(time / unitData.divisor)
  265.             else
  266.                 adjustedTime = ""
  267.             end
  268.             return string.format("%s %s", adjustedTime, unitData.unit)
  269.         end
  270.     end
  271.     return "0 sec."  -- fallback, if time is 0 or negative
  272. end
  273.  
  274. function conversion(exact, text, addPlus, afterDecimalPoint)
  275.     afterDecimalPoint = afterDecimalPoint or 0.1
  276.     addPlus = addPlus or false
  277.  
  278.     local units = unitFormatCaps and {"", "K", "M", "G", "T", "P", "E", "Z", "Y"} or {"", "k", "m", "g", "t", "p", "e", "z", "y"}
  279.     local pot = 1
  280.     local absExact = math.abs(exact)
  281.  
  282.     while absExact >= (1000^pot) do
  283.         pot = pot + 1
  284.     end
  285.  
  286.     local value = roundD(exact / (1000^(pot - 1)), afterDecimalPoint)
  287.     local out = tostring(value)
  288.    
  289.     if addPlus and value > 0 then out = "+" .. out end
  290.     if text then out = out .. (unitFormatCaps and " " .. units[pot] or units[pot]) end
  291.  
  292.     return out
  293. end
  294.  
  295. function protCall(func, errIndex)
  296.     local success, result = pcall(func)
  297.     if success and result ~= nil then
  298.         return result
  299.     else
  300.         displayError(errIndex)
  301.     end
  302. end
  303.  
  304. function indexOf(array, value)
  305.     for i, v in ipairs(array) do
  306.         if v == value then
  307.             return i
  308.         end
  309.     end
  310.     return nil
  311. end
  312.  
  313. local energy, maxEnergy, installedCells, installedProviders, lastInput, lastOutput, filledPercentage, maxTransfer, lowBatAlarmEnabled = nil
  314. local inputHistory, outputHistory, flowHistory = {}, {}, {}
  315.  
  316. function drawStaticElements()
  317.     local p = 2
  318.     local chartBGColour = 256
  319.     draw(p, w-3, p, 1, borderColour) -- top
  320.     draw(p, w-3, h-1, 1, borderColour) -- bottom
  321.     draw(p, 1, p, h-p, borderColour) -- left
  322.     draw(w-1, 1, p, h-p, borderColour) -- right
  323.     draw(27, 1, p, h-p, borderColour) -- vertical divider
  324.     draw(28, 71, h/2+2, 1, borderColour) -- horizontal divider
  325.     draw(63, 1, h/2+3, 15, borderColour) -- small vertical divider
  326.     draw(2, 25, 28, 1, borderColour) -- small horizontal divider
  327.  
  328.     writeAt(5, 2, "  BATTERY  ", sectionTitleColour)
  329.     writeAt(30, 2, "  ENERGY FLOW:  ", sectionTitleColour)
  330.     writeAt(5, 28, "  STATS:  ", sectionTitleColour)
  331.  
  332.     local configButtonText = "SETTINGS " .. string.char(26)
  333.     local configButtonSpaceMaker = "  "
  334.     local buttonX = w - #configButtonText - 3
  335.     local buttonY = 2
  336.     for i = 1, #configButtonText do
  337.         configButtonSpaceMaker = configButtonSpaceMaker .. " "
  338.     end
  339.     writeAt(buttonX - 1, buttonY, configButtonSpaceMaker)
  340.     writeAt(buttonX, buttonY, configButtonText, colors.white, colors.gray)
  341.  
  342.     writeAt(30, 21, "  INPUT:  ", sectionTitleColour)
  343.     writeAt(66, 21, "  OUTPUT:  ", sectionTitleColour)
  344. end
  345.  
  346. function drawInputGraph()
  347.     table.insert(inputHistory, lastInput)
  348.     removeOverflow(inputHistory, 34)
  349.  
  350.     local inGraph = newWindow(m, 29, 23, 33, 13, chartBGColour)
  351.     local ox, oy = inGraph.getSize()
  352.     local graph_min = 0
  353.     local graph_max = 13
  354.     local rf_min, rf_max = getRange(inputHistory)
  355.    
  356.     for i = 0, #inputHistory do
  357.         local value = inputHistory[#inputHistory-i]
  358.         if not value then break end
  359.         local result = math.floor(math.min(math.max((value * (graph_max - graph_min) / rf_max) + graph_min, graph_min), graph_max) + 0.5)
  360.         if result ~= result then result = 0 end -- NaN check
  361.         draw(ox-i, 1, oy-result+1, result, inputChartColour, inGraph)
  362.     end
  363.  
  364.     local maxRf = conversion(rf_max, true, true) .. "RF/t"
  365.     local txt, fg, bg = inGraph.getLine(1)
  366.     for i = 0, #maxRf do
  367.         local char = maxRf:sub(1+i, 1+i)
  368.         local b = fromBlit(bg:sub(1+i, 1+i)) or chartBGColour
  369.         writeAt(1+i, 1, char, baseTextColour, b, inGraph)
  370.     end
  371.  
  372.     local maxPercent = tostring(round((rf_max/maxTransfer)*100) .. "%")
  373.     for i = 0, #maxPercent do
  374.         local c = ox-#maxPercent+i+1
  375.         local char = maxPercent:sub(1+i, 1+i)
  376.         local b = fromBlit(inGraph.getLine(1 select)) or chartBGColour -- safety if getLine reused
  377.         writeAt(c, 1, char, baseTextColour, b, inGraph)
  378.     end
  379.  
  380.     local minRf = "0RF/t"
  381.     local _, _, bglow = inGraph.getLine(oy)
  382.     for i = 0, #minRf do
  383.         local char = minRf:sub(1+i, 1+i)
  384.         local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
  385.         writeAt(1+i, oy, char, baseTextColour, b, inGraph)
  386.     end
  387.  
  388.     local minPercent = "0%"
  389.     for i = 0, #minPercent do
  390.         local c = ox-#minPercent+i+1
  391.         local char = minPercent:sub(1+i, 1+i)
  392.         local b = fromBlit(bglow:sub(c, c)) or chartBGColour
  393.         writeAt(c, oy, char, baseTextColour, b, inGraph)
  394.     end
  395.  
  396.     draw(39, 22, 21, 1, borderColour) -- erase leftover data on title bar
  397.     local rf_tick = conversion(lastInput, true, true) .. "RF/t "
  398.     writeAt(39, 21, rf_tick, (lastInput > 0 and inputChartColour or idleColour))
  399.     writeAt(39+#rf_tick, 21, "(" .. tostring(round((lastInput/maxTransfer)*100) .. "%)  "))
  400.  
  401.     if lastInput == maxTransfer then
  402.         local maxStrings = {
  403.             "MAX INPUT!",
  404.             "Add / Upgrade",
  405.             "Providers."
  406.         }
  407.         drawBar(46-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
  408.         writeAt(46-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
  409.         writeAt(46-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
  410.         writeAt(46-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
  411.     end
  412. end
  413.  
  414. function drawOutputGraph()
  415.     table.insert(outputHistory, lastOutput)
  416.     removeOverflow(outputHistory, 34)
  417.    
  418.     local outGraph = newWindow(m, 65, 23, 33, 13, chartBGColour)
  419.     local ox, oy = outGraph.getSize()
  420.     local graph_min = 0
  421.     local graph_max = 13
  422.     local rf_min, rf_max = getRange(outputHistory)
  423.    
  424.     for i = 0, #outputHistory do
  425.         local value = outputHistory[#outputHistory-i]
  426.         if not value then break end
  427.         local result = math.floor(math.min(math.max((value * (graph_max - graph_min) / rf_max) + graph_min, graph_min), graph_max) + 0.5)
  428.         if result ~= result then result = 0 end -- NaN check
  429.         draw(ox-i, 1, oy-result+1, result, outputChartColour, outGraph)
  430.     end
  431.  
  432.     local maxRf = conversion(rf_max*-1, true, true) .. "RF/t"
  433.     local txt, fg, bgtop = outGraph.getLine(1)
  434.     for i = 0, # maxRf do
  435.         local char = maxRf:sub(1+i, 1+i)
  436.         local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
  437.         writeAt(1+i, 1, char, baseTextColour, b, outGraph)
  438.     end
  439.  
  440.     local maxPercent = tostring(round((rf_max/maxTransfer)*100) .. "%")
  441.     for i = 0, #maxPercent do
  442.         local c = ox-#maxPercent+i+1
  443.         local char = maxPercent:sub(1+i, 1+i)
  444.         local b = fromBlit(bgtop:sub(c, c)) or chartBGColour
  445.         writeAt(c, 1, char, baseTextColour, b, outGraph)
  446.     end
  447.  
  448.     local minRf = "0RF/t"
  449.     local _, _, bglow = outGraph.getLine(oy)
  450.     for i = 0, #minRf do
  451.         local char = minRf:sub(1+i, 1+i)
  452.         local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
  453.         writeAt(1+i, oy, char, baseTextColour, b, outGraph)
  454.     end
  455.  
  456.     local minPercent = "0%"
  457.     for i = 0, #minPercent do
  458.         local c = ox-#minPercent+i+1
  459.         local char = minPercent:sub(1+i, 1+i)
  460.         local b = fromBlit(bglow:sub(c, c)) or chartBGColour
  461.         writeAt(c, oy, char, baseTextColour, b, outGraph)
  462.     end
  463.  
  464.     draw(76, 22, 21, 1, borderColour) -- erase leftover data on title bar
  465.     local rf_tick = conversion(lastOutput*-1, true) .. "RF/t "
  466.     writeAt(76, 21, rf_tick, (lastOutput > 0 and outputChartColour or idleColour))
  467.     writeAt(76+#rf_tick, 21, "(" .. tostring(round((lastOutput/maxTransfer)*100) .. "%)  ")
  468.  
  469.     if lastOutput == maxTransfer then
  470.         local maxStrings = {
  471.             "MAX OUTPUT!",
  472.             "Add / Upgrade",
  473.             "Providers."
  474.         }
  475.         drawBar(82-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
  476.         writeAt(82-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
  477.         writeAt(82-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
  478.         writeAt(82-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
  479.     end
  480. end
  481.  
  482. function drawFlowGraph()
  483.     table.insert(flowHistory, lastInput-lastOutput)
  484.     removeOverflow(flowHistory, 70)
  485.    
  486.     local flowGraph = newWindow(m, 29, 4, 69, 16, chartBGColour)
  487.     local fx, fy = flowGraph.getSize()
  488.     local graph_min = 0
  489.     local graph_max = 8
  490.     local rf_min, rf_max = getRange(flowHistory)
  491.     local scale = math.max(math.abs(rf_min), rf_max) -- scale
  492.    
  493.     for i = 0, #flowHistory do
  494.         local value = flowHistory[#flowHistory-i]
  495.         if not value then break end
  496.         value = math.abs(value)
  497.         local result = math.floor(math.min(math.max((value * (graph_max - graph_min) / scale) + graph_min, graph_min), graph_max) + 0.5)
  498.         if result ~= result then result = 0 end -- NaN check
  499.        
  500.         if result == 0 then
  501.             writeAt(fx-i, 8, "_", 32768, chartBGColour, flowGraph)
  502.         elseif flowHistory[#flowHistory-i] < 0 then
  503.             draw(fx-i, 1, 9, result, outputChartColour, flowGraph)
  504.         else
  505.             draw(fx-i, 1, 9-result, result, inputChartColour, flowGraph)
  506.         end
  507.     end
  508.  
  509.     local lastFlow = flowHistory[#flowHistory]
  510.     draw(45, 15, 2, 1, borderColour) -- erase leftover data
  511.     writeAt(45, 2, conversion(lastFlow, true, true) .. "RF/t  ", (lastFlow == 0 and idleColour or (lastFlow > 0 and inputChartColour or outputChartColour)))
  512.  
  513.     local positiveRf = conversion(scale, true, true) .. "RF/t"
  514.     local txt, fg, bgtop = flowGraph.getLine(1)
  515.     for i = 0, #positiveRf do
  516.         local char = positiveRf:sub(1+i, 1+i)
  517.         local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
  518.         writeAt(1+i, 1, char, baseTextColour, b, flowGraph)
  519.     end
  520.  
  521.     local noRF = "0 RF/t"
  522.     local _, _, bgn = flowGraph.getLine(8)
  523.     for i = 0, #noRF do
  524.         local char = noRF:sub(1+i, 1+i)
  525.         local b = fromBlit(bgn:sub(1+i, 1+i)) or chartBGColour
  526.         writeAt(1+i, 8, char, baseTextColour, b, flowGraph)
  527.     end
  528.  
  529.     local negativeRf = conversion(scale*-1, true, true) .. "RF/t"
  530.     local _, _, bgbot = flowGraph.getLine(16)
  531.     for i = 0, #negativeRf do
  532.         local char = negativeRf:sub(1+i, 1+i)
  533.         local b = fromBlit(bgbot:sub(1+i, 1+i)) or chartBGColour
  534.         writeAt(1+i, 16, char, baseTextColour, b, flowGraph)
  535.     end
  536. end
  537.  
  538. function drawBattery()
  539.     drawBar(4, 22, 4, 23, chartBGColour) -- battery background
  540.    
  541.     chargeHeight = round(filledPercentage*23)
  542.     drawBar(4, 22, 27-chargeHeight, chargeHeight, batteryChargeColour)
  543.  
  544.     local percent = tostring(round((energy/maxEnergy)*100, 2)) .. "%"
  545.     local percentPos = (22/2) - (string.len(percent)/2) + 4
  546.     writeAt(percentPos, 15, percent, baseTextColour, chartBGColour)
  547.  
  548.     local stored = "STORED: " .. conversion(energy, true, false, 0.01) .. "RF"
  549.     local storedPos = (22/2) - (string.len(stored)/2) + 4
  550.     writeAt(storedPos, 5, stored, baseTextColour, chartBGColour)
  551.  
  552.     local flow = lastInput - lastOutput
  553.     local chargeStatus = "Idle"
  554.     local timeStatus = ""
  555.     local statusColour = 16
  556.     local time = "Inf."
  557.     if (flow < 0) then
  558.         statusColour = 16384
  559.         chargeStatus = "Depleting"
  560.         timeStatus = "Empty In:"
  561.         time = adjustTime(energy / (lastOutput - lastInput) / 20)
  562.     elseif flow > 0 then
  563.         statusColour = 32
  564.         chargeStatus = "Charging"
  565.         timeStatus = "Full In:"
  566.         time = adjustTime((maxEnergy - energy) / (lastInput - lastOutput) / 20)
  567.     end
  568.     local timeString = timeStatus ~= "" and timeStatus .. " " .. time or time
  569.     local timePos = (22/2) - (string.len(timeString)/2) + 4
  570.     writeAt((22/2) - (string.len(chargeStatus)/2) + 4, 24, chargeStatus, baseTextColour, chartBGColour)
  571.     writeAt(timePos, 25, timeString, baseTextColour, chartBGColour)
  572. end
  573.  
  574. function drawStatistics()
  575.     writeAt(4, 30, "Size: " .. imWidth .. " x " .. imHeight .. " x " .. imLength, baseTextColour)
  576.     writeAt(4, 31, "Cells: " .. installedCells, baseTextColour)
  577.     writeAt(4, 32, "Providers: " .. installedProviders, baseTextColour)
  578.     writeAt(4, 33, "Empty Slots: " .. tostring(((imWidth - 2) * (imLength - 2) * (imHeight - 2))) - installedCells - installedProviders, baseTextColour)
  579.     writeAt(4, 34, "MaxTransfer: " .. conversion(maxTransfer, true) .. "/t", baseTextColour)
  580.     writeAt(4, 35, "MaxCapacity: " .. conversion(maxEnergy, true), baseTextColour)
  581. end
  582.  
  583. installedCells = protCall(im.getInstalledCells, 1)
  584. installedProviders = protCall(im.getInstalledProviders, 1)
  585. maxEnergy = protCall(im.getMaxEnergy, 1)
  586. maxEnergy = maxEnergy/dataCorrection
  587. maxTransfer = protCall(im.getTransferCap, 1)
  588. maxTransfer = maxTransfer/dataCorrection
  589. imWidth = protCall(im.getWidth, 1)
  590. imLength = protCall(im.getLength, 1)
  591. imHeight = protCall(im.getHeight, 1)
  592. drawStaticElements()
  593. drawStatistics()
  594.  
  595. local monitor_touched, x, y;
  596. local function touch()
  597.     local color = false
  598.     while true do
  599.         local event_data = table.pack(os.pullEvent())
  600.         if event_data[1] == "monitor_touch" then
  601.             monitor_touched, x, y = table.unpack(event_data, 2, event_data.n)
  602.             break
  603.         end
  604.     end
  605. end
  606.  
  607. local function display()
  608.     while true do
  609.         energy = protCall(im.getEnergy, 1)
  610.         energy = energy/dataCorrection
  611.         lastInput = protCall(im.getLastInput, 1)
  612.         lastInput = lastInput/dataCorrection
  613.         lastOutput = protCall(im.getLastOutput, 1)
  614.         lastOutput = lastOutput/dataCorrection
  615.         filledPercentage = protCall(im.getEnergyFilledPercentage, 1)
  616.    
  617.         drawInputGraph()
  618.         drawOutputGraph()
  619.         drawFlowGraph()
  620.         drawBattery()
  621.    
  622.         local activeTransfer = math.max(lastInput, lastOutput)
  623.         local provUsage = round((activeTransfer/maxTransfer)*100, 2)
  624.    
  625.         os.sleep(screenRefreshRate)
  626.     end
  627. end
  628.  
  629. function saveConfig(name, value)
  630.     local filename = shell.getRunningProgram()
  631.     local lines = {}
  632.  
  633.     for line in io.lines(filename) do
  634.         table.insert(lines, line)
  635.     end
  636.    
  637.     -- Update local setting
  638.     local settingsMap = {
  639.         borderColour = { var = "borderColour", line = 2 },
  640.         chartBGColour = { var = "chartBGColour", line = 3 },
  641.         inputChartColour = { var = "inputChartColour", line = 4 },
  642.         outputChartColour = { var = "outputChartColour", line = 5 },
  643.         batteryChargeColour = { var = "batteryChargeColour", line = 6 },
  644.         idleColour = { var = "idleColour", line = 7 },
  645.         sectionTitleColour = { var = "sectionTitleColour", line = 8 },
  646.         baseTextColour = { var = "baseTextColour", line = 9 },
  647.         screenRefreshRate = { var = "screenRefreshRate", line = 11 },
  648.         unitFormatCaps = { var = "unitFormatCaps", line = 12 },
  649.     }
  650.  
  651.     if settingsMap[name] then
  652.         if name == "borderColour" then borderColour = value
  653.         elseif name == "chartBGColour" then chartBGColour = value
  654.         elseif name == "inputChartColour" then inputChartColour = value
  655.         elseif name == "outputChartColour" then outputChartColour = value
  656.         elseif name == "batteryChargeColour" then batteryChargeColour = value
  657.         elseif name == "idleColour" then idleColour = value
  658.         elseif name == "sectionTitleColour" then sectionTitleColour = value
  659.         elseif name == "baseTextColour" then baseTextColour = value
  660.         elseif name == "screenRefreshRate" then screenRefreshRate = value
  661.         elseif name == "unitFormatCaps" then unitFormatCaps = value
  662.         end
  663.  
  664.         lines[settingsMap[name].line] = "local " .. settingsMap[name].var .. " = " .. tostring(value)
  665.     end
  666.  
  667.     local h = io.open(filename, 'w')
  668.     for _, line in ipairs(lines) do
  669.         h:write(line .. "\n")
  670.     end
  671.     h:close()
  672. end
  673.  
  674. function dropdownMenu(x, y, options)
  675.     local width = 0
  676.     local optionText = ""
  677.     for i = 1, #options do
  678.         options[i] = tostring(options[i])
  679.         if #options[i] > width then
  680.             width = #options[i]
  681.         end
  682.     end
  683.  
  684.     for i = 1, #options do
  685.         optionText = " " .. options[i] .. "  "
  686.         for j = 1, width - #options[i] do
  687.             optionText = optionText .. " "
  688.         end
  689.  
  690.         writeAt(x - 2 - width, y + i, optionText, 1, 256)
  691.     end
  692.  
  693.     while true do
  694.         local event, side, ox, oy = os.pullEvent("monitor_touch")
  695.         if(ox >= x - 2 - width and ox <= x and oy >= y + 1 and oy <= y + #options) then
  696.             return oy-y
  697.         else
  698.             return nil
  699.         end
  700.     end
  701. end
  702.  
  703. function drawSettings(settingsX, settingsY, sWidth, sHeight)
  704.     local sTextColour = 1
  705.     local sBGColour = 128
  706.  
  707.     drawBar(settingsX - 2, sWidth, settingsY - 2, 1, sBGColour)
  708.     drawBar(settingsX - 2, sWidth, settingsY + sHeight - 3, 1, sBGColour)
  709.     drawBar(settingsX - 2, 1, settingsY - 2, sHeight, sBGColour)
  710.     drawBar(settingsX - 2 + sWidth, 1, settingsY - 2, sHeight, sBGColour)
  711.  
  712.     local labels = {
  713.         "Borders: ",
  714.         "Chart Background: ",
  715.         "Input Chart: ",
  716.         "Output Chart: ",
  717.         "Battery: ",
  718.         "Idle: ",
  719.         "Section Titles: ",
  720.         "Base Text: ",
  721.     }
  722.  
  723.     for i, label in ipairs(labels) do
  724.         writeAt(settingsX, i + settingsY - 1, label)
  725.     end
  726.    
  727.     writeAt(settingsX, settingsY + 9, "Refresh Rate: ")
  728.     writeAt(settingsX, settingsY + 10, "Unit Style: ")
  729.  
  730.     local configs = {
  731.         {row = 0, config = borderColour},
  732.         {row = 1, config = chartBGColour},
  733.         {row = 2, config = inputChartColour},
  734.         {row = 3, config = outputChartColour},
  735.         {row = 4, config = batteryChargeColour},
  736.         {row = 5, config = idleColour},
  737.         {row = 6, config = sectionTitleColour},
  738.         {row = 7, config = baseTextColour},
  739.         {row = 9, config = screenRefreshRate},
  740.         {row = 10, config = unitFormatCaps},
  741.     }
  742.    
  743.     for i, data in ipairs(configs) do
  744.         local row = data.row
  745.         local config = data.config
  746.         local configText
  747.  
  748.         if row <= 7 then
  749.             configText = tostring(colorText[indexOf(colorDec, config)]) .. " "
  750.             writeAt(settingsX + 18, settingsY + row, "  ", 1, config)
  751.             writeAt(settingsX + 20, settingsY + row, "            ", 1, sBGColour)
  752.         elseif row == 9 then
  753.             configText = tostring(refreshRates.display[indexOf(refreshRates.values, config)]) .. " "
  754.             writeAt(settingsX + 18, settingsY + row, "              ", 1, sBGColour)
  755.         elseif row == 10 then
  756.             configText = tostring(unitStyle.display[indexOf(unitStyle.values, config)]) .. " "
  757.             writeAt(settingsX + 18, settingsY + row, "              ", 1, sBGColour)
  758.         end
  759.    
  760.        
  761.         writeAt(settingsX + 21, settingsY + row, configText, sTextColour, sBGColour)
  762.         writeAt(settingsX + 32, settingsY + row, string.char(31), sTextColour, sBGColour)
  763.        
  764.     end
  765.  
  766.     writeAt(settingsX, settingsY - 2, "  SETTINGS  ", sTextColour, 32768)
  767.     writeAt(settingsX, settingsY + sHeight - 5, " " .. string.char(27) .. " BACK ", sTextColour, 16384)
  768.     writeAt(settingsX + 10, settingsY + sHeight - 5, " RESET ", sTextColour, 512)
  769. end
  770.  
  771. function settings()
  772.     local sWidth = 37
  773.     local sHeight = 17
  774.  
  775.     local settingsX = (w / 2) - math.floor(sWidth / 2) + 2
  776.     local settingsY = (h / 2) - math.floor(sHeight / 2) + 2
  777.  
  778.     local colourConfigs = {
  779.         [settingsY + 0] = "borderColour",
  780.         [settingsY + 1] = "chartBGColour",
  781.         [settingsY + 2] = "inputChartColour",
  782.         [settingsY + 3] = "outputChartColour",
  783.         [settingsY + 4] = "batteryChargeColour",
  784.         [settingsY + 5] = "idleColour",
  785.         [settingsY + 6] = "sectionTitleColour",
  786.         [settingsY + 7] = "baseTextColour",
  787.     }
  788.     local numberConfigs = {
  789.         [settingsY + 9] = "screenRefreshRate",
  790.         [settingsY + 10] = "unitFormatCaps",
  791.     }
  792.  
  793.     while true do
  794.         m.clear()
  795.         drawSettings(settingsX, settingsY, sWidth, sHeight)
  796.         local event, side, x, y = os.pullEvent("monitor_touch")
  797.  
  798.         if x >= settingsX + 18 and x <= settingsX + 32 then
  799.             if colourConfigs[y] then
  800.                 -- colour dropdown menus
  801.                 local optionIndex = dropdownMenu(settingsX + 32, y, colorText)
  802.                 if optionIndex ~= nil then
  803.                     saveConfig(colourConfigs[y], colorDec[optionIndex])
  804.                 end
  805.             elseif y == settingsY + 9 then
  806.                 -- refresh rate
  807.                 local optionIndex = dropdownMenu(settingsX + 32, y, refreshRates.display)
  808.                 if optionIndex ~= nil then
  809.                     saveConfig("screenRefreshRate", refreshRates.values[optionIndex])
  810.                 end
  811.             elseif y == settingsY + 10 then
  812.                 -- unit style
  813.                 local optionIndex = dropdownMenu(settingsX + 32, y, unitStyle.display)
  814.                 if optionIndex ~= nil then
  815.                     saveConfig("unitFormatCaps", unitStyle.values[optionIndex])
  816.                 end
  817.             end
  818.         elseif x >= settingsX and x <= settingsX + 7 and y == settingsY + sHeight - 5 then
  819.             -- exit
  820.             return
  821.         elseif x >= settingsX + 10 and x <= settingsX + 16 and y == settingsY + sHeight - 5 then
  822.             -- reset
  823.             local defaults = {
  824.                 borderColour = 128,
  825.                 chartBGColour = 128,
  826.                 inputChartColour = 32,
  827.                 outputChartColour = 16384,
  828.                 batteryChargeColour = 8,
  829.                 idleColour = 16,
  830.                 sectionTitleColour = 1,
  831.                 baseTextColour = 1,
  832.                 screenRefreshRate = 0.1,
  833.                 unitFormatCaps = true
  834.             }
  835.  
  836.             for configName, value in pairs(defaults) do
  837.                 saveConfig(tostring(configName), value)
  838.             end
  839.         end
  840.     end
  841. end
  842.  
  843. local redrawRequired = false
  844. while true do
  845.     if redrawRequired then
  846.         m.clear()
  847.         drawStaticElements()
  848.         drawStatistics()
  849.         redrawRequired = false
  850.     end
  851.  
  852.     parallel.waitForAny(touch, display)
  853.     if y == 2 and x >= 87 and x <= 96 then
  854.         redrawRequired = true
  855.         m.clear()
  856.         settings()
  857.     end
  858. end
  859.  
Add Comment
Please, Sign In to add comment