mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-05 15:33:42 +00:00
add draft apds9930
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package apds9930
|
||||
|
||||
import "tinygo.org/x/drivers"
|
||||
|
||||
type Dev struct {
|
||||
bus drivers.I2C
|
||||
_txerr error
|
||||
addr uint16
|
||||
buf [8]byte
|
||||
}
|
||||
|
||||
func New(bus drivers.I2C, addr uint8) Dev {
|
||||
return Dev{bus: bus, addr: uint16(addr)}
|
||||
}
|
||||
|
||||
func (d *Dev) Init() error {
|
||||
d.txNew()
|
||||
d.txWrite8(regENABLE, 0x00) // disable all features.
|
||||
d.txWrite8(regATIME, 0xee) // set default integration time.
|
||||
d.txWrite8(regPPULSE, 0x04)
|
||||
d.txWrite8(regWTIME, 0xee) // set default wait time.
|
||||
d.txWrite8(regPTIME, 0xff) // set default pulse count.
|
||||
d.txWrite8(regCONTROL, 0x20)
|
||||
return d.txErr()
|
||||
}
|
||||
|
||||
func (d *Dev) EnableProximity() error {
|
||||
d.txNew()
|
||||
d.txWrite8(regENABLE, 8|4|2|1)
|
||||
return d.txErr()
|
||||
}
|
||||
|
||||
func (d *Dev) ProximityAvailable() bool {
|
||||
d.txNew()
|
||||
return d.txRead8(regSTATUS)&0x20 == 1
|
||||
}
|
||||
|
||||
func (d *Dev) ReadProximity() uint8 {
|
||||
d.txNew()
|
||||
h := d.txRead8(regPDATAH)
|
||||
l := d.txRead8(regPDATAL)
|
||||
if d.txErr() != nil {
|
||||
return 0
|
||||
}
|
||||
return (h << 8) | l
|
||||
}
|
||||
|
||||
func (d *Dev) txRead8(reg uint8) uint8 {
|
||||
if d.txErr() != nil {
|
||||
return 0
|
||||
}
|
||||
d.buf[0] = reg | 0xa0
|
||||
d._txerr = d.bus.Tx(d.addr, d.buf[:1], d.buf[1:2])
|
||||
return d.buf[1]
|
||||
}
|
||||
|
||||
func (d *Dev) txWrite8(reg uint8, val uint8) {
|
||||
if d.txErr() != nil {
|
||||
return
|
||||
}
|
||||
d.buf[0] = reg | 0x80
|
||||
d.buf[1] = val
|
||||
d._txerr = d.bus.Tx(d.addr, d.buf[:2], nil)
|
||||
}
|
||||
|
||||
func (d *Dev) txNew() { d._txerr = nil }
|
||||
|
||||
func (d *Dev) txErr() error { return d._txerr }
|
||||
@@ -0,0 +1,19 @@
|
||||
package apds9930
|
||||
|
||||
const (
|
||||
regENABLE = 0x00
|
||||
regATIME = 0x01
|
||||
regPTIME = 0x02
|
||||
regWTIME = 0x03
|
||||
regPILTL = 0x08
|
||||
regPILTH = 0x09
|
||||
regPIHTL = 0x0A
|
||||
regPIHTH = 0x0B
|
||||
regCONFIG = 0x0D
|
||||
regPPULSE = 0x0E
|
||||
regCONTROL = 0x0F
|
||||
regSTATUS = 0x13
|
||||
regPDATAL = 0x18
|
||||
regPDATAH = 0x19
|
||||
regPOFFSET = 0x1E
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/apds9930"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Sleep to catch any errors through the serial monitor.
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
bus := machine.I2C0
|
||||
// use Nano 33 BLE Sense's internal I2C bus
|
||||
err := bus.Configure(machine.I2CConfig{
|
||||
SCL: machine.GP1,
|
||||
SDA: machine.GP0,
|
||||
Frequency: 100 * machine.KHz,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
sensor := apds9930.New(bus, 0x39)
|
||||
|
||||
err = sensor.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = sensor.EnableProximity()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
println("proximity enabled!")
|
||||
for {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
prox := sensor.ReadProximity()
|
||||
println("proximity:", prox)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user