WordClockESP/src/Clock.cpp

160 lines
5.1 KiB
C++
Raw Normal View History

2021-03-06 22:06:09 +01:00
//
// 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<int> 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<int> 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<int> 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<int> 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<int> types::es = calcPixels(0, 0, 2);
const std::vector<int> types::ist = calcPixels(3, 0, 3);
const std::vector<int> types::fuenf = calcPixels(8, 0, 4);
const std::vector<int> types::zehn = calcPixels(0, 1, 4);
const std::vector<int> types::zwanzig = calcPixels(5, 1, 7);
const std::vector<int> types::drei = calcPixels(1, 2, 3);
const std::vector<int> types::viertel = calcPixels(5, 2, 7);
const std::vector<int> types::dreiviertel = calcPixels(1, 2, 11);
const std::vector<int> types::vor = calcPixels(0, 3, 3);
const std::vector<int> types::nach = calcPixels(3, 3, 4);
const std::vector<int> types::halb = calcPixels(8, 3, 4);
const std::vector<int> types::zwoelf = calcPixels(0, 4, 5);
const std::vector<int> types::sieben = calcPixels(6, 4, 6);
const std::vector<int> types::ein = calcPixels(0, 5, 3);
const std::vector<int> types::eins = calcPixels(0, 5, 4);
const std::vector<int> types::vier = calcPixels(4, 5, 4);
const std::vector<int> types::acht = calcPixels(8, 5, 4);
const std::vector<int> types::sechs = calcPixels(0, 6, 5);
const std::vector<int> types::zwei = calcPixels(6, 6, 5);
const std::vector<int> types::fuenf2 = calcPixels(1, 7, 5);
const std::vector<int> types::elf = calcPixels(5, 7, 3);
const std::vector<int> types::zehn2 = calcPixels(8, 7, 4);
const std::vector<int> types::neun = calcPixels(0, 8, 4);
const std::vector<int> types::drei2 = calcPixels(4, 8, 4);
const std::vector<int> types::ein2 = calcPixels(6, 8, 3);
const std::vector<int> 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<int> types::calcPixels(uint8_t x, uint8_t y, uint8_t nrPixels) {
std::vector<int> vec;
for (uint8_t i = 0; i < nrPixels; i++) {
vec.push_back(convert(x + i, y));
}
return vec;
}