MAXIM DS3231 accurate Real Time Clock Library
Fork of DS3231 by
ARM_RTC.cpp@3:1af2ff7cfe26, 2018-07-23 (annotated)
- Committer:
- JackB
- Date:
- Mon Jul 23 12:25:27 2018 +0000
- Revision:
- 3:1af2ff7cfe26
RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JackB | 3:1af2ff7cfe26 | 1 | #include "ARM_RTC.h" |
JackB | 3:1af2ff7cfe26 | 2 | |
JackB | 3:1af2ff7cfe26 | 3 | ARM_RTC::ARM_RTC() |
JackB | 3:1af2ff7cfe26 | 4 | { |
JackB | 3:1af2ff7cfe26 | 5 | setTimeZone(1); |
JackB | 3:1af2ff7cfe26 | 6 | dayLightSaving = true; |
JackB | 3:1af2ff7cfe26 | 7 | } |
JackB | 3:1af2ff7cfe26 | 8 | |
JackB | 3:1af2ff7cfe26 | 9 | bool ARM_RTC::checkTimeLost(void) |
JackB | 3:1af2ff7cfe26 | 10 | { |
JackB | 3:1af2ff7cfe26 | 11 | return (atoi(getFormatedDateTime("%Y")) <= 2015) ? true : false; |
JackB | 3:1af2ff7cfe26 | 12 | } |
JackB | 3:1af2ff7cfe26 | 13 | |
JackB | 3:1af2ff7cfe26 | 14 | // BCD to decimal conversion |
JackB | 3:1af2ff7cfe26 | 15 | int ARM_RTC::bcd2dec(int bcd) |
JackB | 3:1af2ff7cfe26 | 16 | { |
JackB | 3:1af2ff7cfe26 | 17 | return(((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F)); |
JackB | 3:1af2ff7cfe26 | 18 | } |
JackB | 3:1af2ff7cfe26 | 19 | |
JackB | 3:1af2ff7cfe26 | 20 | // decimal to BCD conversion |
JackB | 3:1af2ff7cfe26 | 21 | int ARM_RTC::dec2bcd(int dec) |
JackB | 3:1af2ff7cfe26 | 22 | { |
JackB | 3:1af2ff7cfe26 | 23 | return((dec / 10) * 16 + (dec % 10)); |
JackB | 3:1af2ff7cfe26 | 24 | } |
JackB | 3:1af2ff7cfe26 | 25 | |
JackB | 3:1af2ff7cfe26 | 26 | void ARM_RTC::setTimeZone(double TZ) |
JackB | 3:1af2ff7cfe26 | 27 | { |
JackB | 3:1af2ff7cfe26 | 28 | timeZoneOffset = (uint32_t) (TZ * 3600.0f); |
JackB | 3:1af2ff7cfe26 | 29 | } |
JackB | 3:1af2ff7cfe26 | 30 | |
JackB | 3:1af2ff7cfe26 | 31 | double ARM_RTC::getTimeZone(void) |
JackB | 3:1af2ff7cfe26 | 32 | { |
JackB | 3:1af2ff7cfe26 | 33 | return (double)timeZoneOffset / 3600.0f; |
JackB | 3:1af2ff7cfe26 | 34 | } |
JackB | 3:1af2ff7cfe26 | 35 | |
JackB | 3:1af2ff7cfe26 | 36 | void ARM_RTC::setDate(int day, int month, int year) |
JackB | 3:1af2ff7cfe26 | 37 | { |
JackB | 3:1af2ff7cfe26 | 38 | printf("%d-%d-%d\n", day, month, year); |
JackB | 3:1af2ff7cfe26 | 39 | // First read |
JackB | 3:1af2ff7cfe26 | 40 | time_t rawtime; |
JackB | 3:1af2ff7cfe26 | 41 | struct tm t; |
JackB | 3:1af2ff7cfe26 | 42 | struct tm * t2; |
JackB | 3:1af2ff7cfe26 | 43 | |
JackB | 3:1af2ff7cfe26 | 44 | time(&rawtime); |
JackB | 3:1af2ff7cfe26 | 45 | t2 = localtime(&rawtime); |
JackB | 3:1af2ff7cfe26 | 46 | |
JackB | 3:1af2ff7cfe26 | 47 | if (year<100) { |
JackB | 3:1af2ff7cfe26 | 48 | year+=2000; |
JackB | 3:1af2ff7cfe26 | 49 | } |
JackB | 3:1af2ff7cfe26 | 50 | if (year > 1900) |
JackB | 3:1af2ff7cfe26 | 51 | t.tm_year = year - 1900; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 52 | else |
JackB | 3:1af2ff7cfe26 | 53 | t.tm_year = year; |
JackB | 3:1af2ff7cfe26 | 54 | t.tm_mon = month - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 55 | t.tm_mday = day; |
JackB | 3:1af2ff7cfe26 | 56 | t.tm_hour = t2->tm_hour; |
JackB | 3:1af2ff7cfe26 | 57 | t.tm_min = t2->tm_min; |
JackB | 3:1af2ff7cfe26 | 58 | t.tm_sec = t2->tm_sec; |
JackB | 3:1af2ff7cfe26 | 59 | |
JackB | 3:1af2ff7cfe26 | 60 | // Set system date and time |
JackB | 3:1af2ff7cfe26 | 61 | rawtime = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 62 | set_time(rawtime); |
JackB | 3:1af2ff7cfe26 | 63 | |
JackB | 3:1af2ff7cfe26 | 64 | // time_t t_seconds = time(NULL); |
JackB | 3:1af2ff7cfe26 | 65 | // char buffer[32]; |
JackB | 3:1af2ff7cfe26 | 66 | // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); |
JackB | 3:1af2ff7cfe26 | 67 | // printf("%s\n", buffer); |
JackB | 3:1af2ff7cfe26 | 68 | } |
JackB | 3:1af2ff7cfe26 | 69 | |
JackB | 3:1af2ff7cfe26 | 70 | // set time register |
JackB | 3:1af2ff7cfe26 | 71 | void ARM_RTC::setTime(int hours, int minutes, int seconds) |
JackB | 3:1af2ff7cfe26 | 72 | { |
JackB | 3:1af2ff7cfe26 | 73 | // First read |
JackB | 3:1af2ff7cfe26 | 74 | time_t rawtime; |
JackB | 3:1af2ff7cfe26 | 75 | struct tm t; |
JackB | 3:1af2ff7cfe26 | 76 | struct tm * t2; |
JackB | 3:1af2ff7cfe26 | 77 | |
JackB | 3:1af2ff7cfe26 | 78 | time(&rawtime); |
JackB | 3:1af2ff7cfe26 | 79 | t2 = localtime(&rawtime); |
JackB | 3:1af2ff7cfe26 | 80 | |
JackB | 3:1af2ff7cfe26 | 81 | t.tm_hour = hours; |
JackB | 3:1af2ff7cfe26 | 82 | t.tm_min = minutes; |
JackB | 3:1af2ff7cfe26 | 83 | t.tm_sec = seconds; |
JackB | 3:1af2ff7cfe26 | 84 | t.tm_year = t2->tm_year; |
JackB | 3:1af2ff7cfe26 | 85 | // t.tm_year = 2016-1900; |
JackB | 3:1af2ff7cfe26 | 86 | t.tm_mon = t2->tm_mon; |
JackB | 3:1af2ff7cfe26 | 87 | t.tm_mday = t2->tm_mday; |
JackB | 3:1af2ff7cfe26 | 88 | // t.tm_wday = t2->tm_wday; |
JackB | 3:1af2ff7cfe26 | 89 | // 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); |
JackB | 3:1af2ff7cfe26 | 90 | |
JackB | 3:1af2ff7cfe26 | 91 | // Set system date and time |
JackB | 3:1af2ff7cfe26 | 92 | rawtime = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 93 | set_time(rawtime); |
JackB | 3:1af2ff7cfe26 | 94 | |
JackB | 3:1af2ff7cfe26 | 95 | // time_t t_seconds = time(NULL); |
JackB | 3:1af2ff7cfe26 | 96 | // char buffer[32]; |
JackB | 3:1af2ff7cfe26 | 97 | // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); |
JackB | 3:1af2ff7cfe26 | 98 | // printf("%s\n", buffer); |
JackB | 3:1af2ff7cfe26 | 99 | } |
JackB | 3:1af2ff7cfe26 | 100 | |
JackB | 3:1af2ff7cfe26 | 101 | void ARM_RTC::setDateTime(int day, int month, int year, int hours, int minutes, int seconds) |
JackB | 3:1af2ff7cfe26 | 102 | { |
JackB | 3:1af2ff7cfe26 | 103 | // First read |
JackB | 3:1af2ff7cfe26 | 104 | time_t rawtime; |
JackB | 3:1af2ff7cfe26 | 105 | struct tm t; |
JackB | 3:1af2ff7cfe26 | 106 | struct tm * t2; |
JackB | 3:1af2ff7cfe26 | 107 | |
JackB | 3:1af2ff7cfe26 | 108 | time(&rawtime); |
JackB | 3:1af2ff7cfe26 | 109 | t2 = localtime(&rawtime); |
JackB | 3:1af2ff7cfe26 | 110 | |
JackB | 3:1af2ff7cfe26 | 111 | if (year<100) { |
JackB | 3:1af2ff7cfe26 | 112 | year+=2000; |
JackB | 3:1af2ff7cfe26 | 113 | } |
JackB | 3:1af2ff7cfe26 | 114 | if (year > 1900) |
JackB | 3:1af2ff7cfe26 | 115 | t.tm_year = year - 1900; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 116 | else |
JackB | 3:1af2ff7cfe26 | 117 | t.tm_year = year; |
JackB | 3:1af2ff7cfe26 | 118 | t.tm_mon = month - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 119 | t.tm_mday = day; |
JackB | 3:1af2ff7cfe26 | 120 | |
JackB | 3:1af2ff7cfe26 | 121 | t.tm_hour = hours; |
JackB | 3:1af2ff7cfe26 | 122 | t.tm_min = minutes; |
JackB | 3:1af2ff7cfe26 | 123 | t.tm_sec = seconds; |
JackB | 3:1af2ff7cfe26 | 124 | |
JackB | 3:1af2ff7cfe26 | 125 | // Set system date and time |
JackB | 3:1af2ff7cfe26 | 126 | rawtime = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 127 | set_time(rawtime); |
JackB | 3:1af2ff7cfe26 | 128 | |
JackB | 3:1af2ff7cfe26 | 129 | // time_t t_seconds = time(NULL); |
JackB | 3:1af2ff7cfe26 | 130 | // char buffer[32]; |
JackB | 3:1af2ff7cfe26 | 131 | // strftime(buffer, 32, "%A %d-%m-%Y %H:%M:%S", localtime(&t_seconds)); |
JackB | 3:1af2ff7cfe26 | 132 | // printf("%s\n", buffer); |
JackB | 3:1af2ff7cfe26 | 133 | } |
JackB | 3:1af2ff7cfe26 | 134 | |
JackB | 3:1af2ff7cfe26 | 135 | |
JackB | 3:1af2ff7cfe26 | 136 | // read the date and time registers and set system clock |
JackB | 3:1af2ff7cfe26 | 137 | void ARM_RTC::setSystemClock(void) |
JackB | 3:1af2ff7cfe26 | 138 | { |
JackB | 3:1af2ff7cfe26 | 139 | // int dayOfWeek, day, month, year, hours, minutes, seconds; |
JackB | 3:1af2ff7cfe26 | 140 | // readDateTime(&dayOfWeek, &day, &month, &year, &hours, &minutes, &seconds); |
JackB | 3:1af2ff7cfe26 | 141 | |
JackB | 3:1af2ff7cfe26 | 142 | // getDateTime(&t); |
JackB | 3:1af2ff7cfe26 | 143 | |
JackB | 3:1af2ff7cfe26 | 144 | // time_t secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 145 | // Get weekday |
JackB | 3:1af2ff7cfe26 | 146 | // t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 147 | // set_time(secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 148 | } |
JackB | 3:1af2ff7cfe26 | 149 | |
JackB | 3:1af2ff7cfe26 | 150 | void ARM_RTC::readDateTime(void) |
JackB | 3:1af2ff7cfe26 | 151 | { |
JackB | 3:1af2ff7cfe26 | 152 | // First read |
JackB | 3:1af2ff7cfe26 | 153 | time_t rawtime; |
JackB | 3:1af2ff7cfe26 | 154 | struct tm t; |
JackB | 3:1af2ff7cfe26 | 155 | struct tm * t2; |
JackB | 3:1af2ff7cfe26 | 156 | time(&rawtime); |
JackB | 3:1af2ff7cfe26 | 157 | t2 = localtime(&rawtime); |
JackB | 3:1af2ff7cfe26 | 158 | |
JackB | 3:1af2ff7cfe26 | 159 | // time(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 160 | // t = localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 161 | } |
JackB | 3:1af2ff7cfe26 | 162 | |
JackB | 3:1af2ff7cfe26 | 163 | void ARM_RTC::getDateTime(struct tm *t) |
JackB | 3:1af2ff7cfe26 | 164 | { |
JackB | 3:1af2ff7cfe26 | 165 | time(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 166 | t = localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 167 | } |
JackB | 3:1af2ff7cfe26 | 168 | |
JackB | 3:1af2ff7cfe26 | 169 | void ARM_RTC::setDateTime(struct tm *time) |
JackB | 3:1af2ff7cfe26 | 170 | { |
JackB | 3:1af2ff7cfe26 | 171 | secondsEpoch = mktime(time); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 172 | set_time(secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 173 | } |
JackB | 3:1af2ff7cfe26 | 174 | |
JackB | 3:1af2ff7cfe26 | 175 | void ARM_RTC::setDateTimeSecsSince1900(uint32_t secsSince1900) |
JackB | 3:1af2ff7cfe26 | 176 | { |
JackB | 3:1af2ff7cfe26 | 177 | setDateTimeSecsSince1970(secsSince1900 - NTP_OFFSET); |
JackB | 3:1af2ff7cfe26 | 178 | } |
JackB | 3:1af2ff7cfe26 | 179 | |
JackB | 3:1af2ff7cfe26 | 180 | void ARM_RTC::setDateTimeSecsSince1970(uint32_t secsSince1970) |
JackB | 3:1af2ff7cfe26 | 181 | { |
JackB | 3:1af2ff7cfe26 | 182 | set_time(secsSince1970); |
JackB | 3:1af2ff7cfe26 | 183 | } |
JackB | 3:1af2ff7cfe26 | 184 | |
JackB | 3:1af2ff7cfe26 | 185 | // read the date and time registers and return secondsEpoch |
JackB | 3:1af2ff7cfe26 | 186 | uint32_t ARM_RTC::getDateTimeSecsSince1970(void) |
JackB | 3:1af2ff7cfe26 | 187 | { |
JackB | 3:1af2ff7cfe26 | 188 | return (mktime(&t) - timeZoneOffset); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 189 | } |
JackB | 3:1af2ff7cfe26 | 190 | |
JackB | 3:1af2ff7cfe26 | 191 | // read the date and time registers and return secondsEpoch |
JackB | 3:1af2ff7cfe26 | 192 | time_t ARM_RTC::getDateTimeSecsSince1970TZ(void) |
JackB | 3:1af2ff7cfe26 | 193 | { |
JackB | 3:1af2ff7cfe26 | 194 | return mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 195 | } |
JackB | 3:1af2ff7cfe26 | 196 | |
JackB | 3:1af2ff7cfe26 | 197 | int ARM_RTC::getDayOfWeek(int mday, int month, int year) // y > 1752, 1 <= m <= 12 |
JackB | 3:1af2ff7cfe26 | 198 | { |
JackB | 3:1af2ff7cfe26 | 199 | t.tm_year = year - 1900; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 200 | t.tm_mon = month - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 201 | t.tm_mday = mday; |
JackB | 3:1af2ff7cfe26 | 202 | t.tm_hour = 0; |
JackB | 3:1af2ff7cfe26 | 203 | t.tm_min = 0; |
JackB | 3:1af2ff7cfe26 | 204 | t.tm_sec = 0; |
JackB | 3:1af2ff7cfe26 | 205 | time_t secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 206 | t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 207 | return t.tm_wday; // (0=Sunday, 6=Saturday) |
JackB | 3:1af2ff7cfe26 | 208 | |
JackB | 3:1af2ff7cfe26 | 209 | // static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; |
JackB | 3:1af2ff7cfe26 | 210 | // year -= month < 3; |
JackB | 3:1af2ff7cfe26 | 211 | // return ((year + year/4 - year/100 + year/400 + t[month-1] + mday) % 7) + 1; // 01 - 07, 01 = Sunday |
JackB | 3:1af2ff7cfe26 | 212 | } |
JackB | 3:1af2ff7cfe26 | 213 | |
JackB | 3:1af2ff7cfe26 | 214 | char * ARM_RTC::getDayOfWeekName(void) // y > 1752, 1 <= m <= 12 |
JackB | 3:1af2ff7cfe26 | 215 | { |
JackB | 3:1af2ff7cfe26 | 216 | time_t secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 217 | t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 218 | strftime(buffer, 32, "%a", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 219 | return buffer; |
JackB | 3:1af2ff7cfe26 | 220 | } |
JackB | 3:1af2ff7cfe26 | 221 | |
JackB | 3:1af2ff7cfe26 | 222 | // char *buffer; |
JackB | 3:1af2ff7cfe26 | 223 | // buffer = ARM_RTC.getFormatedDateTime("%A %d-%m-%Y %H:%M:%S"); |
JackB | 3:1af2ff7cfe26 | 224 | // TFT.gotoxy(0,7); |
JackB | 3:1af2ff7cfe26 | 225 | // TFT.printf("Date: %s\n", buffer); |
JackB | 3:1af2ff7cfe26 | 226 | char * ARM_RTC::getFormatedDateTime(char *format) |
JackB | 3:1af2ff7cfe26 | 227 | { |
JackB | 3:1af2ff7cfe26 | 228 | time_t t_seconds = time(NULL); |
JackB | 3:1af2ff7cfe26 | 229 | strftime(buffer, 40, format, localtime(&t_seconds)); |
JackB | 3:1af2ff7cfe26 | 230 | return buffer; |
JackB | 3:1af2ff7cfe26 | 231 | } |
JackB | 3:1af2ff7cfe26 | 232 | |
JackB | 3:1af2ff7cfe26 | 233 | bool ARM_RTC::getSummerTime(void) |
JackB | 3:1af2ff7cfe26 | 234 | { |
JackB | 3:1af2ff7cfe26 | 235 | getDateTime(&t); |
JackB | 3:1af2ff7cfe26 | 236 | |
JackB | 3:1af2ff7cfe26 | 237 | time_t secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 238 | t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 239 | strftime(buffer, 32, "%j", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 240 | int dayOfYearC = atoi(buffer); |
JackB | 3:1af2ff7cfe26 | 241 | |
JackB | 3:1af2ff7cfe26 | 242 | strftime(buffer, 32, "%Y", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 243 | int year = atoi(buffer); |
JackB | 3:1af2ff7cfe26 | 244 | |
JackB | 3:1af2ff7cfe26 | 245 | int index = (year - 2011) * 5; |
JackB | 3:1af2ff7cfe26 | 246 | if (index < 0) |
JackB | 3:1af2ff7cfe26 | 247 | index = 0; |
JackB | 3:1af2ff7cfe26 | 248 | if (index > 440) // (2099 - 2011) * 5 = 440 |
JackB | 3:1af2ff7cfe26 | 249 | index = 440; |
JackB | 3:1af2ff7cfe26 | 250 | |
JackB | 3:1af2ff7cfe26 | 251 | int monthS = atoi(SummerTime[index+1]); |
JackB | 3:1af2ff7cfe26 | 252 | int dayS = atoi(SummerTime[index+2]); |
JackB | 3:1af2ff7cfe26 | 253 | |
JackB | 3:1af2ff7cfe26 | 254 | t.tm_mon = monthS - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 255 | t.tm_mday = dayS; |
JackB | 3:1af2ff7cfe26 | 256 | secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 257 | t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 258 | strftime(buffer, 32, "%j", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 259 | int dayOfYearS = atoi(buffer); |
JackB | 3:1af2ff7cfe26 | 260 | |
JackB | 3:1af2ff7cfe26 | 261 | int monthE = atoi(SummerTime[index+3]); |
JackB | 3:1af2ff7cfe26 | 262 | int dayE = atoi(SummerTime[index+4]); |
JackB | 3:1af2ff7cfe26 | 263 | |
JackB | 3:1af2ff7cfe26 | 264 | t.tm_mon = monthE - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 265 | t.tm_mday = dayE; |
JackB | 3:1af2ff7cfe26 | 266 | secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 267 | t = *localtime(&secondsEpoch); |
JackB | 3:1af2ff7cfe26 | 268 | strftime(buffer, 32, "%j", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 269 | int dayOfYearE = atoi(buffer); |
JackB | 3:1af2ff7cfe26 | 270 | |
JackB | 3:1af2ff7cfe26 | 271 | return ((dayOfYearC >= dayOfYearS) && (dayOfYearC < dayOfYearE)) ? true : false; |
JackB | 3:1af2ff7cfe26 | 272 | } |
JackB | 3:1af2ff7cfe26 | 273 | |
JackB | 3:1af2ff7cfe26 | 274 | int ARM_RTC::dayOfYearC(void) |
JackB | 3:1af2ff7cfe26 | 275 | { |
JackB | 3:1af2ff7cfe26 | 276 | getDateTime(&t); |
JackB | 3:1af2ff7cfe26 | 277 | |
JackB | 3:1af2ff7cfe26 | 278 | time_t secondsEpoch = mktime(&t); // seconds since the Epoch |
JackB | 3:1af2ff7cfe26 | 279 | strftime(buffer, 32, "%j", localtime(&secondsEpoch)); |
JackB | 3:1af2ff7cfe26 | 280 | return atoi(buffer); |
JackB | 3:1af2ff7cfe26 | 281 | } |
JackB | 3:1af2ff7cfe26 | 282 | |
JackB | 3:1af2ff7cfe26 | 283 | char * ARM_RTC::getSunRise(void) |
JackB | 3:1af2ff7cfe26 | 284 | { |
JackB | 3:1af2ff7cfe26 | 285 | return (char*) SunRise[dayOfYearC()]; |
JackB | 3:1af2ff7cfe26 | 286 | } |
JackB | 3:1af2ff7cfe26 | 287 | |
JackB | 3:1af2ff7cfe26 | 288 | char * ARM_RTC::getSunSet(void) |
JackB | 3:1af2ff7cfe26 | 289 | { |
JackB | 3:1af2ff7cfe26 | 290 | return (char*) SunSet[dayOfYearC()]; |
JackB | 3:1af2ff7cfe26 | 291 | } |
JackB | 3:1af2ff7cfe26 | 292 | |
JackB | 3:1af2ff7cfe26 | 293 | char * ARM_RTC::getDayLength(void) |
JackB | 3:1af2ff7cfe26 | 294 | { |
JackB | 3:1af2ff7cfe26 | 295 | return (char*) DayLength[dayOfYearC()]; |
JackB | 3:1af2ff7cfe26 | 296 | } |
JackB | 3:1af2ff7cfe26 | 297 | |
JackB | 3:1af2ff7cfe26 | 298 | int ARM_RTC::getSunRiseMinute(void) |
JackB | 3:1af2ff7cfe26 | 299 | { |
JackB | 3:1af2ff7cfe26 | 300 | int doy = dayOfYearC(); |
JackB | 3:1af2ff7cfe26 | 301 | int h = atoi(substr((char*)SunRise[doy], 0, 2)); |
JackB | 3:1af2ff7cfe26 | 302 | int m = atoi(substr((char*)SunRise[doy], 3, 2)); |
JackB | 3:1af2ff7cfe26 | 303 | return h * 60 + m; |
JackB | 3:1af2ff7cfe26 | 304 | } |
JackB | 3:1af2ff7cfe26 | 305 | |
JackB | 3:1af2ff7cfe26 | 306 | int ARM_RTC::getSunSetMinute(void) |
JackB | 3:1af2ff7cfe26 | 307 | { |
JackB | 3:1af2ff7cfe26 | 308 | int doy = dayOfYearC(); |
JackB | 3:1af2ff7cfe26 | 309 | int h = atoi(substr((char*)SunSet[doy], 0, 2)); |
JackB | 3:1af2ff7cfe26 | 310 | int m = atoi(substr((char*)SunSet[doy], 3, 2)); |
JackB | 3:1af2ff7cfe26 | 311 | return h * 60 + m; |
JackB | 3:1af2ff7cfe26 | 312 | } |
JackB | 3:1af2ff7cfe26 | 313 | |
JackB | 3:1af2ff7cfe26 | 314 | bool ARM_RTC::checkSunRise(void) |
JackB | 3:1af2ff7cfe26 | 315 | { |
JackB | 3:1af2ff7cfe26 | 316 | int dayOfWeek, mday, month, year, hours, minutes, seconds; |
JackB | 3:1af2ff7cfe26 | 317 | // readDateTime(&dayOfWeek, &mday, &month, &year, &hours, &minutes, &seconds); |
JackB | 3:1af2ff7cfe26 | 318 | |
JackB | 3:1af2ff7cfe26 | 319 | // t.tm_year = year - 1900; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 320 | // t.tm_mon = month - 1; // adjust for tm structure required values |
JackB | 3:1af2ff7cfe26 | 321 | // t.tm_mday = mday; |
JackB | 3:1af2ff7cfe26 | 322 | // t.tm_hour = 0; |
JackB | 3:1af2ff7cfe26 | 323 | // t.tm_min = 0; |
JackB | 3:1af2ff7cfe26 | 324 | // t.tm_sec = 0; |
JackB | 3:1af2ff7cfe26 | 325 | |
JackB | 3:1af2ff7cfe26 | 326 | int absMinute = t.tm_hour * 60 + t.tm_min; |
JackB | 3:1af2ff7cfe26 | 327 | int SunRiseMinute = getSunRiseMinute(); |
JackB | 3:1af2ff7cfe26 | 328 | int SunSetMinute = getSunSetMinute(); |
JackB | 3:1af2ff7cfe26 | 329 | |
JackB | 3:1af2ff7cfe26 | 330 | return ((absMinute >= SunRiseMinute) && (absMinute < SunSetMinute)) ? true : false; |
JackB | 3:1af2ff7cfe26 | 331 | } |
JackB | 3:1af2ff7cfe26 | 332 | |
JackB | 3:1af2ff7cfe26 | 333 | void ARM_RTC::substr(char *s, char *d, int pos, int len) |
JackB | 3:1af2ff7cfe26 | 334 | { |
JackB | 3:1af2ff7cfe26 | 335 | char *t; |
JackB | 3:1af2ff7cfe26 | 336 | s = s+pos; |
JackB | 3:1af2ff7cfe26 | 337 | t = s+len; |
JackB | 3:1af2ff7cfe26 | 338 | while (s != t) { |
JackB | 3:1af2ff7cfe26 | 339 | *d=*s; |
JackB | 3:1af2ff7cfe26 | 340 | s++; |
JackB | 3:1af2ff7cfe26 | 341 | d++; |
JackB | 3:1af2ff7cfe26 | 342 | } |
JackB | 3:1af2ff7cfe26 | 343 | *d='\0'; |
JackB | 3:1af2ff7cfe26 | 344 | } |
JackB | 3:1af2ff7cfe26 | 345 | |
JackB | 3:1af2ff7cfe26 | 346 | char * ARM_RTC::substr(char *s, int pos, int len) |
JackB | 3:1af2ff7cfe26 | 347 | { |
JackB | 3:1af2ff7cfe26 | 348 | char *t; |
JackB | 3:1af2ff7cfe26 | 349 | char *d; |
JackB | 3:1af2ff7cfe26 | 350 | d = buffer; |
JackB | 3:1af2ff7cfe26 | 351 | s = s+pos; |
JackB | 3:1af2ff7cfe26 | 352 | t = s+len; |
JackB | 3:1af2ff7cfe26 | 353 | while (s != t) { |
JackB | 3:1af2ff7cfe26 | 354 | *d=*s; |
JackB | 3:1af2ff7cfe26 | 355 | s++; |
JackB | 3:1af2ff7cfe26 | 356 | d++; |
JackB | 3:1af2ff7cfe26 | 357 | } |
JackB | 3:1af2ff7cfe26 | 358 | *d='\0'; |
JackB | 3:1af2ff7cfe26 | 359 | return buffer; |
JackB | 3:1af2ff7cfe26 | 360 | } |