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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/apds9960"
     8  )
     9  
    10  func main() {
    11  
    12  	// use Nano 33 BLE Sense's internal I2C bus
    13  	machine.I2C1.Configure(machine.I2CConfig{
    14  		SCL:       machine.SCL1_PIN,
    15  		SDA:       machine.SDA1_PIN,
    16  		Frequency: machine.TWI_FREQ_400KHZ,
    17  	})
    18  
    19  	sensor := apds9960.New(machine.I2C1)
    20  
    21  	// use default settings
    22  	sensor.Configure(apds9960.Configuration{})
    23  
    24  	if !sensor.Connected() {
    25  		println("APDS-9960 not connected!")
    26  		return
    27  	}
    28  
    29  	sensor.EnableProximity() // enable proximity engine
    30  
    31  	for {
    32  
    33  		if sensor.ProximityAvailable() {
    34  			p := sensor.ReadProximity()
    35  			println("Proximity:", p)
    36  		}
    37  		time.Sleep(time.Millisecond * 100)
    38  	}
    39  
    40  }