-
Notifications
You must be signed in to change notification settings - Fork 1
/
SeitanClock.ino
99 lines (93 loc) · 2.39 KB
/
SeitanClock.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
#include <ESP8266mDNS.h>
#include "config.h"
#include "ap.h"
#include "http_server.h"
#include "wifi.h"
#include "clock.h"
#include "display.h"
#include "light_sensor.h"
#define SETUP_PIN 13
// for how long show additional info (date/temperature) when button is pressed
#define SHOW_INFO_TIMER 2000
// should be ok for all cases
#define REFRESH_TIME 500
void setup() {
pinMode(SETUP_PIN, INPUT);
Serial.begin(115200);
clockInit();
displayInit();
}
void loop() {
configStructure cfg;
rtcData tm;
readConfig(&cfg);
if (!digitalRead(SETUP_PIN) || cfg.wifi_ssid.length() == 0) {
configStructure cfg; //reset config to default
displaySetupMode(&cfg);
startAP(&cfg);
}
wifiConnect(&cfg);
delay(2000);
MDNS.begin(cfg.host_name.c_str());
startWebServer(&cfg);
updateNTPTime(&cfg);
int loop_count = 0;
int light_avg_count = 0;
float light_avg = 0;
bool dot_state = false;
int info_type = 0; // 0 = time 1 = show year 2 = show month+day 3 = show temperature
int info_timer = 0;
while (true) {
handleWebServer();
MDNS.update();
if(!digitalRead(SETUP_PIN)) { // if setup button is pressed, display temperature, followed by date
info_type = 1;
}
if (loop_count >= REFRESH_TIME) {
loop_count = 0;
if (dot_state) { //read rtc every second (every second time dot_state updates);
getTime(&cfg, &tm);
}
dot_state = !dot_state;
switch(info_type) {
case 1:
displayYear(&cfg, &tm);
break;
case 2:
displayMonthDay(&cfg, &tm);
break;
case 3:
displayTemperature(&cfg, &tm);
break;
default:
displayTime(&cfg, &tm, dot_state);
}
if (light_avg_count == 10){
light_avg_count = 0;
if (light_avg > cfg.light_sensor_value){
cfg.color_active = cfg.color_day;
brightness(cfg.brightness_day);
}
else{
cfg.color_active = cfg.color_night;
brightness(cfg.brightness_night);
}
}
light_avg = light_avg * light_avg_count + lightValue();
++light_avg_count;
light_avg = light_avg/(light_avg_count);
}
if (info_timer >= SHOW_INFO_TIMER) {
info_timer = 0;
++info_type;
if (info_type > 3) {
info_type = 0;
}
}
if (info_type != 0) {
++info_timer;
}
++loop_count;
delay(1);
}
}