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