-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructured the caterpillar example to make it cleaner
- Loading branch information
Showing
3 changed files
with
81 additions
and
75 deletions.
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,63 @@ | ||
use copper::clock::{OptionCuTime, RobotClock}; | ||
use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle}; | ||
use copper::CuResult; | ||
use cu_rp_gpio::RPGpioMsg; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct CaterpillarMsg(bool); | ||
|
||
pub struct CaterpillarSource { | ||
state: bool, | ||
} | ||
|
||
impl CuTaskLifecycle for CaterpillarSource { | ||
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
Ok(Self { state: true }) | ||
} | ||
} | ||
|
||
impl CuSrcTask for CaterpillarSource { | ||
type Output = RPGpioMsg; | ||
|
||
fn process(&mut self, clock: &RobotClock, output: &mut CuMsg<Self::Output>) -> CuResult<()> { | ||
// forward the state to the next task | ||
self.state = !self.state; | ||
output.payload = RPGpioMsg { | ||
on: self.state, | ||
creation: clock.now().into(), | ||
actuation: OptionCuTime::none(), | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct CaterpillarTask {} | ||
|
||
impl CuTaskLifecycle for CaterpillarTask { | ||
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
Ok(Self {}) | ||
} | ||
} | ||
|
||
impl CuTask for CaterpillarTask { | ||
type Input = RPGpioMsg; | ||
type Output = RPGpioMsg; | ||
|
||
fn process( | ||
&mut self, | ||
_clock: &RobotClock, | ||
input: &CuMsg<Self::Input>, | ||
output: &mut CuMsg<Self::Output>, | ||
) -> CuResult<()> { | ||
// forward the state to the next task | ||
output.payload = input.payload; | ||
Ok(()) | ||
} | ||
} |