]> 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:

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