github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/rtcinterrupt/rtcinterrupt.go (about)

     1  //go:build rp2040
     2  
     3  package main
     4  
     5  // This example demonstrates scheduling a delayed interrupt by real time clock.
     6  //
     7  // An interrupt may execute user callback function or used for its side effects
     8  // like waking up from sleep or dormant states.
     9  //
    10  // The interrupt can be configured to repeat.
    11  //
    12  // There is no separate method to disable interrupt, use 0 delay for that.
    13  //
    14  // Unfortunately, it is not possible to use time.Duration to work with RTC directly,
    15  // that would introduce a circular dependency between "machine" and "time" packages.
    16  
    17  import (
    18  	"fmt"
    19  	"machine"
    20  	"time"
    21  )
    22  
    23  func main() {
    24  
    25  	// Schedule and enable recurring interrupt.
    26  	// The callback function is executed in the context of an interrupt handler,
    27  	// so regular restrictions for this sort of code apply: no blocking, no memory allocation, etc.
    28  	// Please check the online documentation for the details about interrupts:
    29  	// https://tinygo.org/docs/concepts/compiler-internals/interrupts/
    30  	delay := time.Minute + 12*time.Second
    31  	machine.RTC.SetInterrupt(uint32(delay.Seconds()), true, func() { println("Peekaboo!") })
    32  
    33  	for {
    34  		fmt.Printf("%v\r\n", time.Now().Format(time.RFC3339))
    35  		time.Sleep(1 * time.Second)
    36  	}
    37  }