Devbatt
From wiki.gp2x.org
Contents |
Description
The /dev/batt device file contains the current charge level of the power source, either batteries or the AC adapter.
The value is returned as a number between about 650 and 1000
Generally:
- >900
- AC Adapter
- >745
- Full batteries
- 678-745
- Medium batteries
- <678
- Empty batteries
C Example
int batterycharge(void)
{
int devbatt;
unsigned short currentval=0;
devbatt = open("/dev/batt", O_RDONLY);
read (devbatt, ¤tval, 2);
close (devbatt);
return (currentval);
}
Python Example
# reads the battery value from /dev/batt, averaging 20 samples
from array import array
f = open("/dev/batt", "rb")
data = array('h') # an array of 'signed short'
data.fromfile(f, 20)
f.close()
total = 0
for value in data:
total += value
total /= len(data)
print "battery value: %d" % total
Squidge's Table
The values returned from /dev/batt were checked against a variable voltage power supply that can provide between 0.1 and 30V in 0.1V increments. Voltages between 1.7 and 3.7V were checked, and the following table formed:
if (battval > 1016) v = 37; else if (battval > 974) v = 33; else if (battval > 943) v = 32; else if (battval > 915) v = 31; else if (battval > 896) v = 30; else if (battval > 837) v = 29; else if (battval > 815) v = 28; else if (battval > 788) v = 27; else if (battval > 745) v = 26; else if (battval > 708) v = 25; else if (battval > 678) v = 24; else if (battval > 649) v = 23; else if (battval > 605) v = 22; else if (battval > 573) v = 21; else if (battval > 534) v = 20; else if (battval > 496) v = 19; else if (battval > 448) v = 18; else v = 17;
37 means 3.7V, 17 means 1.7V, etc. Generally speaking, anything over 900 means an A/C adaptor is plugged in. Rechargable batteries normally start at 2.6V and die at around the 2.4V mark.
To make best use of this table, and to prevent fluctuating voltages, it is best to take several measurements and then take the average before using this table to find the voltage. If you read the battery level at the end of every vertical frame in your game or emulator for example, a good number of measurements is 50, meaning the battery voltage is updated once per second (assuming your game or emulator runs at 50 frames/second).

