]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

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