diff options
| -rw-r--r-- | examples/constant-temperature.rs | 43 | ||||
| -rw-r--r-- | examples/temperature.rs (renamed from examples/random-temperature.rs) | 25 |
2 files changed, 20 insertions, 48 deletions
diff --git a/examples/constant-temperature.rs b/examples/constant-temperature.rs deleted file mode 100644 index fed76e5..0000000 --- a/examples/constant-temperature.rs +++ /dev/null | |||
| @@ -1,43 +0,0 @@ | |||
| 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 temperature_sensor = device.create_temperature_sensor( | ||
| 25 | "temperature-sensor-id", | ||
| 26 | "Temperature Sensor Name", | ||
| 27 | embassy_ha::TemperatureUnit::Celcius, | ||
| 28 | ); | ||
| 29 | |||
| 30 | spawner.must_spawn(temperature(temperature_sensor)); | ||
| 31 | |||
| 32 | device.run(&mut stream).await; | ||
| 33 | } | ||
| 34 | |||
| 35 | #[embassy_executor::task] | ||
| 36 | async fn temperature(mut sensor: embassy_ha::TemperatureSensor<'static>) { | ||
| 37 | loop { | ||
| 38 | sensor.publish(42.0); | ||
| 39 | Timer::after_secs(1).await; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | example_main!(); | ||
diff --git a/examples/random-temperature.rs b/examples/temperature.rs index 2bcae57..32f4887 100644 --- a/examples/random-temperature.rs +++ b/examples/temperature.rs | |||
| @@ -21,19 +21,34 @@ async fn main_task(spawner: Spawner) { | |||
| 21 | }, | 21 | }, |
| 22 | ); | 22 | ); |
| 23 | 23 | ||
| 24 | let temperature_sensor = device.create_temperature_sensor( | 24 | let constant_temperature_sensor = device.create_temperature_sensor( |
| 25 | "temperature-sensor-id", | 25 | "constant-temperature-sensor-id", |
| 26 | "Temperature Sensor Name", | 26 | "Constant Temperature Sensor", |
| 27 | embassy_ha::TemperatureUnit::Celcius, | 27 | embassy_ha::TemperatureUnit::Celcius, |
| 28 | ); | 28 | ); |
| 29 | 29 | ||
| 30 | spawner.must_spawn(temperature(temperature_sensor)); | 30 | let random_temperature_sensor = device.create_temperature_sensor( |
| 31 | "random-temperature-sensor-id", | ||
| 32 | "Random Temperature Sensor", | ||
| 33 | embassy_ha::TemperatureUnit::Celcius, | ||
| 34 | ); | ||
| 35 | |||
| 36 | spawner.must_spawn(constant_temperature_task(constant_temperature_sensor)); | ||
| 37 | spawner.must_spawn(random_temperature_task(random_temperature_sensor)); | ||
| 31 | 38 | ||
| 32 | device.run(&mut stream).await; | 39 | device.run(&mut stream).await; |
| 33 | } | 40 | } |
| 34 | 41 | ||
| 35 | #[embassy_executor::task] | 42 | #[embassy_executor::task] |
| 36 | async fn temperature(mut sensor: embassy_ha::TemperatureSensor<'static>) { | 43 | async fn constant_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { |
| 44 | loop { | ||
| 45 | sensor.publish(42.0); | ||
| 46 | Timer::after_secs(1).await; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | #[embassy_executor::task] | ||
| 51 | async fn random_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { | ||
| 37 | loop { | 52 | loop { |
| 38 | sensor.publish(rand::random_range(0.0..50.0)); | 53 | sensor.publish(rand::random_range(0.0..50.0)); |
| 39 | Timer::after_secs(1).await; | 54 | Timer::after_secs(1).await; |
