Get battery level in C? - Linux

This is a discussion on Get battery level in C? - Linux ; Hi, how can I read the battery level on Linux, using C? It has somehting to do with /dev/apm, but I can't find anything more about it. -- ------------------------------------ Gernot Frisch http://www.glbasic.com...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Get battery level in C?

  1. Get battery level in C?

    Hi,

    how can I read the battery level on Linux, using C?

    It has somehting to do with /dev/apm, but I can't find anything more about
    it.



    --
    ------------------------------------
    Gernot Frisch
    http://www.glbasic.com


  2. Re: Get battery level in C?

    Gernot Frisch wrote:
    > Hi,
    >
    > how can I read the battery level on Linux, using C?
    >
    > It has somehting to do with /dev/apm, but I can't find anything more
    > about it.


    cat /proc/acpi/battery/BAT0/state

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html

  3. Re: Get battery level in C?



    >
    > cat /proc/acpi/battery/BAT0/state
    >


    I don't want to parse an ascii file. I just want to read the battery
    percentage from a binary file for a game if possible.


  4. Re: Get battery level in C?

    Gernot Frisch wrote:

    >
    >
    >>
    >> cat /proc/acpi/battery/BAT0/state
    >>

    >
    > I don't want to parse an ascii file. I just want to read the battery
    > percentage from a binary file for a game if possible.
    >


    do you want to write a game and you aren't capable of parsing a simple ascii
    file? anyway

    int batteryLevel() {
    int batLev = -1;
    FILE* f = fopen( "/proc/acpi/battery/BAT0/state", "r" );
    if( f ) {
    fscanf( f, "%i", &batLev );
    fclose( f );
    }
    return batLev;
    }

    --
    SF

    Games are very educational. Scrabble teaches us vocabulary, Monopoly teaches
    us cash-flow management, and Dungeons & Dragons teaches us to loot dead bodies.

  5. Re: Get battery level in C?


    > do you want to write a game and you aren't capable of parsing a simple
    > ascii
    > file? anyway


    Oh! The file is only one number!?
    On my VirutalPC image the file did not exist, so I could not tell, and
    googling gave me a rather complex file.
    I'll try at home. Thank you.


  6. Re: Get battery level in C?

    Hi,

    at my system something like this happens, so the posted program won't work:

    > cat /proc/acpi/battery/CMB1/state

    present: yes
    capacity state: ok
    charging state: charged
    present rate: 0 mA
    remaining capacity: 5800 mAh
    present voltage: 12454 mV

    There may also be a 2nd battery (CMB2). So I'd do the following:

    * parse the output of
    > cat /proc/acpi/battery/*/state |grep "remaining"

    (= remaining capacity: XXXX mAh)
    * add the value of all batteries together

    or

    * treat all files /proc/acpi/battery/*/state like this:
    batlev = 0;
    f = fopen (filename, "r");
    if (f)
    {
    while (fscanf (f, "%s: %i", &desc, &val))
    { if (strcasecmp (desc, "remaining capacity") == 0) batlev += val; }
    }

    Lots of fun!!!


+ Reply to Thread