Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screenSide = "front"
- local modemSide = "back"
- local capacitorSide = "left"
- local capLabel = "Capacitor"
- local JoulesToRF = 0.4 --this value is the multiplicator for Energy readouts
- local IECNET = true
- -- end of config
- local args = {...}
- --IECNET setup
- local gpsEnabled = false
- local net = "Energynet"
- local host = 0
- function iecnetSetup()
- if (IECNET) then
- if (peripheral.isPresent(modemSide)) then
- if (peripheral.getType(modemSide) == "modem") then
- --peripheral is present, starting IECNET
- rednet.open(modemSide)
- host = rednet.lookup(net,"IEC-Host")
- if (host == nil) then
- print(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
- IECNET = false
- end
- x,y,z = gps.locate()
- if (x == nil) then
- print("GPS not available, will not send Location Data")
- else
- gpsEnabled = true;
- end
- rednet.broadcast("Online",net)
- else
- print("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- else
- print("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- end
- end
- function getLocation()
- x,y,z = gps.locate()
- if (x == nil) then
- gpsEnabled = false
- return nil
- else
- if (not gpsEnabled) then
- gpsEnabled = true
- end
- x = math.floor(x+0.5)
- y = math.floor(y+0.5)
- z = math.floor(z+0.5)
- return tostring(x).." "..tostring(y).." "..tostring(z)
- end
- end
- --end of IECNET primary functions
- hasScreen = false
- mon = nil
- bank = nil
- function peripheralSetup()
- if (peripheral.isPresent(screenSide)) then
- if (peripheral.getType(screenSide) == "monitor") then
- print("Screen found on side: \""..screenSide.."\" running with screen.")
- mon = peripheral.wrap(screenSide)
- x,y = mon.getSize()
- if (x < 16 or y < 12) then
- print("Screen does not meet the minimum size: [16 : 12], current size: ["..x.." : "..y.."] ,running screenless")
- hasScreen = false
- else
- hasScreen = true
- end
- else
- print("Peripheral is present on side: \""..screenSide.."\" but this is not a monitor.")
- end
- else
- print("No screen was found, running screenless.")
- end
- if (peripheral.isPresent(capacitorSide)) then
- if (peripheral.getType(capacitorSide) == "Induction Matrix") then
- print("Capacitor found on side: \""..capacitorSide.."\"!")
- bank = peripheral.wrap(capacitorSide)
- else
- error("Peripheral is present on side: \""..capacitorSide.."\" but this is not a valid capacitor.")
- end
- else
- error("No capacitor was found on the specified side, exiting.")
- end
- end
- function nextLine()
- x,y = mon.getCursorPos()
- mon.setCursorPos(1,y+1)
- end
- function divider()
- x,y = mon.getSize()
- mon.write("+")
- for i=1,(x-2),1 do
- mon.write("-")
- end
- mon.write("+")
- end
- function screenHead(label)
- x,y = mon.getSize()
- mon.write("+-["..label.."]")
- length = 4+ string.len(label)+1
- for i=1,(x-length),1 do
- mon.write("-")
- end
- mon.write("+")
- nextLine()
- end
- function head()
- mon.setCursorPos(1,1)
- x,y = mon.getSize()
- mon.write("+")
- for i=1,(x-13)/2,1 do
- mon.write("-")
- end
- mon.write("[")
- mon.setTextColor(colors.yellow)
- mon.write("IEC-OS.aic")
- mon.setTextColor(colors.white)
- mon.write("]")
- for i=1,(x-13)/2,1 do
- mon.write("-")
- end
- mon.write("+")
- end
- function foot()
- x,y = mon.getSize()
- mon.setCursorPos(1,y)
- mon.write("+")
- for i=1,x-(12),1 do
- mon.write("-")
- end
- mon.write("[")
- if (IECNET) then
- mon.setTextColor(colors.green)
- mon.write("Online")
- mon.setTextColor(colors.white)
- mon.write("]--+")
- else
- mon.setTextColor(colors.red)
- mon.write("Offline")
- mon.setTextColor(colors.white)
- mon.write("]-+")
- end
- end
- function functionals()
- mon.clear()
- head()
- foot()
- mon.setCursorPos(1,2)
- end
- function round(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function getPercentFilled()
- return math.floor((((bank.getEnergy()*JoulesToRF)/(bank.getMaxEnergy()*JoulesToRF))*100)*10)/10
- end
- function printPercentFilled(startLine)
- percent = getPercentFilled()
- mon.setCursorPos(4,startLine)
- mon.write("Filled to: ")
- percent = getPercentFilled()
- if (percent > 30) then
- mon.setTextColor(colors.green)
- elseif (percent > 15) then
- mon.setTextColor(colors.yellow)
- elseif (percent > 7) then
- mon.setTextColor(colors.orange)
- else
- mon.setTextColor(colors.red)
- end
- mon.setCursorPos(4,startLine+1)
- mon.write(percent)
- mon.setTextColor(colors.white)
- mon.write("%")
- mon.setCursorPos(4,startLine+2)
- end
- function getBankIO()
- return math.floor((bank.getInput()*JoulesToRF - bank.getOutput()*JoulesToRF)*100)/100
- end
- function getBankIOWithUnit(bankIO)
- unit = "RF/t"
- inverted = false
- if (bankIO < 0) then
- bankIO = bankIO*-1
- inverted = true
- end
- if ((bankIO/1000) > 1) then
- unit = "kRF/t"
- bankIO = round(bankIO/1000,3)
- if ((bankIO/1000) > 1) then
- unit = "MRF/t"
- bankIO = round(bankIO/1000,3)
- if ((bankIO/1000) > 1) then
- unit = "GRF/t"
- bankIO = round(bankIO/1000,2)
- if ((bankIO/1000) > 1) then
- unit = "TRF/t"
- bankIO = round(bankIO/1000,2)
- if ((bankIO/1000) > 1) then
- unit = "PRF/t"
- bankIO = round(bankIO/1000,2)
- end
- end
- end
- end
- end
- if (inverted) then
- bankIO = bankIO*-1
- end
- return bankIO,unit
- end
- function printIO(startLine)
- bankIO = getBankIO()
- mon.setCursorPos(4,startLine)
- mon.write("I/O:")
- if (bankIO > 0) then
- mon.setTextColor(colors.green)
- elseif (bankIO < 0) then
- mon.setTextColor(colors.red)
- else
- mon.setTextColor(colors.blue)
- end
- mon.setCursorPos(4,startLine+1)
- bankIO,unit = getBankIOWithUnit(bankIO)
- mon.write(bankIO)
- mon.setTextColor(colors.white)
- mon.write(" "..unit)
- mon.setCursorPos(4,startLine+3)
- end
- function getETA()
- bankIO = getBankIO()
- sign = ""
- eta = 0
- if (bankIO > 0) then
- sign = "+"
- eta = math.floor((round(((bank.getMaxEnergy()*JoulesToRF) - (bank.getEnergy()*JoulesToRF)) / bankIO)/20)*10)/10 --time in seconds untill bank is filled up
- elseif (bankIO < 0) then
- sign = "-"
- bankIO = bankIO*-1
- eta = math.floor(((round(bank.getEnergy()*JoulesToRF / bankIO)/20)*10)/10) --time in seconds untill bank is empty
- else
- sign = "="
- end
- days = 0
- hours = 0
- minutes = 0
- seconds = eta
- if (seconds > 60) then
- minutes = math.floor(seconds/60)
- seconds = math.floor(seconds % 60)
- end
- if (minutes > 60) then
- hours = math.floor(minutes/60)
- minutes = minutes % 60
- end
- if (hours > 24) then
- days = math.floor(hours/24)
- hours = hours % 24
- end
- s = days.."d"..hours.."h"..minutes.."m"..seconds.."s"
- if (days <= 0) then
- s = hours.."h"..minutes.."m"..seconds.."s"
- if (hours <= 0) then
- s = minutes.."m"..seconds.."s"
- if (minutes <= 0) then
- s = seconds.."s"
- end
- end
- end
- return sign,s
- end
- function printETA(startLine)
- sign,eta = getETA()
- mon.setCursorPos(4,startLine)
- if (sign == "+") then
- mon.write("Full in:")
- mon.setTextColor(colors.white)
- elseif (sign == "-") then
- mon.write("Empty in:")
- mon.setTextColor(colors.red)
- else
- mon.write("Full in:")
- mon.setCursorPos(4,startLine+1)
- if( getPercentFilled() == 100) then
- eta = "Overfilling!"
- else
- eta = "No I/O"
- return
- end
- end
- mon.setCursorPos(4,startLine+1)
- mon.write(eta)
- mon.setTextColor(colors.white)
- mon.setCursorPos(4,startLine+2)
- end
- function printMainBar(startLine)
- x = 2
- mon.setCursorPos(x,startLine)
- filledPercent = getPercentFilled()
- filledPercent = math.ceil(filledPercent/10)
- for i=1,10-filledPercent,1 do
- x,y = mon.getCursorPos()
- mon.write("|")
- mon.setCursorPos(x,y+1)
- end
- for i=1,filledPercent,1 do
- x,y = mon.getCursorPos()
- mon.write("#")
- mon.setCursorPos(x,y+1)
- end
- end
- function readouts(startLine)
- mon.setCursorPos(4,startLine)
- printPercentFilled(startLine)
- nextLine()
- x,y = mon.getCursorPos()
- printIO(y)
- nextLine()
- x,y = mon.getCursorPos()
- printETA(y)
- printMainBar(startLine)
- end
- --rednet handeling
- local ticksSinceLastPing = 0
- function rednetHandeling(sender,message,prot)
- if (message == "pong" and prot == net) then --online checker
- ticksSinceLastPing = 0
- return
- end
- end
- function routine()
- if (hasScreen) then
- functionals()
- readouts(2)
- end
- if (ticksSinceLastPing > 5) then
- IECNET = false
- else
- IECNET = true
- end
- if (IECNET) then
- message = {}
- message["online"] = true
- message["label"] = capLabel
- sign, eta = getETA()
- message["eta"] = eta
- message["sign"] = sign
- bankIO,unit = getBankIOWithUnit(getBankIO())
- message["bankIO"] = bankIO
- message["unit"] = unit
- message["fillstat"] = getPercentFilled()
- rednet.broadcast(message,net)
- end
- rednet.broadcast("ping",net) --ping the network to see if it is online
- end
- iecnetSetup()
- peripheralSetup()
- local timer = os.startTimer(1)
- while true do
- event,a1,a2,a3 = os.pullEvent()
- if (event == "timer" and a1 == timer) then
- ticksSinceLastPing = ticksSinceLastPing +1
- routine()
- timer = os.startTimer(1)
- elseif (event == "rednet_message") then
- rednetHandeling(a1,a2,a3)
- end
- end
Add Comment
Please, Sign In to add comment