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

Ambient light

This sample reads the ambient light sensor five times, a second apart. It primes the sensor first with pbl_als_prime(), which lets sensors that sample continuously start before the first read, and releases it at the end. The levels are raw counts up to PBL_ALS_LEVEL_MAX; pbl_als_to_lux() converts them on boards that calibrate their sensor, and returns them unchanged elsewhere.

On a watch, cover the sensor or point it at a light while the sample runs. The emulator’s simulated sensor reads 0.

Run:

pbl samples samples/drivers/als

Boards:

every board

Sources

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

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

#define READS     5
#define PERIOD_MS 1000

int main(void) {
  int ret = 0;

  printf("als: levels 0 to %u, lux: %s\n", (unsigned int)PBL_ALS_LEVEL_MAX,
         pbl_als_has_lux(ALS) ? "yes" : "no");

  /* Priming lets sensors that sample continuously start before the first read. */
  pbl_als_prime(ALS);

  for (int i = 0; i < READS; i++) {
    uint32_t level;

    pbl_sleep_ms(PERIOD_MS);

    ret = pbl_als_read(ALS, &level);
    if (ret != 0) {
      fprintf(stderr, "als: read failed (%d)\n", ret);
      break;
    }

    /* Uncalibrated sensors return the level unchanged. */
    printf("als: level %u, %u %s\n", (unsigned int)level, (unsigned int)pbl_als_to_lux(ALS, level),
           pbl_als_has_lux(ALS) ? "lux" : "counts");
  }

  pbl_als_release(ALS);

  if (ret != 0) {
    return ret;
  }

  printf("Sample done\n");

  return 0;
}

Output

als: levels 0 to \d+, lux: (yes|no)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
Sample done

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

Overview

  • Sources
  • Output