using deep sleep

This commit is contained in:
Martin Brodbeck 2023-01-11 11:12:02 +01:00
parent ce08d17a11
commit 20f912f919
3 changed files with 33 additions and 11 deletions

View file

@ -1,5 +1,4 @@
#include <cstdio>
#include <ctime>
#include <algorithm>
#include <chrono>
@ -9,10 +8,6 @@
#include "hardware/pio.h"
#include "hardware/rtc.h"
#include "hardware/structs/scb.h"
// #include "hardware/xosc.h"
// #nclude "hardware/rosc.h"
// #include "hardware/clocks.h"
// #include "hardware/pll.h"
#include "pico/cyw43_arch.h"
#include "pico/sleep.h"
@ -97,13 +92,17 @@ int main() {
int8_t hour = isDST(dt) ? dt.hour + 1 : dt.hour;
#ifdef DEBUG
printf("%d-%02d-%02d %02d:%02d.%02d\n", dt.year, dt.month, dt.day, hour, dt.min, dt.sec);
#endif
// If there was a waste bin pickup found AND we are in the evening (>= 18:00) …
if (it != dates.end() && hour >= 18 && hour < 23) {
auto wasteDate = *it;
size_t count{0};
auto currentTime = time_us_64();
WS2812 led(WS2812_PIN);
while (currentTime < timestamp) {
size_t index = count % wasteDate.wasteTypes.size();
@ -145,15 +144,16 @@ int main() {
}
led.blinkReady();
sleep_ms(800);
datetime_t dtUntil = dt;
add_one_hour(dtUntil); // next hour
dtUntil.sec = 0;
dtUntil.min = 0;
led.deinit(); // deinit LED before going to deep sleep
perform_sleep(dtUntil);
recover_from_sleep(scb_orig, clock0_orig, clock1_orig);
led.init(); // init LED again after deep sleep
}
}

View file

@ -1,10 +1,8 @@
#include "ws2812.h"
WS2812::WS2812(uint gpio, PIO pio, uint sm) : m_pio{pio}, m_sm{sm} {
uint offset = pio_add_program(m_pio, &ws2812_program);
ws2812_program_init(m_pio, m_sm, offset, gpio, 800000, true);
switchColor(Color::OFF);
}
WS2812::WS2812(uint gpio, PIO pio, uint sm) : m_pio{pio}, m_gpio{gpio}, m_sm{sm} { init(); }
WS2812::~WS2812() { deinit(); }
void WS2812::switchColor(Color color) { putPixel(static_cast<uint32_t>(color)); }
@ -17,4 +15,21 @@ void WS2812::blinkReady() {
}
}
void WS2812::init() {
if (m_offset != 0)
return;
m_offset = pio_add_program(m_pio, &ws2812_program);
ws2812_program_init(m_pio, m_sm, m_offset, m_gpio, 800000, true);
switchColor(Color::OFF);
sleep_ms(1);
}
void WS2812::deinit() {
switchColor(Color::OFF);
sleep_ms(1);
pio_remove_program(m_pio, &ws2812_program, m_offset);
m_offset = 0;
}
void WS2812::putPixel(uint32_t pixel_grb) { pio_sm_put_blocking(m_pio, m_sm, pixel_grb << 8u); }

View file

@ -17,11 +17,18 @@ enum class Color : uint32_t {
class WS2812 {
public:
WS2812(uint gpio, PIO pio = pio0, uint sm = 0);
virtual ~WS2812();
WS2812(const WS2812 &other) = delete;
WS2812 &operator=(const WS2812 &) = delete;
void switchColor(Color color);
void blinkReady();
void init();
void deinit();
private:
void putPixel(uint32_t pixel_rgb);
PIO m_pio;
uint m_gpio;
uint m_sm;
uint m_offset{0};
};