use cu29::prelude::*;
use cu29_helpers::basic_copper_setup;
use std::fs;
use std::path::{Path, PathBuf};
pub mod tasks {
use cu29::prelude::*;
use std::thread::sleep;
pub struct ExampleSrc {}
impl Freezable for ExampleSrc {}
impl CuSrcTask for ExampleSrc {
type Output<'m> = output_msg!(i32);
fn new(_config: Option<&ComponentConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self {})
}
fn process(&mut self, _clock: &RobotClock, new_msg: &mut Self::Output<'_>) -> CuResult<()> {
new_msg.set_payload(42);
Ok(())
}
}
pub struct ExampleTask {
sleep_duration_ms: u64,
}
impl Freezable for ExampleTask {}
impl CuTask for ExampleTask {
type Input<'m> = input_msg!(i32);
type Output<'m> = output_msg!(i32);
fn new(config: Option<&ComponentConfig>) -> CuResult<Self>
where
Self: Sized,
{
let sleep_duration_ms: u64 = config.unwrap().get("sleep_duration_ms").unwrap();
Ok(Self { sleep_duration_ms })
}
fn process(
&mut self,
_clock: &RobotClock,
input: &Self::Input<'_>,
output: &mut Self::Output<'_>,
) -> CuResult<()> {
let Some(payload) = input.payload() else {
return Ok(());
};
debug!(
"Task is tasking a {}ms time to process input: {}",
self.sleep_duration_ms, &payload
);
sleep(std::time::Duration::from_millis(self.sleep_duration_ms));
debug!("Task ({}ms) done.", self.sleep_duration_ms);
output.set_payload(payload + 1);
Ok(())
}
}
pub struct ExampleSink {}
impl Freezable for ExampleSink {}
impl CuSinkTask for ExampleSink {
type Input<'m> = input_msg!(i32);
fn new(_config: Option<&ComponentConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self {})
}
fn process(&mut self, _clock: &RobotClock, _input: &Self::Input<'_>) -> CuResult<()> {
Ok(())
}
}
}
#[copper_runtime(config = "copperconfig.ron")]
struct App {}
const SLAB_SIZE: Option<usize> = Some(150 * 1024 * 1024);
fn main() {
let logger_path = "logs/background.copper";
if let Some(parent) = Path::new(logger_path).parent() {
if !parent.exists() {
fs::create_dir_all(parent).expect("Failed to create logs directory");
}
}
let copper_ctx = basic_copper_setup(&PathBuf::from(logger_path), SLAB_SIZE, true, None)
.expect("Failed to setup logger.");
debug!("Logger created at {}.", path = &logger_path);
let clock = copper_ctx.clock;
debug!("Creating application... ");
let mut application = App::new(clock.clone(), copper_ctx.unified_logger.clone(), None)
.expect("Failed to create application.");
debug!("Running... starting clock: {}.", clock.now());
application
.start_all_tasks()
.expect("Failed to start application.");
application.run().expect("Failed to run application.");
application
.stop_all_tasks()
.expect("Failed to stop application.");
debug!("End of program: {}.", clock.now());
}