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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  )
     8  
     9  func main() {
    10  	//sleep for 2 secs for console
    11  	time.Sleep(2 * time.Second)
    12  
    13  	config := machine.WatchdogConfig{
    14  		TimeoutMillis: 1000,
    15  	}
    16  
    17  	println("configuring watchdog for max 1 second updates")
    18  	machine.Watchdog.Configure(config)
    19  
    20  	// From this point the watchdog is running and Update must be
    21  	// called periodically, per the config
    22  	machine.Watchdog.Start()
    23  
    24  	// This loop should complete because watchdog update is called
    25  	// every 100ms.
    26  	start := time.Now()
    27  	println("updating watchdog for 3 seconds")
    28  	for i := 0; i < 30; i++ {
    29  		time.Sleep(100 * time.Millisecond)
    30  		machine.Watchdog.Update()
    31  		fmt.Printf("alive @ %v\n", time.Now().Sub(start))
    32  	}
    33  
    34  	// This loop should cause a watchdog reset after 1s since
    35  	// there is no update call.
    36  	start = time.Now()
    37  	println("entering tight loop")
    38  	for {
    39  		time.Sleep(100 * time.Millisecond)
    40  		fmt.Printf("alive @ %v\n", time.Now().Sub(start))
    41  	}
    42  }