From 9993b102a9b39b0339636f062abbb2d22735de9c Mon Sep 17 00:00:00 2001 From: Geoff Duddridge Date: Wed, 9 Oct 2024 04:40:14 +0000 Subject: [PATCH] add coop struct and shell fn's --- .devcontainer/devcontainer.json | 4 +- Cargo.toml | 2 + src/coop.rs | 43 ++++++++++++++++++ src/{ => coop}/chicken.rs | 19 ++++---- src/main.rs | 78 ++++++++++++++++++++++----------- 5 files changed, 110 insertions(+), 36 deletions(-) create mode 100644 src/coop.rs rename src/{ => coop}/chicken.rs (90%) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 46b1894..4a54e57 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -22,11 +22,11 @@ "rust-lang.rust-analyzer", "mhutchie.git-graph", "albert.TabOut", - "serayuzgur.crates", "yzhang.markdown-all-in-one", "ms-vscode.cpptools", "actboy168.tasks", - "tamasfe.even-better-toml" + "tamasfe.even-better-toml", + "fill-labs.dependi" ] }, "forwardPorts": [ diff --git a/Cargo.toml b/Cargo.toml index be3ff2b..4f5e514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,8 @@ license = "MIT OR Apache-2.0" [dependencies] sunrise-next = { version = "*", features = ["no-std"] } embassy-executor = { version = "0.6.0" } +embassy-sync = { version = "0.6.0" } +embassy-futures = { version = "0.1.1" } embassy-time = { version = "0.3.2", features = ["generic-queue"] } esp-hal-embassy = { version = "*", features = ["esp32"] } esp-println = { version = "0.11.0", features = ["esp32", "log"] } diff --git a/src/coop.rs b/src/coop.rs new file mode 100644 index 0000000..e1394cc --- /dev/null +++ b/src/coop.rs @@ -0,0 +1,43 @@ +mod chicken; +use crate::coop::chicken::Chicken; + +pub struct Coop { + chicken: Chicken, +} + +impl Coop { + pub fn new() -> Coop { + Coop { + // chicken: { Chicken { awake: false } }, + chicken: Chicken::new(), + } + } + + pub async fn run(&self) { + loop { + self.is_time_to_wake_up().await; + self.turn_on_light(); + self.open_door(); + // join(self.open_door(), GATE_POSITION.send(self.door_position())).await; + self.is_light_enough_to_see().await; + self.turn_off_light(); + self.is_time_to_get_ready_for_bed().await; + self.turn_on_light(); + self.close_door(); + self.is_time_for_sleep().await; + self.turn_off_light(); + } + } + + pub async fn is_time_to_wake_up(&self) {} + pub async fn is_light_enough_to_see(&self) {} + pub async fn is_time_to_get_ready_for_bed(&self) {} + pub async fn is_time_for_sleep(&self) {} + pub fn turn_on_light(&self) {} + pub fn turn_off_light(&self) {} + pub fn open_door(&self) {} + pub fn close_door(&self) {} + pub fn door_position(&self) -> u32 { + 1 + } +} diff --git a/src/chicken.rs b/src/coop/chicken.rs similarity index 90% rename from src/chicken.rs rename to src/coop/chicken.rs index 17a2158..0ea580f 100644 --- a/src/chicken.rs +++ b/src/coop/chicken.rs @@ -2,12 +2,13 @@ // use chrono::LocalResult; // use esp_idf_svc::sys::__time_t; // use std::time::SystemTime; -use sunrise::{DawnType, SolarDay, SolarEvent}; +// use sunrise::{DawnType, SolarDay, SolarEvent}; pub struct Chicken { // wake_time: Option, // bed_time: Option, - awake: bool, + // awake: bool, + // awake: bool, } // todo: need a timer that is a future to wait on, then have an async process @@ -18,16 +19,16 @@ impl Chicken { Self { // wake_time: Option::None, // bed_time: Option::None, - awake: false, + // awake: false, } } - async fn wake_and_sleep() { - // day.dawn.await; - // awake = true; - // day.dusk.await; - // awake = false; - } + // async fn wake_and_sleep() { + // day.dawn.await; + // awake = true; + // day.dusk.await; + // awake = false; + // } } // impl Chicken { diff --git a/src/main.rs b/src/main.rs index c874410..8c6402a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,24 +3,18 @@ #![feature(type_alias_impl_trait)] use embassy_executor::Spawner; -use embassy_time::{Duration, Timer}; +// use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, system::SystemControl}; -use sunrise::{sunrise_sunset, DawnType, SolarDay, SolarEvent}; +use esp_println::print; +// use sunrise::{sunrise_sunset, DawnType, SolarDay, SolarEvent}; +use embassy_futures::join::join; +use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel}; -mod chicken; -use crate::chicken::Chicken; +mod coop; -#[embassy_executor::task] -async fn one_second_task() { - let mut count = 0; - - loop { - esp_println::println!("Spawn Task Count: {}", count); - count += 1; - Timer::after(Duration::from_millis(1_000)).await; - } -} +// Declare a channel of 2 u32s +static GATE_POSITION: Channel = Channel::new(); #[main] async fn main(spawner: Spawner) { @@ -33,19 +27,53 @@ async fn main(spawner: Spawner) { esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); - spawner.spawn(one_second_task()).unwrap(); + // let mut count = 0; + // loop { + // esp_println::println!("Main Task Count: {}", count); + // count += 1; + // Timer::after(Duration::from_millis(5_000)).await; + // } - let mut count = 0; - loop { - esp_println::println!("Main Task Count: {}", count); - count += 1; - Timer::after(Duration::from_millis(5_000)).await; - } + spawner.spawn(run_app()).unwrap(); + let coop: coop::Coop = coop::Coop::new(); + + join(coop.run(), GATE_POSITION.send(coop.door_position())).await; + // loop { + // coop.is_time_to_wake_up().await; + // coop.turn_on_light(); + // join(coop.open_door(), GATE_POSITION.send(coop.door_position())).await; + // coop.is_light_enough_to_see().await; + // coop.turn_off_light(); + // coop.is_time_to_get_ready_for_bed().await; + // coop.turn_on_light(); + // coop.close_door(); + // coop.is_time_for_sleep().await; + // coop.turn_off_light(); + // } } #[embassy_executor::task] -async fn print_circadia_stats() { - let dawn = SolarDay::new(43.6532, -79.3832, 2016, 1, 1) - .with_altitude(54.) - .event_time(SolarEvent::Dawn(DawnType::Civil)); +async fn run_app() { + loop { + let posn = GATE_POSITION.receive().await; + print!("position: {posn}"); + } } + +// #[embassy_executor::task] +// async fn print_circadia_stats() { +// let dawn = SolarDay::new(43.6532, -79.3832, 2016, 1, 1) +// .with_altitude(54.) +// .event_time(SolarEvent::Dawn(DawnType::Civil)); +// } + +// #[embassy_executor::task] +// async fn one_second_task() { +// let mut count = 0; + +// loop { +// esp_println::println!("Spawn Task Count: {}", count); +// count += 1; +// Timer::after(Duration::from_millis(1_000)).await; +// } +// }