aboutsummaryrefslogtreecommitdiff
path: root/examples/button.rs
blob: ea8c4a9d92226eff5a9520c689793fa9953a5e69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
mod common;

use common::AsyncTcp;
use embassy_executor::{Executor, Spawner};
use static_cell::StaticCell;

static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new();

#[embassy_executor::task]
async fn main_task(spawner: Spawner) {
    let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS"));

    let mut device = embassy_ha::new(
        RESOURCES.init(Default::default()),
        embassy_ha::DeviceConfig {
            device_id: "example-device-id",
            device_name: "Example Device Name",
            manufacturer: "Example Device Manufacturer",
            model: "Example Device Model",
        },
    );

    let button = embassy_ha::create_button(
        &device,
        "button-sensor-id",
        embassy_ha::ButtonConfig::default(),
    );

    spawner.must_spawn(button_task(button));

    embassy_ha::run(&mut device, &mut stream).await.unwrap();
}

#[embassy_executor::task]
async fn button_task(mut button: embassy_ha::Button<'static>) {
    loop {
        button.pressed().await;
        tracing::info!("The button has been pressed");
    }
}

example_main!();