Skip to content

Commit

Permalink
add coop struct and shell fn's
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffdudds committed Oct 9, 2024
1 parent 918d9af commit 9993b10
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
43 changes: 43 additions & 0 deletions src/coop.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 10 additions & 9 deletions src/chicken.rs → src/coop/chicken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
// bed_time: Option<u32>,
awake: bool,
// awake: bool,
// awake: bool,
}

// todo: need a timer that is a future to wait on, then have an async process
Expand All @@ -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 {
Expand Down
78 changes: 53 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CriticalSectionRawMutex, u32, 2> = Channel::new();

#[main]
async fn main(spawner: Spawner) {
Expand All @@ -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;
// }
// }

0 comments on commit 9993b10

Please sign in to comment.