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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  	"tinygo.org/x/drivers/bmi160"
     8  )
     9  
    10  func main() {
    11  	time.Sleep(5 * time.Second)
    12  
    13  	machine.SPI0.Configure(machine.SPIConfig{})
    14  	sensor := bmi160.NewSPI(machine.A5, machine.SPI0)
    15  	sensor.Configure()
    16  
    17  	if !sensor.Connected() {
    18  		println("BMI160 not connected")
    19  		return
    20  	}
    21  
    22  	for {
    23  		time.Sleep(time.Second)
    24  
    25  		t, err := sensor.ReadTemperature()
    26  		if err != nil {
    27  			println("Error reading temperature", err)
    28  			continue
    29  		}
    30  		fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
    31  
    32  		accelX, accelY, accelZ, err := sensor.ReadAcceleration()
    33  		if err != nil {
    34  			println("Error reading acceleration", err)
    35  			continue
    36  		}
    37  		fmt.Printf("Acceleration: %.2fg %.2fg %.2fg\n", float32(accelX)/1e6, float32(accelY)/1e6, float32(accelZ)/1e6)
    38  
    39  		gyroX, gyroY, gyroZ, err := sensor.ReadRotation()
    40  		if err != nil {
    41  			println("Error reading rotation", err)
    42  			continue
    43  		}
    44  		fmt.Printf("Rotation: %.2f°/s %.2f°/s %.2f°/s\n", float32(gyroX)/1e6, float32(gyroY)/1e6, float32(gyroZ)/1e6)
    45  	}
    46  }