|
|
#1 |
|
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 731
Karma: 72743
Join Date: Feb 2008
Location: Here or there
Device: iRex iLiad, iRex DR800S. K4NT. Kobo Aura, Aura One, Libra 2.
|
Battery status
Hi there,
while looking at /proc on the iLiad and dreaming on some ideas I noticed there isn't an easy way to get the battery status as it doesn't exist any related file on /proc. So, professional as I am I looked at the sources and there was the answer, hidden inside eventPoller function in powerMgr.c file [1]: ioctl call to /dev/battery device.Initially I thougth on writing a simple lua script translating that code, but as lua itself has no ioctl module it can't be done with pure lua. My second try was with python (use ericshliao's [2]), you can find the proof of concept on attached file. Future? now I'm dreaming with an iLiad extension module for lua, which would include these useful functions to power our lua scripts... Hope you find it interesting, Iņigo [1] http://iliad.hpelbers.org/irexdox/po...8c-source.html [2] https://www.mobileread.com/forums/showthread.php?t=30169 Last edited by Iņigo; 10-05-2009 at 05:58 PM. Reason: uploaded correct file |
|
|
|
|
|
#2 |
|
JSR FFD2
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
|
Ioctl() is no simple to handle in a generic way: the parameters are often pointers to structs. But Perl, Python, and Python do it, so.....
It's not difficult to write functions in C, and to make a Lua module with it. I did that with liberdm once, to play with the screen refresh functions. We could make a Lua module with handy Iliad functions. After that you could use them in a Lua script. Of course this is not very handy for experimenting. Directly scripting in Lua would be much cooler. Edit: There is a Lua module to pack / unpack C-structs exists: http://www.inf.puc-rio.br/~roberto/struct/ Edit: I asked on the Lua list... Alien (http://alien.luaforge.net/) might be a way to implement things like ioctl(). It requires libffi (which is not present on the Iliad, but is a cool thing to have). I'll have a look at this when I have some more spare time...
Last edited by hansel; 10-06-2009 at 04:50 AM. |
|
|
|
| Advert | |
|
|
|
|
#3 | |
|
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 731
Karma: 72743
Join Date: Feb 2008
Location: Here or there
Device: iRex iLiad, iRex DR800S. K4NT. Kobo Aura, Aura One, Libra 2.
|
Quote:
I'm for the module for useful functions. We don't need a fully operational ioctl, only those handy functions exported to lua world. F.e. pm_getBatteryStatus, hw_detectMMC, and so on. This extension module should be included with iliad-lua package, so any lua developer could use it. In fact my bat.py code was to drive us to this discussion ![]() So, which other functions are interesting to be wrapped? Iņigo PS: if I underestand correctly, lua alien/FFI module is like ctypes in python, a way to call functions present in any dynamic library of the OS. Ok, it could be quite useful. |
|
|
|
|
|
|
#4 | ||
|
JSR FFD2
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
|
Quote:
Don't know if it is worth the effort yet...Quote:
Last edited by hansel; 10-06-2009 at 04:42 PM. |
||
|
|
|
|
|
#5 |
|
JSR FFD2
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
|
Update
I managed to build a Lua package with Alien. Alien passes its own included tests...
|
|
|
|
| Advert | |
|
|
|
|
#6 |
|
JSR FFD2
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
|
It works! You can call any function in a shard library without compiling!
I'll update the official Lua package soon. I attach the preliminary version for those who want to play with it (install on the Iliad with ipkg install). Iņigo: thanks for pushing me into a new adventure ![]() Code:
#!/usr/bin/env lua
require "alien"
require 'bit'
local bor, band, lshift = bit.bor, bit.band, bit.lshift
local function _IOR(x,y,t)
local IOCPARM_MASK = 0x7f
local IOC_READ = 0x80000000
local a = lshift(string.byte(x),8)
local b = lshift(band(t, IOCPARM_MASK), 16)
return bor(IOC_READ, a, b, y)
end
-- open the library (the functions we want are in the default libs)
local lib = alien.default
-- get the functions we need
local open, close, ioctl = lib.open, lib.close, lib.ioctl
-- specify the API
open:types {ret='int', 'string', 'int'}
close:types {ret='int', 'int'}
ioctl:types {ret='int', 'int', 'int', 'ref int'}
local ret, fd, val
local READ_CHARGE = _IOR('b', 1, 4)
local READ_TIME = _IOR('b', 2, 4)
fd = open("/dev/battery", 0)
ret, val = ioctl(fd, READ_TIME, 0)
if val == 0xffff then
ret, val = ioctl(fd, READ_CHARGE, 0)
print(string.format('Battery: %d%%, now charging', val))
else
print(string.format("Battery: %d:%2d hours remaining", math.floor(val/60), val%60))
end
close(fd)
Last edited by hansel; 10-07-2009 at 05:18 PM. Reason: added luabitop |
|
|
|
![]() |
| Tags |
| battery status |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| iLiad iLiad Battery Hacks: Part 2 -- 35 hours of battery life! A how-to guide. | jharker | iRex Developer's Corner | 107 | 11-15-2013 09:15 AM |
| O Battery life, battery, wherefore art thou, battery life? | DeusExMe | Amazon Kindle | 30 | 02-21-2012 07:28 PM |
| Getting rid of battery status line | colonel | Sony Reader Dev Corner | 7 | 04-22-2009 11:12 AM |
| Replacement Battery Status | firekat | iRex | 7 | 12-21-2007 08:11 AM |
| iLiad Status? | RibRdb2 | iRex Developer's Corner | 11 | 09-25-2006 10:50 AM |