mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-03 06:27:47 +00:00
23833e69c7
* Added alarm features * more functions * chore: fix typo * feat(ds3231): add Alarm2Mode type and mode consts * docs(ds3231): add docstrings for SQW functions * feat(ds3231): add methods for Alarm2 * fix(ds3231): use Alarm2Mode for SetAlarm2 method * docs(ds3231): add example for alarms * docs(ds3231): refactor alarms example * make main function more concise to avoid llvm error for pico * ci(ds3231): split basic and alarm tests * style(ds3231): reorder private funcs to the bottom * docs(ds3231): add docstring for alarm modes * docs(ds3231): make alarm docstrings more descriptive * chore(ds3231): fix typo in docstring * style(ds3231): reorder public functions * style(ds3231): reorder private methods * chore(ds3231): add missing error handling * feat(ds3231): use setter funcs for en/disabling instead of separate funcs * fix(ds3231): correctly enable alarms in example * style(ds3231): rename SetEnable32K to SetEnabled32K for consistency * refactor(ds3231): replace legacy with regmap package * refactor(ds3231): use Write32 instead of Tx for SetAlarm1 * chore(ds3231): remove fmt deps and and use println in examples * refactor(ds3231): read temperature as uint16 --------- Co-authored-by: Matthias Fulz <mfulz@olznet.de>
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
// Connects to an DS3231 I2C Real Time Clock (RTC) and sets both alarms. It then repeatedly checks
|
|
// if the alarms are firing and prints out a message if that is the case.
|
|
package main
|
|
|
|
import (
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/ds3231"
|
|
)
|
|
|
|
func main() {
|
|
machine.I2C0.Configure(machine.I2CConfig{})
|
|
|
|
rtc := ds3231.New(machine.I2C0)
|
|
rtc.Configure()
|
|
|
|
valid := rtc.IsTimeValid()
|
|
if !valid {
|
|
date := time.Date(2019, 12, 05, 20, 34, 12, 0, time.UTC)
|
|
rtc.SetTime(date)
|
|
}
|
|
|
|
// Set alarm1 so it triggers when the seconds match 59 => repeats every minute at dd:hh:mm:59
|
|
if err := rtc.SetAlarm1(time.Date(0, 0, 0, 0, 0, 59, 0, time.UTC), ds3231.A1_SECOND); err != nil {
|
|
println("Error while setting Alarm1")
|
|
}
|
|
if err := rtc.SetEnabledAlarm1(true); err != nil {
|
|
println("Error while enabling Alarm1")
|
|
}
|
|
|
|
// Set alarm2 so it triggers when the minutes match 35 => repeats every hour at dd:hh:35:ss
|
|
if err := rtc.SetAlarm2(time.Date(0, 0, 0, 0, 35, 0, 0, time.UTC), ds3231.A2_MINUTE); err != nil {
|
|
println("Error while setting Alarm2")
|
|
}
|
|
if err := rtc.SetEnabledAlarm2(true); err != nil {
|
|
println("Error while enabling Alarm2")
|
|
}
|
|
|
|
running := rtc.IsRunning()
|
|
if !running {
|
|
err := rtc.SetRunning(true)
|
|
if err != nil {
|
|
println("Error configuring RTC")
|
|
}
|
|
}
|
|
|
|
for {
|
|
dt, err := rtc.ReadTime()
|
|
if err != nil {
|
|
println("Error reading date:", err)
|
|
continue
|
|
}
|
|
|
|
a1 := rtc.IsAlarm1Fired()
|
|
a2 := rtc.IsAlarm2Fired()
|
|
|
|
println(dt.Format(time.DateTime), "A1:", a1, "A2:", a2)
|
|
|
|
if a1 {
|
|
if err := rtc.ClearAlarm1(); err != nil {
|
|
println("Error while clearing alarm1")
|
|
}
|
|
}
|
|
if a2 {
|
|
if err := rtc.ClearAlarm2(); err != nil {
|
|
println("Error while clearing alarm2")
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 1)
|
|
}
|
|
}
|