From 20f912f919a56dca59d23609431548a76500b091 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jan 2023 11:12:02 +0100 Subject: [PATCH] using deep sleep --- src/abfall.cpp | 12 ++++++------ src/ws2812.cpp | 25 ++++++++++++++++++++----- src/ws2812.h | 7 +++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/abfall.cpp b/src/abfall.cpp index 0f133b9..f914b7c 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -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 } } diff --git a/src/ws2812.cpp b/src/ws2812.cpp index c7ad5ac..edf35b7 100644 --- a/src/ws2812.cpp +++ b/src/ws2812.cpp @@ -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(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); } \ No newline at end of file diff --git a/src/ws2812.h b/src/ws2812.h index 229fccf..f7746bb 100644 --- a/src/ws2812.h +++ b/src/ws2812.h @@ -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}; };