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

Poll in Rust

The poll sample written in Rust, on the pebbleos crate.

PollGroup::wait() returns the queue a message is in, which is() compares with each of them.

Run:

pbl samples samples/rust/kernel/poll

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::time::Duration;

use pebbleos::msgq::MsgQueue;
use pebbleos::poll::PollGroup;
use pebbleos::thread::{self, Priority, Thread};
use pebbleos::time::Timeout;
use pebbleos::{Result, println};

#[derive(Clone, Copy)]
struct ButtonEvent {
    button: u8,
    pressed: bool,
}

#[derive(Clone, Copy)]
struct AccelSample {
    x: i16,
    y: i16,
    z: i16,
}

// One queue per source, each with its own message type and depth.
static BUTTONS: MsgQueue<ButtonEvent, 4> = MsgQueue::new();
static ACCEL: MsgQueue<AccelSample, 8> = MsgQueue::new();
static EVENTS: PollGroup = PollGroup::new();

static SOURCE: Thread = Thread::new();

fn source() -> Result<()> {
    BUTTONS.put(
        ButtonEvent {
            button: 1,
            pressed: true,
        },
        Timeout::FOREVER,
    )?;
    thread::sleep(Duration::from_millis(10));
    ACCEL.put(
        AccelSample {
            x: 10,
            y: -20,
            z: 1000,
        },
        Timeout::FOREVER,
    )?;
    thread::sleep(Duration::from_millis(10));
    BUTTONS.put(
        ButtonEvent {
            button: 1,
            pressed: false,
        },
        Timeout::FOREVER,
    )
}

#[pebbleos::main]
fn main() -> Result<()> {
    // Queues join before the first wait, and for good.
    EVENTS.add(&BUTTONS);
    EVENTS.add(&ACCEL);

    SOURCE.spawn(c"source", Priority::MAIN.lower(1), || {
        if let Err(e) = source() {
            println!("source: put failed ({e})");
        }
    })?;

    // The wait leaves the message in its queue.
    while let Some(q) = EVENTS.wait(Duration::from_millis(100)) {
        if q.is(&BUTTONS) {
            if let Ok(event) = BUTTONS.get(Timeout::NO_WAIT) {
                let action = if event.pressed { "pressed" } else { "released" };
                println!("loop: button {} {action}", event.button);
            }
        } else if q.is(&ACCEL)
            && let Ok(sample) = ACCEL.get(Timeout::NO_WAIT)
        {
            println!("loop: accel {} {} {}", sample.x, sample.y, sample.z);
        }
    }
    println!("loop: nothing for 100 ms");

    println!("Sample done");

    Ok(())
}

Output

loop: button 1 pressed
loop: accel 10 -20 1000
loop: button 1 released
loop: nothing for 100 ms
Sample done

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

Overview

  • Sources
  • Output