mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
* new helper: AutoDiscoverRTCClock, which detects: DS3231 (on i2c addr 0x68), or RV3028 (on i2c addr 0x52)
* repeater and room server: ver bump (v5), now use AutoDiscoverRTCClock * various Board::begin() now should do Wire.begin(), with custom pin mappings if needed * examples removed: ping client/server, and test_admin
This commit is contained in:
parent
c4cc3dd1ec
commit
a6a6bef371
13 changed files with 155 additions and 755 deletions
61
src/helpers/AutoDiscoverRTCClock.cpp
Normal file
61
src/helpers/AutoDiscoverRTCClock.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include "AutoDiscoverRTCClock.h"
|
||||
#include "RTClib.h"
|
||||
#include <Melopero_RV3028.h>
|
||||
|
||||
static RTC_DS3231 rtc_3231;
|
||||
static bool ds3231_success = false;
|
||||
|
||||
static Melopero_RV3028 rtc_rv3028;
|
||||
static bool rv3028_success = false;
|
||||
|
||||
#define DS3231_ADDRESS 0x68
|
||||
#define RV3028_ADDRESS 0x52
|
||||
|
||||
bool AutoDiscoverRTCClock::i2c_probe(TwoWire& wire, uint8_t addr) {
|
||||
wire.beginTransmission(addr);
|
||||
uint8_t error = wire.endTransmission();
|
||||
return (error == 0);
|
||||
}
|
||||
|
||||
void AutoDiscoverRTCClock::begin(TwoWire& wire) {
|
||||
if (i2c_probe(wire, DS3231_ADDRESS)) {
|
||||
ds3231_success = rtc_3231.begin(&wire);
|
||||
}
|
||||
if (i2c_probe(wire, RV3028_ADDRESS)) {
|
||||
rtc_rv3028.initI2C(wire);
|
||||
rtc_rv3028.writeToRegister(0x35, 0x00);
|
||||
rtc_rv3028.writeToRegister(0x37, 0xB4); // Direct Switching Mode (DSM): when VDD < VBACKUP, switchover occurs from VDD to VBACKUP
|
||||
rtc_rv3028.set24HourMode(); // Set the device to use the 24hour format (default) instead of the 12 hour format
|
||||
rv3028_success = true;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t AutoDiscoverRTCClock::getCurrentTime() {
|
||||
if (ds3231_success) {
|
||||
return rtc_3231.now().unixtime();
|
||||
}
|
||||
if (rv3028_success) {
|
||||
return DateTime(
|
||||
rtc_rv3028.getYear(),
|
||||
rtc_rv3028.getMonth(),
|
||||
rtc_rv3028.getDate(),
|
||||
rtc_rv3028.getHour(),
|
||||
rtc_rv3028.getMinute(),
|
||||
rtc_rv3028.getSecond()
|
||||
).unixtime();
|
||||
}
|
||||
return _fallback->getCurrentTime();
|
||||
}
|
||||
|
||||
void AutoDiscoverRTCClock::setCurrentTime(uint32_t time) {
|
||||
if (ds3231_success) {
|
||||
rtc_3231.adjust(DateTime(time));
|
||||
} else if (rv3028_success) {
|
||||
auto dt = DateTime(time);
|
||||
uint8_t weekday = (dt.day() + (uint16_t)((2.6 * dt.month()) - 0.2) - (2 * (dt.year() / 100)) + dt.year() + (uint16_t)(dt.year() / 4) + (uint16_t)(dt.year() / 400)) % 7;
|
||||
|
||||
rtc_rv3028.setTime(dt.year(), dt.month(), weekday, dt.day(), dt.hour(), dt.minute(), dt.second());
|
||||
} else {
|
||||
_fallback->setCurrentTime(time);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue