Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mon = peripheral.find("monitor")
- if not mon then
- error("Por favor, adjunta monitor")
- end
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- button = {}
- charts = {}
- function clearAll()
- button = {}
- charts = {}
- mon.clear()
- end
- function addButton(nombre, func, xMin, xMax, yMin, yMax, offColor, onColor)
- color = offColor or colors.red
- activeColor = onColor or colors.lime
- button[nombre] = {}
- button[nombre]["func"] = func
- button[nombre]["active"] = false
- button[nombre]["xMin"] = xMin
- button[nombre]["yMin"] = yMin
- button[nombre]["xMax"] = xMax
- button[nombre]["yMax"] = yMax
- button[nombre]["color"] = color
- button[nombre]["activeColor"] = activeColor
- end
- function addChart(nombre, xMin, xMax, yMin, yMax, valor, color, etiqueta)
- charts[nombre] = {}
- charts[nombre]["xMin"] = xMin
- charts[nombre]["xMax"] = xMax
- charts[nombre]["yMin"] = yMin
- charts[nombre]["yMax"] = yMax
- charts[nombre]["valor"] = valor --El valor es en porcentaje
- charts[nombre]["color"] = color
- charts[nombre]["etiqueta"] = etiqueta
- end
- function updateChart(nombre, valor, color)
- charts[nombre]["valor"] = valor
- charts[nombre]["color"] = color
- screenChart()
- end
- function screenButton()
- local currcolor
- for nombre, data in pairs(button)do
- local active = data["active"]
- if active == true then
- currcolor = data["activeColor"]
- else
- currcolor = data["color"]
- end
- fillButton(nombre, currcolor, data)
- end
- end
- function screenChart()
- for nombre, data in pairs(charts)do
- fillChart(nombre, data)
- end
- end
- function fillButton(text, color, bData)
- mon.setBackgroundColor(color)
- local yspot = math.floor((bData["yMin"] + bData["yMax"])/2)
- local xspot = math.floor((bData["xMax"] - bData["xMin"] - string.len(text)) /2) +1
- for j = bData["yMin"], bData["yMax"] do
- mon.setCursorPos(bData["xMin"], j)
- if j == yspot then
- for k = 0, bData["xMax"] - bData["xMin"] - string.len(text) + 1 do
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xMin"], bData["xMax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function fillChart(nombre, cData)
- local h = cData["yMax"] - cData["yMin"]
- local percentPerStep = math.floor(100/h)
- local barHeight = math.floor(cData["valor"]/percentPerStep)
- local xspot = math.floor((cData["xMax"] - cData["xMin"]) /2) -1
- local yspot = math.floor((cData["yMin"] + cData["yMax"]) /2)
- local text = cData["valor"] .. "%"
- if cData["etiqueta"] ~= nil then
- etiquetaX = math.floor((cData["xMax"] - cData["xMin"] - string.len(cData["etiqueta"])) /2)
- etiqueta(cData["xMin"] + etiquetaX, cData["yMax"] + 1, cData["etiqueta"])
- end
- local emptyColor = 0
- if cData["color"] == colors.gray then
- emptyColor = colors.lightGray
- else
- emptyColor = colors.gray
- end
- if cData["color"] == colors.white then
- mon.setTextColor(colors.black)
- end
- for j = cData["yMin"], cData["yMax"] do
- mon.setCursorPos(cData["xMin"], j)
- if j == yspot then
- for k = 0, cData["xMax"] - cData["xMin"] - string.len(text) + 1 do
- if barHeight + cData["yMax"] >= cData["yMax"] + (cData["yMax"] - j) then
- mon.setBackgroundColor(cData["color"])
- else
- mon.setBackgroundColor(emptyColor)
- end
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = cData["xMin"], cData["xMax"] do
- if barHeight + cData["yMax"] >= cData["yMax"] + (cData["yMax"] - j) then
- mon.setBackgroundColor(cData["color"])
- else
- mon.setBackgroundColor(emptyColor)
- end
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- end
- function toggleButton(nombre)
- button[nombre]["active"] = not button[nombre]["active"]
- screenButton()
- end
- function flashButton(nombre)
- toggleButton(nombre)
- sleep(0.25)
- toggleButton(nombre)
- end
- function checkXY(x, y)
- for nombre, data in pairs(button) do
- if y>=data["yMin"] and y<= data["yMax"] then
- if x>=data["xMin"] and x<=data["xMax"] then
- data["func"]()
- flashButton(nombre)
- return true
- end
- end
- end
- return false
- end
- function addTitulo(text)
- w, h = mon.getSize()
- mon.setCursorPos((w-string.len(text))/2+1, 1)
- mon.write(text)
- end
- function etiqueta(x, y, text)
- mon.setCursorPos(x, y)
- mon.write(text)
- end
- function addEtiqueta(x,y,text,color)
- mon.setCursorPos(x,y)
- mon.setTextColor(color)
- mon.write(text)
- mon.setTextColor(colors.white)
- end
- function startButtonCharts()
- mon.clear()
- screenButton()
- screenChart()
- end
- -- FUNCIONES Prog Princial:
- --function getClick()
- -- event,side,xPos,yPos=os.pullEvent("monitor_touch")
- -- checkXY(xPos,yPos)
- --end
- --
- --while true do
- -- getClick()
- --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement