diff options
| author | diogo464 <[email protected]> | 2025-12-04 13:06:27 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-04 13:06:27 +0000 |
| commit | 945f38391052773c6b16e54006a434ff7c9f5d98 (patch) | |
| tree | 1f3360316f7304e26a788582bd488726bd8259d2 /examples/button.rs | |
| parent | b3e47f9c9268e01533a809f83b4f3ecd379c4b22 (diff) | |
added more examples
Diffstat (limited to 'examples/button.rs')
| -rw-r--r-- | examples/button.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/button.rs b/examples/button.rs new file mode 100644 index 0000000..cfef3e3 --- /dev/null +++ b/examples/button.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | mod common; | ||
| 2 | |||
| 3 | use common::AsyncTcp; | ||
| 4 | use embassy_executor::{Executor, Spawner}; | ||
| 5 | use static_cell::StaticCell; | ||
| 6 | |||
| 7 | static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new(); | ||
| 8 | |||
| 9 | #[embassy_executor::task] | ||
| 10 | async fn main_task(spawner: Spawner) { | ||
| 11 | let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS")); | ||
| 12 | |||
| 13 | let mut device = embassy_ha::Device::new( | ||
| 14 | RESOURCES.init(Default::default()), | ||
| 15 | embassy_ha::DeviceConfig { | ||
| 16 | device_id: "example-device-id", | ||
| 17 | device_name: "Example Device Name", | ||
| 18 | manufacturer: "Example Device Manufacturer", | ||
| 19 | model: "Example Device Model", | ||
| 20 | }, | ||
| 21 | ); | ||
| 22 | |||
| 23 | let button = device.create_button("button-sensor-id", "Button Name"); | ||
| 24 | |||
| 25 | spawner.must_spawn(button_task(button)); | ||
| 26 | |||
| 27 | device.run(&mut stream).await; | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn button_task(mut button: embassy_ha::Button<'static>) { | ||
| 32 | loop { | ||
| 33 | button.pressed().await; | ||
| 34 | println!("The button has been pressed"); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | example_main!(); | ||
