-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroutines.ino
238 lines (219 loc) · 7.93 KB
/
routines.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//format bytes as KB, MB or GB with indicator
String humanReadableSize( size_t bytes ) {
if ( bytes < 1024) {
return String( bytes ) + "B";
} else if ( bytes < ( 1024 * 1024 ) ) {
return String( bytes / 1024.0 ) + "KB";
} else if (bytes < ( 1024 * 1024 * 1024 ) ) {
return String( bytes / 1024.0 / 1024.0 ) + "MB";
} else {
return String( bytes / 1024.0 / 1024.0 / 1024.0 ) + "GB";
}
}
float mapFloat( double x, const double in_min, const double in_max, const double out_min, const double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
String formattedTime( const time_t t) { ///output a time with if necessary leading zeros
String s = String(hour(t)) + ":";
if ( (byte)minute(t) < 10 ) {
s += "0";
}
s += String(minute(t)) + ":";
if ( (byte)second(t) < 10 ) {
s += "0";
}
s += second(t);
return s;
}
time_t localTime() {
time_t t = now() + ( timeZone * 3600 );
if ( dstEnabled && isDST( month(t), day(t), dayOfWeek(t) ) ) {
t += 3600;
}
return t;
}
// IsDST(): returns true if during DST, false otherwise
// For CEST time!! http://stackoverflow.com/a/22761920
boolean isDST( int month, int day, int dow ) {
if ( month < 3 || month > 11 ) {
return false; // January, February, and December are out.
}
if ( month > 3 && month < 11 ) {
return true; // April to October are in
}
int previousSunday = day - dow;
if ( month == 3 ) {
return previousSunday >= 25; // In March, we are DST if our previous Sunday was on or after the 25th.
}
if (month == 10) {
return previousSunday < 25; // In November we must be before the first Sunday to be DST. That means the previous Sunday must be before the 25st.
}
}
void setPercentage( const byte thisChannel, const time_t secondsToday ) {
if ( secondsToday != 0 ) { ///to solve flashing at midnight due to secondsToday which cant be smaller than 0 -- so at midnight there is no adjusting
byte thisTimer = 0;
while ( channel[thisChannel].timer[thisTimer].time < secondsToday ) {
thisTimer++;
}
float dimPercentage = channel[thisChannel].timer[thisTimer].percentage - channel[thisChannel].timer[thisTimer - 1].percentage;
time_t numberOfSecondsBetween = channel[thisChannel].timer[thisTimer].time - channel[thisChannel].timer[thisTimer - 1].time;
time_t secondsSinceLastTimer = secondsToday - channel[thisChannel].timer[thisTimer - 1].time;
float changePerSecond = dimPercentage / numberOfSecondsBetween;
channel[thisChannel].currentPercentage = channel[thisChannel].timer[thisTimer - 1].percentage + ( secondsSinceLastTimer * changePerSecond );
//check if channel has a minimum set
if ( channel[thisChannel].currentPercentage < channel[thisChannel].minimumLevel ) {
channel[thisChannel].currentPercentage = channel[thisChannel].minimumLevel;
}
analogWrite( channel[thisChannel].pin, ( int )mapFloat( channel[thisChannel].currentPercentage, 0, 100, 0, PWMdepth ) );
}
}
void updateChannels() {
if ( programOverride ) {
return;
}
//get the current timeInSeconds and use that as time argument for setPercentage
time_t secondsToday = elapsedSecsToday( localTime() ); //elapsedSecsToday lives in TimeLib.h
for ( byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
setPercentage(thisChannel, secondsToday );
}
}
bool defaultTimersAreLoaded() { //this function loads the timers or returns FALSE
//find 'default.aqu' on SPIFFS disk and if present load the timerdata from this file
//return false on error
File f = SPIFFS.open( "/default.aqu", "r" );
if (!f) {
Serial.println( F("No default timer file found.") );
return false;
}
String lineBuf;
byte currentTimer = 0;
byte thisChannel;
while ( f.position() < f.size() ) {
lineBuf = f.readStringUntil( '\n' );
if ( lineBuf.indexOf( "[" ) > -1 ) {
String thisChannelStr;
thisChannelStr = lineBuf.substring( lineBuf.indexOf("[") + 1, lineBuf.indexOf("]") );
thisChannel = thisChannelStr.toInt();
currentTimer = 0;
} else {
String thisPercentage;
String thisTime;
thisTime = lineBuf.substring( 0, lineBuf.indexOf(",") );
thisPercentage = lineBuf.substring( lineBuf.indexOf(",") + 1 );
channel[thisChannel].timer[currentTimer].time = thisTime.toInt();
channel[thisChannel].timer[currentTimer].percentage = thisPercentage.toInt();
currentTimer++;
channel[thisChannel].numberOfTimers = currentTimer;
}
}
f.close();
//add the 24:00 timers ( copy of timer percentage no: 0 )
for (thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
channel[thisChannel].timer[channel[thisChannel].numberOfTimers].time = 86400;
channel[thisChannel].timer[channel[thisChannel].numberOfTimers].percentage = channel[thisChannel].timer[0].percentage;
}
return true;
}
void setNewHostname() {
WiFi.hostname( myWIFIhostname );
WiFi.mode( WIFI_OFF );
WiFi.begin();
OLED.clear();
OLED.setTextAlignment( TEXT_ALIGN_CENTER );
OLED.setFont( ArialMT_Plain_16 );
OLED.drawString( 64, 10, F( "Setting" ) );
OLED.drawString( 64, 30, F( "hostname..." ) );
OLED.display();
while ( WiFi.status() != WL_CONNECTED ) {
delay(20);
yield();
}
hostNameChanged = false;
}
void writeConfigFile() {
File f = SPIFFS.open( configFile , "w");
if (!f) {
Serial.println( F( "config file error. No data saved." ) );
return;
}
f.println( "timezone=" + String( timeZone ) );
f.println( "pwmfrequency=" + String( PWMfrequency ) );
f.println( "pwmdepth=" + String( PWMdepth ) );
f.println( "ntpinterval=" + String( ntpInterval ) );
f.print( "apply_dst=" ); f.println( dstEnabled ? F( "enabled" ) : F( "disabled" ) );
f.print( "oled=" ); f.println( OLEDflipped ? F( "flipped" ) : F( "normal" ) );
f.close();
Serial.print( F( "System settings saved as " ) ); Serial.println( configFile );
}
void writeMinimumLevelFile() {
File f = SPIFFS.open( minimumLevelFile , "w");
if (!f) {
Serial.println( F( "Minimum level file error. No data saved." ) );
return;
}
for ( byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
f.println( channel[thisChannel].minimumLevel );
}
f.close();
}
void readMinimumLevelFile() {
File f = SPIFFS.open( minimumLevelFile , "r");
if (!f) {
Serial.println( F( "Minimum level file error. No data read." ) );
return;
}
for ( byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
String s = f.readStringUntil('\n');
channel[thisChannel].minimumLevel = s.toFloat();
}
f.close();
}
void writeChannelNameFile() {
File f = SPIFFS.open( channelNameFile , "w");
if (!f) {
Serial.println( F( "Channel name file error. No data saved." ) );
return;
}
for ( byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
f.println( channel[thisChannel].name );
}
f.close();
}
void readChannelNameFile() {
File f = SPIFFS.open( channelNameFile , "r");
if (!f) {
Serial.println( F( "Channel name file error. No data read." ) );
return;
}
for (byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
String s = f.readStringUntil('\n');
s.trim();
channel[thisChannel].name = s;
}
f.close();
}
void writeChannelColorFile() {
File f = SPIFFS.open( channelColorFile , "w");
if (!f) {
Serial.println( F( "Channel color file error. No data saved." ) );
return;
}
for ( byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
f.println( channel[thisChannel].color );
}
f.close();
}
void readChannelColorFile() {
File f = SPIFFS.open( channelColorFile , "r");
if (!f) {
Serial.println( F( "Channel color file error. No data read." ) );
return;
}
for (byte thisChannel = 0; thisChannel < numberOfChannels; thisChannel++ ) {
String s = f.readStringUntil('\n');
s.trim();
channel[thisChannel].color = s;
}
f.close();
}