···1010use core::cell::RefCell;
1111use core::fmt::Write;
1212use core::str::from_utf8;
1313-use cortex_m::asm::delay;
1413use cyw43::JoinOptions;
1514use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
1615use defmt::info;
1716use defmt::*;
1818-use embassy_embedded_hal::shared_bus::I2cDeviceError;
1917use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
2018use embassy_executor::Spawner;
2119use embassy_net::StackResources;
···2422use embassy_rp::clocks::RoscRng;
2523use embassy_rp::flash::Async;
2624use embassy_rp::gpio::Input;
2727-use embassy_rp::i2c::{Error, I2c};
2525+use embassy_rp::i2c::I2c;
2826use embassy_rp::peripherals::{DMA_CH0, I2C0, PIO0, SPI0};
2927use embassy_rp::pio::{InterruptHandler, Pio};
3028use embassy_rp::rtc::{DateTime, DayOfWeek};
···126124 let btn_c = Input::new(p.PIN_14, Pull::Down);
127125 let rtc_alarm = Input::new(p.PIN_8, Pull::Down);
128126127127+ //Setup i2c bus
128128+ let config = embassy_rp::i2c::Config::default();
129129+ let i2c = i2c::I2c::new_blocking(p.I2C0, p.PIN_5, p.PIN_4, config);
130130+ static I2C_BUS: StaticCell<I2c0Bus> = StaticCell::new();
131131+ let i2c_bus = NoopMutex::new(RefCell::new(i2c));
132132+ let i2c_bus = I2C_BUS.init(i2c_bus);
133133+134134+ let i2c_dev = I2cDevice::new(i2c_bus);
135135+ let mut rtc_device = PCF85063::new(i2c_dev);
136136+137137+ if btn_a.is_high() {
138138+ //Clears the alarm on start if A button is pressed (manual start)
139139+ _ = rtc_device.disable_all_alarms();
140140+ _ = rtc_device.clear_alarm_flag();
141141+ }
142142+129143 let spi = Spi::new(
130144 p.SPI0,
131145 clk,
···158172 seed,
159173 );
160174161161- //rtc setup
162162- // let mut rtc = embassy_rp::rtc::Rtc::new(p.RTC);
163163-164175 spawner.must_spawn(net_task(runner));
165176 //Attempt to connect to wifi to get RTC time loop for 2 minutes
166177 let mut wifi_connection_attempts = 0;
···185196 Timer::after(Duration::from_secs(1)).await;
186197 wifi_connection_attempts += 1;
187198 }
188188-189189- //Setup i2c bus
190190- let config = embassy_rp::i2c::Config::default();
191191- let i2c = i2c::I2c::new_blocking(p.I2C0, p.PIN_5, p.PIN_4, config);
192192- static I2C_BUS: StaticCell<I2c0Bus> = StaticCell::new();
193193- let i2c_bus = NoopMutex::new(RefCell::new(i2c));
194194- let i2c_bus = I2C_BUS.init(i2c_bus);
195195-196196- let i2c_dev = I2cDevice::new(i2c_bus);
197197- let mut rtc_device = PCF85063::new(i2c_dev);
198199199200 if connected_to_wifi {
200201 info!("waiting for DHCP...");
···329330 let cycle = Duration::from_millis(100);
330331 let mut current_cycle = 0;
331332 let mut time_to_scan = true;
332332- //15 minutes(ish) idk it's late and my math is so bad rn
333333- let reset_cycle = 9_000;
333333+ //5 minutes(ish) idk it's late and my math is so bad rn
334334+ let reset_cycle = 3_000;
334335335336 //Turn off led to signify that the badge is ready
336337 user_led.set_low();
337338338339 //RTC alarm stuff
339339- // info!("going to sleep");
340340- // Timer::after(Duration::from_millis(5_000)).await;
341341- // //Set the rtc and sleep for 15 minutes
342342- // //goes to sleep for 15 mins
343343- // _ = rtc_device.clear_alarm_flag();
344344- // _ = rtc_device.set_alarm_minutes(5);
345345- // _ = rtc_device.control_alarm_minutes(Control::On);
346346- // _ = rtc_device.control_alarm_interrupt(Control::On);
347347- // power.set_low();
340340+ let mut go_to_sleep = false;
341341+ let mut reset_cycles_till_sleep = 0;
342342+ //Like 15ish mins??
343343+ let sleep_after_cycles = 4;
344344+345345+ if rtc_alarm.is_high() {
346346+ //sleep happened
347347+ go_to_sleep = true;
348348+ info!("Alarm went off");
349349+ _ = rtc_device.disable_all_alarms();
350350+ _ = rtc_device.clear_alarm_flag();
351351+ } else {
352352+ info!("Alarm was clear")
353353+ }
348354349355 loop {
350356 //Change Image Button
351357 if btn_c.is_high() {
352358 info!("Button C pressed");
359359+ reset_cycles_till_sleep = 0;
353360 let current_image = CURRENT_IMAGE.load(core::sync::atomic::Ordering::Relaxed);
354361 let new_image = DisplayImage::from_u8(current_image).unwrap().next();
355362 CURRENT_IMAGE.store(new_image.as_u8(), core::sync::atomic::Ordering::Relaxed);
···361368 if btn_a.is_high() {
362369 println!("{:?}", current_cycle);
363370 info!("Button A pressed");
371371+ reset_cycles_till_sleep = 0;
364372 user_led.toggle();
365373 Timer::after(Duration::from_millis(500)).await;
366374 continue;
···368376369377 if btn_down.is_high() {
370378 info!("Button Down pressed");
379379+ reset_cycles_till_sleep = 0;
371380 SCREEN_TO_SHOW.lock(|screen| {
372381 screen.replace(Screen::WifiList);
373382 });
···378387379388 if btn_up.is_high() {
380389 info!("Button Up pressed");
390390+ reset_cycles_till_sleep = 0;
381391 SCREEN_TO_SHOW.lock(|screen| {
382392 screen.replace(Screen::Badge);
383393 });
···388398389399 if btn_b.is_high() {
390400 info!("Button B pressed");
401401+ reset_cycles_till_sleep = 0;
391402 SCREEN_TO_SHOW.lock(|screen| {
392403 if *screen.borrow() == Screen::Badge {
393404 //IF on badge screen and b pressed reset wifi count
···447458448459 if time_to_scan {
449460 info!("Scanning for wifi networks");
461461+ reset_cycles_till_sleep += 1;
450462 time_to_scan = false;
451463 let mut scanner = control.scan(Default::default()).await;
452464 while let Some(bss) = scanner.next().await {
···460472 current_cycle = 0;
461473 time_to_scan = true;
462474 }
475475+476476+ if reset_cycles_till_sleep >= sleep_after_cycles {
477477+ info!("Going to sleep");
478478+ reset_cycles_till_sleep = 0;
479479+ go_to_sleep = true;
480480+ }
481481+482482+ if go_to_sleep {
483483+ info!("going to sleep");
484484+ Timer::after(Duration::from_secs(25)).await;
485485+ //Set the rtc and sleep for 15 minutes
486486+ //goes to sleep for 15 mins
487487+ _ = rtc_device.disable_all_alarms();
488488+ _ = rtc_device.clear_alarm_flag();
489489+ _ = rtc_device.set_alarm_seconds(5);
490490+ _ = rtc_device.control_alarm_seconds(Control::On);
491491+ _ = rtc_device.control_alarm_interrupt(Control::On);
492492+ power.set_low();
493493+ }
494494+463495 current_cycle += 1;
464496 Timer::after(cycle).await;
465497 }
+7-8
src/pcf85063a/mod.rs
···11#![allow(dead_code)]
22+33+///None of this code is mine. It's https://crates.io/crates/pcf85063a
44+/// I just needed a synchronous version so did a local clone and pulled the async and awaits
25pub mod alarm;
36pub mod datetime;
4758use embedded_hal_1::i2c::I2c;
69710/// All possible errors in this crate
88-#[derive(Debug)]
99-#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1111+#[derive(Debug, defmt::Format)]
1012pub enum Error<E> {
1113 /// I2C bus error
1214 I2C(E),
···80828183/// Two possible choices, used for various enable/disable bit flags
8284#[allow(non_camel_case_types)]
8383-#[derive(Copy, Clone, Debug)]
8484-#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8585+#[derive(Copy, Clone, Debug, defmt::Format)]
8586pub enum Control {
8687 /// Enable some feature, eg. timer
8788 On,
···9091}
91929293/// PCF8563 driver
9393-#[derive(Debug, Default)]
9494-#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9494+#[derive(Debug, Default, defmt::Format)]
9595pub struct PCF85063<I2C> {
9696 /// The concrete I2C device implementation.
9797 i2c: I2C,
···193193 }
194194}
195195196196-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197197-#[cfg_attr(feature = "defmt", derive(defmt::Format))]
196196+#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
198197#[repr(u8)]
199198pub enum OutputFrequency {
200199 Hz32768 = 0b000,