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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/gps"
     8  )
     9  
    10  func main() {
    11  	println("GPS UART Example")
    12  	machine.UART1.Configure(machine.UARTConfig{BaudRate: 9600})
    13  	ublox := gps.NewUART(machine.UART1)
    14  	parser := gps.NewParser()
    15  	var fix gps.Fix
    16  	for {
    17  		s, err := ublox.NextSentence()
    18  		if err != nil {
    19  			println(err)
    20  			continue
    21  		}
    22  
    23  		fix, err = parser.Parse(s)
    24  		if err != nil {
    25  			println(err)
    26  			continue
    27  		}
    28  		if fix.Valid {
    29  			print(fix.Time.Format("15:04:05"))
    30  			print(", lat=")
    31  			print(fix.Latitude)
    32  			print(", long=")
    33  			print(fix.Longitude)
    34  			print(", altitude=", fix.Altitude)
    35  			print(", satellites=", fix.Satellites)
    36  			if fix.Speed != 0 {
    37  				print(", speed=")
    38  				print(fix.Speed)
    39  			}
    40  			if fix.Heading != 0 {
    41  				print(", heading=")
    42  				print(fix.Heading)
    43  			}
    44  			println()
    45  		} else {
    46  			println("No fix")
    47  		}
    48  		time.Sleep(200 * time.Millisecond)
    49  	}
    50  }