Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Start Config--
- --Valid values are "RF", "J", and "EU".
- displayedEnergyType = "RF"
- batteryEnergyType = "RF"
- debug = 0
- updateDelay = 4
- normalizeAmount = 100
- modemChannel = 5695
- bgColor = colors.black
- txtColor = colors.red
- pgFromColor = colors.red
- pgToColor = colors.green
- pgTextColor = colors.black
- --End Config--
- function clear()
- if mon.isColor() == true then
- mon.setBackgroundColor(bgColor)
- end
- mon.clear()
- mon.setCursorPos(1,1)
- end
- function drawTextLeft(y, text, color_txt, color_bg)
- if mon.isColor() == true then
- mon.setTextColor(color_txt)
- mon.setBackgroundColor(color_bg)
- end
- mon.setCursorPos(2,y)
- mon.write(text)
- end
- function drawTextRight(y, text, color_txt, color_bg)
- if mon.isColor() == true then
- mon.setTextColor(color_txt)
- mon.setBackgroundColor(color_bg)
- end
- if monX - #text >= 1 then
- mon.setCursorPos(monX - #text, y)
- mon.write(text)
- else
- mon.setCursorPos(1, y)
- mon.write("Number is too big!")
- end
- end
- function drawTextCenter(y, text, color_txt, color_bg)
- if mon.isColor() == true then
- mon.setTextColor(color_txt)
- mon.setBackgroundColor(color_bg)
- end
- mon.setCursorPos(monX/2 - #text/2 + 1, y)
- mon.write(text)
- end
- function drawLine(x, y, length, size, color_bar)
- for yPos = y, y+size-1 do
- if mon.isColor() == true then
- mon.setBackgroundColor(color_bar)
- end
- mon.setCursorPos(x, yPos)
- mon.write(string.rep(" ", length))
- end
- end
- function drawProg(x, y, name, length, size, minVal, maxVal)
- drawLine(x, y, length, size, pgFromColor)
- local barSize = math.floor((minVal/maxVal)*length)
- drawLine(x, y, barSize, size, pgToColor)
- local text = name .." ".. math.floor((minVal/maxVal)*100) .."%"
- drawTextCenter(y+size/2, text, pgTextColor, pgToColor)
- end
- function split(str, delimiter)
- local result = {}
- local from = 1
- local d_from, d_to = string.find(str, delimiter, from)
- while d_from do
- table.insert(result, string.sub(str, from, d_from - 1))
- from = d_to + 1
- d_from, d_to = string.find(str, delimiter, from)
- end
- table.insert(result, string.sub(str, from))
- return result
- end
- function formatnum(number)
- local result = number
- while true do
- result, s = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
- if s == 0 then break
- end
- end
- return result
- end
- function convertEnergy(number)
- energyRatio = 1
- if batteryEnergyType == "J" then
- if displayedEnergyType == "RF" then energyRatio = 0.4 end
- if displayedEnergyType == "EU" then energyRatio = 0.1 end
- elseif batteryEnergyType == "RF" then
- if displayedEnergyType == "J" then energyRatio = 2.5 end
- if displayedEnergyType == "EU" then energyRatio = 0.25 end
- elseif batteryEnergyType == "EU" then
- if displayedEnergyType == "RF" then energyRatio = 4 end
- if displayedEnergyType == "J" then energyRatio = 10 end
- else error("Unsupported energy type: \""..batteryEnergyType.."\" for the battery.") end
- return string.format("%.2f",math.floor(tonumber(number) * energyRatio))
- end
- function getEnergyPerTick(energy)
- local input = {}
- if prevenergy[1] == nil then
- prevenergy[1] = energy
- end
- local limit = math.min(normalizeAmount, #prevenergy)
- input[1] = math.floor((energy - prevenergy[1]) / (updateDelay))
- for i=2,limit do
- input[i] = math.floor((prevenergy[i-1] - prevenergy[i]) / (updateDelay))
- end
- for i=0,limit-1 do
- local ii = 1+limit-i
- prevenergy[ii] = prevenergy[ii-1]
- end
- prevenergy[1] = energy
- calcenergy = input[1]
- for i=2,#input do
- calcenergy = calcenergy + input[i]
- end
- return math.floor(input[1]), math.floor((calcenergy) / limit)
- end
- function startup()
- modem = peripheral.find("modem")
- mon = peripheral.find("monitor")
- if mon == nil then
- mon = term
- end
- monX, monY = mon.getSize()
- prevDayEnergy = -1
- currentDayEnergy = -1
- ticksRun = 0
- prevenergy = {}
- running = true
- modem.open(modemChannel)
- print("Listening for transmission...")
- end
- function shutdown()
- modem.close(modemChannel)
- if term.isColor() == true then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.clear()
- term.setCursorPos(1,1)
- term.write("Terminated Energy Monitor.")
- term.setCursorPos(1,2)
- term.write("Come back soon!")
- term.setCursorPos(1,3)
- end
- function pullEvent()
- event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEventRaw()
- if event == "modem_message" then
- if message ~= nil then
- updateMonitor(split(message,";"))
- end
- elseif event == "terminate" then
- running = false
- end
- end
- function updateMonitor(tbl)
- if tbl ~= nil then
- if mon ~= term then
- term.clear()
- term.setCursorPos(1,1)
- print("Recieving info...")
- if debug == 1 then
- print("Debug:")
- for i=1,#prevenergy do
- print("prevenergy"..i.." = "..prevenergy[i])
- end
- end
- end
- local energy = 0
- local max = 0
- for x,y in pairs(tbl) do
- if x == 1 then energy = y
- elseif x == 2 then max = y
- end
- end
- if energy ~= 0 and prevDayEnergy == -1 then
- prevDayEnergy = energy
- currentDayEnergy = energy
- end
- ticksRun = ticksRun + updateDelay
- if (ticksRun >= 24000) then
- ticksRun = 0
- prevDayEnergy = currentDayEnergy
- currentDayEnergy = energy
- end
- local percent = math.floor((energy/max)*100)
- local dailyEnergy = currentDayEnergy - prevDayEnergy
- input, inputNormalized = getEnergyPerTick(energy)
- energy = convertEnergy(energy)
- print("energy" .. type(energy) .. energy)
- max = convertEnergy(max)
- print("max" .. type(max) .. max)
- input = convertEnergy(input)
- inputNormalized = convertEnergy(inputNormalized)
- dailyEnergy = convertEnergy(dailyEnergy)
- clear()
- drawTextLeft(2, "Current Power:", txtColor, bgColor)
- drawTextRight(2, formatnum(energy).." "..displayedEnergyType, txtColor, bgColor)
- drawTextLeft(4, "Max Power:", txtColor, bgColor)
- drawTextRight(4, formatnum(max).." "..displayedEnergyType, txtColor, bgColor)
- drawTextLeft(6,"Input/Output: ", txtColor, bgColor)
- drawTextRight(6, formatnum(input).." "..displayedEnergyType.."/t", txtColor, bgColor)
- drawTextLeft(8,"Input/Output (Normalized): ", txtColor, bgColor)
- drawTextRight(8, formatnum(inputNormalized).." "..displayedEnergyType.."/t", txtColor, bgColor)
- drawTextLeft(10, "Loss/Gain from Prev. Day:", txtColor, bgColor)
- drawTextRight(10, formatnum(dailyEnergy).." "..displayedEnergyType, txtColor, bgColor)
- drawProg(2, 12, "Stored: ", monX-2, 3, percent, 100, pgToColor, pgFromColor)
- end
- end
- modem, mon, monX, monY, prevenergy, running = nil
- startup()
- while running do
- pullEvent()
- end
- shutdown()
Advertisement
Add Comment
Please, Sign In to add comment