Reply
Thread Tools
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#1
Hi
how can I read battery status (voltage, load, etc.) in C++
thanks in advance
 
Posts: 1,463 | Thanked: 1,916 times | Joined on Feb 2008 @ Edmonton, AB
#2
Go read the source for conky, my changes to linux.c i think. It's in the garage under the project monky.
 
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#3
I don't understand this one:
Code:
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
(line 1629)

I'm new to C++
 
Posts: 1,463 | Thanked: 1,916 times | Joined on Feb 2008 @ Edmonton, AB
#4
you can either read it from sysfs (power kernel only)
or using dbus calls (works on all kernels but doesn't update while charging)
i suggest doing both then...

the part you're looking at is from the sysfs loop, each fgets() is supposed to read a line of the file or stream, so if it fails to do so or reaches eof it will return null. so it's trying to read max 256 chars into buf, then the rest of the loop figures out what it just read and where to store it. Then it goes to the next line and repeats. So it's supposed to exit the loop when it hits the end of the file anyways, since the !feof is in the "while" condition, but there can be some other type of error that would return null and exit the loop on the line you're looking at also. It would crash if you didn't have that line there.
Also keep in mind Conky supports multiple batteries, which it has that [idx] thing everywhere...

Last edited by Creamy Goodness; 2011-06-08 at 18:21.
 
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#5
and I found another way to do it!
Code:
lshal | grep battery > /path/to/file
and then read with fstream
 
Reply


 
Forum Jump


All times are GMT. The time now is 14:01.