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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/pcf8563"
     9  )
    10  
    11  var (
    12  	i2c = machine.I2C0
    13  	rtc = pcf8563.New(i2c)
    14  )
    15  
    16  func main() {
    17  	i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
    18  	rtc.Reset()
    19  
    20  	time.Sleep(3 * time.Second)
    21  	rtc.SetTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC))
    22  
    23  	rtc.SetTimer(15 * time.Second)
    24  	rtc.EnableTimerInterrupt()
    25  
    26  	prev := -1
    27  
    28  	for {
    29  		for {
    30  			t, _ := rtc.ReadTime()
    31  			if prev != t.Second() {
    32  				fmt.Printf("%s\r\n", t.String())
    33  				prev = t.Second()
    34  
    35  				if rtc.TimerTriggered() {
    36  					fmt.Printf("timer triggered\r\n")
    37  					rtc.ClearTimer()
    38  					rtc.SetTimer(10 * time.Second)
    39  				}
    40  
    41  				break
    42  			}
    43  			time.Sleep(time.Millisecond * 100)
    44  		}
    45  	}
    46  }