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
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • 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

Delayed work in Rust

The delayed work sample written in Rust, on the pebbleos crate.

DelayedWork<T> holds the data its function gets, here the counts.

Run:

pbl samples samples/rust/kernel/delayed_work

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::sync::atomic::{AtomicU32, Ordering};
use core::time::Duration;

use pebbleos::println;
use pebbleos::thread;
use pebbleos::work::{DelayedWork, WorkQueue};

const DEBOUNCE: Duration = Duration::from_millis(200);
const HEARTBEAT: Duration = Duration::from_millis(20);

static DEBOUNCE_WORK: DelayedWork<AtomicU32> = DelayedWork::new(button_settled, AtomicU32::new(0));
static HEARTBEAT_WORK: DelayedWork<AtomicU32> = DelayedWork::new(heartbeat, AtomicU32::new(0));

fn button_settled(presses: &'static DelayedWork<AtomicU32>) {
    let presses = presses.fetch_add(1, Ordering::Relaxed) + 1;
    println!("button: settled, press {presses}");
}

// Unlike a timer function this runs on a thread, and may block or take locks.
fn heartbeat(beats: &'static DelayedWork<AtomicU32>) {
    beats.fetch_add(1, Ordering::Relaxed);
    // Counts from the last expiry, not from now, so the period does not drift.
    beats.schedule_next(HEARTBEAT);
}

// What a button interrupt would do on every edge, bounces included.
fn button_edge() {
    DEBOUNCE_WORK.reschedule(WorkQueue::system(), DEBOUNCE);
}

#[pebbleos::main]
fn main() {
    let system = WorkQueue::system();

    println!("main: five edges 5 ms apart, each pushing the work back");
    for _ in 0..5 {
        button_edge();
        thread::sleep(Duration::from_millis(5));
    }
    thread::sleep(2 * DEBOUNCE);

    // schedule() keeps a delay that is running, where reschedule() replaces
    // it.
    DEBOUNCE_WORK.schedule(system, DEBOUNCE);
    let kept = !DEBOUNCE_WORK.schedule(system, Duration::from_secs(10));
    println!("main: scheduled twice, first delay kept: {}", yes_no(kept));
    thread::sleep(2 * DEBOUNCE);

    HEARTBEAT_WORK.schedule(system, HEARTBEAT);
    thread::sleep(5 * HEARTBEAT + HEARTBEAT / 2);

    // Stops it for good, even though its function schedules it again.
    HEARTBEAT_WORK.cancel_sync();
    println!(
        "main: heartbeat stopped after {} beats, pending: {}",
        HEARTBEAT_WORK.load(Ordering::Relaxed),
        yes_no(HEARTBEAT_WORK.is_pending())
    );

    println!("Sample done");
}

fn yes_no(b: bool) -> &'static str {
    if b { "yes" } else { "no" }
}

Output

main: five edges 5 ms apart, each pushing the work back
button: settled, press 1
main: scheduled twice, first delay kept: yes
button: settled, press 2
main: heartbeat stopped after \d beats, pending: no
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/rust/kernel/delayed_work.md.

Overview

  • Sources
  • Output