lawrencehead

Untitled

Mar 30th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.33 KB | None | 0 0
  1. #include "esphome.h"
  2. #include <NeoPixelBrightnessBus.h>
  3.  
  4. const uint16_t PixelCount = 113; // this example assumes 4 pixels, making it smaller will cause a failure
  5. const uint8_t PixelPin = 23; // make sure to set this to the correct pin, ignored for Esp8266
  6.  
  7. //Define your NeoPixel setup. Please read https://github.com/Makuna/NeoPixelBus/wiki/NeoPixelBus-object#neo-methods
  8. // NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266Uart1800KbpsMethod> strip(PixelCount, PixelPin);
  9. //NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266DmaSk6812Method> strip(PixelCount, PixelPin);
  10. NeoPixelBrightnessBus< NeoGrbFeature, NeoWs2812xMethod> strip(PixelCount, PixelPin);
  11.  
  12. //LED definitions. First led should be 0.
  13.  
  14. int leds_time_it_is[] = {0, 1, 2, 3}; // It is
  15. int leds_minutes[] = {42, 43, 44, 45, 46, 47}; // Minutes LEDS
  16.  
  17. // 49/50/51 = past
  18. // 39/40 = to
  19. int leds_time_minutes[][13] = {
  20. {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?
  21. { 49, 50, 51, 17, 18, 19, 20, -1, -1, -1, -1, -1, -1}, // five past
  22. { 49, 50, 51, 33, 34, 35, -1, -1, -1, -1, -1, -1, -1}, // ten past
  23. { 49, 50, 51, 7, 8, 9, 10, 11, 12, -1, -1, -1, -1}, // quarter past
  24. { 49, 50, 51, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1}, // twenty past
  25. { 49, 50, 51, 21, 22, 23, 24, 25, 17, 18, 19, 20, -1}, // twentyfive past
  26. { 49, 50, 51, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1}, // half past
  27. { 39, 40, 21, 22, 23, 24, 25, 17, 18, 19, 20, -1, -1}, // twenty five to
  28. { 39, 40, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1}, // twenty to
  29. { 39, 40, 7, 8, 9, 10, 11, 12, -1, -1, -1, -1, -1}, // quarter to
  30. { 39, 40, 33, 34, 35, -1, -1, -1, -1, -1, -1, -1, -1}, // ten to
  31. { 39, 40, 17, 18, 19, 20, -1, -1, -1, -1, -1, -1, -1}, // five to
  32. {103, 104, 105, 106, 107, 108, 109, -1, -1, -1, -1, -1, -1} // 12 o'clock midday? (12)
  33. };
  34.  
  35.  
  36. int leds_time_hours[][6] = {
  37. { 59, 60, 61, 62, 63, 64}, // twelve (0)
  38. { 99, 100, 101, -1, -1, -1}, // one
  39. { 78, 79, -1, -1, -1, -1}, // two
  40. { 94, 95, 96, 97, 98, -1}, // three
  41. { 55, 56, 57, 58, -1, -1}, // four
  42. { 73, 74, 75, 76, -1, -1}, // five
  43. { 92, 93, -1, -1, -1, -1}, // six
  44. { 65, 66, 67, 68, 65, -1}, // seven
  45. { 87, 88, 89, 90, -1, -1}, // eight
  46. { 70, 71, 72, -1, -1, -1}, // nine
  47. {111, 112, -1, -1, -1, -1}, // ten
  48. { 81, 82, 83, 84, 85, -1}, // eleven
  49. { 59, 60, 61, 62, 63, 64} // twelve (12)
  50. };
  51.  
  52. int cijfer_links[][17] = {
  53. {110, 109, 108, 107, 106, 101, 95, 90, 84, 81, 75, 69, 68, 67, 66, 65, -1},
  54. {101, 96, 90, 89, 88, 87, 86, 85, 84, 81, -1, -1, -1, -1, -1, -1, -1},
  55. {110, 105, 101, 100, 95, 90, 86, 84, 81, 78, 75, 69, 68, 64, -1, -1, -1},
  56. {111, 105, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 66, 65, -1, -1},
  57. {111, 110, 109, 108, 98, 87, 78, 70, 69, 68, 67, 66, 65, 64, -1, -1, -1},
  58. {111, 110, 109, 108, 105, 101, 98, 95, 90, 87, 84, 81, 78, 75, 70, 66, 65},
  59. {110, 109, 108, 107, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 66, 65, -1},
  60. {111, 95, 90, 86, 85, 84, 78, 75, 70, 69, 68, -1, -1, -1, -1, -1, -1},
  61. {110, 109, 107, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 66, 65},
  62. {110, 109, 106, 101, 98, 95, 90, 87, 84, 81, 78, 75, 69, 68, 67, 66, 65}
  63. };
  64.  
  65. int cijfer_rechts[][17] = {
  66. { 49, 48, 47, 46, 45, 41, 35, 30, 24, 21, 15, 8, 7, 6, 5, 4, -1},
  67. { 41, 36, 30, 29, 28, 27, 26, 25, 24, 21, -1, -1, -1, -1, -1, -1, -1},
  68. { 49, 45, 44, 41, 39, 35, 30, 27, 24, 21, 17, 15, 8, 3, -1, -1, -1},
  69. { 50, 44, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 5, 4, -1, -1},
  70. { 50, 49, 48, 47, 38, 27, 18, 9, 8, 7, 6, 5, 4, 3, -1, -1, -1},
  71. { 50, 49, 48, 47, 44, 41, 38, 35, 30, 27, 24, 21, 18, 15, 9, 5, 4},
  72. { 49, 48, 47, 46, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 5, 4, -1},
  73. { 50, 35, 30, 26, 25, 24, 18, 15, 9, 8, 7, -1, -1, -1, -1, -1, -1},
  74. { 49, 48, 46, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 5, 4},
  75. { 49, 48, 45, 41, 38, 35, 30, 27, 24, 21, 18, 15, 8, 7, 6, 5, 4}
  76. };
  77.  
  78. int hour = -1;
  79. int minute = -1;
  80. int second = -1;
  81. int red = 124;
  82. int green = 124;
  83. int blue = 124;
  84. int white = 124;
  85. RgbColor color;
  86. RgbColor color2;
  87. int brightness = 50;
  88. int brightness2;
  89. bool change = false;
  90. bool show_date = false;
  91. int hue = 0;
  92. int counter = 0;
  93. int cycle_snelheid = 100;
  94. std::string tm;
  95.  
  96. // New variables for fading effect
  97. const int FADE_STEPS = 50;
  98. const int FADE_DELAY = 10;
  99. bool fading = false;
  100. int fade_step = 0;
  101. RgbColor old_colors[PixelCount];
  102. RgbColor new_colors[PixelCount];
  103.  
  104. class Wordclock : public Component, public CustomAPIDevice {
  105. public:
  106. void setup() override {
  107.  
  108. strip.Begin();
  109. strip.ClearTo(RgbColor(0, 0, 0));
  110. strip.Show();
  111.  
  112. // Start all LED with on and default color and brightness to check if everything is working...
  113. for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(red, 0, 0)); strip.Show(); delay(10); }
  114. for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(0, green, 0)); strip.Show(); delay(10); }
  115. for(int i = 0; i < PixelCount; i++) { strip.SetPixelColor(i, RgbColor(0, 0, blue)); strip.Show(); delay(10); }
  116. strip.ClearTo(RgbColor(0, 0, 0));
  117. strip.Show();
  118. register_service(&Wordclock::on_setled, "setled", {"number","red", "blue", "green", "white"});
  119. }
  120.  
  121. void on_setled(int number, int red, int blue, int green, int white) {
  122. if (number < PixelCount || number > 0) {
  123. ESP_LOGD("setled", "Setting led number %d to color %i %i %i %i", number, red, green, blue, white );
  124. strip.SetPixelColor(number, RgbColor(red, green, blue));
  125. strip.Show();
  126. }
  127. else { ESP_LOGE("setled", "Not a valid LED Number - out of range"); }
  128. }
  129.  
  130. void start_fade() {
  131. fading = true;
  132. fade_step = 0;
  133. for (int i = 0; i < PixelCount; i++) {
  134. old_colors[i] = strip.GetPixelColor(i);
  135. }
  136. }
  137.  
  138. void apply_fade() {
  139. if (!fading) return;
  140.  
  141. fade_step++;
  142. float progress = static_cast<float>(fade_step) / FADE_STEPS;
  143.  
  144. for (int i = 0; i < PixelCount; i++) {
  145. RgbColor intermediate_color = RgbColor::LinearBlend(old_colors[i], new_colors[i], progress);
  146. strip.SetPixelColor(i, intermediate_color);
  147. }
  148.  
  149. strip.Show();
  150.  
  151. if (fade_step >= FADE_STEPS) {
  152. fading = false;
  153. } else {
  154. delay(FADE_DELAY);
  155. }
  156. }
  157.  
  158. void loop() override {
  159. auto time = id(current_time).now();
  160. auto ldr2 = id(ldr);
  161. auto time_mode = id(timemodus).state;
  162.  
  163. auto ledlight = id(neopixelbuslight).remote_values;
  164.  
  165. int h = time.hour;
  166. int m = time.minute;
  167. int s = time.second;
  168. int day = time.day_of_month;
  169. int month = time.month;
  170.  
  171.  
  172.  
  173. //convert float 0.0 till 1.0 into int 0 till 255
  174. red = (int)(ledlight.get_red()*255);
  175. green = (int)(ledlight.get_green()*255);
  176. blue = (int)(ledlight.get_blue()*255);
  177. white = (int)(ledlight.get_white()*255);
  178.  
  179. // RgbColor color2 = RgbColor(red, green, blue);
  180.  
  181. if(id(colorcycle).state == 1){
  182. color = HsbColor((float)hue/255, 1.0, 1.0);
  183. if(counter >= (int) (100 / id(ccspeed).state)){
  184. hue++;
  185. counter = 0;
  186. change = 1;
  187. // ESP_LOGD("loop", "Hue changed to %i", hue);
  188. }
  189. if(hue == 256){
  190. hue = 0;
  191. }
  192. counter++;
  193. }
  194. else{
  195. color2 = RgbColor(red, green, blue);
  196. if (color != color2) {
  197. change = 1;
  198. color = color2;
  199. ESP_LOGD("loop", "Color change rgb %i %i %i", red, green, blue);
  200. }
  201. }
  202.  
  203. //check if light is on and set brightness
  204. if (ledlight.get_state() > 0 ) {
  205. if (id(useldr).state == 1) {
  206. brightness2 = (int)(ldr2.get_state()/100*255);
  207. }
  208. else {
  209. brightness2 = (int)(ledlight.get_brightness()*255);
  210. }
  211.  
  212. if (brightness != brightness2) {
  213. change = 1;
  214. brightness = brightness2;
  215. }
  216. if (brightness == 0) {
  217. change = 1;
  218. brightness = 1;
  219. }
  220. }
  221. else {
  222. brightness = 0;
  223. change = 1;
  224. //ESP_LOGD("loop", "fastledlight state off - b: %i rgb %i %i %i", brightness, red, green, blue);
  225. // delay(500);
  226. }
  227.  
  228. strip.SetBrightness(brightness);
  229.  
  230. //check if valid time. Blink red,green,blue until valid time is present
  231.  
  232. if (time.is_valid() == false) {
  233. ESP_LOGE("loop", "Got invalid time from current_time Time: %i:%i", h, m );
  234. strip.SetPixelColor(0, RgbColor(255, 0, 0)); strip.Show(); delay(250);
  235. strip.SetPixelColor(0, RgbColor(0, 255, 0)); strip.Show(); delay(250);
  236. strip.SetPixelColor(0, RgbColor(0, 0, 255)); strip.Show(); delay(250);
  237. strip.SetPixelColor(0, RgbColor(0, 0, 0)); strip.Show();
  238. }
  239. else {
  240.  
  241. if(s == 50 && show_date == 0 && id(showdate).state == 1){
  242. show_date = 1;
  243. }
  244. else if(s== 54 && show_date == 1){
  245. change = 1;
  246. show_date = 0;
  247. }
  248.  
  249.  
  250. // only update once in a minute or on change
  251. if(h != hour || m != minute || change == 1 || time_mode != tm || (show_date == 1 && (s != second || change == 1))) {
  252. hour = h;
  253. minute = m;
  254. second = s;
  255. change = 0;
  256. tm = time_mode;
  257.  
  258. if(show_date == 1){
  259. start_fade();
  260. for(int i = 0; i < PixelCount; i++) {
  261. new_colors[i] = RgbColor(0, 0, 0);
  262. }
  263. for(int i = 0; i < 17; i++) {
  264. if(s == 50 || s == 51){
  265. new_colors[cijfer_links[day / 10][i]] = color;
  266. new_colors[cijfer_rechts[day % 10][i]] = color;
  267. }
  268. else if(s == 52 || s == 53){
  269. new_colors[cijfer_links[month / 10][i]] = color;
  270. new_colors[cijfer_rechts[month % 10][i]] = color;
  271. }
  272. }
  273. }
  274. else if (hour >= 0){
  275. start_fade();
  276. for(int i = 0; i < PixelCount; i++) {
  277. new_colors[i] = RgbColor(0, 0, 0);
  278. }
  279. int tmp_hour;
  280. int tmp_minute = 0;
  281. int minutessum = minute % 5;
  282.  
  283.  
  284. if ((tm == "Minutes-min" && minute <= 30) || (tm == "Minutes-plus" && minute <= 34)){
  285. tmp_hour = hour % 12;
  286. }
  287. else{
  288. tmp_hour = (hour + 1) % 12;
  289. }
  290.  
  291. if(tm == "Minutes-plus"){
  292. tmp_minute = (minute - (minute % 5)) / 5;
  293. //for(int i = 0; i < minutessum; i++) { strip.SetPixelColor(leds_minutes[i], color);} // This gradually lights up the 'minutes' one LED at a time
  294. // One each minute over the 5 minutes
  295. if((tmp_minute != 0) && (tmp_minute != 12)) { // This stops it showing 'minutes' when it's on the hour (ie, it is minutes 9 oclock)
  296. for (int i = 0; i < 6; i++) {
  297. strip.SetPixelColor(leds_minutes[i], color); // This just has all the 6 'minutes' LEDs on all the time
  298. }
  299. }
  300. }
  301. else if(tm == "Minutes-min"){
  302. tmp_minute = (minute + 4 - ((minute + 4) % 5)) / 5;
  303. //if (minutessum > 0) {
  304. // for(int i = minutessum-1; i < 4; i++) { strip.SetPixelColor(leds_minutes[i], color);}
  305. //}
  306. for (int i = 0; i < 6; i++) {
  307. strip.SetPixelColor(leds_minutes[i], color);
  308. }
  309. }
  310.  
  311.  
  312. for(int i = 0; i < 4; i++) { new_colors[leds_time_it_is[i]] = color; }
  313. for(int i = 0; i < 13; i++) { if(leds_time_minutes[tmp_minute][i] >= 0) { new_colors[leds_time_minutes[tmp_minute][i]] = color; } }
  314. for(int i = 0; i < 6; i++) { if(leds_time_hours[tmp_hour][i] >= 0) { new_colors[leds_time_hours[tmp_hour][i]] = color; } }
  315.  
  316. if((tmp_minute != 0) && (tmp_minute != 12)) {
  317. for (int i = 0; i < 6; i++) {
  318. new_colors[leds_minutes[i]] = color;
  319. }
  320. }
  321. }
  322. }
  323. }
  324.  
  325. apply_fade();
  326. }
  327. };
Advertisement
Add Comment
Please, Sign In to add comment