This repository was archived by the owner on May 6, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMyTime.cpp
More file actions
218 lines (189 loc) · 6.78 KB
/
Copy pathMyTime.cpp
File metadata and controls
218 lines (189 loc) · 6.78 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
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
#include "MyTime.h"
MyTime::MyTime (int Address){
eeprom_time_address = Address;
// eeprom_slope_address += Address;
// eepromBadAddress += eeprom_slope_address;
// eepromAttAddress += eepromBadAddress;
read_eeprom_time(Address);
slope = get_slope();
}
MyTime::MTinit(){
get_rtc_time();
readBadTrials();
}
void MyTime::read_eeprom_time (int eeprom_address){
eeprom_time.sec = EEPROM.read(eeprom_address);
eeprom_time.min = EEPROM.read(++eeprom_address);
eeprom_time.hour = EEPROM.read(++eeprom_address);
eeprom_time.mday = EEPROM.read(++eeprom_address);
eeprom_time.mon = EEPROM.read(++eeprom_address);
int16_t x = EEPROM.read(++eeprom_address);
x *= 100;
eeprom_time.year = EEPROM.read(++eeprom_address);
eeprom_time.year += x;
}
void MyTime::write_eeprom_time (int eeprom_address){
EEPROM.write(eeprom_address,rtc_time.sec);
EEPROM.write(++eeprom_address,rtc_time.min);
EEPROM.write(++eeprom_address,rtc_time.hour);
EEPROM.write(++eeprom_address,rtc_time.mday);
EEPROM.write(++eeprom_address,rtc_time.mon);
byte x = rtc_time.year/100;
EEPROM.write(++eeprom_address,x);
x = rtc_time.year%100;
EEPROM.write(++eeprom_address,x);
}
void MyTime::get_rtc_time(){
DS3231_get(&rtc_time); // Read the time from the RTC module
// Serial.println("RTC Time got is:");
// MyTime::printTime(&rtc_time);
}
/*
void MyTime::printTime(ts * pt){
Serial.print("Time: ");
Serial.print(pt->hour);
Serial.print(":");
Serial.print(pt->min);
Serial.print(":");
Serial.println(pt->sec);
Serial.print("Date: ");
Serial.print(pt->mday);
Serial.print("/");
Serial.print(pt->mon);
Serial.print("/");
Serial.println(pt->year);
}
*/
byte MyTime::get_slope(){
// return int (EEPROM.read(eeprom_slope_address));
return 2;
}
double MyTime::return_delta_time(){
struct tm * time1;
// struct tm * time2;
double x;
struct ts rtc_time; // used to hold time data from the RTC
DS3231_get(&rtc_time); // Read the time from the RTC module
time1->tm_sec = eeprom_time.sec;
// Serial.print("eeprom_time.sec = ");
// Serial.println(eeprom_time.sec);
time1->tm_min = eeprom_time.min;
// Serial.print("eeprom_time.min = ");
// Serial.println(eeprom_time.min);
time1->tm_hour = eeprom_time.hour;
// Serial.print("eeprom_time.hour = ");
// Serial.println(eeprom_time.hour);
time1->tm_mday = eeprom_time.mday;
// Serial.print("eeprom_time.mday = ");
// Serial.println(eeprom_time.mday);
time1->tm_mon = eeprom_time.mon;
// Serial.print("eeprom_time.mon = ");
// Serial.println(eeprom_time.mon);
time1->tm_year = eeprom_time.year;
// Serial.print("eeprom_time.year = ");
// Serial.println(eeprom_time.year);
time_t EEpromTime = mktime(time1);
Serial.print("time_t EEpromTime = ");
Serial.println(EEpromTime);
time1->tm_sec = rtc_time.sec;
Serial.print("rtc_time.sec = ");
Serial.println(rtc_time.sec);
time1->tm_min = rtc_time.min;
Serial.print("rtc_time.min = ");
Serial.println(rtc_time.min);
Serial.print("time1->tm_min = ");
Serial.println(time1->tm_min);
time1->tm_hour = rtc_time.hour;
Serial.print("rtc_time.hour = ");
Serial.println(rtc_time.hour);
time1->tm_mday = rtc_time.mday;
Serial.print("rtc_time.mday = ");
Serial.println(rtc_time.mday);
time1->tm_mon = rtc_time.mon;
Serial.print("rtc_time.mon = ");
Serial.println(rtc_time.mon);
time1->tm_year = rtc_time.year;
Serial.print("rtc_time.year = ");
Serial.println(rtc_time.year);
time_t RtcTime = mktime(time1);
Serial.print("time_t RtcTime = ");
Serial.println(RtcTime);
// x = difftime(RtcTime, EEpromTime);
// Serial.println("Start return delta time");
// Serial.print("delta time is: ");
// Serial.println(x);
return 200.0;
}
bool MyTime::is_ok(){
double x;
if(badTrials <=5 ){
// Serial.print("is_ok = OK bad trials = ");
// Serial.println(badTrials);
return 1;
} else{
// Serial.print("is_ok = bad bad trials = ");
// Serial.println(badTrials);
MyTime::get_rtc_time(); // time when the function was called
MyTime::read_eeprom_time(eeprom_time_address);
// MyTime::printTime(&eeprom_time);
// MyTime::printTime(&rtc_time);
x = MyTime::return_delta_time();
// x /= 60;
//Serial.println(x);
// if (x >= slope) {
return 1;
// } else {
// return 1;
// }
}
}
void MyTime::resetBad(){
badTrials = 0;
badAttempts = 0;
EEPROM.write((eeprom_time_address + eeprom_slope_address + eepromBadAddress),0);
EEPROM.write((eeprom_time_address + eeprom_slope_address + eepromBadAddress + 1),0);
}
void MyTime::updateBad(){
Serial.println("START MyTime::updateBad START");
badTrials ++;
EEPROM.write((eeprom_time_address + eeprom_slope_address + eepromBadAddress),badTrials);
Serial.print("badTrials = ");
Serial.println(badTrials);
Serial.println("After updating, allready in the EEProm");
Serial.println(" ");
if(badTrials > 5){
Serial.println("Since bad trials was greater then 5");
Serial.print("badAttempts = ");
Serial.println(badAttempts);
if(badAttempts <= 2){
badAttempts++;
EEPROM.write((eeprom_time_address + eeprom_slope_address + eepromBadAddress + 1),badAttempts);
Serial.println("Since badAttempts was smaller then 2, increase and update");
Serial.print("updated badAttempts = ");
Serial.println(badAttempts);
} else {
Serial.println("Since badAttempts was bigger then 2 ");
Serial.println("badAttempts = 0");
Serial.println("Also updating in the EEProm");
badAttempts = 0;
EEPROM.write((eeprom_time_address + eeprom_slope_address + eepromBadAddress + 1),badAttempts);
Serial.println("Getting RTC time and updating into EEProm");
Serial.println("for next round, in case it will get over 5");
MyTime::get_rtc_time();
MyTime::write_eeprom_time(eeprom_time_address);
// MyTime::read_eeprom_time(eeprom_time_address);
// MyTime::printTime(&eeprom_time);
}
}
Serial.println("END MyTime::updateBad END");
}
void MyTime::readBadTrials(){
// Serial.println("START MyTime::readBadTrials");
badTrials = EEPROM.read((eeprom_time_address + eeprom_slope_address + eepromBadAddress));
// Serial.print("badTrials");
// Serial.println(badTrials);
badAttempts = EEPROM.read((eeprom_time_address + eeprom_slope_address + eepromBadAddress + 1));
// Serial.print("badAttempts");
// Serial.println(badAttempts);
// Serial.println("END MyTime::readBadTrials");
}