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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
| #include "RX8025.h"
#if defined(__AVR__) #include <avr/pgmspace.h> #elif defined(ESP8266) #include <pgmspace.h> #endif
#include <Arduino.h>
#define RX8025_address 0x32
#define SECONDS_FROM_1970_TO_2000 946684800 - (8 * 60 * 60)
static const uint8_t daysInMonth[] PROGMEM = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) { if (y >= 2000) y -= 2000; uint16_t days = d; for (uint8_t i = 1; i < m; ++i) days += pgm_read_byte(daysInMonth + i - 1); if (m > 2 && isleapYear(y)) ++days; return days + 365 * y + (y + 3) / 4 - 1; }
static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) { return ((days * 24L + h) * 60 + m) * 60 + s; }
DateTime::DateTime(uint32_t t) { t -= SECONDS_FROM_1970_TO_2000;
ss = t % 60; t /= 60; mm = t % 60; t /= 60; hh = t % 24; uint16_t days = t / 24; uint8_t leap; for (yOff = 0;; ++yOff) { leap = isleapYear(yOff); if (days < 365 + leap) break; days -= 365 + leap; } for (m = 1;; ++m) { uint8_t daysPerMonth = pgm_read_byte(daysInMonth + m - 1); if (leap && m == 2) ++daysPerMonth; if (days < daysPerMonth) break; days -= daysPerMonth; } d = days + 1; }
DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) { if (year >= 2000) { year -= 2000; } yOff = year; m = month; d = day; hh = hour; mm = min; ss = sec; }
DateTime::DateTime(const char *date, const char *time) { static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; static char buff[4] = {'0', '0', '0', '0'}; int y; sscanf(date, "%s %c %d", buff, &d, &y); yOff = y >= 2000 ? y - 2000 : y; m = (strstr(month_names, buff) - month_names) / 3 + 1; sscanf(time, "%c:%c:%c", &hh, &mm, &ss); }
uint32_t DateTime::unixtime(void) const { uint32_t t; uint16_t days = date2days(yOff, m, d); t = time2long(days, hh, mm, ss); t += SECONDS_FROM_1970_TO_2000; return t; }
static uint8_t bcd2bin(uint8_t val) { return val - 6 * (val >> 4); }
static uint8_t bin2bcd(uint8_t val) { return val + 6 * (val / 10); }
bool isleapYear(const uint8_t y) { if (y & 3) { return false; } return (y % 100 || y % 400 == 0); }
RX8025::RX8025() { RX8025_Control[0] = 0x20; RX8025_Control[1] = 0x00; }
void RX8025::setRtcTime(uint8_t s, uint8_t m, uint8_t h, uint8_t d, uint8_t mh, uint8_t y) { Wire.beginTransmission(RX8025_address); Wire.write((byte)0x00); Wire.write(decToBcd(s)); Wire.write(decToBcd(m)); Wire.write(decToBcd(h)); Wire.write(0x1); Wire.write(decToBcd(d)); Wire.write(decToBcd(mh)); Wire.write(decToBcd(y)); Wire.endTransmission(); }
byte RX8025::getData(byte regaddr) { Wire.beginTransmission(RX8025_address); Wire.write(regaddr); Wire.endTransmission(); Wire.requestFrom(RX8025_address, 1); return Wire.read(); }
void RX8025::RX8025_init(void) { Wire.begin(); Wire.beginTransmission(RX8025_address); Wire.write(0xe0); for (unsigned char i = 0; i < 2; i++) { Wire.write(RX8025_Control[i]); } Wire.endTransmission(); }
byte RX8025::decToBcd(byte val) { return ((val / 10 * 16) + (val % 10)); }
byte RX8025::bcdToDec(byte val) { return ((val / 16 * 10) + (val % 16)); }
byte RX8025::getSecond() { byte buff = getData(RX8025_SEC); return bcdToDec(buff & 0x7f); }
byte RX8025::getMinute() { byte buff = getData(RX8025_MIN); return bcdToDec(buff & 0x7f); }
byte RX8025::getHour() { byte buff = getData(RX8025_HR); return bcdToDec(buff & 0x3f); }
byte RX8025::getDoW() { byte buff = getData(RX8025_WEEK); return bcdToDec(buff & 0x07); }
byte RX8025::getDate() { byte buff = getData(RX8025_DATE); return bcdToDec(buff & 0x3f); }
byte RX8025::getMonth() { byte buff = getData(RX8025_MTH); return bcdToDec(buff & 0x1f); }
byte RX8025::getYear() { byte buff = getData(RX8025_YR); return bcdToDec(buff & 0xff); }
long RX8025::getUnixtime() { Wire.beginTransmission(RX8025_address); Wire.write(0x00); Wire.endTransmission(); Wire.requestFrom(RX8025_address, 7); uint16_t ss = bcdToDec(Wire.read() & 0x7F); uint16_t mm = bcdToDec(Wire.read() & 0x7f); uint16_t hh = bcdToDec(Wire.read() & 0x3f); Wire.read(); uint16_t d = bcdToDec(Wire.read() & 0x3f); uint16_t m = bcdToDec(Wire.read() & 0x1f); uint16_t y = bcdToDec(Wire.read() & 0xff) + 2000; return DateTime(y, m, d, hh, mm, ss).unixtime(); }
|