Skip to content

Commit 178174a

Browse files
authored
Removes the lifecycle trait (copper-project#115)
* Removes the lifecycle trait It split artificially the implementation of tasks and couples the passed types. * Fixedd comments + template * Another pass on removing the lifecycle trait.
1 parent 13c79fe commit 178174a

File tree

26 files changed

+354
-386
lines changed

26 files changed

+354
-386
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -173,33 +173,33 @@ pub struct FlippingSource {
173173
state: bool,
174174
}
175175
176-
// You need to provide at least "new". But you have other hooks in to the Lifecycle you can leverage
177-
// to maximize your opportunity to not use resources outside of the critical execution path: for example start, stop,
178-
// pre_process, post_process etc...
179-
impl CuTaskLifecycle for FlippingSource {
180-
fn new(_config: Option<&copper::config::ComponentConfig>) -> CuResult<Self>
176+
// We implement the CuSrcTask trait for our task as it is a source / driver (with no internal input from Copper itself).
177+
impl<'cl> CuSrcTask<'cl> for FlippingSource {
178+
type Output = output_msg!('cl, RPGpioPayload);
179+
180+
// You need to provide at least "new" out of the lifecycle methods.
181+
// But you have other hooks in to the Lifecycle you can leverage to maximize your opportunity
182+
// to not use resources outside of the critical execution path: for example start, stop,
183+
// pre_process, post_process etc...
184+
fn new(config: Option<&copper::config::ComponentConfig>) -> CuResult<Self>
181185
where
182186
Self: Sized,
183187
{
188+
// the config is passed from the RON config file as a Map.
184189
Ok(Self { state: true })
185190
}
186-
}
187-
188-
// We implement the CuSrcTask trait for our task as it is a source / driver (with no internal input from Copper itself).
189-
impl<'cl> CuSrcTask<'cl> for FlippingSource {
190-
type Output = output_msg!('cl, RPGpioPayload);
191-
191+
192192
// Process is called by the runtime at each cycle. It will give:
193193
// 1. the reference to a monotonic clock
194194
// 2. a mutable reference to the output message (so no need to allocate of copy anything)
195195
// 3. a CuResult to handle errors
196196
fn process(&mut self, clock: &RobotClock, output: Self::Output) -> CuResult<()> {
197197
self.state = !self.state; // Flip our internal state and send the message in our output.
198-
output.payload = RPGpioPayload {
198+
output.set_payload(RPGpioPayload {
199199
on: self.state,
200200
creation: Some(clock.now()).into(),
201201
actuation: Some(clock.now()).into(),
202-
};
202+
});
203203
Ok(())
204204
}
205205
}

components/sinks/cu_iceoryx2_sink/src/lib.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cu29::clock::RobotClock;
22
use cu29::config::ComponentConfig;
33
use cu29::cutask::{CuMsg, CuMsgPayload};
4-
use cu29::cutask::{CuSinkTask, CuTaskLifecycle, Freezable};
4+
use cu29::cutask::{CuSinkTask, Freezable};
55
use cu29::input_msg;
66
use cu29_log_derive::debug;
77
use cu29_traits::{CuError, CuResult};
@@ -25,10 +25,12 @@ where
2525

2626
impl<P> Freezable for IceoryxSink<P> where P: CuMsgPayload {}
2727

28-
impl<P> CuTaskLifecycle for IceoryxSink<P>
28+
impl<'cl, P> CuSinkTask<'cl> for IceoryxSink<P>
2929
where
30-
P: CuMsgPayload,
30+
P: CuMsgPayload + 'cl,
3131
{
32+
type Input = input_msg!('cl, P);
33+
3234
fn new(config: Option<&ComponentConfig>) -> CuResult<Self>
3335
where
3436
Self: Sized,
@@ -72,18 +74,6 @@ where
7274
Ok(())
7375
}
7476

75-
fn stop(&mut self, _clock: &RobotClock) -> CuResult<()> {
76-
self.publisher = None;
77-
Ok(())
78-
}
79-
}
80-
81-
impl<'cl, P> CuSinkTask<'cl> for IceoryxSink<P>
82-
where
83-
P: CuMsgPayload + 'cl,
84-
{
85-
type Input = input_msg!('cl, P);
86-
8777
fn process(&mut self, _clock: &RobotClock, input: Self::Input) -> CuResult<()> {
8878
let publisher = self
8979
.publisher
@@ -101,4 +91,9 @@ where
10191

10292
Ok(())
10393
}
94+
95+
fn stop(&mut self, _clock: &RobotClock) -> CuResult<()> {
96+
self.publisher = None;
97+
Ok(())
98+
}
10499
}

components/sinks/cu_lewansoul/src/lib.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bincode::error::{DecodeError, EncodeError};
44
use bincode::{Decode, Encode};
55
use cu29::clock::RobotClock;
66
use cu29::config::ComponentConfig;
7-
use cu29::cutask::{CuMsg, CuSinkTask, CuTaskLifecycle, Freezable};
7+
use cu29::cutask::{CuMsg, CuSinkTask, Freezable};
88
use cu29::{input_msg, CuError, CuResult};
99
use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits};
1010
use std::io::{self, Read, Write};
@@ -178,7 +178,29 @@ impl Freezable for Lewansoul {
178178
// This driver is stateless as the IDs are recreate at new time, we keep the default implementation.
179179
}
180180

181-
impl CuTaskLifecycle for Lewansoul {
181+
#[derive(Debug, Clone, Default)]
182+
pub struct ServoPositionsPayload {
183+
pub positions: [Angle; MAX_SERVOS],
184+
}
185+
186+
impl Encode for ServoPositionsPayload {
187+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
188+
let angles: [f32; MAX_SERVOS] = self.positions.map(|a| a.value);
189+
angles.encode(encoder)
190+
}
191+
}
192+
193+
impl Decode for ServoPositionsPayload {
194+
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
195+
let angles: [f32; 8] = Decode::decode(decoder)?;
196+
let positions: [Angle; 8] = angles.map(Angle::new::<radian>);
197+
Ok(ServoPositionsPayload { positions })
198+
}
199+
}
200+
201+
impl<'cl> CuSinkTask<'cl> for Lewansoul {
202+
type Input = input_msg!('cl, ServoPositionsPayload);
203+
182204
fn new(config: Option<&ComponentConfig>) -> CuResult<Self>
183205
where
184206
Self: Sized,
@@ -220,30 +242,6 @@ impl CuTaskLifecycle for Lewansoul {
220242

221243
Ok(Lewansoul { port, ids })
222244
}
223-
}
224-
225-
#[derive(Debug, Clone, Default)]
226-
pub struct ServoPositionsPayload {
227-
pub positions: [Angle; MAX_SERVOS],
228-
}
229-
230-
impl Encode for ServoPositionsPayload {
231-
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
232-
let angles: [f32; MAX_SERVOS] = self.positions.map(|a| a.value);
233-
angles.encode(encoder)
234-
}
235-
}
236-
237-
impl Decode for ServoPositionsPayload {
238-
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
239-
let angles: [f32; 8] = Decode::decode(decoder)?;
240-
let positions: [Angle; 8] = angles.map(Angle::new::<radian>);
241-
Ok(ServoPositionsPayload { positions })
242-
}
243-
}
244-
245-
impl<'cl> CuSinkTask<'cl> for Lewansoul {
246-
type Input = input_msg!('cl, ServoPositionsPayload);
247245

248246
fn process(&mut self, _clock: &RobotClock, _input: Self::Input) -> CuResult<()> {
249247
todo!()

components/sinks/cu_rp_gpio/src/lib.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use bincode::{Decode, Encode};
22
use cu29::config::ComponentConfig;
3-
use cu29::cutask::{CuMsg, CuSinkTask, CuTaskLifecycle, Freezable};
3+
use cu29::cutask::{CuMsg, CuSinkTask, Freezable};
4+
use cu29::input_msg;
45
use cu29::CuResult;
5-
use cu29::{clock, input_msg};
66
use serde::{Deserialize, Serialize};
77

88
#[cfg(mock)]
99
use cu29_log_derive::debug;
1010

11+
use cu29::clock::RobotClock;
1112
#[cfg(hardware)]
1213
use {
1314
cu29::CuError,
@@ -64,7 +65,9 @@ impl From<RPGpioPayload> for Level {
6465

6566
impl Freezable for RPGpio {}
6667

67-
impl CuTaskLifecycle for RPGpio {
68+
impl<'cl> CuSinkTask<'cl> for RPGpio {
69+
type Input = input_msg!('cl, RPGpioPayload);
70+
6871
fn new(config: Option<&ComponentConfig>) -> CuResult<Self>
6972
where
7073
Self: Sized,
@@ -86,12 +89,8 @@ impl CuTaskLifecycle for RPGpio {
8689
let pin = pin_nb;
8790
Ok(Self { pin })
8891
}
89-
}
90-
91-
impl<'cl> CuSinkTask<'cl> for RPGpio {
92-
type Input = input_msg!('cl, RPGpioPayload);
9392

94-
fn process(&mut self, _clock: &clock::RobotClock, msg: Self::Input) -> CuResult<()> {
93+
fn process(&mut self, _clock: &RobotClock, msg: Self::Input) -> CuResult<()> {
9594
#[cfg(hardware)]
9695
self.pin.write((*msg.payload().unwrap()).into());
9796

components/sinks/cu_rp_sn754410/src/lib.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bincode::error::{DecodeError, EncodeError};
44
use bincode::{Decode, Encode};
55
use cu29::clock::{CuTime, RobotClock};
66
use cu29::config::ComponentConfig;
7-
use cu29::cutask::{CuMsg, CuSinkTask, CuTaskLifecycle, Freezable};
7+
use cu29::cutask::{CuMsg, CuSinkTask, Freezable};
88
use cu29::{input_msg, CuResult};
99
use serde::{Deserialize, Serialize};
1010

@@ -126,7 +126,19 @@ impl SN754410 {
126126
}
127127
}
128128

129-
impl CuTaskLifecycle for SN754410 {
129+
impl Freezable for SN754410 {
130+
fn freeze<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
131+
Encode::encode(&self.current_power, encoder)
132+
}
133+
134+
fn thaw<D: Decoder>(&mut self, decoder: &mut D) -> Result<(), DecodeError> {
135+
self.current_power = Decode::decode(decoder)?;
136+
Ok(())
137+
}
138+
}
139+
140+
impl<'cl> CuSinkTask<'cl> for SN754410 {
141+
type Input = input_msg!('cl, MotorPayload);
130142
fn new(config: Option<&ComponentConfig>) -> CuResult<Self>
131143
where
132144
Self: Sized,
@@ -162,26 +174,6 @@ impl CuTaskLifecycle for SN754410 {
162174
debug!("Enabling SN754410.");
163175
self.enable_pwms()
164176
}
165-
fn stop(&mut self, _clock: &RobotClock) -> CuResult<()> {
166-
debug!("Disabling SN754410.");
167-
self.disable_pwms()
168-
}
169-
}
170-
171-
impl Freezable for SN754410 {
172-
fn freeze<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
173-
Encode::encode(&self.current_power, encoder)
174-
}
175-
176-
fn thaw<D: Decoder>(&mut self, decoder: &mut D) -> Result<(), DecodeError> {
177-
self.current_power = Decode::decode(decoder)?;
178-
Ok(())
179-
}
180-
}
181-
182-
impl<'cl> CuSinkTask<'cl> for SN754410 {
183-
type Input = input_msg!('cl, MotorPayload);
184-
185177
fn process(&mut self, clock: &RobotClock, input: Self::Input) -> CuResult<()> {
186178
if let Some(power) = input.payload() {
187179
if self.dryrun {
@@ -224,27 +216,30 @@ impl<'cl> CuSinkTask<'cl> for SN754410 {
224216
}
225217
Ok(())
226218
}
219+
220+
fn stop(&mut self, _clock: &RobotClock) -> CuResult<()> {
221+
debug!("Disabling SN754410.");
222+
self.disable_pwms()
223+
}
227224
}
228225

229226
pub mod test_support {
230227
use crate::MotorPayload;
231228
use cu29::clock::RobotClock;
232229
use cu29::config::ComponentConfig;
233-
use cu29::cutask::{CuMsg, CuSrcTask, CuTaskLifecycle, Freezable};
230+
use cu29::cutask::{CuMsg, CuSrcTask, Freezable};
234231
use cu29::{output_msg, CuResult};
235232

236233
pub struct SN754410TestSrc;
237234

238235
impl Freezable for SN754410TestSrc {}
239236

240-
impl CuTaskLifecycle for SN754410TestSrc {
237+
impl<'cl> CuSrcTask<'cl> for SN754410TestSrc {
238+
type Output = output_msg!('cl, MotorPayload);
239+
241240
fn new(_config: Option<&ComponentConfig>) -> CuResult<Self> {
242241
Ok(Self {})
243242
}
244-
}
245-
246-
impl<'cl> CuSrcTask<'cl> for SN754410TestSrc {
247-
type Output = output_msg!('cl, MotorPayload);
248243

249244
fn process(&mut self, _clock: &RobotClock, _new_msg: Self::Output) -> CuResult<()> {
250245
todo!()

components/sinks/cu_rp_sn754410/tests/sn754410_tester.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cu29::cutask::{CuSinkTask, CuTaskLifecycle};
1+
use cu29::cutask::CuSinkTask;
22
use cu29_derive::copper_runtime;
33
use cu29_helpers::basic_copper_setup;
44
use cu29_log_derive::debug;

0 commit comments

Comments
 (0)