From d1145a138fbe156ef865df0a9383565180f6d5b8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 7 Jun 2022 11:51:59 +0200 Subject: [PATCH] use FakeTemperatureSensor as an actor --- .../actors/src/fake_temperature_sensor.rs | 28 ++++++++++++++----- actor-example/actors/src/lib.rs | 3 ++ .../firmware/src/bin/temperature-monitor.rs | 17 +++++++---- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/actor-example/actors/src/fake_temperature_sensor.rs b/actor-example/actors/src/fake_temperature_sensor.rs index 4b4db0530d..b34455ce2a 100644 --- a/actor-example/actors/src/fake_temperature_sensor.rs +++ b/actor-example/actors/src/fake_temperature_sensor.rs @@ -1,6 +1,6 @@ -use rtic_actor_traits::Post; +use rtic_actor_traits::{Post, Receive}; -use crate::TemperatureReadingCelsius; +use crate::{DoTemperatureRead, TemperatureReadingCelsius}; pub struct FakeTemperatureSensor

where @@ -23,8 +23,13 @@ where temperature: initial_temperature, } } +} - pub fn read(&mut self) { +impl

Receive for FakeTemperatureSensor

+where + P: Post, +{ + fn receive(&mut self, _: DoTemperatureRead) { self.outbox .post(TemperatureReadingCelsius(self.temperature)) .expect("OOM"); @@ -41,7 +46,10 @@ mod tests { #[test] fn on_read_it_posts_reading() { let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), 0, 0); - sensor.read(); + + // manually send a message + let message = DoTemperatureRead; + sensor.receive(message); let spy = sensor.outbox; let posted_messages = spy.posted_messages::(); @@ -52,7 +60,10 @@ mod tests { fn reading_starts_at_initial_temperature() { let initial_temperature = 1; let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), initial_temperature, 0); - sensor.read(); + + // manually send a message + let message = DoTemperatureRead; + sensor.receive(message); let spy = sensor.outbox; let mut posted_messages = spy.posted_messages::(); @@ -67,8 +78,11 @@ mod tests { let initial_temperature = 42; let delta = 1; let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), initial_temperature, delta); - sensor.read(); - sensor.read(); + + // manually send a message + let message = DoTemperatureRead; + sensor.receive(message); + sensor.receive(message); let spy = sensor.outbox; let mut posted_messages = spy.posted_messages::(); diff --git a/actor-example/actors/src/lib.rs b/actor-example/actors/src/lib.rs index dae2c619e5..928f986a9e 100644 --- a/actor-example/actors/src/lib.rs +++ b/actor-example/actors/src/lib.rs @@ -8,6 +8,9 @@ pub use fake_temperature_sensor::FakeTemperatureSensor; pub use temperature_monitor::TemperatureMonitor; // Messages +#[derive(Clone, Copy, Debug)] +pub struct DoTemperatureRead; + pub struct TemperatureAlert; #[derive(Clone, Debug, PartialEq)] diff --git a/actor-example/firmware/src/bin/temperature-monitor.rs b/actor-example/firmware/src/bin/temperature-monitor.rs index 6636d5828e..8e9b761369 100644 --- a/actor-example/firmware/src/bin/temperature-monitor.rs +++ b/actor-example/firmware/src/bin/temperature-monitor.rs @@ -6,7 +6,8 @@ use firmware as _; #[rtic::app(device = nrf52840_hal::pac, dispatchers = [RADIO])] mod app { use actors::{ - FakeTemperatureSensor, TemperatureAlert, TemperatureMonitor, TemperatureReadingCelsius, + DoTemperatureRead, FakeTemperatureSensor, TemperatureAlert, TemperatureMonitor, + TemperatureReadingCelsius, }; use rtic_actor_traits::Receive; use systick_monotonic::*; @@ -36,6 +37,9 @@ mod app { #[actors] struct Actors { + #[subscribe(DoTemperatureRead)] + temperature_sensor: FakeTemperatureSensor, + #[init(AlertHandler)] #[subscribe(TemperatureAlert)] alert_handler: AlertHandler, @@ -50,7 +54,7 @@ mod app { #[local] struct Local { - temperature_sensor: FakeTemperatureSensor, + poster: Poster, } #[monotonic(binds = SysTick, default = true)] @@ -71,17 +75,20 @@ mod app { ( Shared {}, - Local { temperature_sensor }, + Local { poster }, init::Monotonics(mono), Actors { temperature_monitor, + temperature_sensor, }, ) } - #[task(local = [temperature_sensor])] + #[task(local = [poster])] fn periodic(cx: periodic::Context) { - cx.local.temperature_sensor.read(); + // input to the actor network + cx.local.poster.post(DoTemperatureRead).expect("OOM"); + periodic::spawn_after(1.secs()).expect("OOM"); }