Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DS3231 by
ARM_RTC.cpp
00001 #include "ARM_RTC.h" 00002 00003 ARM_RTC::ARM_RTC() 00004 { 00005 setTimeZone(1); 00006 dayLightSaving = true; 00007 } 00008 00009 bool ARM_RTC::checkTimeLost(void) 00010 { 00011 return (atoi(getFormatedDateTime("%Y")) <= 2015) ? true : false; 00012 } 00013 00014 // BCD to decimal conversion 00015 int ARM_RTC::bcd2dec(int bcd) 00016 { 00017 return(((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F)); 00018 } 00019 00020 // decimal to BCD conversion 00021 int ARM_RTC::dec2bcd(int dec) 00022 { 00023 return((dec / 10) * 16 + (dec % 10)); 00024 } 00025 00026 void ARM_RTC::setTimeZone(double TZ) 00027 { 00028 timeZoneOffset = (uint32_t) (TZ * 3600.0f); 00029 } 00030 00031 double ARM_RTC::getTimeZone(void) 00032 { 00033 return (double)timeZoneOffset / 3600.0f; 00034 } 00035 00036 void ARM_RTC::setDate(int day, int month, int year) 00037 { 00038 printf("%d-%d-%d\n", day, month, year); 00039 // First read 00040 time_t rawtime; 00041 struct tm t; 00042 struct tm * t2; 00043 00044 time(&rawtime); 00045 t2 = localtime(&rawtime); 00046 00047 if (year<100) { 00048 year+=2000; 00049 } 00050 if (year > 1900) 00051 t.tm_year = year - 1900; // adjust for tm structure required values 00052 else 00053 t.tm_year = year; 00054 t.tm_mon = month - 1; // adjust for tm structure required values 00055 t.tm_mday = day; 00056 t.tm_hour = t2->tm_hour; 00057 t.tm_min = t2->tm_min; 00058 t.tm_sec = t2->tm_sec; 00059 00060 // Set system date and time 00061 rawtime = mktime(&t); // seconds since the Epoch 00062 set_time(rawtime); 00063 00064 // time_t t_seconds = time(NULL); 00065 // char buffer[32]; 00066 // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); 00067 // printf("%s\n", buffer); 00068 } 00069 00070 // set time register 00071 void ARM_RTC::setTime(int hours, int minutes, int seconds) 00072 { 00073 // First read 00074 time_t rawtime; 00075 struct tm t; 00076 struct tm * t2; 00077 00078 time(&rawtime); 00079 t2 = localtime(&rawtime); 00080 00081 t.tm_hour = hours; 00082 t.tm_min = minutes; 00083 t.tm_sec = seconds; 00084 t.tm_year = t2->tm_year; 00085 // t.tm_year = 2016-1900; 00086 t.tm_mon = t2->tm_mon; 00087 t.tm_mday = t2->tm_mday; 00088 // t.tm_wday = t2->tm_wday; 00089 // printf("%d-%d-%d %d:%d:%d\n", t.tm_year, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); 00090 00091 // Set system date and time 00092 rawtime = mktime(&t); // seconds since the Epoch 00093 set_time(rawtime); 00094 00095 // time_t t_seconds = time(NULL); 00096 // char buffer[32]; 00097 // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); 00098 // printf("%s\n", buffer); 00099 } 00100 00101 void ARM_RTC::setDateTime(int day, int month, int year, int hours, int minutes, int seconds) 00102 { 00103 // First read 00104 time_t rawtime; 00105 struct tm t; 00106 struct tm * t2; 00107 00108 time(&rawtime); 00109 t2 = localtime(&rawtime); 00110 00111 if (year<100) { 00112 year+=2000; 00113 } 00114 if (year > 1900) 00115 t.tm_year = year - 1900; // adjust for tm structure required values 00116 else 00117 t.tm_year = year; 00118 t.tm_mon = month - 1; // adjust for tm structure required values 00119 t.tm_mday = day; 00120 00121 t.tm_hour = hours; 00122 t.tm_min = minutes; 00123 t.tm_sec = seconds; 00124 00125 // Set system date and time 00126 rawtime = mktime(&t); // seconds since the Epoch 00127 set_time(rawtime); 00128 00129 // time_t t_seconds = time(NULL); 00130 // char buffer[32]; 00131 // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); 00132 // printf("%s\n", buffer); 00133 } 00134 00135 00136 // read the date and time registers and set system clock 00137 void ARM_RTC::setSystemClock(void) 00138 { 00139 // int dayOfWeek, day, month, year, hours, minutes, seconds; 00140 // readDateTime(&dayOfWeek, &day, &month, &year, &hours, &minutes, &seconds); 00141 00142 // getDateTime(&t); 00143 00144 // time_t secondsEpoch = mktime(&t); // seconds since the Epoch 00145 // Get weekday 00146 // t = *localtime(&secondsEpoch); 00147 // set_time(secondsEpoch); 00148 } 00149 00150 void ARM_RTC::readDateTime(void) 00151 { 00152 // First read 00153 time_t rawtime; 00154 struct tm t; 00155 struct tm * t2; 00156 time(&rawtime); 00157 t2 = localtime(&rawtime); 00158 00159 // time(&secondsEpoch); 00160 // t = localtime(&secondsEpoch); 00161 } 00162 00163 void ARM_RTC::getDateTime(struct tm *t) 00164 { 00165 time(&secondsEpoch); 00166 t = localtime(&secondsEpoch); 00167 } 00168 00169 void ARM_RTC::setDateTime(struct tm *time) 00170 { 00171 secondsEpoch = mktime(time); // seconds since the Epoch 00172 set_time(secondsEpoch); 00173 } 00174 00175 void ARM_RTC::setDateTimeSecsSince1900(uint32_t secsSince1900) 00176 { 00177 setDateTimeSecsSince1970(secsSince1900 - NTP_OFFSET); 00178 } 00179 00180 void ARM_RTC::setDateTimeSecsSince1970(uint32_t secsSince1970) 00181 { 00182 set_time(secsSince1970); 00183 } 00184 00185 // read the date and time registers and return secondsEpoch 00186 uint32_t ARM_RTC::getDateTimeSecsSince1970(void) 00187 { 00188 return (mktime(&t) - timeZoneOffset); // seconds since the Epoch 00189 } 00190 00191 // read the date and time registers and return secondsEpoch 00192 time_t ARM_RTC::getDateTimeSecsSince1970TZ(void) 00193 { 00194 return mktime(&t); // seconds since the Epoch 00195 } 00196 00197 int ARM_RTC::getDayOfWeek(int mday, int month, int year) // y > 1752, 1 <= m <= 12 00198 { 00199 t.tm_year = year - 1900; // adjust for tm structure required values 00200 t.tm_mon = month - 1; // adjust for tm structure required values 00201 t.tm_mday = mday; 00202 t.tm_hour = 0; 00203 t.tm_min = 0; 00204 t.tm_sec = 0; 00205 time_t secondsEpoch = mktime(&t); // seconds since the Epoch 00206 t = *localtime(&secondsEpoch); 00207 return t.tm_wday; // (0=Sunday, 6=Saturday) 00208 00209 // static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; 00210 // year -= month < 3; 00211 // return ((year + year/4 - year/100 + year/400 + t[month-1] + mday) % 7) + 1; // 01 - 07, 01 = Sunday 00212 } 00213 00214 char * ARM_RTC::getDayOfWeekName(void) // y > 1752, 1 <= m <= 12 00215 { 00216 time_t secondsEpoch = mktime(&t); // seconds since the Epoch 00217 t = *localtime(&secondsEpoch); 00218 strftime(buffer, 32, "%a", localtime(&secondsEpoch)); 00219 return buffer; 00220 } 00221 00222 // char *buffer; 00223 // buffer = ARM_RTC.getFormatedDateTime("%A %d-%m-%Y %H:%M:%S"); 00224 // TFT.gotoxy(0,7); 00225 // TFT.printf("Date: %s\n", buffer); 00226 char * ARM_RTC::getFormatedDateTime(char *format) 00227 { 00228 time_t t_seconds = time(NULL); 00229 strftime(buffer, 40, format, localtime(&t_seconds)); 00230 return buffer; 00231 } 00232 00233 bool ARM_RTC::getSummerTime(void) 00234 { 00235 getDateTime(&t); 00236 00237 time_t secondsEpoch = mktime(&t); // seconds since the Epoch 00238 t = *localtime(&secondsEpoch); 00239 strftime(buffer, 32, "%j", localtime(&secondsEpoch)); 00240 int dayOfYearC = atoi(buffer); 00241 00242 strftime(buffer, 32, "%Y", localtime(&secondsEpoch)); 00243 int year = atoi(buffer); 00244 00245 int index = (year - 2011) * 5; 00246 if (index < 0) 00247 index = 0; 00248 if (index > 440) // (2099 - 2011) * 5 = 440 00249 index = 440; 00250 00251 int monthS = atoi(SummerTime[index+1]); 00252 int dayS = atoi(SummerTime[index+2]); 00253 00254 t.tm_mon = monthS - 1; // adjust for tm structure required values 00255 t.tm_mday = dayS; 00256 secondsEpoch = mktime(&t); // seconds since the Epoch 00257 t = *localtime(&secondsEpoch); 00258 strftime(buffer, 32, "%j", localtime(&secondsEpoch)); 00259 int dayOfYearS = atoi(buffer); 00260 00261 int monthE = atoi(SummerTime[index+3]); 00262 int dayE = atoi(SummerTime[index+4]); 00263 00264 t.tm_mon = monthE - 1; // adjust for tm structure required values 00265 t.tm_mday = dayE; 00266 secondsEpoch = mktime(&t); // seconds since the Epoch 00267 t = *localtime(&secondsEpoch); 00268 strftime(buffer, 32, "%j", localtime(&secondsEpoch)); 00269 int dayOfYearE = atoi(buffer); 00270 00271 return ((dayOfYearC >= dayOfYearS) && (dayOfYearC < dayOfYearE)) ? true : false; 00272 } 00273 00274 int ARM_RTC::dayOfYearC(void) 00275 { 00276 getDateTime(&t); 00277 00278 time_t secondsEpoch = mktime(&t); // seconds since the Epoch 00279 strftime(buffer, 32, "%j", localtime(&secondsEpoch)); 00280 return atoi(buffer); 00281 } 00282 00283 char * ARM_RTC::getSunRise(void) 00284 { 00285 return (char*) SunRise[dayOfYearC()]; 00286 } 00287 00288 char * ARM_RTC::getSunSet(void) 00289 { 00290 return (char*) SunSet[dayOfYearC()]; 00291 } 00292 00293 char * ARM_RTC::getDayLength(void) 00294 { 00295 return (char*) DayLength[dayOfYearC()]; 00296 } 00297 00298 int ARM_RTC::getSunRiseMinute(void) 00299 { 00300 int doy = dayOfYearC(); 00301 int h = atoi(substr((char*)SunRise[doy], 0, 2)); 00302 int m = atoi(substr((char*)SunRise[doy], 3, 2)); 00303 return h * 60 + m; 00304 } 00305 00306 int ARM_RTC::getSunSetMinute(void) 00307 { 00308 int doy = dayOfYearC(); 00309 int h = atoi(substr((char*)SunSet[doy], 0, 2)); 00310 int m = atoi(substr((char*)SunSet[doy], 3, 2)); 00311 return h * 60 + m; 00312 } 00313 00314 bool ARM_RTC::checkSunRise(void) 00315 { 00316 int dayOfWeek, mday, month, year, hours, minutes, seconds; 00317 // readDateTime(&dayOfWeek, &mday, &month, &year, &hours, &minutes, &seconds); 00318 00319 // t.tm_year = year - 1900; // adjust for tm structure required values 00320 // t.tm_mon = month - 1; // adjust for tm structure required values 00321 // t.tm_mday = mday; 00322 // t.tm_hour = 0; 00323 // t.tm_min = 0; 00324 // t.tm_sec = 0; 00325 00326 int absMinute = t.tm_hour * 60 + t.tm_min; 00327 int SunRiseMinute = getSunRiseMinute(); 00328 int SunSetMinute = getSunSetMinute(); 00329 00330 return ((absMinute >= SunRiseMinute) && (absMinute < SunSetMinute)) ? true : false; 00331 } 00332 00333 void ARM_RTC::substr(char *s, char *d, int pos, int len) 00334 { 00335 char *t; 00336 s = s+pos; 00337 t = s+len; 00338 while (s != t) { 00339 *d=*s; 00340 s++; 00341 d++; 00342 } 00343 *d='\0'; 00344 } 00345 00346 char * ARM_RTC::substr(char *s, int pos, int len) 00347 { 00348 char *t; 00349 char *d; 00350 d = buffer; 00351 s = s+pos; 00352 t = s+len; 00353 while (s != t) { 00354 *d=*s; 00355 s++; 00356 d++; 00357 } 00358 *d='\0'; 00359 return buffer; 00360 }
Generated on Tue Jul 19 2022 23:05:54 by
1.7.2
