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

6024fa9ab736bccf5e0be187d4fe3b0586d8822f
[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 then
84                         return "N/A"
85                 end
86                 
87                 local hrs = mins_left / 60
88                 local min = mins_left % 60
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