-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeensyTimeManager.cpp
70 lines (59 loc) · 1.65 KB
/
TeensyTimeManager.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
/*
* TimeManager.cpp
*
* Created on: Nov 28, 2014
* Author: Konstantin Gredeskoul
* Code: https://github.com/kigster
*
* (c) 2014 All rights reserved, MIT License.
*/
#include "FlixCapacitor.h"
#ifdef ENABLE_CLOCK
#include "TeensyTimeManager.h"
#include <Time.h>
#include <Arduino.h>
TeensyTimeManager::TeensyTimeManager() {
const char *monthNamesList[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
for (int i = 0; i < 12; i ++) { monthNames[i] = (char *)monthNamesList[i]; }
}
bool TeensyTimeManager::setTimeToCompileTime() {
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
// and configure the RTC with this info
time_t compileTime = makeTime(tm) + 30;
Teensy3Clock.set(compileTime);
setTime(compileTime);
return true;
}
return false;
}
bool TeensyTimeManager::getTime(const char *str) {
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3)
return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool TeensyTimeManager::getDate(const char *str) {
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3)
return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthNames[monthIndex]) == 0)
break;
}
if (monthIndex >= 12)
return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
#endif // ENABLE_CLOCK