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

Touchscreen

This sample powers the touchscreen, TOUCH, up for 10 seconds and prints where each touch starts and ends, by polling pbl_touch_get_last(). It also prints the properties of the panel. pbl_touch_enable() and pbl_touch_disable() count users: the controller stays powered until the last user disables it.

The driver also reports touches to the input subsystem as events, which Input events shows. That is what the system uses, because polling can miss a touch shorter than the polling period.

On a watch, touch the screen while the sample runs. On the emulator, use pbl emulator touch or pbl emulator swipe; on a native board, click in the display window.

Run:

pbl samples samples/drivers/touch

Boards:

every board

Sources

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

#include <pbl/drivers/touch.h>
#include <pbl/kernel/delay.h>
#include <pbl/kernel/time.h>

#define LISTEN_S 10
#define POLL_MS  20

static const char *prv_yes_no(bool value) {
  return value ? "yes" : "no";
}

int main(void) {
  const struct pbl_touch_props *props = pbl_touch_get_props(TOUCH);
  struct pbl_touch_sample prev = {};
  uint64_t end;
  int touches = 0;
  int ret;

  /* The coordinates come mapped to the display: the properties say how the panel is mounted. */
  printf("touch: %dx%d, inverted x: %s, inverted y: %s, swapped: %s\n", props->size_x,
         props->size_y, prv_yes_no(props->inverted_x), prv_yes_no(props->inverted_y),
         prv_yes_no(props->swapped_x_y));

  /* Enabling counts users: the controller stays powered until the last one disables it. */
  ret = pbl_touch_enable(TOUCH);
  if (ret != 0) {
    fprintf(stderr, "touch: enable failed (%d)\n", ret);
    return ret;
  }

  printf("touch: listening for %d s\n", LISTEN_S);

  /*
   * Touches also go to the input subsystem, as events: polling the last sample is enough to
   * follow a finger here.
   */
  end = pbl_uptime_ms() + LISTEN_S * 1000U;
  while (pbl_uptime_ms() < end) {
    struct pbl_touch_sample now;

    pbl_touch_get_last(TOUCH, &now);
    if (now.down && !prev.down) {
      printf("touch: down at %d,%d\n", now.x, now.y);
      touches++;
    } else if (!now.down && prev.down) {
      printf("touch: up at %d,%d\n", prev.x, prev.y);
    }
    prev = now;

    pbl_sleep_ms(POLL_MS);
  }

  printf("touch: %d touches\n", touches);

  ret = pbl_touch_disable(TOUCH);
  if (ret != 0) {
    fprintf(stderr, "touch: disable failed (%d)\n", ret);
    return ret;
  }

  printf("touch: enabled after disable: %s\n", prv_yes_no(pbl_touch_is_enabled(TOUCH)));
  printf("Sample done\n");

  return 0;
}

Output

touch: \d+x\d+, inverted x: (yes|no), inverted y: (yes|no), swapped: (yes|no)
touch: listening for 10 s
touch: \d+ touches
touch: enabled after disable: no
Sample done

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

Overview

  • Sources
  • Output