diff --git a/examples/cu_caterpillar/copperconfig.ron b/examples/cu_caterpillar/copperconfig.ron index cfb8613e5..6748cc271 100644 --- a/examples/cu_caterpillar/copperconfig.ron +++ b/examples/cu_caterpillar/copperconfig.ron @@ -3,12 +3,12 @@ tasks: [ ( id: "src", - type: "CaterpillarSource", + type: "tasks::CaterpillarSource", base_period_ns: 1000000000, ), ( id: "ct-0", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -21,7 +21,7 @@ ), ( id: "ct-1", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -34,7 +34,7 @@ ), ( id: "ct-2", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -47,7 +47,7 @@ ), ( id: "ct-3", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -60,7 +60,7 @@ ), ( id: "ct-4", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -73,7 +73,7 @@ ), ( id: "ct-5", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -86,7 +86,7 @@ ), ( id: "ct-6", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( @@ -99,7 +99,7 @@ ), ( id: "ct-7", - type: "CaterpillarTask", + type: "tasks::CaterpillarTask", base_period_ns: 1000000000, ), ( diff --git a/examples/cu_caterpillar/src/main.rs b/examples/cu_caterpillar/src/main.rs index ecf4e378a..5aa95ae79 100644 --- a/examples/cu_caterpillar/src/main.rs +++ b/examples/cu_caterpillar/src/main.rs @@ -1,84 +1,27 @@ -use copper::clock::{ClockProvider, OptionCuTime, RobotClock}; -use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle}; -use copper::CuResult; +pub mod tasks; + +use copper::clock::ClockProvider; +use copper::clock::CuDuration; +use copper::clock::CuTime; use copper_derive::copper_runtime; use copper_helpers::basic_logger_runtime_setup; use copper_log_derive::debug; -use cu_rp_gpio::RPGpioMsg; -use serde::{Deserialize, Serialize}; use std::path::PathBuf; use std::thread::sleep; use std::time::Duration; #[copper_runtime(config = "copperconfig.ron")] -struct TheVeryHungryCaterpillar {} - -#[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 - where - Self: Sized, - { - Ok(Self { state: true }) - } -} - -impl CuSrcTask for CaterpillarSource { - type Output = RPGpioMsg; - - fn process(&mut self, clock: &RobotClock, output: &mut CuMsg) -> 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 - where - Self: Sized, - { - Ok(Self {}) - } -} - -impl CuTask for CaterpillarTask { - type Input = RPGpioMsg; - type Output = RPGpioMsg; - - fn process( - &mut self, - _clock: &RobotClock, - input: &CuMsg, - output: &mut CuMsg, - ) -> CuResult<()> { - // forward the state to the next task - output.payload = input.payload; - Ok(()) - } -} +struct CaterpillarApplication {} fn main() { let logger_runtime = basic_logger_runtime_setup(&PathBuf::from("/tmp/caterpillar.copper"), true) .expect("Failed to setup logger."); + debug!("Logger created."); let clock = logger_runtime.get_clock(); - debug!("Application created."); + debug!("Creating application... "); let mut application = - TheVeryHungryCaterpillar::new(clock.clone()).expect("Failed to create runtime."); + CaterpillarApplication::new(clock.clone()).expect("Failed to create runtime."); debug!("Running... starting clock: {}.", clock.now()); application.run(2).expect("Failed to run application."); debug!("End of program: {}.", clock.now()); diff --git a/examples/cu_caterpillar/src/tasks.rs b/examples/cu_caterpillar/src/tasks.rs new file mode 100644 index 000000000..d3355b9b5 --- /dev/null +++ b/examples/cu_caterpillar/src/tasks.rs @@ -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 + where + Self: Sized, + { + Ok(Self { state: true }) + } +} + +impl CuSrcTask for CaterpillarSource { + type Output = RPGpioMsg; + + fn process(&mut self, clock: &RobotClock, output: &mut CuMsg) -> 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 + where + Self: Sized, + { + Ok(Self {}) + } +} + +impl CuTask for CaterpillarTask { + type Input = RPGpioMsg; + type Output = RPGpioMsg; + + fn process( + &mut self, + _clock: &RobotClock, + input: &CuMsg, + output: &mut CuMsg, + ) -> CuResult<()> { + // forward the state to the next task + output.payload = input.payload; + Ok(()) + } +}