feat: wordclock works!

This commit is contained in:
Philip Stark 2023-05-09 20:01:36 +02:00
parent 5b0930d616
commit 7f091409cf
8 changed files with 1405 additions and 12 deletions

View file

@ -0,0 +1,305 @@
#include "wordclock.h"
namespace esphome {
namespace wordclock {
void Wordclock::setup() {
// this->time->update();
// this->light_state->add_effects({this->randomTwinkle});
// this->start_idle_animation();
}
void Wordclock::start_idle_animation() {
this->display->set_enabled(false);
// auto call =
this->light_state->turn_on().set_effect("random_twinkle").perform();
// call.set_effect("random_twinkle");
// call.perform();
}
void Wordclock::end_idle_animation() {
this->light_state->turn_off().perform();
this->display->set_enabled(true);
// this->light->clear_effect_data();
// this->display->get_light()->set_effect_active(false);
// auto call1 = this->light_state->turn_on();
// call1.set_effect("None");
// call1.perform();
// this->light->all().set(Color(0xF0FF00));
// this->display->fill(Color(0));
// this->light_state->turn_off().perform();
// call2.perform();
}
void Wordclock::update() {
// esphome::addressable_light::AddressableLightDisplay it = *(this->display);
// ESP_LOGD("loop", "beep");
// ESP_LOGD("loop", "time is now [%02i:%02i:%02i]", this->time->now().hour, this->time->now().minute, this->time->now().second);
// ESP_LOGE("wordclock.cpp", "display_ptr: 0x%x", it);
// ESP_LOGE("wordclock.cpp", "this: 0x%x", (this));
// ESP_LOGE("wordclock.cpp", "this->display: 0x%x", (this->display));
// ESP_LOGE("wordclock.cpp", "&this->display: 0x%x", &(this->display));
// ESP_LOGE("loop", "time_ptr: %i", this->time);
// it.line(0,0,0,0, Color(0x00FF00));
esphome::time::ESPTime time = this->time->now();
this->find_hour(9);
this->find_minute(31);
if (time.is_valid() == false) {
if (this->valid_time) {
ESP_LOGD("loop", "time is not valid [%02i:%02i:%02i]", time.hour, time.minute, time.second);
this->start_idle_animation();
this->valid_time = false;
return;
}
}
else {
if (!this->valid_time) {
this->end_idle_animation();
this->valid_time = true;
ESP_LOGD("wordclock.cpp", "time is now valid [%02i:%02i:%02i]", time.hour, time.minute, time.second);
return;
}
// for (uint8_t idx = 0;idx < ; idx++) {
// }
// std::vector<uint16_t> *minute = this->find_minute(time.minute);
// std::vector<uint16_t> *hour = this->find_hour(time.hour);
this->display->fill(Color(0x000000));
int8_t minute = this->find_minute(time.minute);
int8_t hour = this->find_hour((time.hour + this->hour_offsets->at(minute)) % 24);
for (uint16_t segment_idx : *this->static_segments){
this->draw_segment(segment_idx);
}
for (uint16_t segment_idx : this->minutes->at(minute)){
this->draw_segment(segment_idx);
}
for (uint16_t segment_idx : this->hours->at(hour)){
this->draw_segment(segment_idx);
}
// this->draw_segment(0);
// this->draw_segment(1);
// this->draw_segment(4);
// this->draw_segment(10);
// this->draw_segment(19);
// this->light->range(0, 10).fade_to_white(100);
// this->display->get_light()->range(18, 35).set(Color(0xf0ff0f));
// SegmentCoords s = this->segments->at(time);
// ESP_LOGD("wordclock.cpp", "time is now [%02i:%02i:%02i]", time.hour, time.minute, time.second);
// ESP_LOGD("wordclock.cpp", "x1: %i, y1: %i, x2: %i, y2: %i", s.x1, s.y1, s.x2, s.y2);
// this->display->draw_pixel_at(0, 0, Color(0xFF0000));
// this->display->draw_pixel_at(1, 0, Color(0x00FF00));
// this->display->draw_pixel_at(2, 0, Color(0x0000FF));
// if (time.second != second) {
// second = time.second;
// ESP_LOGD("loop", "time is now [%02i:%02i:%02i]", time.hour, time.minute, time.second);
// }
// if(time.hour != hour || time.minute != minute) {
// hour = time.hour;
// minute = time.minute;
// if (hour >= 0 && time.is_valid() == true){
// display_time(time.hour, time.minute, CRGB(red, green, blue));
// ESP_LOGE("loop", "Update Time: %i:%i Brightness: %i RGB: %i-%i-%i", time.hour, time.minute, brightness, red, green, blue);
// }
// }
// }
// if (!this->reading_ && !mode_funcs_.empty()) {
// this->reading_ = true;
// this->read_mode_(0);
}
}
void Wordclock::draw_segment(uint16_t segment_id) {
SegmentCoords s = this->segments->at(segment_id);
// ESP_LOGD("wordclock.cpp", "x1: %i, y1: %i, x2: %i, y2: %i", s.x1, s.y1, s.x2, s.y2);
this->display->line(s.x1, s.y1, s.x2, s.y2, esphome::Color(0xFFFFFF));
}
// void Wordclock::set_writer(display_writer_t &&writer) {
// this->writer_ = writer;
// }
void Wordclock::add_segment(SegmentCoords segment) {
// if (!this->segments) {
// }
this->segments->push_back(segment);
// this->writer_ = writer;
}
// std::vector<uint16_t> * Wordclock::find_hour(uint8_t hour) {
// std::vector<uint16_t> *empty_vector; // = new std::vector<uint16_t>();
// uint16_t last_defined_hour = -1;
// for (int i = 0; i < this->hours->size(); i++) {
// if (this->hours->at(i).size()) {
// if (hour == i) {
// return &(this->hours->at(i));
// }
// else {
// last_defined_hour = i;
// }
// }
// else {
// empty_vector = &(this->hours->at(i));
// if (hour == i) {
// if (last_defined_hour == -1) return empty_vector;
// return &(this->hours->at(last_defined_hour));
// }
// }
// }
// return empty_vector;
// }
int8_t Wordclock::find_hour(uint8_t hour) {
uint16_t last_defined_hour = -1;
for (int i = 0; i < this->hours->size(); i++) {
if (this->hours->at(i).size()) {
last_defined_hour = i;
if (hour == i) {
return last_defined_hour;
}
}
if (hour == i) break;
}
return last_defined_hour;
}
int8_t Wordclock::find_minute(uint8_t minute) {
uint16_t last_defined_minute = -1;
for (int i = 0; i < this->minutes->size(); i++) {
if (this->minutes->at(i).size()) {
last_defined_minute = i;
if (minute == i) {
return last_defined_minute;
}
}
if (minute == i) break;
}
return last_defined_minute;
}
// std::vector<uint16_t> * Wordclock::find_minute(uint8_t minute) {
// std::vector<uint16_t> *empty_vector;// = new std::vector<uint16_t>();
// uint16_t last_defined_minute = -1;
// for (int i = 0; i < this->minutes->size(); i++) {
// if (this->minutes->at(i).size()) {
// if (minute == i) {
// return &(this->minutes->at(i));
// }
// else {
// last_defined_minute = i;
// }
// }
// else {
// empty_vector = &(this->minutes->at(i));
// if (minute == i) {
// if (last_defined_minute == -1) return empty_vector;
// return &(this->minutes->at(last_defined_minute));
// }
// }
// }
// return empty_vector;
// }
void Wordclock::add_hour_offset(uint8_t index, int8_t offset) {
(*this->hour_offsets)[index] = offset;
}
void Wordclock::add_hour(uint8_t hour, std::vector<uint16_t> *segments) {
for (uint16_t i : *segments){
this->hours->at(hour).push_back(i);
}
}
void Wordclock::add_minute(uint8_t minute, std::vector<uint16_t> *segments) {
for (uint16_t i : *segments){
this->minutes->at(minute).push_back(i);
}
}
void Wordclock::add_static(uint16_t segment_id) {
this->static_segments->push_back(segment_id);
}
// uint16_t **
// Wordclock::Wordclock(std::vector<uint16_t> *minutes, std::vector<uint16_t> *hours, SegmentCoords *segments)
// Wordclock::Wordclock(uint16_t **minutes, uint16_t **hours, SegmentCoords *segments)
// : PollingComponent(1000) {
// // this->minutes = minutes;
// // this->hours = hours;
// // this->segments = segments;
// // std::vector<uint16_t> minutes[60];
// // std::vector<uint16_t> hours[24];
// }
Wordclock::Wordclock()
: PollingComponent(1000) {
// this->minutes = std::vector<std::vector<uint16_t>>();
// // for (int i=0; i<60; i++) this->minutes.push_back(std::vector<uint16_t>());
// this->hours = std::vector<std::vector<uint16_t>>();
// // for (int i=0; i<24; i++) this->minutes.push_back(std::vector<uint16_t>());
// this->segments = std::vector<SegmentCoords>();
}
Wordclock::Wordclock(esphome::time::RealTimeClock *time, esphome::addressable_light::AddressableLightDisplay *display, esphome::light::AddressableLightState *light_state)
: PollingComponent(16) {
// ESP_LOGE("wordclock.cpp", "this: 0x%x", (this));
// ESP_LOGE("wordclock.cpp", "display: 0x%x", (display));
// ESP_LOGE("wordclock.cpp", "&display: 0x%x", (&display));
this->time = time;
this->display = display;
this->light = this->display->get_light();
this->light_state = light_state;
// light::AddressableLight *light = this->display->get_light();
// light::AddressableRainbowLightEffect *light_addressablerainbowlighteffect;
// light::AddressableTwinkleEffect *light_addressabletwinkleeffect;
// light::AddressableRandomTwinkleEffect *light_addressablerandomtwinkleeffect;
// this->rainbow = new light::AddressableRainbowLightEffect("rainbow");
// this->rainbow->set_speed(10);
// this->rainbow->set_width(50);
// this->twinkle = new light::AddressableTwinkleEffect("twinkle");
// this->twinkle->set_twinkle_probability(0.05f);
// this->twinkle->set_progress_interval(4);
// this->randomTwinkle = new light::AddressableRandomTwinkleEffect("random_twinkle");
// this->randomTwinkle->set_twinkle_probability(0.05f);
// this->randomTwinkle->set_progress_interval(32);
this->hour_offsets = new std::vector<int8_t>(60, 0);
this->minutes = new std::vector<std::vector<uint16_t>>(60, std::vector<uint16_t>());
// for (int i=0; i<60; i++) this->minutes->push_back(std::vector<uint16_t>());
this->hours = new std::vector<std::vector<uint16_t>>(24, std::vector<uint16_t>());
// for (int i=0; i<24; i++) this->hours->push_back(std::vector<uint16_t>());
this->segments = new std::vector<SegmentCoords>();
this->static_segments = new std::vector<uint16_t>();
}
} // namespace wordclock
} // namespace esphome