-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightClock.ino
133 lines (98 loc) · 2.75 KB
/
lightClock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifdef ESP8266
# include <ArduinoOTA.h>
# include <ESP8266WiFi.h>
# include <ESP8266mDNS.h>
# include <WiFiManager.h>
#endif
#include <Adafruit_NeoPixel.h>
#include <Time.h>
#include <Timezone.h> // https://github.com/JChristensen/Timezone
#include "NtpClient.h"
Adafruit_NeoPixel pixels = { 60, 5, NEO_GRB + NEO_KHZ800 };
static uint32_t PIXEL_COLOR_MINUTES;
static uint32_t PIXEL_COLOR_HOURS;
void infoLight(uint32_t color) {
for (int i = 0; i < 60; i++) {
pixels.setPixelColor(i, color);
pixels.show();
delay(10);
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
}
time_t getLocalTime() {
//Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; //Central European Standard Time
Timezone localTimezone(CEST, CET);
return localTimezone.toLocal(now());
}
void setup() {
Serial.begin(115200);
pixels.begin();
pixels.clear();
pixels.show();
PIXEL_COLOR_MINUTES = pixels.Color(192, 0, 64);
PIXEL_COLOR_HOURS = pixels.Color(32, 0, 224);
char clock_name[32];
sprintf(clock_name, "lightclock-%06x", ESP.getChipId());
ArduinoOTA.setHostname(clock_name);
WiFi.hostname(clock_name);
Serial.println("Initializing ...");
infoLight(pixels.Color(255, 255, 255));
Serial.println("Starting WiFiManager ...");
WiFiManager wifiManager;
wifiManager.setConfigPortalTimeout(120);
wifiManager.autoConnect(clock_name);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Waiting for WiFi ...");
infoLight(pixels.Color(255, 0, 0));
delay(500);
}
// Show that we are connected
Serial.println("WiFi connected...");
infoLight(pixels.Color(0, 255, 0));
ArduinoOTA.begin();
syncTime();
Serial.println("Setup complete.");
}
uint8_t p(uint8_t i) {
return (i + 15) % 60;
}
void loop() {
ArduinoOTA.handle();
time_t t = getLocalTime();
uint8_t h = (hour(t) % 12) * 5;
uint8_t m = minute(t);
static uint8_t last_m = 255;
if (last_m == m) {
// no change -> no repaint
return;
}
last_m = m;
h += m / 12;
h = p(h);
m = p(m);
if (h == m) {
paintOverlayClockHands(m);
} else {
paintClockHands(h, m);
}
pixels.show();
}
void paintOverlayClockHands(uint8_t m) {
for (uint8_t i = (m + 59) % 60; i != m; i = (i + 59) % 60) {
pixels.setPixelColor(i, PIXEL_COLOR_HOURS);
pixels.show();
delay(50);
}
pixels.setPixelColor(m, PIXEL_COLOR_MINUTES);
}
void paintClockHands(uint8_t h, uint8_t m) {
for (uint8_t i = h; i != m; i = (i + 1) % 60) {
pixels.setPixelColor(i, PIXEL_COLOR_MINUTES);
}
for (uint8_t i = m; i != h; i = (i + 1) % 60) {
pixels.setPixelColor(i, PIXEL_COLOR_HOURS);
}
}