-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
123 lines (96 loc) · 2.72 KB
/
main.cpp
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
#include <Arduino.h>
#ifndef UNIT_TEST
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>
//
// Connect a 4x6 matrix to D1 pin
// the first 4 leds will be the ones of seconds and the second four the
// tens of seconds and so on
//
// L5 - L4
// | |
// L6 L3
// etc | |
// L1 L7 L2
// | | |
// L9 - L8 L1
//
#define PIN D1
#define PIXELS 24 // 6 by 4 array
WiFiManager wifiManager;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600*2, 60000);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ400);
volatile bool tick;
void inline timer0_ISR (void) {
tick = true;
// reprime the timer
timer0_write(ESP.getCycleCount() + 80000000L); // 80MHz == 1sec
}
void setup()
{
// start serial so we can easily debug through it
Serial.begin(115200);
// start and clear the strip
strip.begin();
strip.clear();
strip.show();
// start wifi manager, it will either make connection
// or open ap for saving the network config
wifiManager.autoConnect();
// start ntp client
timeClient.begin();
// connect timer interrupt
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(timer0_ISR);
timer0_write(ESP.getCycleCount() + 80000000L); // 80MHZ / 1sec
interrupts();
// prime our first tick
tick = true;
}
void show(uint8_t offset, int t) {
// offset 0 for seconds
// offset 8 for minutes
// offset 12 for hours
uint8_t tens = t / 10;
uint8_t ones = t % 10;
// ones go up
for (uint8_t i = 0; i < 4; i++) {
if(bitRead(ones, i)) {
strip.setPixelColor(offset+7-i, strip.Color(0, 0, 255));
}
}
// tens go down
for (uint8_t i = 0; i < 4; i++) {
if(bitRead(tens, i)) {
strip.setPixelColor(offset+i, strip.Color(0, 0, 255));
}
}
}
void loop()
{
// update on every tick
if(tick) {
// update timeclient
timeClient.update();
// debug output to serial
Serial.println(timeClient.getFormattedTime());
// clear the strip
strip.clear();
// update the strip
show(16, timeClient.getSeconds());
show(8, timeClient.getMinutes());
show(0, timeClient.getHours());
// push to the strip
strip.show();
// wait for next tick
tick = false;
}
}
#endif