Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- frequency = 2 --How many seconds before refreshing the program
- refreshCycle = 5 --How many cycles before measuring energy variation
- cycleCounter = 5 --Initial value for cycle counter
- pressedButton = 1 --Term button that is pressed initially
- termHeaderColor = colors.blue --Term headers Color
- headerColor = colors.lightGray --Header Color
- frameColor = colors.blue --Frame Color
- bottomCellsColor = colors.magenta --Color for top cells
- topCellsColor = colors.lightBlue --Color for bottom cells
- autoModeColor = colors.yellow --Color for Auto-Mode buttons
- manualModeColor = colors.orange --Color for Manual-mode buttons
- stateOnColor = colors.lime --Color for "On" state
- stateOffColor = colors.red --Color for "Off" state
- emptyBarColor = colors.gray --Color for empty bar spaces
- pressedButtonColor = colors.lightGray --Color for pressed button on term
- energyGainColor = colors.lime --Color for positive energy variation
- energyLossColor = colors.red --Color for negative energy variation
- energySameColor = colors.lightBlue --Color for no energy variation
- energyBarGaps = {21,51,71,91,101} --Energy % under wich the bar will have a specific color (Default => 0-20%, 21-50%, 51-70%, 71-90%,91-100%
- energyBarColors = {colors.red,colors.orange,colors.pink,colors.yellow,colors.lime} --Color for each energy quantity in the bar
- colorCode = {colors.white,colors.pink,colors.yellow,colors.magenta,colors.orange,colors.gray,colors.lime,colors.lightBlue} --Output color for each cell
- energyLevel = {0,0,0,0,0,0,0,0} --Default values for energy levels
- cellEnergy = {0,0,0,0,0,0,0,0} --Default values for energy levels
- curState = {0,0,0,0,0,0,0,0} --Default values for energy levels
- buttonStates = {0,0,0,0,0,0,0,0} --Default values for energy levels
- energyVariation = {0,0,0,0,0,0,0,0}
- legacyEnergy1 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy2 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy3 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy4 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy5 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy6 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy7 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- legacyEnergy8 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- cell0Periph = peripheral.wrap("redstone_energy_cell_0")
- cell1Periph = peripheral.wrap("redstone_energy_cell_1")
- cell2Periph = peripheral.wrap("redstone_energy_cell_2")
- cell3Periph = peripheral.wrap("redstone_energy_cell_3")
- cell4Periph = peripheral.wrap("redstone_energy_cell_4")
- cell5Periph = peripheral.wrap("redstone_energy_cell_5")
- cell6Periph = peripheral.wrap("redstone_energy_cell_6")
- cell7Periph = peripheral.wrap("redstone_energy_cell_7")
- function chooseBarColor(energy)
- for i=1,5 do
- if energy < energyBarGaps[i] then
- return energyBarColors[i]
- end
- end
- end
- function getProviders()
- cell0 = cell0Periph.getPowerProvider()
- cell1 = cell1Periph.getPowerProvider()
- cell2 = cell2Periph.getPowerProvider()
- cell3 = cell3Periph.getPowerProvider()
- cell4 = cell4Periph.getPowerProvider()
- cell5 = cell5Periph.getPowerProvider()
- cell6 = cell6Periph.getPowerProvider()
- cell7 = cell7Periph.getPowerProvider()
- end
- function drawTermTitles() --Writes headers on terminal
- term.setBackgroundColor(termHeaderColor)
- term.setCursorPos(1,1)
- term.write(" Energy Cell Charge"..string.rep(" ",32))
- term.setCursorPos(1,10)
- term.write(" Energy Variation")
- refreshTime = frequency * refreshCycle
- str = ("(For every "..refreshTime.." seconds) ")
- strCount = string.len(str)
- emptySpaces = 34-strCount
- term.write(string.rep(" ",emptySpaces))
- term.write(str)
- term.setBackgroundColor(colors.black)
- end
- function writeBarGraph(cellNum, energyValue, state)
- term.setTextColor(colorCode[cellNum])
- term.write(" Cell "..cellNum..": [")
- w,h = term.getCursorPos()
- term.setBackgroundColor(emptyBarColor)
- term.write(string.rep(" ",25))
- term.setBackgroundColor(colors.black)
- term.write("] ")
- if state == 0 then
- term.setTextColor(stateOffColor)
- term.write("Off")
- else
- term.setTextColor(stateOnColor)
- term.write("On ")
- end
- term.setTextColor(colorCode[cellNum])
- term.write(" "..energyLevel[cellNum].."%")
- term.setTextColor(colors.white)
- term.setCursorPos(w,h)
- color = chooseBarColor(energyValue)
- term.setBackgroundColor(color)
- term.write(string.rep(" ",energyValue/4))
- term.setBackgroundColor(colors.black)
- end
- function writeStatus()
- for x=1,8 do
- term.setCursorPos(1,x+1)
- writeBarGraph(x, energyLevel[x], curState[x])
- end
- end
- function writeEnergyVariation() --Write energy variation for each cell (on terminal)
- for n=1,8 do
- if pressedButton == n then
- term.setBackgroundColor(pressedButtonColor)
- end
- term.setTextColor(colorCode[n])
- if n > 4 then
- y = 12
- else
- y = 11
- end
- xpos = n
- if n > 4 then
- xpos = xpos - 4
- end
- x = (xpos*10+3*xpos)-12
- term.setCursorPos(x,y)
- term.write("[C"..n..":")
- value = energyVariation[n]
- if value > 0 then --Decides color and signal
- term.setTextColor(energyGainColor)
- signal = "+"
- writeValue = value
- else
- if value < 0 then
- term.setTextColor(energyLossColor)
- signal = "-"
- writeValue = value * (-1)
- else
- term.setTextColor(energySameColor)
- signal = " "
- writeValue = value
- end
- end
- if writeValue > 999 then --Converts to GJ if needed
- writeValue = math.ceil(writeValue/1000)
- size = "GJ"
- else
- size = "MJ"
- end
- valueString = tostring(signal..writeValue..size)
- chars = string.len(valueString)
- spaces = 7 - chars
- for i=1,spaces do
- term.write(" ") --Keeps format for smaller numbers
- end
- term.write(valueString)
- term.setTextColor(colorCode[n])
- term.write("]")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- end
- function drawChartGrid() --Writes chart grid
- term.setBackgroundColor(colorCode[pressedButton]) --Use color of current power line
- term.setTextColor(colors.black)
- for o=1,7 do
- term.setCursorPos(5,12+o)
- term.write("#")
- end
- term.setCursorPos(1,18)
- term.write(string.rep("#",51))
- for p=1,3 do
- term.setCursorPos(5,p*2+11)
- term.write("_")
- end
- for q=1,10 do
- term.setCursorPos(q*5+1,18)
- term.write("|")
- end
- term.setBackgroundColor(colors.black)
- end
- function writeChartTitles() --Write guide numbers on chart
- term.setTextColor(colorCode[pressedButton])
- for r=1,3 do
- term.setCursorPos(1,r*2+11)
- quant = 240*(3-r)
- if quant < 100 then
- if quant < 10 then
- term.write (" ")
- else
- term.write(" ")
- end
- end
- term.write(quant.."G")
- end
- term.setCursorPos(51,19)
- term.write("0")
- for g=1,9 do
- secs = tostring(g*50)
- xpos = (51-5*g)
- term.setCursorPos(xpos,19)
- term.write(secs)
- end
- term.setCursorPos(1,19)
- term.write("SxJ")
- term.setTextColor(colors.white)
- end
- function drawChartData() --Draws data on chart
- if pressedButton == 1 then
- legacyEnergy = legacyEnergy1
- else
- if pressedButton == 2 then
- legacyEnergy = legacyEnergy2
- else
- if pressedButton == 3 then
- legacyEnergy = legacyEnergy3
- else
- if pressedButton == 4 then
- legacyEnergy = legacyEnergy4
- else
- if pressedButton == 5 then
- legacyEnergy = legacyEnergy5
- else
- if pressedButton == 6 then
- legacyEnergy = legacyEnergy6
- else
- if pressedButton == 7 then
- legacyEnergy = legacyEnergy7
- else
- legacyEnergy = legacyEnergy8
- end
- end
- end
- end
- end
- end
- end
- for a=1,46 do
- xpos = 52-a
- val = legacyEnergy[a]
- if val > 0 then
- posVal = math.ceil(val/120000)
- else
- posVal = 1
- end
- ypos = 18-posVal
- if legacyEnergy[a] > legacyEnergy[a+1] then
- term.setBackgroundColor(energyGainColor)
- else
- if legacyEnergy[a] < legacyEnergy[a+1] then
- term.setBackgroundColor(energyLossColor)
- else
- term.setBackgroundColor(energySameColor)
- end
- end
- term.setCursorPos(xpos,ypos)
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- end
- function drawChart() --Merges function related to legacy energy chart
- drawChartGrid()
- writeChartTitles()
- drawChartData()
- end
- function drawTerminalScreen() --Merges functions relatet to the terminal screen
- term.clear()
- drawTermTitles()
- writeStatus()
- writeEnergyVariation()
- drawChart()
- end
- function setOutput()
- curColors = 0
- for v=1,8 do
- if buttonStates[v] == 0 then
- if energyLevel[v] < 50 then
- curState[v] = 1
- else
- if energyLevel[v] > 90 then
- curState[v] = 0
- end
- end
- end
- end
- curColors = curState[1] * colors.white + curState[2] * colors.pink + curState[3] * colors.yellow + curState[4] * colors.magenta + curState[5] * colors.orange + curState[6] * colors.gray + curState[7] * colors.lime + curState[8] * colors.lightBlue
- rs.setBundledOutput("right", curColors)
- end
- monitor = peripheral.wrap("monitor_0")
- function getEnergy() --Gets % of energy on each cell
- energyLevel[1] = math.floor((cell0.energyStored/cell0.maxEnergyStored)*100)
- energyLevel[2] = math.floor((cell1.energyStored/cell1.maxEnergyStored)*100)
- energyLevel[3] = math.floor((cell2.energyStored/cell2.maxEnergyStored)*100)
- energyLevel[4] = math.floor((cell3.energyStored/cell3.maxEnergyStored)*100)
- energyLevel[5] = math.floor((cell4.energyStored/cell4.maxEnergyStored)*100)
- energyLevel[6] = math.floor((cell5.energyStored/cell5.maxEnergyStored)*100)
- energyLevel[7] = math.floor((cell6.energyStored/cell6.maxEnergyStored)*100)
- energyLevel[8] = math.floor((cell7.energyStored/cell7.maxEnergyStored)*100)
- end
- function getCellEnergy() --Gets amount of energy stored in each cell
- cellEnergy[1] = cell0.energyStored
- cellEnergy[2] = cell1.energyStored
- cellEnergy[3] = cell2.energyStored
- cellEnergy[4] = cell3.energyStored
- cellEnergy[5] = cell4.energyStored
- cellEnergy[6] = cell5.energyStored
- cellEnergy[7] = cell6.energyStored
- cellEnergy[8] = cell7.energyStored
- end
- function refreshLegacyEnergy() --Updates energy history
- table.insert(legacyEnergy1,1,cellEnergy[1])
- table.remove(legacyEnergy1,48)
- table.insert(legacyEnergy2,1,cellEnergy[2])
- table.remove(legacyEnergy2,48)
- table.insert(legacyEnergy3,1,cellEnergy[3])
- table.remove(legacyEnergy3,48)
- table.insert(legacyEnergy4,1,cellEnergy[4])
- table.remove(legacyEnergy4,48)
- table.insert(legacyEnergy5,1,cellEnergy[5])
- table.remove(legacyEnergy5,48)
- table.insert(legacyEnergy6,1,cellEnergy[6])
- table.remove(legacyEnergy6,48)
- table.insert(legacyEnergy7,1,cellEnergy[7])
- table.remove(legacyEnergy7,48)
- table.insert(legacyEnergy8,1,cellEnergy[8])
- table.remove(legacyEnergy8,48)
- end
- function getEnergyVariation() --Calculates energy variation
- energyVariation[1] = legacyEnergy1[1] - legacyEnergy1[2]
- energyVariation[2] = legacyEnergy2[1] - legacyEnergy2[2]
- energyVariation[3] = legacyEnergy3[1] - legacyEnergy3[2]
- energyVariation[4] = legacyEnergy4[1] - legacyEnergy4[2]
- energyVariation[5] = legacyEnergy5[1] - legacyEnergy5[2]
- energyVariation[6] = legacyEnergy6[1] - legacyEnergy6[2]
- energyVariation[7] = legacyEnergy7[1] - legacyEnergy7[2]
- energyVariation[8] = legacyEnergy8[1] - legacyEnergy8[2]
- end
- function calculateEnergy() --Merges functions related with energy calculation
- getEnergy()
- if cycleCounter == refreshCycle then --Refresh legacy if enough cycles have passed
- getCellEnergy()
- refreshLegacyEnergy()
- getEnergyVariation()
- end
- end
- function drawGrid() --Draws Grid
- monitor.setBackgroundColor(headerColor)
- monitor.setTextColor(frameColor)
- monitor.setCursorPos(1,1)
- monitor.write(string.rep("#",19).."Energy Charge"..string.rep("#",18)) --Draws Top Row
- monitor.setBackgroundColor(colors.black)
- for i=1,18 do --Draw 2-18th rows
- monitor.setCursorPos(1,i+1)
- monitor.write("#"..string.rep(" ",48).."#")
- end
- monitor.setCursorPos(1,19)
- monitor.write(string.rep("#",50)) --Draw 19th row
- monitor.setTextColor(colors.white)
- end
- function drawColorGuide() --Draw the guide for color-codes
- monitor.setCursorPos(4,19)
- monitor.setTextColor(bottomCellsColor)
- monitor.write("#")
- monitor.setTextColor(colors.white)
- monitor.write("=Bottom Cell ")
- monitor.setTextColor(topCellsColor)
- monitor.write("#")
- monitor.setTextColor(colors.white)
- monitor.write("=Top Cell")
- monitor.setCursorPos(32,19)
- monitor.setBackgroundColor(autoModeColor)
- monitor.write(" ")
- monitor.setBackgroundColor(colors.black)
- monitor.write("=Auto ")
- monitor.setBackgroundColor(manualModeColor)
- monitor.write(" ")
- monitor.setBackgroundColor(colors.black)
- monitor.write("=Manual")
- end
- function drawButtons() --Draws override buttons and
- for m=1,8 do
- monitor.setCursorPos(36,m*2+1)
- if buttonStates[m] == 0 then --Sees if button is Auto or Manual mode
- monitor.setBackgroundColor(autoModeColor)
- else
- monitor.setBackgroundColor(manualModeColor)
- end
- if curState[m] == 0 then --Sees if on or off
- monitor.setTextColor(stateOffColor)
- monitor.write(" Off ")
- else
- monitor.setTextColor(stateOnColor)
- monitor.write(" On ")
- end
- monitor.setTextColor(colors.white) --Resets colors
- monitor.setBackgroundColor(colors.black)
- end
- end
- function drawEnergyGrid() --Draws base grid for bars
- monitor.setBackgroundColor(emptyBarColor)
- for q=1,8 do
- monitor.setCursorPos(9,q*2+1)
- monitor.write(string.rep(" ",20))
- end
- monitor.setBackgroundColor(colors.black)
- end
- function drawEnergyTitles() --Draws titles for energy graph
- monitor.setTextColor(bottomCellsColor)
- for o=1,4 do
- monitor.setCursorPos(2,2*o+1)
- monitor.write("Cell "..o)
- end
- monitor.setTextColor(topCellsColor)
- for p=1,4 do
- monitor.setCursorPos(2,2*p+9)
- monitor.write("Cell "..p+4)
- end
- monitor.setTextColor(colors.white) --Resets text color
- end
- function writeEnergy() --Writes energy % for each cell
- for u=1,8 do
- monitor.setTextColor(colorCode[u]) --Color each level for corresponding output
- monitor.setCursorPos(31,2*u+1)
- monitor.write(energyLevel[u].."%")
- end
- monitor.setTextColor(colors.white) --Resets text color
- end
- function drawEnergyVariation()
- for b=1,8 do
- monitor.setCursorPos(42,b*2+1)
- value = energyVariation[b]
- if value > 0 then --Decides color and signal
- monitor.setTextColor(energyGainColor)
- signal = "+"
- writeValue = value
- else
- if value < 0 then
- monitor.setTextColor(energyLossColor)
- signal = "-"
- writeValue = value * (-1)
- else
- monitor.setTextColor(energySameColor)
- signal = " "
- writeValue = value
- end
- end
- if writeValue > 999 then --Converts to GJ if needed
- writeValue = math.ceil(writeValue/1000)
- size = "GJ"
- else
- size = "MJ"
- end
- valueString = tostring(signal..writeValue..size)
- chars = string.len(valueString)
- spaces = 7 - chars
- for i=1,spaces do
- monitor.write(" ") --Keeps format for smaller numbers
- end
- monitor.write(valueString)
- end
- end
- function drawEnergyBar() --Draw energy bars
- for y=1,8 do
- monitor.setCursorPos(9,2*y+1)
- h = energyLevel[y]
- color = chooseBarColor(h)
- monitor.setBackgroundColor(color)
- g = h/5 --How many spaces need to be filled
- monitor.write(string.rep(" ",g)) --Draw bar
- end
- monitor.setBackgroundColor(colors.black) --Resets background color
- end
- function drawEnergyGraph() --Merge functions related to the energy graph
- drawEnergyGrid()
- drawEnergyTitles()
- writeEnergy()
- drawEnergyBar()
- end
- function drawMonitor() --Merges monitor-related functions
- monitor.clear()
- drawGrid()
- drawColorGuide()
- drawButtons()
- drawEnergyGraph()
- drawEnergyVariation()
- end
- function buttonPress(x,y) --Detects wich button was pressed
- if x > 35 and x < 41 then
- y = (y-1)/2
- if y > 0 and y < 9 then
- overrideButton(y)
- os.queueEvent("timer") --Forces refresh
- end
- end
- end
- function overrideButton(button) --Apply button press
- f = button
- if buttonStates[f] == 0 then
- curState[f] = 1
- buttonStates[f] = 1
- else
- if buttonStates[f] == 1 then
- curState[f] = 0
- buttonStates[f] = 2
- else
- buttonStates[f] = 0
- end
- end
- end
- function termClick(x,y)
- if y == 11 or y == 12 then
- row = y-10
- if x < 13 then
- col = 1
- else
- if x > 13 and x < 26 then
- col = 2
- else
- if x > 26 and x < 39 then
- col = 3
- else
- if x > 39 then
- col = 4
- end
- end
- end
- end
- button = (row-1)*4+col
- pressedButton = button
- os.queueEvent("timer") --Forces refresh
- end
- end
- function countCycle()
- end
- while true do
- getProviders()
- calculateEnergy()
- drawTerminalScreen()
- drawMonitor()
- setOutput()
- term.setCursorPos(7,15) --Debug lines
- term.write(cycleCounter)
- term.setCursorPos(15,15)
- term.write(refreshCycle)
- term.setCursorPos(23,15)
- term.write(frequency)
- os.startTimer(frequency-0.4)
- repeat
- event, p1, clickx, clicky = os.pullEvent()
- if event == "monitor_touch" then --Detects monitor touch
- buttonPress(clickx,clicky)
- else
- if event == "mouse_click" and p1 == 1 then --Detects term click
- termClick(clickx,clicky)
- end
- end
- until event == "timer" --Wait until refreshing
- if cycleCounter == refreshCycle then
- cycleCounter = 0 --Resets counter if max cycle is reached
- else
- cycleCounter = cycleCounter + 1 --Otherwise increment it by 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment