Files
ds3231/example/main.cpp
T
BlackMarkandClaude Opus 5.5 081f2e74cd example: the code shows what the LED does, and a wrong comment goes
The comment on the failed alarm clear claimed it holds the LED dark; the
next clock read rewrites the LED a tenth of a second later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 21:56:05 +02:00

34 lines
953 B
C++

#include <ds3231/ds3231.hpp>
#include <libavr/libavr.hpp>
using namespace avr::literals;
// One source for the tiny85 (open-drain software I2C on the USI pins) and
// the mega328P (TWI hardware).
using dev = avr::device<{.clock = 8_MHz}>;
using bus = dev::i2c<{.frequency = 100_kHz}>;
using rtc = ds3231::device<bus>;
using led = dev::output<avr::pb3>;
int main()
{
avr::init<bus, led>();
if (auto stopped = rtc::oscillator_stopped(); stopped.value_or(false)) {
const bool seeded = rtc::write_clock({{2026, 1, 1}, {12, 0, 0}}) &&
rtc::set_alarm1({}, ds3231::alarm1_rate::once_per_second) &&
rtc::clear_oscillator_stopped();
led::write(seeded);
}
while (true) {
if (auto now = rtc::read_clock(); now.has_value()) {
led::write(now->second % 2 == 0);
}
if (auto fired = rtc::alarm1_fired(); fired.value_or(false) && !rtc::clear_alarm1()) {
led::clear();
}
dev::delay<100_ms>();
}
}