-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeHandling.ino
More file actions
130 lines (104 loc) · 3.41 KB
/
TimeHandling.ino
File metadata and controls
130 lines (104 loc) · 3.41 KB
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
// Timestamps are stored 48 bytes deep in the EEPROM.
#define TIMESTAMP_STORAGE_EEPROM_OFFSET 48
#define RED dates[2]
#define GREEN dates[1]
#define YELLOW dates[0]
/**
* Store the time and date from the Timestamp within the RTC.
* This will preserve the second counter from the RTC.
*/
void updateRTCFromTimestamp(TimeStamp *tsp){
DateTime dt = Clock.read();
dt.Minute = tsp->minute;
dt.Hour = tsp->hour;
dt.Day = tsp->day;
dt.Month = tsp->month;
dt.Year = tsp->year;
dt.Second = 0;
Clock.write(dt);
}
void readTimestampFromRTC(TimeStamp *tsp){
DateTime dt = Clock.read();
tsp->second = dt.Second;
tsp->minute = dt.Minute;
tsp->hour = dt.Hour;
tsp->day = dt.Day;
tsp->month = dt.Month;
tsp->year = dt.Year;
}
void copyTimestamp(TimeStamp *from, TimeStamp *to){
to->second = from->second;
to->minute = from->minute;
to->hour = from->hour;
to->day = from->day;
to->month = from->month;
to->year = from->year;
to->century = from->century;
}
boolean isTimeStampSane(TimeStamp *tsp){
if(tsp->second > 59)return false; // 00-59 seconds per minute
if(tsp->minute > 59)return false; // 00-59 minutes per hour
if(tsp->hour > 23)return false; // 00-23 hours per day
if(tsp->month > 12 || tsp->month < 1)return false; // 01-12 months per year
if(tsp->day < 1)return false; // 01-?? days in a month, day 0 does not compute...
uint16_t year = ((uint16_t)tsp->century)*(uint16_t)100 + ((uint16_t)tsp->year);
boolean isLeapYear = ((year%4) == 0 ) && (((year % 100) != 0) || ((year % 1000) == 0));
// February has 28 days (29 in a leap year)
// TODO: Check leap year calculation, this is pretty shitty... ;-)
if(tsp->month == 2){
if(tsp->day > (isLeapYear?29:28))return false;
}
// January, March, May, July, August, October and December have 31 days-
if(tsp->month == 1 || tsp->month == 3 || tsp->month == 5 || tsp->month == 7 || tsp->month == 8 || tsp->month == 10 || tsp->month == 12 ){
if(tsp->day > 31)return false;
}
return true;
}
void storeTimestamp(int tsIndex, TimeStamp *tsp){
int offset = TIMESTAMP_STORAGE_EEPROM_OFFSET + (tsIndex * sizeof(TimeStamp));
EEPROM[offset++] = tsp->second;
EEPROM[offset++] = tsp->minute;
EEPROM[offset++] = tsp->hour;
EEPROM[offset++] = tsp->day;
EEPROM[offset++] = tsp->month;
EEPROM[offset++] = tsp->year;
EEPROM[offset++] = tsp->century;
}
void readTimestamp(int tsIndex, TimeStamp *tsp){
int offset = TIMESTAMP_STORAGE_EEPROM_OFFSET + (tsIndex * sizeof(TimeStamp));
tsp->second = EEPROM[offset++];
tsp->minute = EEPROM[offset++];
tsp->hour = EEPROM[offset++];
tsp->day = EEPROM[offset++];
tsp->month = EEPROM[offset++];
tsp->year = EEPROM[offset++];
tsp->century = EEPROM[offset++];
}
void storeAllTimestamps(){
for(int i = 0; i < 3; i++){
storeTimestamp(i, &dates[i]);
}
}
void readAllTimestamps(){
for(int i = 0; i < 3; i++){
readTimestamp(i, &dates[i]);
}
}
void setNewTarget(){
storeTimestamp(2, &dates[2]);
blankAll();
delay(200);
myDFPlayer.play(11);
turnOnSequence();
delay(750);
}
void doWarp(){
Serial.println(F("88mph! 1.21GW!!! WOOOoooooo....."));
copyTimestamp(&GREEN, &YELLOW);
copyTimestamp(&RED, &GREEN);
updateRTCFromTimestamp(&GREEN);
storeAllTimestamps();
blankAllMonths();
playSelectSound();
turnOnSequence();
}