Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- energy monitor v2
- p = peripheral.wrap("right") -- wrap Energy cell
- m = peripheral.wrap("top") -- wrap monitor
- m.setTextColor(colors.green) -- set colors
- m.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- function colorsopt(backcol,barcol)
- colorback = backcol
- colorbar = barcol
- end
- function chooseColors()
- coloragain = false
- print("Choose your colors:")
- print("1)")
- term.setTextColor(colors.black)
- print("backgound")
- term.setTextColor(colors.red)
- print("bar")
- print("2)")
- term.setTextColor(colors.red)
- print("backgound")
- term.setTextColor(colors.green)
- print("bar")
- event,key = os.pullEvent("key")
- if key == 2 then
- colorsopt(colors.black,colors.red)
- elseif key == 3 then
- colorsopt(colors.red,colors.green)
- else
- print("Invalid number. Please try again.")
- sleep(0.5)
- term.clear()
- term.setCursorPos(1,1)
- coloragain = true
- end
- end
- function getInfo()
- me = p.getMaxEnergyStored() -- get energy levels
- e = p.getEnergyStored() -- ..
- end
- function calculate()
- perc = e / me -- e.g. 1000/4000 returns 0.25
- perc = perc * 100 -- needed for calculation
- end
- function getMonitor()
- x,y = m.getSize() -- get size
- if vorh == "horizontal" then
- Xvalue = x / 100 -- get multiplier
- else
- Xvalue = y / 100
- end
- end
- function display()
- if bar then
- displayBar()
- else
- displayRound(true)
- for q = 1,20 do
- displayRound(false)
- end
- end
- end
- function displayRound(full)
- x = 1 + math.floor(0.5 * x)
- y = 1 + math.floor(0.5 * y)
- r = math.min(x,y) - 1
- if full then
- percfinal = 3.15
- colorb = colors.red
- else
- percfinal = perc * 3.15
- colorb = colors.green
- end
- for i = 0,percfinal,0.01 do
- py = r - (math.sin(i) * r)
- px = r - (math.cos(i) * r)
- m.setBackgroundColor(colorb)
- m.setCursorPos((px + (0.4 * x) - 0.5),(py + y - 1))
- m.write(" ")
- sleep(0.1)
- m.setBackgroundColor(colors.black)
- m.setCursorPos(1,r-1)
- perc = math.ceil(perc * 100)
- m.write("The cell is "..perc.." % full.")
- end
- end
- function displayBar()
- monperc = perc * Xvalue -- amount of pixels to fill
- fillspace = math.ceil(monperc) -- get number, e.g. not 1,5
- if vorh == "vertical" then
- fillspace = fillspace - 1
- fillspace = y - fillspace
- end
- m.setBackgroundColor(colorback)
- -- sleep(0.1) -- remove "--" if experiencing lagg
- m.clear() -- clear monitor
- m.setBackgroundColor(colorbar)
- if vorh == "horizontal" then
- for i = 1,fillspace do
- for l = 1,y do
- m.setCursorPos(i,l)
- m.write(" ")
- end
- end
- else
- for i = fillspace,y do
- for l = 1,x do
- m.setCursorPos(l,i)
- m.write(" ")
- end
- end
- end
- end
- function chooseForm()
- while formagain do
- formagain = false
- print("Choose your form: (type the number)")
- print("1) Round")
- print("2) Bar")
- event,key = os.pullEvent("key")
- if key == 2 then
- bar = false
- elseif key == 3 then
- bar = true
- else
- print("Invalid number. Please try again.")
- sleep(0.5)
- term.clear()
- term.setCursorPos(1,1)
- formagain = true
- end
- end
- end
- formagain = true
- chooseForm()
- if bar then
- print("Do you want the monitor vertical or horizontal?")
- vorh = read()
- chooseColors()
- end
- while true do --loop
- getInfo()
- sleep(0.1) -- short interval due to lagg issues
- if bar then
- calculate()
- sleep(0.1)
- end
- getMonitor()
- sleep(0.1)
- display()
- sleep(0.3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment