tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/pcf8523/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  	"tinygo.org/x/drivers/pcf8523"
     7  )
     8  
     9  func main() {
    10  	machine.I2C0.Configure(machine.I2CConfig{})
    11  	dev := pcf8523.New(machine.I2C0)
    12  
    13  	// make sure the battery takes over if power is lost
    14  	err := dev.SetPowerManagement(pcf8523.PowerManagement_SwitchOver_ModeStandard)
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	// set RTC once, i.e. from `date -u +"%Y-%m-%dT%H:%M:%SZ"`
    20  	now, _ := time.Parse(time.RFC3339, "2023-09-18T20:31:38Z")
    21  	err = dev.SetTime(now)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	for {
    27  		ts, err := dev.ReadTime()
    28  		if err != nil {
    29  			panic(err)
    30  		}
    31  		println("tick-tock, it's: " + ts.String())
    32  		time.Sleep(2 * time.Second)
    33  	}
    34  }