]> git.madduck.net Git - etc/awesome.git/blob - widget/contrib/tpbat/smapi.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wiki updated
[etc/awesome.git] / widget / contrib / tpbat / smapi.lua
1
2 --[[
3                                                   
4      smapi.lua                                    
5      Interface with thinkpad battery information  
6                                                   
7      Licensed under GNU General Public License v2 
8       * (c) 2013, Conor Heine                     
9                                                   
10 --]]
11
12 local first_line   = require("lain.helpers").first_line
13
14 local string       = { format = string.format }
15 local tonumber     = tonumber
16 local setmetatable = setmetatable
17
18 local smapi = {}
19 local apipath = "/sys/devices/platform/smapi"
20
21 -- Most are readable values, but some can be written to (not implemented, yet?)
22 local readable = {
23     barcoding                  = true,
24     charging_max_current       = true,
25     charging_max_voltage       = true,
26     chemistry                  = true,
27     current_avg                = true,
28     current_now                = true,
29     cycle_count                = true,
30     design_capacity            = true,
31     design_voltage             = true,
32     dump                       = true,
33     first_use_date             = true,
34     force_discharge            = false,
35     group0_voltage             = true,
36     group1_voltage             = true,
37     group2_voltage             = true,
38     group3_voltage             = true,
39     inhibit_charge_minutes     = false,
40     installed                  = true,
41     last_full_capacity         = true,
42     manufacture_date           = true,
43     manufacturer               = true,
44     model                      = true,
45     power_avg                  = true,
46     power_now                  = true,
47     remaining_capacity         = true,
48     remaining_charging_time    = true,
49     remaining_percent          = true,
50     remaining_percent_error    = true,
51     remaining_running_time     = true,
52     remaining_running_time_now = true,
53     serial                     = true,
54     start_charge_thresh        = false,
55     state                      = true,
56     stop_charge_thresh         = false,
57     temperature                = true,
58     voltage                    = true,
59 }
60
61 function smapi:battery(name)
62     local bat = {}
63
64     bat.name = name
65     bat.path = apipath .. "/" .. name
66
67     function bat:get(item)
68         return self.path ~= nil and readable[item] and first_line(self.path .. "/" .. item) or nil
69     end
70
71     function bat:installed()
72         return self:get("installed") == "1"
73     end
74
75     function bat:status()
76         return self:get('state')
77     end
78
79     -- Remaining time can either be time until battery dies or time until charging completes
80     function bat:remaining_time()
81         local time_val = bat_now.status == 'discharging' and 'remaining_running_time' or 'remaining_charging_time'
82         local mins_left = self:get(time_val)
83
84         if not mins_left:find("^%d+") then return "N/A" end
85
86         local hrs = math.floor(mins_left / 60)
87         local min = mins_left % 60
88
89         return string.format("%02d:%02d", hrs, min)
90     end
91
92     function bat:percent()
93         return tonumber(self:get("remaining_percent"))
94     end
95
96     return setmetatable(bat, {__metatable = false, __newindex = false})
97 end
98
99 return smapi