MCPcopy Create free account
hub / github.com/crosspoint-reader/crosspoint-reader / getTime

Method getTime

lib/hal/HalClock.cpp:50–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50bool HalClock::getTime(uint8_t& hour, uint8_t& minute) const {
51 if (!_available) return false;
52
53 const unsigned long now = millis();
54 if (_lastPollMs != 0 && (now - _lastPollMs) < CLOCK_POLL_MS) {
55 hour = _cachedHour;
56 minute = _cachedMinute;
57 return true;
58 }
59
60 // Read 3 bytes starting at register 0x00: seconds, minutes, hours
61 Wire.beginTransmission(I2C_ADDR_DS3231);
62 Wire.write(DS3231_SEC_REG);
63 if (Wire.endTransmission(false) != 0) {
64 if (!_hasCachedTime) return false;
65 _lastPollMs = now;
66 hour = _cachedHour;
67 minute = _cachedMinute;
68 return true;
69 }
70 Wire.requestFrom(I2C_ADDR_DS3231, (uint8_t)3);
71 if (Wire.available() < 3) {
72 if (!_hasCachedTime) return false;
73 _lastPollMs = now;
74 hour = _cachedHour;
75 minute = _cachedMinute;
76 return true;
77 }
78
79 Wire.read(); // seconds — not needed
80 const uint8_t rawMin = Wire.read();
81 const uint8_t rawHour = Wire.read();
82
83 _cachedMinute = bcdToDec(rawMin & 0x7F);
84 // Handle 12/24h mode: bit 6 high = 12h mode
85 if (rawHour & 0x40) {
86 // 12h mode: bit 5 = PM, bits 4-0 = hours (1-12)
87 uint8_t h12 = bcdToDec(rawHour & 0x1F);
88 bool pm = rawHour & 0x20;
89 if (h12 == 12) h12 = 0;
90 _cachedHour = pm ? (h12 + 12) : h12;
91 } else {
92 // 24h mode: bits 5-0 = hours (0-23)
93 _cachedHour = bcdToDec(rawHour & 0x3F);
94 }
95 _lastPollMs = now;
96 _hasCachedTime = true;
97
98 hour = _cachedHour;
99 minute = _cachedMinute;
100 return true;
101}
102
103bool HalClock::formatTime(char* buf, size_t bufSize, uint8_t utcOffsetQuarterHoursBiased, bool use12Hour) const {
104 if (bufSize < (use12Hour ? 9u : 6u)) return false;

Callers

nothing calls this directly

Calls 4

bcdToDecFunction · 0.85
writeMethod · 0.45
availableMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected