Firmware Controller
This crate provides a macro named controller that makes it easy to decouple interactions between
components in a no_std environment.
Intro
This crate provides a macro named controller that makes it easy to write controller logic for
firmware.
The controller is responsible for control of all the peripherals based on commands it receives from other parts of the code. It also notifies peers about state changes and events via signals. This macro generates all the boilerplate code and client-side API for you.
Usage
It's best described by an example so let's take example of a very simple firmware that controls an LED:
use controller;
// The controller struct. This is where you define the state of your firmware.
// The controller implementation. This is where you define the logic of your firmware.
async
// This is just a very silly client that keeps flipping the power state every 1 second.
async
Details
The controller macro will generated the following for you:
- A
newmethod that takes the fields of the struct as arguments and returns the struct. - For each
publishedfield:- Setter for this field, named
set_<field-name>(soset_statehere), which broadcasts any changes made to this field. - Two client-side types:
- struct named
<struct-name><field-name-in-pascal-case>Changed(soControllerStateChangedforstatefield), containing two public fields, namedpreviousandnewfields representing the previous and new values of the field, respectively. - Type named
<struct-name><field-name-in-pascal-case>(soControllerStateforstatefield), which implementsfutures::Stream, yielding each state change as the change struct described above.
- struct named
- Setter for this field, named
runmethod with signaturepub async fn run(&mut self);which runs the controller logic, proxying calls from the client to the implementations here and their return value back to the clients (internally via channels). Typically you'd call it at the end of yourmainor run it as a task.- Client-side API for this struct, named
<struct-name>Client(ControllerClienthere) which provides exactly the same methods (except signal methods) defined in this implementation that other parts of the code use to call these methods. - For each
signalmethod:- The method body, that broadcasts the signal to all the clients that are listening to it.
- Two client-side types:
- struct, named
<struct-name><method-name-in-pascal-case>Args(ControllerPowerErrorArgshere), containing all the arguments of this method, as public fields. - Type named
<struct-name><method-name-in-pascal-case>(ControllerPowerErrorhere) which implementsfutures::Stream, yielding each signal broadcasted as the args struct described above.
- struct, named
Dependencies assumed
The controller macro assumes that you have the following dependencies in your Cargo.toml:
futureswithasync-awaitfeature enabled.embassy-sync
Known limitations & Caveats
- Currently only works as a singleton: you can create multiple instances of the controller but if you run them simultaneously, they'll interfere with each others' operation. We hope to remove this limitation in the future. Having said that, most firmware applications will only need a single controller instance.
- Method args/return type can't be reference types.
- Methods must be async.
- The maximum number of subscribers state change and signal streams is 16. We plan to provide an attribute to make this configurable in the future.
- The type of all published fields must implement
CloneandDebug. - The signal and published fields' streams must be continuely polled. Otherwise notifications will be missed.