Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.wrap("monitor_1")
- local sender, message
- rednet.open("right")
- monitor.setTextScale(1)--0.75)
- sizeX, sizeY = monitor.getSize()
- function tablelength(T)
- local count = 0
- for i in pairs(T) do count = count + 1 end
- return count
- end
- function rearrange(data)
- local temp = {}
- temp[1] = data[6]
- temp[2] = data[5]
- temp[3] = data[4]
- temp[4] = data[3]
- temp[5] = data[2]
- temp[6] = data[1]
- temp[7] = data[10]
- temp[8] = data[9]
- temp[9] = data[8]
- temp[10] = data[7]
- return temp
- end
- function getData(data, value, index)
- if not index then
- local temp = 0
- for i, j in pairs(data) do
- temp = temp + (j[value] or 0)
- end
- return temp
- end
- return data[index][value]
- end
- function getOutput(data, m, n, val)
- local temp = 0
- for i = m, n do
- if getData(data, "stored", i) > val then temp = temp + val end
- end
- return temp
- end
- function drawLine(x1, x2, y, color)
- preX, preY = monitor.getCursorPos()
- monitor.setCursorPos(x1, y)
- monitor.setBackgroundColor(color)
- monitor.write(" ")
- if x2 > x1 then monitor.write(string.rep(" ", x2 - x1)) end
- monitor.setCursorPos(preX, preY)
- monitor.setBackgroundColor(colors.black)
- end
- function getColor(state)
- if state > .75 then return colors.green end
- if state > .60 then return colors.lime end
- if state > .30 then return colors.yellow end
- if state > .15 then return colors.orange end
- return colors.red
- end
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function drawBar(minx, miny, maxx, maxy, state, color, frame)
- color = color or getColor(state)
- frame = frame or colors.gray
- for i = miny, maxy do
- drawLine(minx, maxx, i, frame)
- end
- miny = miny + 1
- maxy = maxy - 1
- minx = minx + 1
- maxx = maxx - 1
- local l = miny + math.floor((1.0 - state) * (maxy - miny))
- if l > miny then
- for i = miny, l - 1 do
- drawLine(minx, maxx, i, colors.black)
- end
- end
- if state == 0 then return nil end
- for y = maxy, l, -1 do
- drawLine(minx, maxx, y, color)
- end
- end
- function form(rf)
- local suf = " RF"
- if rf > 1000 then
- rf = round(rf / 1000, 2)
- suf = " kRF"
- end
- if rf > 1000 then
- rf = round(rf / 1000, 2)
- suf = " mRF"
- end
- return tostring(rf) .. suf
- end
- local data = 100
- local i = -5
- monitor.clear()
- monitor.setCursorPos(2, sizeY - 3)
- monitor.write("Storage")
- monitor.setCursorPos(2, sizeY - 6)
- monitor.write("Output")
- monitor.setCursorPos(2, sizeY - 9)
- monitor.write("Capacity")
- while true do
- repeat
- repeat
- sender, message = rednet.receive("nOBatteryStatus", 5)
- until message ~= nil
- data = textutils.unserialize(message)
- until tablelength(data) == 10
- data = rearrange(data)
- local stored = getData(data, "stored")
- local maximum = getData(data, "max")
- local out = getOutput(data, 1, 5, 2000)
- drawBar(sizeX / 2 + 1, 2, sizeX - 1, sizeY - 1, stored / maximum)
- monitor.setCursorPos(1, sizeY - 1)
- monitor.write(string.rep(" ", math.floor(sizeX / 2)))
- monitor.setCursorPos(1, sizeY - 2)
- monitor.write(string.rep(" ", math.floor(sizeX / 2)))
- monitor.setCursorPos(1, sizeY - 5)
- monitor.write(string.rep(" ", math.floor(sizeX / 2)))
- monitor.setCursorPos(1, sizeY - 8)
- monitor.write(string.rep(" ", math.floor(sizeX / 2)))
- monitor.setCursorPos(2, sizeY - 1)
- monitor.write("("..form(stored)..")")
- monitor.setCursorPos(2, sizeY - 2)
- monitor.write(tostring(round(stored / maximum * 100, 2)).." %")
- monitor.setCursorPos(2, sizeY - 5)
- monitor.write(tostring(out).." RF/t")
- monitor.setCursorPos(2, sizeY - 8)
- monitor.write(form(maximum))
- local send = {}
- send["idt"] = "Energy"
- send["idn"] = 1
- send["stored"] = stored
- send["max"] = maximum
- send["out"] = out
- send = textutils.serialize(send)
- rednet.broadcast(send, "nOStatusBroadcast")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement