Advertisement
7Roses

opencomputer battery checkScript

Jan 15th, 2017
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. --[[
  2.   Author:  7Rose
  3.   date:    2017-01-15
  4.   description:
  5.     deamon for monitoring the computers/robots internal energy
  6.     if your using this on a computer know that this is the total of all
  7.     the battery/computers energy buffers.
  8.  
  9.   release notes:
  10.     version 1.0: initial release, currently this only shows
  11.                  you a percent on the right bottom corner of your screen.
  12.     version 1.1: added stop functionality for stopping this service.
  13. ]]
  14.  
  15.  
  16. local gpu = require("component").gpu
  17. local event = require("event");
  18. local computer = require("computer");
  19.  
  20. local timeout = 0.5;
  21. local timer = nil;
  22.  
  23. local function round(num, numDecimalPlaces)
  24.   local mult = 10^(numDecimalPlaces or 0)
  25.   return math.floor(num * mult + 0.5) / mult
  26. end
  27.  
  28. local getBatteryPercent = function()
  29.   return round(computer.energy() / computer.maxEnergy(),2);
  30. end
  31.  
  32. local showMessage= function(level)
  33.   -- check monitor, if monochrome then send only text
  34.   -- if multicollor -> green=70+,orange=40+, red = 40-
  35.   -- for first release only send the number.
  36.   local monitorDepth = 1;
  37.   local w,h = gpu.getResolution();
  38.   if level>0.99 then level = 1 end
  39.  
  40.   if monitorDepth ==1 then
  41.       gpu.set(w-3,h,(level*100) .. "% ");
  42.   end
  43. end
  44.  
  45. local function timerCallBack()
  46.   showMessage(getBatteryPercent());
  47.   timer = event.timer(timeout,timerCallBack);
  48. end
  49.  
  50. function start(config)
  51.   if timer==nil then
  52.     timer = event.timer(timeout,timerCallBack);
  53.   else
  54.     gpu.set(w,h,"!");
  55.   end
  56. end
  57.  
  58. function stop(config)
  59.   if timer~=nil then
  60.     event.cancel(timer);
  61.   end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement