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

centerwork: mouse_resize_handler added; #395
[etc/awesome.git] / widget / contrib / tpbat / smapi.lua
1 --[[
2
3      smapi.lua
4      Interface with thinkpad battery information
5
6      Licensed under GNU General Public License v2
7       * (c) 2013, Conor Heine
8
9 --]]
10
11 local first_line   = require("lain.helpers").first_line
12
13 local string       = { format = string.format }
14 local tonumber     = tonumber
15 local setmetatable = setmetatable
16
17 local smapi = {}
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 not mins_left:find("^%d+") then return "N/A" end
84
85         local hrs = math.floor(mins_left / 60)
86         local min = mins_left % 60
87
88         return string.format("%02d:%02d", hrs, min)
89     end
90
91     function bat:percent()
92         return tonumber(self:get("remaining_percent"))
93     end
94
95     return setmetatable(bat, {__metatable = false, __newindex = false})
96 end
97
98 return smapi