diff --git a/src/abfall.cpp b/src/abfall.cpp index aad5c27..d0c97ef 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -1,6 +1,9 @@ -// #include +#include #include +#include +#include +#include #include #include "hardware/pio.h" @@ -15,7 +18,7 @@ #include "utils.h" #include "ws2812.h" -using std::string; +using namespace std; #define WS2812_PIN 22 @@ -24,7 +27,7 @@ int main() { rtc_init(); #ifdef DEBUG - printf("!!! DEBUG mode!!!\n\n"); + printf("!!! DEBUG mode!!!\n"); #endif WS2812 led(WS2812_PIN); @@ -64,12 +67,65 @@ int main() { printf("Number of Dates: %d\n", dates.size()); #endif while (true) { + auto timestamp = time_us_64() + 60000000ull; + rtc_get_datetime(&dt); - datetime_to_str(datetime_str, sizeof(datetime_buf), &dt); + // datetime_to_str(datetime_str, sizeof(datetime_buf), &dt); // printf("DateTime: %s\n", datetime_str); - led.switchColor(Color::OFF); - sleep_ms(5000); + + chrono::year_month_day tomorrowYMD(chrono::year{dt.year}, + chrono::month{static_cast(dt.month)}, + chrono::day{static_cast(dt.day + 1)}); + auto it = std::find_if(dates.begin(), dates.end(), [&tomorrowYMD](const WasteDate &date) { + return date.date == tomorrowYMD; + }); + + if (it != dates.end()) { + printf("\n+++ FOUND +++\n"); + auto wasteDate = *it; + size_t count{0}; + auto currentTime = time_us_64(); + while (currentTime < timestamp) { + size_t index = count % wasteDate.wasteTypes.size(); + + switch (wasteDate.wasteTypes.at(index)) { + case Waste::GelberSack: + led.switchColor(Color::YELLOW); + break; + case Waste::Restmuell: + led.switchColor(Color::RED); + break; + case Waste::Papiertonne: + led.switchColor(Color::BLUE); + break; + case Waste::Problemstoffmobil: + led.switchColor(Color::CYAN); + break; + case Waste::Biotonne: + led.switchColor(Color::GREEN); + break; + } + + auto timeLeft = timestamp - currentTime; + // std::cout << "Info: " << timeLeft << std::endl; + + if (timeLeft > 3000000) { + sleep_us(3000000); + } else { + // std::cout << "Switching LED off." << std::endl; + // led.switchColor(Color::OFF); + sleep_us(timeLeft); + } + + currentTime = time_us_64(); + count++; + } + + } else { + led.blinkReady(); + sleep_until(static_cast(timestamp)); + } } cyw43_arch_deinit();