pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • Input events
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Battery

This sample reads the battery three times, a second apart: its voltage, current and temperature, whether USB power is connected, whether the charger charges and in which phase. It sets a connection handler too, which prints USB power coming or going while it runs.

The battery is read through the charger of the board, its PMIC, which the sample enables in its boards/ files. Plug or unplug the charging cable while it runs to see the handler and the charge state change.

Run:

pbl samples samples/drivers/battery

Boards:

every board

Sources

src/main.c
#include <stdint.h>
#include <stdio.h>

#include <pbl/drivers/battery.h>
#include <pbl/kernel/delay.h>

#define READS 3

static const char *const s_status[] = {
  [BatteryChargeStatusUnknown] = "unknown",     [BatteryChargeStatusComplete] = "complete",
  [BatteryChargeStatusTrickle] = "trickle",     [BatteryChargeStatusCC] = "constant current",
  [BatteryChargeStatusCV] = "constant voltage",
};

static void prv_usb_changed(bool is_connected) {
  printf("battery: USB power %s\n", is_connected ? "connected" : "removed");
}

int main(void) {
  battery_init();

  /* Called from a work queue whenever USB power comes or goes. */
  battery_set_connection_handler(prv_usb_changed);

  for (int i = 0; i < READS; i++) {
    BatteryConstants c;
    BatteryChargeStatus status;
    int ret;

    ret = battery_get_constants(&c);
    if (ret != 0) {
      fprintf(stderr, "battery: measurement failed (%d)\n", ret);
      return ret;
    }

    ret = battery_charge_status_get(&status);
    if (ret != 0) {
      fprintf(stderr, "battery: charge status failed (%d)\n", ret);
      return ret;
    }

    printf("battery: %d mV, %d uA, %d mC\n", (int)c.v_mv, (int)c.i_ua, (int)c.t_mc);
    printf("battery: USB %s, %s, charge %s\n",
           battery_is_usb_connected() ? "connected" : "disconnected",
           battery_charge_controller_thinks_we_are_charging() ? "charging" : "not charging",
           s_status[status]);

    pbl_sleep_ms(1000);
  }

  battery_set_connection_handler(nullptr);

  printf("Sample done\n");

  return 0;
}

Output

battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z ]+
battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z ]+
battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z ]+
Sample done

Generated from PebbleOS 9e782207f. This page is maintained in the pebbleos repository: docs/samples/drivers/battery.md.

Overview

  • Sources
  • Output