// // Created by lukas on 06.03.21. // #include "Clock.h" Clock::Clock() : strip(NUMPIXELS, D5, NEO_GRB + NEO_KHZ800), refreshTicker() { } void Clock::init() { strip.begin(); strip.clear(); strip.show(); refreshTicker.attach(10, [this]() { this->refreshTime(); }); this->refreshTime(); } void Clock::paintAllwaysOnLeds() { printWord(types::es, Adafruit_NeoPixel::Color(150, 150, 0)); printWord(types::ist, Adafruit_NeoPixel::Color(150, 0, 150)); printWord(types::uhr, Adafruit_NeoPixel::Color(0, 0, 150)); } void Clock::printWord(std::vector word, uint32_t color) { for (const int i : word) { strip.setPixelColor(i, color); } } void Clock::refreshTime() { strip.clear(); // grab the current instant in unix seconds time_t now = time(nullptr); if (now <= 604800) { strip.show(); return; } tm* loctime = localtime(&now); const uint8_t hour = loctime->tm_hour; const uint8_t minute = loctime->tm_min; paintAllwaysOnLeds(); setTime(hour, minute); strip.show(); Serial.print("time now: "); Serial.print(hour); Serial.print("h "); Serial.print(minute); Serial.println("M"); } void Clock::setTime(uint8_t hour, uint8_t minute) { const uint8_t minuteselector = minute / 5; // if minuteselector >= 4 +1 to hour if (minuteselector >= 4) hour++; // convert to 12h format if (hour >= 12) hour = hour - 12; std::vector hourWord; hourWord = hour == 1 ? types::ein : hour == 2 ? types::zwei : hour == 3 ? types::drei : hour == 4 ? types::vier : hour == 5 ? types::fuenf2 : hour == 6 ? types::sechs : hour == 7 ? types::sieben : hour == 8 ? types::acht : hour == 9 ? types::neun : hour == 10 ? types::zehn2 : hour == 11 ? types::elf : hour == 0 ? types::zwoelf : hourWord; printWord(hourWord, Adafruit_NeoPixel::Color(0, 150, 0)); // fuenf / zehn / viertl word std::vector minuteWord; if (minuteselector == 1 || minuteselector == 5 || minuteselector == 7 || minuteselector == 11) minuteWord = types::fuenf; else if (minuteselector == 2 || minuteselector == 4 || minuteselector == 8 || minuteselector == 10) minuteWord = types::zehn; else if (minuteselector == 3) minuteWord = types::viertel; else if (minuteselector == 9) minuteWord = types::dreiviertel; else if (minuteselector == 6) minuteWord = types::halb; printWord(minuteWord, Adafruit_NeoPixel::Color(0, 150, 0)); // vor / nach std::vector vornachWord; if (minuteselector == 1 || minuteselector == 2 || minuteselector == 3 || minuteselector == 7 || minuteselector == 8) vornachWord = types::nach; else if (minuteselector == 4 || minuteselector == 5 || minuteselector == 10 || minuteselector == 11) vornachWord = types::vor; printWord(vornachWord, Adafruit_NeoPixel::Color(150, 150, 150)); // halb if (minuteselector >= 4 && minuteselector <= 8) printWord(types::halb, Adafruit_NeoPixel::Color(150, 0, 0)); } const std::vector types::es = calcPixels(0, 0, 2); const std::vector types::ist = calcPixels(3, 0, 3); const std::vector types::fuenf = calcPixels(8, 0, 4); const std::vector types::zehn = calcPixels(0, 1, 4); const std::vector types::zwanzig = calcPixels(5, 1, 7); const std::vector types::drei = calcPixels(1, 2, 3); const std::vector types::viertel = calcPixels(5, 2, 7); const std::vector types::dreiviertel = calcPixels(1, 2, 11); const std::vector types::vor = calcPixels(0, 3, 3); const std::vector types::nach = calcPixels(3, 3, 4); const std::vector types::halb = calcPixels(8, 3, 4); const std::vector types::zwoelf = calcPixels(0, 4, 5); const std::vector types::sieben = calcPixels(6, 4, 6); const std::vector types::ein = calcPixels(0, 5, 3); const std::vector types::eins = calcPixels(0, 5, 4); const std::vector types::vier = calcPixels(4, 5, 4); const std::vector types::acht = calcPixels(8, 5, 4); const std::vector types::sechs = calcPixels(0, 6, 5); const std::vector types::zwei = calcPixels(6, 6, 5); const std::vector types::fuenf2 = calcPixels(1, 7, 5); const std::vector types::elf = calcPixels(5, 7, 3); const std::vector types::zehn2 = calcPixels(8, 7, 4); const std::vector types::neun = calcPixels(0, 8, 4); const std::vector types::drei2 = calcPixels(4, 8, 4); const std::vector types::ein2 = calcPixels(6, 8, 3); const std::vector types::uhr = calcPixels(9, 8, 3); uint8_t types::convert(uint8_t x, uint8_t y) { const bool upRow = (x % 2) == 0; int val = x * 9 + y; // if its a row upwards we need to calculate the additional down and up pixels if (upRow) // we need to go the rest down (9 -y) and up (*2) and subtract the too many added 10 val += ((9 - y) * 2) - 10; return val; } std::vector types::calcPixels(uint8_t x, uint8_t y, uint8_t nrPixels) { std::vector vec; for (uint8_t i = 0; i < nrPixels; i++) { vec.push_back(convert(x + i, y)); } return vec; }