Thread: iLiad Battery status
View Single Post
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, 378 views)

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