Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. /*
  2. ds1307 support module
  3. not tested but it almost similar to ds3231, so it maybe work
  4. Copyright (C) 2018 by Pavel Chauzov <poulch at mail dot ru>
  5. */
  6. #pragma once
  7. #undef I2C_SUPPORT
  8. #define I2C_SUPPORT 1 // Explicitly request I2C support.
  9. #include <TimeLib.h>
  10. #define DS1307ADDR 0x68
  11. #define _bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
  12. #define _decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
  13. time_t getTime_rtc() {
  14. uint8_t data[7];
  15. tmElements_t tm;
  16. i2c_write_uint8(DS1307ADDR,0);
  17. i2c_read_buffer(DS1307ADDR, data, 7);
  18. tm.Second = _bcdToDec(data[0]);
  19. tm.Minute = _bcdToDec(data[1]);
  20. tm.Hour = _bcdToDec(data[2]);
  21. tm.Wday = _bcdToDec(data[3]);
  22. tm.Day = _bcdToDec(data[4]);
  23. tm.Month = _bcdToDec(data[5]);
  24. tm.Year = y2kYearToTm(_bcdToDec(data[6]));
  25. return makeTime(tm);
  26. }
  27. uint8_t setTime_rtc(time_t nt) {
  28. uint8_t data[8];
  29. tmElements_t ct;
  30. breakTime(nt, ct);
  31. data[0] = 0;
  32. data[1] = _decToBcd(ct.Second);
  33. data[2] = _decToBcd(ct.Minute);
  34. data[3] = _decToBcd(ct.Hour);
  35. data[4] = _decToBcd(ct.Wday);
  36. data[5] = _decToBcd(ct.Day);
  37. data[6] = _decToBcd(ct.Month);
  38. data[7] = _decToBcd(tmYearToY2k(ct.Year));
  39. uint8_t s = i2c_write_buffer(DS1307ADDR, data, 8);
  40. return s;
  41. }