led switching logic implemented.

This commit is contained in:
Martin Brodbeck 2023-01-03 12:46:57 +01:00
parent 867ef315d3
commit b0f8855f22

View file

@ -1,6 +1,9 @@
// #include <cstdio>
#include <cstdio>
#include <ctime>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
#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<unsigned>(dt.month)},
chrono::day{static_cast<unsigned>(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<absolute_time_t>(timestamp));
}
}
cyw43_arch_deinit();