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

     1  // Connects to an QMI8658C I2C accelerometer/gyroscope and print the read data.
     2  // This example was made with the "WaveShare RP2040 Round LCD 1.28in" in mind.
     3  // For more infor about this development board:
     4  // https://www.waveshare.com/wiki/RP2040-LCD-1.28
     5  package main
     6  
     7  import (
     8  	"machine"
     9  	"time"
    10  
    11  	imu "tinygo.org/x/drivers/qmi8658c"
    12  )
    13  
    14  func main() {
    15  	i2c := machine.I2C1
    16  	// This is the default pinout for the "WaveShare RP2040 Round LCD 1.28in"
    17  	err := i2c.Configure(machine.I2CConfig{
    18  		SDA:       machine.GP6,
    19  		SCL:       machine.GP7,
    20  		Frequency: 100000,
    21  	})
    22  	if err != nil {
    23  		println("unable to configure I2C:", err)
    24  		return
    25  	}
    26  	// Create a new device
    27  	d := imu.New(i2c)
    28  
    29  	// Check if the device is connected
    30  	if !d.Connected() {
    31  		println("unable to connect to sensor")
    32  		return
    33  	}
    34  
    35  	// This IMU has multiple configurations like output data rate, multiple
    36  	// measurements scales, low pass filters, low power modes, all the vailable
    37  	// values can be found in the datasheet and were defined at registers file.
    38  	// This is the default configuration which will be used if the `nil` value
    39  	// is passed do the `Configure` method.
    40  	config := imu.Config{
    41  		SPIMode:     imu.SPI_4_WIRE,
    42  		SPIEndian:   imu.SPI_BIG_ENDIAN,
    43  		SPIAutoInc:  imu.SPI_AUTO_INC,
    44  		AccEnable:   imu.ACC_ENABLE,
    45  		AccScale:    imu.ACC_8G,
    46  		AccRate:     imu.ACC_NORMAL_1000HZ,
    47  		AccLowPass:  imu.ACC_LOW_PASS_2_62,
    48  		GyroEnable:  imu.GYRO_FULL_ENABLE,
    49  		GyroScale:   imu.GYRO_512DPS,
    50  		GyroRate:    imu.GYRO_1000HZ,
    51  		GyroLowPass: imu.GYRO_LOW_PASS_2_62,
    52  	}
    53  	d.Configure(config)
    54  
    55  	// Read the accelation, rotation and temperature data and print them.
    56  	for {
    57  		acc_x, acc_y, acc_z := d.ReadAcceleration()
    58  		gyro_x, gyro_y, gyro_z := d.ReadRotation()
    59  		temp, _ := d.ReadTemperature()
    60  		println("-------------------------------")
    61  		println("acc:", acc_x, acc_y, acc_z)
    62  		println("gyro:", gyro_x, gyro_y, gyro_z)
    63  		println("temp:", temp)
    64  		time.Sleep(time.Millisecond * 100)
    65  	}
    66  }