rtic-rs / rtic

Real-Time Interrupt-driven Concurrency (RTIC) framework for ARM Cortex-M microcontrollers

Home Page:https://rtic.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I share the spawn handle?

AntMaster7 opened this issue · comments

commented

Hello. I'm trying to do a blinking led using monotonic and spawn which itself works fine. However, I'm trying to pause this blinking when I press a button. I did not figure out how I can share the spawn_handle between different tasks.

I tried something like the following. But it doesn't work.

#[shared]
struct Shared {
    spawn_handle: __rtic_internal_toggle_led_SystickMono_SpawnHandle,
}

#[task(local = [led, state: bool = false])]
fn toggle_led(ctx: toggle_led::Context) {
    // ...

    ctx.shared.spawn_handle.lock(|spawn_handle| {
        *spawn_handle = toggle_led::spawn_after(1.secs()).unwrap();
    });
}

#[task(binds = GPIOTE, shared = [ spawn_handle ], local = [gpiote, state: bool = false])]
fn gpiote_event(mut ctx: gpiote_event::Context) {
    if ctx.local.gpiote.channel0().is_event_triggered() {
        ctx.local.gpiote.channel0().reset_events();

        ctx.shared.spawn_handle.lock(|spawn_handle| {
             spawn_handle.cancel().unwrap(); // Error: cannot move out of `*spawn_handle` which is behind a mutable reference
        });
   }
}
commented

I could solve my issue by wrapping the handle in an Option.