-
Notifications
You must be signed in to change notification settings - Fork 0
/
HorlogeBodet.ino
302 lines (273 loc) · 8.66 KB
/
HorlogeBodet.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <EEPROM.h>
#include <Wire.h>
#include "DS3231.h"
#include "LowPower.h"
//includes pour sleep mode
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
//#define DEBUG_SERIAL 1
#if defined(__AVR_ATmega32U4__)
#define PIN_INTERRUPT 0
#define ID_INTERRUPT 2
#else
#define ID_INTERRUPT 0
#define PIN_INTERRUPT 2
#endif
#define PIN_1 9
#define PIN_2 10
#define PIN_FF 4
#define PULSE_WIDTH 400
#define PERIOD 1000
#define OFFSET_WINTER 3600
#define OFFSET_SUMMER 7200
#define OFFSET_ADDRESS 0
#define FAST_FORWARD_DELAY 400
//positive if clock is disabled (for specified number of cycles)
//used to disable clock for 1h during winter time change
uint8_t disabled = 0;
RTClib RTC;
DS3231 clock;
volatile uint8_t tickCounter=0;
//true if a tick interrupt has been received
volatile uint8_t tickReceived=0;
#ifdef DEBUG_SERIAL
void dumpTimestamp(char* msg){
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println(msg);
}
#endif
/************************************************************************/
/* Wake-up interrupt routine */
/************************************************************************/
void tick(){
if(tickCounter == 0){
tickReceived = true;
}else{
tickReceived = false;
}
tickCounter++;
if(tickCounter == 30){
tickCounter = 0;
}
}
void setup() {
Wire.begin();
pinMode(PIN_1, OUTPUT);
pinMode(PIN_2, OUTPUT);
pinMode(PIN_FF,INPUT_PULLUP);
pinMode(PIN_INTERRUPT, INPUT);
digitalWrite(PIN_1,LOW);
digitalWrite(PIN_2,LOW);
#ifdef DEBUG_SERIAL
Serial.begin(9600);
#endif
//init RTC
clock.enableOscillator(true,false,0); //enable 1Hz on SQW output
DateTime now = RTC.now();
if(now.year() == 2000){
//first run or dead battery, reinit to compile time & date (time must be UTC!)
//DateTime newDT = DateTime(__DATE__, __TIME__);
//DateTime newDT = DateTime(2015,10,31,2,55);
//DateTime newDT = DateTime(2015,10,25,0,55);
DateTime newDT = DateTime(2015,05,21,12,48);
clock.setClockMode(false); // set to 24h
clock.setYear(newDT.year()-2000);
clock.setMonth(newDT.month());
clock.setDate(newDT.day());
clock.setHour(newDT.hour());
clock.setMinute(newDT.minute());
clock.setSecond(newDT.second());
}
attachInterrupt(ID_INTERRUPT, tick, RISING);
#ifdef DEBUG_SERIAL
dumpTimestamp("starting loop");
#endif
}
/************************************************************************/
/* Send pulse to clock motor - pulse polarity is automatically inverted on each call */
/************************************************************************/
void pulse(uint16_t pulseWidth)
{
static uint8_t step = 0;
uint8_t pin;
if (step % 2 == 0){
pin = PIN_1;
}else{
pin = PIN_2;
}
digitalWrite(pin,HIGH);
delay(pulseWidth);
digitalWrite(pin,LOW);
step++;
}
/************************************************************************/
/* record time offset in eeprom */
/************************************************************************/
void recordOffset(uint16_t offset)
{
EEPROM.write(OFFSET_ADDRESS,offset & 0xFF);
EEPROM.write(OFFSET_ADDRESS+1,(offset >> 8 ) & 0xFF);
}
/************************************************************************/
/* read time offset from eeprom */
/************************************************************************/
uint16_t readOffset()
{
uint16_t offset = EEPROM.read(OFFSET_ADDRESS) | (EEPROM.read(OFFSET_ADDRESS+1) << 8);
return offset;
}
/************************************************************************/
/* Calculates DST change.
/* returns 0: no change necessary, 1: summer time change required, -1: winter time change required */
/************************************************************************/
int8_t calcDSTChange()
{
int8_t change = 0;
uint16_t currentOffset = getCurrentTimeOffset();
uint16_t previousOffset = readOffset();
#ifdef DEBUG_SERIAL
Serial.print("currentOffset: ");
Serial.println(currentOffset);
Serial.print("previousOffset: ");
Serial.println(previousOffset);
#endif
if(previousOffset != 0 && (previousOffset != currentOffset)){
//change required
recordOffset(currentOffset);
if(currentOffset == OFFSET_SUMMER){
change = +1;
}else{
if(currentOffset == OFFSET_WINTER){
change = -1;
}
}
}
return change;
}
uint16_t adjustDstEurope(DateTime t)
{
/*You can use the following equations to calculate when DST starts and ends.
The divisions are integer divisions, in which remainders are discarded.
"mod" means the remainder when doing integer division, e.g., 20 mod 7 = 6.
That is, 20 divided by 7 is 2 and 6/7th (where six is the remainder).
With: y = year.
For the United States:
Begin DST: Sunday April (2+6*y-y/4) mod 7+1
End DST: Sunday October (31-(y*5/4+1) mod 7)
Valid for years 1900 to 2006, though DST wasn't adopted until the 1950s-1960s. 2007 and after:
Begin DST: Sunday March 14 - (1 + y*5/4) mod 7
End DST: Sunday November 7 - (1 + y*5/4) mod 7;
European Economic Community:
Begin DST: Sunday March (31 - (5*y/4 + 4) mod 7) at 1h U.T.
End DST: Sunday October (31 - (5*y/4 + 1) mod 7) at 1h U.T.
Since 1996, valid through 2099
(Equations by Wei-Hwa Huang (US), and Robert H. van Gent (EC))
Adjustig Time with DST Europe/France/Paris: UTC+1h in winter, UTC+2h in summer
*/
// last sunday of march
int beginDSTDate= (31 - (5* t.year() /4 + 4) % 7);
//Serial.println(beginDSTDate);
int beginDSTMonth=3;
//last sunday of october
int endDSTDate= (31 - (5 * t.year() /4 + 1) % 7);
//Serial.println(endDSTDate);
int endDSTMonth=10;
// DST is valid as:
if (((t.month() > beginDSTMonth) && (t.month() < endDSTMonth))
|| ((t.month() == beginDSTMonth) && (t.day() > beginDSTDate))
|| ((t.month() == beginDSTMonth) && (t.day() == beginDSTDate) && (t.hour() >= 1))
|| ((t.month() == endDSTMonth) && (t.day() < endDSTDate))
|| ((t.month() == endDSTMonth) && (t.day() == endDSTDate) && (t.hour() < 1)))
return 7200; // DST europe = utc +2 hour (summer time)
else return 3600; // nonDST europe = utc +1 hour (winter time)
}
uint32_t getCurrentTimeOffset()
{
DateTime now = RTC.now();
uint32_t offset = adjustDstEurope(now);
return offset;
}
/*********************************************************************************************/
/* add 1h to the displayed time, and compensates for the time taken to move the clock hands */
/*********************************************************************************************/
void fastForwardToSummerTime(){
detachInterrupt(PIN_INTERRUPT);
for(int p=0;p<121;p++){
pulse(PULSE_WIDTH);
delay(FAST_FORWARD_DELAY);
}
attachInterrupt(ID_INTERRUPT, tick, RISING);
}
void loop() {
unsigned long t1,t2;
//fast-forward mode
if(!digitalRead(PIN_FF)){
detachInterrupt(PIN_INTERRUPT);
do{
pulse(PULSE_WIDTH);
delay(FAST_FORWARD_DELAY);
}while(!digitalRead(PIN_FF));
tickCounter=1;
attachInterrupt(ID_INTERRUPT, tick, RISING);
}
if(tickReceived){
#ifdef DEBUG_SERIAL
dumpTimestamp("tickReceived");
#endif
#ifdef DEBUG_SERIAL
Serial.print("disabled: ");
Serial.println(disabled);
#endif
int8_t changeRequired = calcDSTChange();
#ifdef DEBUG_SERIAL
Serial.print("changeRequired: ");
Serial.println(changeRequired,DEC);
#endif
if(changeRequired == -1){
disabled = 120; //2 cycles per min
}else{
if(changeRequired == 1){
fastForwardToSummerTime();
}
}
//check if DTS time change is required
//TODO implement
//if -1 -> disabled = 3600
//if 1 -> fast-forward for 1h + delta secs (to be measured)
//main loop
if(disabled == 0){
t1 = millis();
pulse(PULSE_WIDTH);
t2 = millis();
}else{
//updates are disabled
disabled--;
}
}
//delay(PERIOD-((t2-t1)));
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is rising
//TODO set BOD_OFF after tests
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_ON);
//LowPower.powerSave(SLEEP_FOREVER, ADC_OFF, BOD_ON,TIMER2_ON);
//LowPower.idle(SLEEP_FOREVER, ADC_OFF,TIMER4_ON,TIMER3_ON, TIMER1_ON, TIMER0_ON,SPI_ON,USART1_ON,TWI_ON,USB_ON);
//TODO supprimer apr�s tests
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
delay(25);
digitalWrite(13,LOW);
}