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

Vibration

This sample buzzes the vibration motor three times. Then, if its driver can brake, it buzzes three more times, each time driving the motor at the braking strength from pbl_vibe_get_braking_strength() for a moment before stopping it, which ends the buzz sooner.

On a watch, feel the difference between the two sets of pulses. The emulator’s simulated motor cannot brake.

Run:

pbl samples samples/drivers/vibe

Boards:

every board

Sources

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

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

#define PULSES   3
#define PULSE_MS 150
#define PAUSE_MS 350
#define BRAKE_MS 40

static int prv_pulse(int8_t strength, int8_t braking) {
  int ret;

  ret = pbl_vibe_start(VIBE, strength);
  if (ret != 0) {
    fprintf(stderr, "vibe: start failed (%d)\n", ret);
    return ret;
  }
  pbl_sleep_ms(PULSE_MS);

  /* Driving the motor against itself for a moment stops it sooner. */
  if (braking != VIBE_STRENGTH_OFF) {
    ret = pbl_vibe_start(VIBE, braking);
    if (ret != 0) {
      fprintf(stderr, "vibe: brake failed (%d)\n", ret);
      return ret;
    }
    pbl_sleep_ms(BRAKE_MS);
  }

  ret = pbl_vibe_stop(VIBE);
  if (ret != 0) {
    fprintf(stderr, "vibe: stop failed (%d)\n", ret);
  }

  return ret;
}

int main(void) {
  int8_t braking = pbl_vibe_get_braking_strength(VIBE);
  int ret;

  for (int i = 0; i < PULSES; i++) {
    ret = prv_pulse(VIBE_STRENGTH_MAX, VIBE_STRENGTH_OFF);
    if (ret != 0) {
      return ret;
    }
    printf("vibe: pulse %d\n", i + 1);
    pbl_sleep_ms(PAUSE_MS);
  }

  if (braking == VIBE_STRENGTH_OFF) {
    printf("vibe: no braking\n");
  } else {
    for (int i = 0; i < PULSES; i++) {
      ret = prv_pulse(VIBE_STRENGTH_MAX, braking);
      if (ret != 0) {
        return ret;
      }
      printf("vibe: braked pulse %d, braking at %d\n", i + 1, braking);
      pbl_sleep_ms(PAUSE_MS);
    }
  }

  printf("Sample done\n");

  return 0;
}

Output

vibe: pulse 1
vibe: pulse 2
vibe: pulse 3
vibe: (no braking|braked pulse 3, braking at -?\d+)
Sample done

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

Overview

  • Sources
  • Output