Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Readers > More E-Book Readers > iRex > iRex Developer's Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 10-05-2009, 04:08 PM   #1
Iņigo
Guru
Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.
 
Posts: 730
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
Attached Files
File Type: zip bat.py.zip (823 Bytes, 411 views)

Last edited by Iņigo; 10-05-2009 at 04:58 PM. Reason: uploaded correct file
Iņigo is offline   Reply With Quote
Old 10-06-2009, 02:29 AM   #2
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
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 03:50 AM.
hansel is offline   Reply With Quote
Advert
Old 10-06-2009, 04:59 AM   #3
Iņigo
Guru
Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.Iņigo did not drink the Kool Aid.
 
Posts: 730
Karma: 72743
Join Date: Feb 2008
Location: Here or there
Device: iRex iLiad, iRex DR800S. K4NT. Kobo Aura, Aura One, Libra 2.
Quote:
Originally Posted by hansel View Post
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...
I've read your message to lua mailing list this morning. I also searched but didn't find anything useful either.

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.
Iņigo is offline   Reply With Quote
Old 10-06-2009, 03:39 PM   #4
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
Arrow Update...

Quote:
Originally Posted by 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.
I managed (with quite some hacking) to cross-build the Alien module for the Iliad. I can 'require' with Lua on the Iliad, but it still has some issues: it will not function properly without modifications... Don't know if it is worth the effort yet...

Quote:
Originally Posted by Iņigo
I'm for the module for useful functions.
That would be easier, but less challenging

Last edited by hansel; 10-06-2009 at 03:42 PM.
hansel is offline   Reply With Quote
Old 10-07-2009, 12:38 AM   #5
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
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...
hansel is offline   Reply With Quote
Advert
Old 10-07-2009, 02:45 AM   #6
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
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)
Attached Files
File Type: gz lua5.1-iliad-1.6.6.ipk.gz (276.8 KB, 374 views)

Last edited by hansel; 10-07-2009 at 04:18 PM. Reason: added luabitop
hansel is offline   Reply With Quote
Reply

Tags
battery status

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

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 08:15 AM
O Battery life, battery, wherefore art thou, battery life? DeusExMe Amazon Kindle 30 02-21-2012 06:28 PM
Getting rid of battery status line colonel Sony Reader Dev Corner 7 04-22-2009 10:12 AM
Replacement Battery Status firekat iRex 7 12-21-2007 07:11 AM
iLiad Status? RibRdb2 iRex Developer's Corner 11 09-25-2006 09:50 AM


All times are GMT -4. The time now is 06:42 AM.


MobileRead.com is a privately owned, operated and funded community.