Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "esphome.h"
- #include <NeoPixelBrightnessBus.h>
- const uint16_t PixelCount = 113; // this example assumes 4 pixels, making it smaller will cause a failure
- const uint8_t PixelPin = 23; // make sure to set this to the correct pin, ignored for Esp8266
- //Define your NeoPixel setup. Please read https://github.com/Makuna/NeoPixelBus/wiki/NeoPixelBus-object#neo-methods
- // NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266Uart1800KbpsMethod> strip(PixelCount, PixelPin);
- //NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266DmaSk6812Method> strip(PixelCount, PixelPin);
- NeoPixelBrightnessBus< NeoGrbFeature, NeoWs2812xMethod> strip(PixelCount, PixelPin);
- //LED definitions. First led should be 0.
- int leds_time_it_is[] = {0, 1, 2, 3}; // It is
- int leds_minutes[] = {42, 43, 44, 45, 46, 47}; // Minutes LEDS
- // 49/50/51 = past
- // 39/40 = to
- int leds_time_minutes[][13] = {
- {103, 104, 105, 106, 107, 108, 109, -1, -1, -1, -1, -1, -1}, // 12 o'clock midnight? (0) Does this need to be changed to the o'clock word?
- { 49, 50, 51, 17, 18, 19, 20, -1, -1, -1, -1, -1, -1}, // five past
- { 49, 50, 51, 33, 34, 35, -1, -1, -1, -1, -1, -1, -1}, // ten past
- { 49, 50, 51, 7, 8, 9, 10, 11, 12, -1, -1, -1, -1}, // quarter past
- { 49, 50, 51, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1}, // twenty past
- { 49, 50, 51, 21, 22, 23, 24, 25, 17, 18, 19, 20, -1}, // twentyfive past
- { 49, 50, 51, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1}, // half past
- { 39, 40, 21, 22, 23, 24, 25, 17, 18, 19, 20, -1, -1}, // twenty five to
- { 39, 40, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1}, // twenty to
- { 39, 40, 7, 8, 9, 10, 11, 12, -1, -1, -1, -1, -1}, // quarter to
- { 39, 40, 33, 34, 35, -1, -1, -1, -1, -1, -1, -1, -1}, // ten to
- { 39, 40, 17, 18, 19, 20, -1, -1, -1, -1, -1, -1, -1}, // five to
- {103, 104, 105, 106, 107, 108, 109, -1, -1, -1, -1, -1, -1} // 12 o'clock midday? (12)
- };
- int leds_time_hours[][6] = {
- { 59, 60, 61, 62, 63, 64}, // twelve (0)
- { 99, 100, 101, -1, -1, -1}, // one
- { 78, 79, -1, -1, -1, -1}, // two
- { 94, 95, 96, 97, 98, -1}, // three
- { 55, 56, 57, 58, -1, -1}, // four
- { 73, 74, 75, 76, -1, -1}, // five
- { 92, 93, -1, -1, -1, -1}, // six
- { 65, 66, 67, 68, 65, -1}, // seven
- { 87, 88, 89, 90, -1, -1}, // eight
- { 70, 71, 72, -1, -1, -1}, // nine
- {111, 112, -1, -1, -1, -1}, // ten
- { 81, 82, 83, 84, 85, -1}, // eleven
- { 59, 60, 61, 62, 63, 64} // twelve (12)
- };
- int cijfer_links[][17] = {
- {110, 109, 108, 107, 106, 101, 95, 90, 84, 81, 75, 69, 68, 67, 66, 65, -1},
- {101, 96, 90, 89, 88, 87, 86, 85, 84, 81, -1, -1, -1, -1, -1, -1, -1},
- {110, 105, 101, 100, 95, 90, 86, 84, 81, 78, 75, 69, 68, 64, -1, -1, -1},
- {111, 105, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 66, 65, -1, -1},
- {111, 110, 109, 108, 98, 87, 78, 70, 69, 68, 67, 66, 65, 64, -1, -1, -1},
- {111, 110, 109, 108, 105, 101, 98, 95, 90, 87, 84, 81, 78, 75, 70, 66, 65},
- {110, 109, 108, 107, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 66, 65, -1},
- {111, 95, 90, 86, 85, 84, 78, 75, 70, 69, 68, -1, -1, -1, -1, -1, -1},
- {110, 109, 107, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 66, 65},
- {110, 109, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 67, 66, 65}
- };
- int cijfer_rechts[][17] = {
- { 49, 48, 47, 46, 45, 41, 35, 30, 24, 21, 15, 8, 7, 6, 5, 4, -1},
- { 41, 36, 30, 29, 28, 27, 26, 25, 24, 21, -1, -1, -1, -1, -1, -1, -1},
- { 49, 45, 44, 41, 39, 35, 30, 27, 24, 21, 17, 15, 8, 3, -1, -1, -1},
- { 50, 44, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 5, 4, -1, -1},
- { 50, 49, 48, 47, 38, 27, 18, 9, 8, 7, 6, 5, 4, 3, -1, -1, -1},
- { 50, 49, 48, 47, 44, 41, 38, 35, 30, 27, 24, 21, 18, 15, 9, 5, 4},
- { 49, 48, 47, 46, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 5, 4, -1},
- { 50, 35, 30, 26, 25, 24, 18, 15, 9, 8, 7, -1, -1, -1, -1, -1, -1},
- { 49, 48, 46, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 5, 4},
- { 49, 48, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 6, 5, 4}
- };
- int hour = -1;
- int minute = -1;
- int second = -1;
- int red = 124;
- int green = 124;
- int blue = 124;
- int white = 124;
- RgbColor color;
- RgbColor color2;
- int brightness = 50;
- int brightness2;
- bool change = false;
- bool show_date = false;
- int hue = 0;
- int counter = 0;
- int cycle_snelheid = 100;
- std::string tm;
- // New variables for fading effect
- const int FADE_STEPS = 50;
- const int FADE_DELAY = 10;
- bool fading = false;
- int fade_step = 0;
- RgbColor old_colors[PixelCount];
- RgbColor new_colors[PixelCount];
- class Wordclock : public Component, public CustomAPIDevice {
- public:
- void setup() override {
- strip.Begin();
- strip.ClearTo(RgbColor(0, 0, 0));
- strip.Show();
- // Start all LED with on and default color and brightness to check if everything is working...
- for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(red, 0, 0)); strip.Show(); delay(10); }
- for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(0, green, 0)); strip.Show(); delay(10); }
- for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(0, 0, blue)); strip.Show(); delay(10); }
- strip.ClearTo(RgbColor(0, 0, 0));
- strip.Show();
- register_service(&Wordclock::on_setled, "setled", {"number","red", "blue", "green", "white"});
- }
- void on_setled(int number, int red, int blue, int green, int white) {
- if (number < PixelCount || number > 0) {
- ESP_LOGD("setled", "Setting led number %d to color %i %i %i %i", number, red, green, blue, white );
- strip.SetPixelColor(number, RgbColor(red, green, blue));
- strip.Show();
- }
- else { ESP_LOGE("setled", "Not a valid LED Number - out of range"); }
- }
- void start_fade() {
- fading = true;
- fade_step = 0;
- for (int i = 0; i < PixelCount; i++) {
- old_colors[i] = strip.GetPixelColor(i);
- }
- }
- void apply_fade() {
- if (!fading) return;
- fade_step++;
- float progress = static_cast<float>(fade_step) / FADE_STEPS;
- for (int i = 0; i < PixelCount; i++) {
- RgbColor intermediate_color = RgbColor::LinearBlend(old_colors[i], new_colors[i], progress);
- strip.SetPixelColor(i, intermediate_color);
- }
- strip.Show();
- if (fade_step >= FADE_STEPS) {
- fading = false;
- } else {
- delay(FADE_DELAY);
- }
- }
- void loop() override {
- auto time = id(current_time).now();
- auto ldr2 = id(ldr);
- auto time_mode = id(timemodus).state;
- auto ledlight = id(neopixelbuslight).remote_values;
- int h = time.hour;
- int m = time.minute;
- int s = time.second;
- int day = time.day_of_month;
- int month = time.month;
- //convert float 0.0 till 1.0 into int 0 till 255
- red = (int)(ledlight.get_red()*255);
- green = (int)(ledlight.get_green()*255);
- blue = (int)(ledlight.get_blue()*255);
- white = (int)(ledlight.get_white()*255);
- // RgbColor color2 = RgbColor(red, green, blue);
- if(id(colorcycle).state == 1){
- color = HsbColor((float)hue/255, 1.0, 1.0);
- if(counter >= (int) (100 / id(ccspeed).state)){
- hue++;
- counter = 0;
- change = 1;
- // ESP_LOGD("loop", "Hue changed to %i", hue);
- }
- if(hue == 256){
- hue = 0;
- }
- counter++;
- }
- else{
- color2 = RgbColor(red, green, blue);
- if (color != color2) {
- change = 1;
- color = color2;
- ESP_LOGD("loop", "Color change rgb %i %i %i", red, green, blue);
- }
- }
- //check if light is on and set brightness
- if (ledlight.get_state() > 0 ) {
- if (id(useldr).state == 1) {
- brightness2 = (int)(ldr2.get_state()/100*255);
- }
- else {
- brightness2 = (int)(ledlight.get_brightness()*255);
- }
- if (brightness != brightness2) {
- change = 1;
- brightness = brightness2;
- }
- if (brightness == 0) {
- change = 1;
- brightness = 1;
- }
- }
- else {
- brightness = 0;
- change = 1;
- //ESP_LOGD("loop", "fastledlight state off - b: %i rgb %i %i %i", brightness, red, green, blue);
- // delay(500);
- }
- strip.SetBrightness(brightness);
- //check if valid time. Blink red,green,blue until valid time is present
- if (time.is_valid() == false) {
- ESP_LOGE("loop", "Got invalid time from current_time Time: %i:%i", h, m );
- strip.SetPixelColor(0, RgbColor(255, 0, 0)); strip.Show(); delay(250);
- strip.SetPixelColor(0, RgbColor(0, 255, 0)); strip.Show(); delay(250);
- strip.SetPixelColor(0, RgbColor(0, 0, 255)); strip.Show(); delay(250);
- strip.SetPixelColor(0, RgbColor(0, 0, 0)); strip.Show();
- }
- else {
- if(s == 50 && show_date == 0 && id(showdate).state == 1){
- show_date = 1;
- }
- else if(s== 54 && show_date == 1){
- change = 1;
- show_date = 0;
- }
- // only update once in a minute or on change
- if(h != hour || m != minute || change == 1 || time_mode != tm || (show_date == 1 && (s != second || change == 1))) {
- hour = h;
- minute = m;
- second = s;
- change = 0;
- tm = time_mode;
- if(show_date == 1){
- start_fade();
- for(int i = 0; i < PixelCount; i++) {
- new_colors[i] = RgbColor(0, 0, 0);
- }
- for(int i = 0; i < 17; i++) {
- if(s == 50 || s == 51){
- new_colors[cijfer_links[day / 10][i]] = color;
- new_colors[cijfer_rechts[day % 10][i]] = color;
- }
- else if(s == 52 || s == 53){
- new_colors[cijfer_links[month / 10][i]] = color;
- new_colors[cijfer_rechts[month % 10][i]] = color;
- }
- }
- }
- else if (hour >= 0){
- start_fade();
- for(int i = 0; i < PixelCount; i++) {
- new_colors[i] = RgbColor(0, 0, 0);
- }
- int tmp_hour;
- int tmp_minute = 0;
- int minutessum = minute % 5;
- if ((tm == "Minutes-min" && minute <= 30) || (tm == "Minutes-plus" && minute <= 34)){
- tmp_hour = hour % 12;
- }
- else{
- tmp_hour = (hour + 1) % 12;
- }
- if(tm == "Minutes-plus"){
- tmp_minute = (minute - (minute % 5)) / 5;
- //for(int i = 0; i < minutessum; i++) { strip.SetPixelColor(leds_minutes[i], color);} // This gradually lights up the 'minutes' one LED at a time
- // One each minute over the 5 minutes
- if((tmp_minute != 0) && (tmp_minute != 12)) { // This stops it showing 'minutes' when it's on the hour (ie, it is minutes 9 oclock)
- for (int i = 0; i < 6; i++) {
- strip.SetPixelColor(leds_minutes[i], color); // This just has all the 6 'minutes' LEDs on all the time
- }
- }
- }
- else if(tm == "Minutes-min"){
- tmp_minute = (minute + 4 - ((minute + 4) % 5)) / 5;
- //if (minutessum > 0) {
- // for(int i = minutessum-1; i < 4; i++) { strip.SetPixelColor(leds_minutes[i], color);}
- //}
- for (int i = 0; i < 6; i++) {
- strip.SetPixelColor(leds_minutes[i], color);
- }
- }
- for(int i = 0; i < 4; i++) { new_colors[leds_time_it_is[i]] = color; }
- for(int i = 0; i < 13; i++) { if(leds_time_minutes[tmp_minute][i] >= 0) { new_colors[leds_time_minutes[tmp_minute][i]] = color; } }
- for(int i = 0; i < 6; i++) { if(leds_time_hours[tmp_hour][i] >= 0) { new_colors[leds_time_hours[tmp_hour][i]] = color; } }
- if((tmp_minute != 0) && (tmp_minute != 12)) {
- for (int i = 0; i < 6; i++) {
- new_colors[leds_minutes[i]] = color;
- }
- }
- }
- }
- }
- apply_fade();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment