-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeObject.cpp
192 lines (117 loc) · 4.48 KB
/
timeObject.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
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
/*~~@@@@ timeObject.cpp @@@@~~
Library to convert times between millis()
& times converted from starting values.
outputs that count up & down.
Returns: Micros, Millis, Seconds, Minutes, Hours, Days, Weeks, Years.
*/
#include "timeObject.h"
timeObject::timeObject() {
}
// Public Methods:
void timeObject::countdownStart() {
Serial.println("Countdown Started");
runCountdown = true;
initialRecordedMillis = millis();
initial_countdown_millis = clock_to_millis(countdown_master.h, countdown_master.m, countdown_master.s);
}
void timeObject::countdownStop() {
Serial.println("Countdown Paused");
runCountdown = false;
}
void timeObject::countdownSetup(int32_t hours, int32_t minutes, int32_t seconds) {
Serial.println("");
Serial.println("Countdown Setup");
tminus = true; // Resets incase time is being added after t=0
tzeroFudge = true; // resets incase time is being added after t=0
initialRecordedMillis = millis();
initial_countdown_millis = clock_to_millis(hours, minutes, seconds);
Serial.printf(" timer_start_time: %02i:%02i:%02i || inital_timer_millis: %i ", hours, minutes, seconds, initial_countdown_millis);
Serial.println("");
Serial.println("");
millis_to_countdown_clock(initial_countdown_millis);
}
void timeObject::countdownLoop() {
if (runCountdown) {
int32_t calcMillis = returnTimePassed(initial_countdown_millis);
millis_to_countdown_clock(calcMillis);
}
updateAPIclocks();
}
void timeObject::countdownPrint(bool forcePrint) {
if (timeUpdated()) {
Serial.println(countdownclock_as_char);
} else if (forcePrint) { // If set true then bypasses update function
Serial.println(countdownclock_as_char);
}
// Serial.println(countdown_clock_string);
//Serial.printf("%02i:%02i:%02i", clock_countDown.h, clock_countDown.m, clock_countDown.s
// Serial.println("");
}
// Private Methods
int32_t timeObject::returnTimePassed(uint32_t countdown_start_millis) {
uint32_t millisPassed = millis() - initialRecordedMillis;
millisPassed = countdown_start_millis - millisPassed;
return millisPassed;
}
// Method to return a total millis value from simple clock data input
int32_t timeObject::clock_to_millis(int32_t hours, int32_t minutes, int32_t seconds) {
int32_t calc;
calc = 1000 * (seconds + 60 * (minutes + (60 * hours)));
return calc;
}
// Method to return simple clock data (HH:MM:SS) from a millis input (updates global variable on completion)
void timeObject::millis_to_countdown_clock(int32_t inputMillis) {
simpleClockData calc;
calc.s = inputMillis / 1000;
// Serial.print("Totals Passed: ");
// Serial.printf("calc.s: %i |", calc.s);
calc.m = calc.s / 60;
// Serial.printf("calc.m: %i |", calc.m);
calc.h = calc.m / 60;
//Serial.printf("calc.h: %i |", calc.h);
//Serial.println("");
//Serial.println("Returned Time: ");
calc.m = calc.m - (60 * calc.h);
calc.s = calc.s - ( 60 * (calc.m + (60 * calc.h)));
if (calc.s < 0) {
calc.s = (calc .s * -1);
tminus = false;
}
if (calc.m < 0) {
calc.m = (calc.m * -1);
}
if (calc.h < 0) {
calc.h = (calc.h * -1);
}
if (calc.s == 0 && calc.m == 0 && calc.h == 0) {
if (tzeroFudge) {
initial_countdown_millis = initial_countdown_millis - 1000;
tzeroFudge = false;
tzero = true; // External flag for API
// Serial.println("FUDGE FACTOR APPLIED");
}
}
countdown_master = calc;
//Serial.print("Return Clock: ");
// Serial.printf("%02i:%02i:%02i", clock_countDown.h, clock_countDown.m, clock_countDown.s);
// Serial.println("");
}
void timeObject::updateAPIclocks() { // Method to update different variables that can be polled by external objects
if (tminus) {
sprintf(countdownclock_as_char, "T- %02i:%02i:%02i", countdown_master.h, countdown_master.m, countdown_master.s);
} else {
sprintf(countdownclock_as_char, "T+ %02i:%02i:%02i", countdown_master.h, countdown_master.m, countdown_master.s);
}
countdown_clock_string = countdownclock_as_char;
}
bool timeObject::timeUpdated() {
if (countdown_master.s != previousSecond || countdown_master.m != previousMinute || countdown_master.h != previousHour) {
previousSecond = countdown_master.s;
previousMinute = countdown_master.m;
previousHour = countdown_master.h;
time_updated = true; // Gltobal variable latches true, can be unlatched externally.
return true;
} else {
return false;
}
}