Advertisement
TeoremaPi

Energia Monitor Portatil

Oct 8th, 2020 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. -- guardamos la pantalla del ordenador y el monitor
  2. m = term.current();               -- monitor
  3.  
  4. -- tamano de la pantalla
  5. wm, hm = m.getSize() -- ancho y alto maximos
  6.  
  7. -- limpiamos la pantalla
  8. m.setBackgroundColor(colors.black)
  9. m.clear()
  10.  
  11. -- conectamos el wifi
  12. rednet.open("back")
  13.  
  14. -- funcion para adecuar los numeros a 3 cifras
  15. function num2str(num, unit, sign)
  16.  
  17.   if num == 0 then
  18.     result = "0 " .. unit
  19.   else
  20.     -- precision
  21.     pres = 4
  22.  
  23.     -- orden de magnitud
  24.     exp = math.floor(math.log10(math.abs(num)))
  25.  
  26.     -- simbolos de multiplicador
  27.     symbols = {"", "k", "M", "G", "T", "P", "E"}
  28.     symbol  = symbols[math.floor(exp/3) + 1]
  29.  
  30.     -- incluimos el simbolo si se pide
  31.     if sign then
  32.       if num >= 0 then
  33.         result = "+"
  34.       else
  35.         result = "-"
  36.       end
  37.     else
  38.       result = ""
  39.     end
  40.  
  41.     -- adecuamos el numero
  42.     num = math.abs(num)
  43.     num = math.floor(num/10^(exp-(pres-1)) + 0.5)/10^(pres-1)    -- redondeamos con pres cifras
  44.     num = num*10^(exp - 3*math.floor(exp/3))  -- ajustamos al multiplo de 10^3
  45.  
  46.     result = result .. tostring(num) .. " " .. symbol .. unit
  47.   end
  48.  
  49.   return result
  50.  
  51. end
  52.  
  53. -- creamos una pantalla de datos
  54. ws = wm - 3 -- ancho de la pantalla
  55. xs = 2      -- posicion de la esquina superior izq
  56. ys = 2      -- altura de la esquina superior izq
  57. sr = math.floor(ws/2+0.5) -- segunda columna
  58.  
  59.  
  60. while true do
  61.   -- recibir datos
  62.   senderId, message, protocol = rednet.receive()
  63.  
  64.   -- guardar la informacion
  65.   d = message
  66.  
  67.   -- limpiamos la pantalla
  68.   m.setBackgroundColor(colors.black)
  69.   m.clear()
  70.  
  71.   -- representar datos
  72.   -- maxima capacidad
  73.   lw = 2 -- linea de escritura
  74.   m.setCursorPos(xs + 2, ys + lw)
  75.   m.setTextColor(colors.white)
  76.   write("Capacity:")
  77.   text = num2str(d.mes, "RF", false)
  78.   m.setCursorPos(xs + ws - string.len(text) - 1, ys + lw)
  79.   m.setTextColor(colors.lime)
  80.   write(num2str(d.mes, "RF", false))
  81.  
  82.   -- energia almacenada
  83.   lw = lw + 1
  84.   m.setCursorPos(xs + 2, ys + lw)
  85.   m.setTextColor(colors.white)
  86.   write("Charge:")
  87.   text = num2str(d.es, "RF", false)
  88.   m.setCursorPos(xs + ws - string.len(text) - 1, ys + lw)
  89.   m.setTextColor(colors.lime)
  90.   write(text)
  91.  
  92.   -- barra de energia
  93.   lw = lw + 1
  94.   paintutils.drawLine(xs + 2, ys + lw, xs + ws - 2, ys + lw, colors.gray)
  95.   pw = math.floor(d.es/d.mes * (ws-3) + 0.5) -- ancho de la barra
  96.   if pw > 0 then
  97.     if d.es/d.mes < 1/3 then
  98.       paintutils.drawLine(xs + 2, ys + lw, xs + pw + 1, ys + lw, colors.red)
  99.     elseif d.es/d.mes < 2/3 then
  100.       paintutils.drawLine(xs + 2, ys + lw, xs + pw + 1, ys + lw, colors.orange)
  101.     else
  102.       paintutils.drawLine(xs + 2, ys + lw, xs + pw + 1, ys + lw, colors.green)
  103.     end
  104.   end
  105.   text = tostring(math.floor(d.es/d.mes*100 + 0.5)) .. "%" --texto del porcentaje
  106.   textColor = ""
  107.   backColor = ""
  108.   for k = 1, string.len(text) do -- color de fondo y del texto
  109.     textColor = textColor .. "0"
  110.     if pw < ws - 3 - string.len(text) + k  then
  111.       backColor = backColor .. "7" -- gris
  112.     else
  113.       backColor = backColor .. "d" -- verde
  114.     end
  115.   end
  116.   m.setCursorPos(xs + ws - string.len(text) - 1, ys + lw)
  117.   term.blit(text, textColor, backColor)
  118.   m.setBackgroundColor(colors.black)
  119.  
  120.   -- tranferencia
  121.   lw = lw + 1
  122.   m.setCursorPos(xs + 2, ys + lw)
  123.   m.setTextColor(colors.white)
  124.   write("I/O:")
  125.   text = num2str(d.tpt, "RF/t", true)
  126.   m.setCursorPos(xs + ws - string.len(text) - 1, ys + lw)
  127.   if d.tpt >= 0 then
  128.     m.setTextColor(colors.lime)
  129.   else
  130.     m.setTextColor(colors.red)
  131.   end
  132.   write(num2str(d.tpt, "RF/t", true))
  133.  
  134.   -- tiempo restante en formato [dd d hh:MM:ss]
  135.   lw = lw + 1
  136.   m.setCursorPos(xs + 2, ys + lw)
  137.   m.setTextColor(colors.white)
  138.   write("T Rem:")
  139.   if d.tpt == 0 then
  140.     t = 20*60*60*24*365
  141.     color = colors.lime
  142.   elseif d.tpt > 0 then
  143.     t = d.mes-d.es/d.tpt  -- tiempo en ticks
  144.     color = colors.lime
  145.   else
  146.     t = -d.es/d.tpt       -- tiempo en ticks
  147.     color = colors.red
  148.   end
  149.   t = t/20                -- tiempo en s
  150.   if t < 60*60*24*365 then
  151.     if math.floor(t%60) < 10 then
  152.       text = ":0" .. math.floor(t%60)
  153.     else
  154.       text = ":" .. math.floor(t%60)
  155.     end
  156.     t = t/60
  157.     if math.floor(t%60) < 10 then
  158.       text = ":0" .. math.floor(t%60) .. text
  159.     else
  160.       text = ":" .. math.floor(t%60) .. text
  161.     end
  162.     t = t/60
  163.     if math.floor(t/24) > 0 then
  164.       if math.floor(t%24) < 10 then
  165.         text = math.floor(t/24) .. "d 0" .. math.floor(t%24) .. text
  166.       else
  167.         text = math.floor(t/24) .. "d " .. math.floor(t%24) .. text
  168.       end
  169.     else
  170.       text = math.floor(t%24) .. text
  171.     end
  172.   else
  173.     text = "- s"
  174.   end
  175.   m.setCursorPos(xs + ws - string.len(text) - 1, ys + lw)
  176.   m.setTextColor(color)
  177.   write(text)
  178.  
  179.   -- marco
  180.   lw = lw + 2
  181.   paintutils.drawLine(xs, ys, xs, ys+lw, colors.lightGray)        -- izquierda
  182.   paintutils.drawLine(xs, ys+lw, xs+ws, ys+lw, colors.lightGray)  -- abajo
  183.   paintutils.drawLine(xs+ws, ys, xs+ws, ys+lw, colors.lightGray)  -- derecha
  184.   paintutils.drawLine(xs, ys, xs+ws, ys, colors.lightGray)        -- arriba
  185.  
  186.   -- titulo
  187.   m.setBackgroundColor(colors.black)
  188.   m.setTextColor(colors.white)
  189.   m.setCursorPos(xs + 2, ys)
  190.   write(" ENERGY CORE ")
  191.  
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement