mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-10 18:03:41 +00:00
DS3231 Alarm features (#805)
* 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>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Connects to an DS3231 I2C Real Time Clock (RTC).
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"strconv"
|
||||
"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)
|
||||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
println(dt.Format(time.DateTime))
|
||||
}
|
||||
temp, _ := rtc.ReadTemperature()
|
||||
println("Temperature:", strconv.FormatFloat(float64(temp)/1000, 'f', -1, 32), "°C")
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user