-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
actor API #537
Open
japaric
wants to merge
9
commits into
master
Choose a base branch
from
actor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
actor API #537
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a58be57
actor API
japaric 11694e1
add split-crate example
japaric 7e749ed
add output to actor-watermark example
japaric 58eef6b
add expected .run files
japaric 3fdc018
bump deps of firmware example
japaric 5e7481f
xtask: hard-enable the memory-watermark feature
japaric 61c6435
add changelog entry
japaric 5a3f275
specify the features that the actor-watermark example requires
japaric d1145a1
use FakeTemperatureSensor as an actor
japaric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Should live in rtic-rs/rtic-examples repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "actors" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies.rtic-actor-traits] | ||
path = "../../actor-traits" | ||
|
||
[dev-dependencies.rtic-post-spy] | ||
path = "../../post-spy" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use rtic_actor_traits::{Post, Receive}; | ||
|
||
use crate::{DoTemperatureRead, TemperatureReadingCelsius}; | ||
|
||
pub struct FakeTemperatureSensor<P> | ||
where | ||
P: Post<TemperatureReadingCelsius>, | ||
{ | ||
delta: i32, | ||
outbox: P, | ||
temperature: i32, | ||
} | ||
|
||
// a real temperature sensor would use the embedded-hal traits (e.g. I2C) or some higher level trait | ||
impl<P> FakeTemperatureSensor<P> | ||
where | ||
P: Post<TemperatureReadingCelsius>, | ||
{ | ||
pub fn new(outbox: P, initial_temperature: i32, delta: i32) -> Self { | ||
Self { | ||
delta, | ||
outbox, | ||
temperature: initial_temperature, | ||
} | ||
} | ||
} | ||
|
||
impl<P> Receive<DoTemperatureRead> for FakeTemperatureSensor<P> | ||
where | ||
P: Post<TemperatureReadingCelsius>, | ||
{ | ||
fn receive(&mut self, _: DoTemperatureRead) { | ||
self.outbox | ||
.post(TemperatureReadingCelsius(self.temperature)) | ||
.expect("OOM"); | ||
self.temperature += self.delta; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rtic_post_spy::PostSpy; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn on_read_it_posts_reading() { | ||
let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), 0, 0); | ||
|
||
// manually send a message | ||
let message = DoTemperatureRead; | ||
sensor.receive(message); | ||
|
||
let spy = sensor.outbox; | ||
let posted_messages = spy.posted_messages::<TemperatureReadingCelsius>(); | ||
assert_eq!(1, posted_messages.count()); | ||
} | ||
|
||
#[test] | ||
fn reading_starts_at_initial_temperature() { | ||
let initial_temperature = 1; | ||
let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), initial_temperature, 0); | ||
|
||
// manually send a message | ||
let message = DoTemperatureRead; | ||
sensor.receive(message); | ||
|
||
let spy = sensor.outbox; | ||
let mut posted_messages = spy.posted_messages::<TemperatureReadingCelsius>(); | ||
assert_eq!( | ||
Some(&TemperatureReadingCelsius(initial_temperature)), | ||
posted_messages.next() | ||
); | ||
} | ||
|
||
#[test] | ||
fn reading_changes_by_delta() { | ||
let initial_temperature = 42; | ||
let delta = 1; | ||
let mut sensor = FakeTemperatureSensor::new(PostSpy::default(), initial_temperature, delta); | ||
|
||
// manually send a message | ||
let message = DoTemperatureRead; | ||
sensor.receive(message); | ||
sensor.receive(message); | ||
|
||
let spy = sensor.outbox; | ||
let mut posted_messages = spy.posted_messages::<TemperatureReadingCelsius>(); | ||
assert_eq!( | ||
Some(&TemperatureReadingCelsius(initial_temperature)), | ||
posted_messages.next() | ||
); | ||
assert_eq!( | ||
Some(&TemperatureReadingCelsius(initial_temperature + delta)), | ||
posted_messages.next() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![no_std] | ||
|
||
mod fake_temperature_sensor; | ||
mod temperature_monitor; | ||
|
||
// Actors | ||
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)] | ||
pub struct TemperatureReadingCelsius(pub i32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use rtic_actor_traits::{Post, Receive}; | ||
|
||
use crate::{TemperatureAlert, TemperatureReadingCelsius}; | ||
|
||
pub struct TemperatureMonitor<P> | ||
where | ||
P: Post<TemperatureAlert>, | ||
{ | ||
outbox: P, | ||
threshold: i32, | ||
} | ||
|
||
impl<P> TemperatureMonitor<P> | ||
where | ||
P: Post<TemperatureAlert>, | ||
{ | ||
pub fn new(outbox: P, threshold: i32) -> Self { | ||
Self { outbox, threshold } | ||
} | ||
} | ||
|
||
impl<P> Receive<TemperatureReadingCelsius> for TemperatureMonitor<P> | ||
where | ||
P: Post<TemperatureAlert>, | ||
{ | ||
fn receive(&mut self, temperature: TemperatureReadingCelsius) { | ||
if temperature.0 >= self.threshold { | ||
self.outbox.post(TemperatureAlert).ok().expect("OOM"); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rtic_post_spy::PostSpy; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn when_temperature_is_above_threshold_it_posts_alert_once() { | ||
let mut monitor = TemperatureMonitor::new(PostSpy::default(), 0); | ||
|
||
// manually send a message | ||
let message = TemperatureReadingCelsius(1); | ||
monitor.receive(message); | ||
|
||
let spy = monitor.outbox; | ||
let posted_messages = spy.posted_messages::<TemperatureAlert>(); | ||
assert_eq!(1, posted_messages.count()); | ||
} | ||
|
||
#[test] | ||
fn when_temperature_is_below_threshold_it_does_not_post_alert() { | ||
let mut monitor = TemperatureMonitor::new(PostSpy::default(), 0); | ||
|
||
let message = TemperatureReadingCelsius(-1); | ||
monitor.receive(message); | ||
|
||
let spy = monitor.outbox; | ||
let posted_messages = spy.posted_messages::<TemperatureAlert>(); | ||
assert_eq!(0, posted_messages.count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
runner = "probe-run --chip nrf52840" | ||
rustflags = [ | ||
"-C", "linker=flip-link", | ||
"-C", "link-arg=-Tdefmt.x", | ||
"-C", "link-arg=--nmagic", | ||
] | ||
|
||
[build] | ||
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) | ||
|
||
[alias] | ||
rb = "run --bin" | ||
rrb = "run --release --bin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
[package] | ||
name = "firmware" | ||
edition = "2018" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
actors = { path = "../actors" } | ||
cortex-m = "0.7.1" | ||
cortex-m-rt = "0.6.13" | ||
cortex-m-rtic = { path = "../.." } | ||
defmt = "0.3.0" | ||
defmt-rtt = "0.3.1" | ||
nrf52840-hal = "0.12.2" | ||
panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
rtic-actor-traits = { path = "../../actor-traits" } | ||
systick-monotonic = "1" | ||
|
||
[features] | ||
# set logging levels here | ||
default = [ | ||
"defmt-default", | ||
# "dependency-a/defmt-trace", | ||
] | ||
|
||
# do NOT modify these features | ||
defmt-default = [] | ||
defmt-trace = [] | ||
defmt-debug = [] | ||
defmt-info = [] | ||
defmt-warn = [] | ||
defmt-error = [] | ||
|
||
# cargo build/run | ||
[profile.dev] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = true # <- | ||
incremental = false | ||
opt-level = 3 # <- | ||
overflow-checks = true # <- | ||
|
||
# cargo test | ||
[profile.test] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = true # <- | ||
incremental = false | ||
opt-level = 3 # <- | ||
overflow-checks = true # <- | ||
|
||
# cargo build/run --release | ||
[profile.release] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = false # <- | ||
incremental = false | ||
lto = 'fat' | ||
opt-level = 3 # <- | ||
overflow-checks = false # <- | ||
|
||
# cargo test --release | ||
[profile.bench] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = false # <- | ||
incremental = false | ||
lto = 'fat' | ||
opt-level = 3 # <- | ||
overflow-checks = false # <- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
ok()
called here when you could call expect directly on the return frompost()
i.e.self.outbox.post(TemperatureAlert).expect("OOM");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doing
expect
on theResult<T, E>
value requires thatE
implements thefmt::Debug
trait. doingok().expect()
places no trait requirements on the typeE
.TemperatureAlert
does not implement thefmt::Debug
trait sopost(TemperatureAlert).expect()
would not compile.deriving
Debug
on the type and leaving out theok()
is also an option (no pun intended). I tend to writeok().expect()
mostly out of bad habit. (lessResult::unwrap
calls produces smaller (code size) binaries but that's micro-optimizing and this is an example so that hardly matters; as I said: bad habit)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, ok makes sense. Thanks for the reply, I was mainly just curious more than critiquing