mirror of
https://github.com/kidoman/embd
synced 2025-01-31 15:53:18 +01:00
Add support for isl29125 RGB light sensor
This commit is contained in:
parent
9a96639962
commit
3f7fe59036
@ -7,9 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/isl29125"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
"github.com/kidoman/embd/sensor/isl29125"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -23,6 +22,7 @@ func main() {
|
||||
|
||||
isl := isl29125.New(isl29125.DefaultConfig, bus)
|
||||
defer isl.Close()
|
||||
isl.Init()
|
||||
|
||||
for {
|
||||
r, err := isl.Reading()
|
||||
|
@ -5,12 +5,15 @@ package isl29125
|
||||
/*
|
||||
|
||||
TODO:
|
||||
- improve constructor
|
||||
- add support for config register 2 and 3
|
||||
- add support for lux calculation
|
||||
- add support for the following:
|
||||
- config register 2 and 3
|
||||
- lux calculation
|
||||
- powerdown / power up
|
||||
- interupt config / monitor
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
@ -18,13 +21,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
sensorI2cAddr = 0x44
|
||||
|
||||
pollDelay = 250
|
||||
|
||||
deviceRegisterAddr = 0x00
|
||||
conf1RegisterAddr = 0x01
|
||||
flagRegisterAdr = 0x08
|
||||
)
|
||||
|
||||
const (
|
||||
@ -43,6 +40,12 @@ const (
|
||||
LuxRange10k = 0x08
|
||||
)
|
||||
|
||||
const (
|
||||
IRAdjustLow = 0x00
|
||||
IRAdjustMed = 0x20
|
||||
IRAdjustHigh = 0x3f
|
||||
)
|
||||
|
||||
const (
|
||||
Resolution16Bit = 0x00
|
||||
Resolution12Bit = 0x10
|
||||
@ -54,6 +57,11 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
RegisterDeviceID = 0x00
|
||||
RegisterConfig1 = 0x01
|
||||
RegisterConfig2 = 0x02
|
||||
RegisterConfig3 = 0x03
|
||||
RegisterFlags = 0x08
|
||||
RegisterGreenLow = 0x09
|
||||
RegisterGreenHigh = 0x0a
|
||||
RegisterRedLow = 0x0b
|
||||
@ -63,6 +71,7 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
FlagReady = 0x00
|
||||
FlagInterrupt = 0x01
|
||||
FlagConversion = 0x02
|
||||
FlagPowerDownOrBrownOut = 0x04
|
||||
@ -72,11 +81,17 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultConfig = ModePowerDown | LuxRange375 | Resolution16Bit | SyncStartOnWrite
|
||||
DefaultConfig = ModeRGB | LuxRange375 | Resolution16Bit | SyncStartOnWrite
|
||||
)
|
||||
|
||||
const (
|
||||
DeviceID = 0x7d
|
||||
SensorAddr = 0x44
|
||||
DeviceID = 0x7d
|
||||
)
|
||||
|
||||
const (
|
||||
CmdGetStatus = 0x08
|
||||
CmdReset = 0x46
|
||||
)
|
||||
|
||||
// Reading represents a single reading from an RGB light sensor
|
||||
@ -97,30 +112,82 @@ type ISL29125 struct {
|
||||
readings chan *Reading
|
||||
quit chan bool
|
||||
|
||||
mode int
|
||||
luxRange int
|
||||
resolution int
|
||||
syncStart int
|
||||
mode uint8
|
||||
}
|
||||
|
||||
// New returns an ISL29125 for a given config
|
||||
func New(config int, bus embd.I2CBus) *ISL29125 {
|
||||
func New(config uint8, bus embd.I2CBus) *ISL29125 {
|
||||
glog.Info("Creating new ISL29125")
|
||||
return &ISL29125{Bus: bus, Poll: pollDelay}
|
||||
return &ISL29125{Bus: bus, Poll: pollDelay, mode: config}
|
||||
}
|
||||
|
||||
func (i *ISL29125) Init() error {
|
||||
|
||||
// verify that i2c device is reachable on specified bus and that it reports back the correct ID
|
||||
id, err := i.Bus.ReadByteFromReg(SensorAddr, RegisterDeviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if DeviceID != id {
|
||||
return fmt.Errorf("Invalid device id. Expected [%x] but device reports [%x]", DeviceID, id)
|
||||
}
|
||||
|
||||
// power down device ( don't know current state; assume it's running )
|
||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig1, ModePowerDown)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// reset device
|
||||
err = i.Bus.WriteByteToReg(SensorAddr, DeviceID, CmdReset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// verify status after reset is ready
|
||||
status, err := i.Bus.ReadByteFromReg(SensorAddr, CmdGetStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status != FlagReady {
|
||||
return fmt.Errorf("Invalid device status. Expected [%x] but device reports [%x]", FlagReady, status)
|
||||
}
|
||||
|
||||
// set config 1 to user specified mode
|
||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig1, i.mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// set config 2 to fixed value
|
||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig2, IRAdjustHigh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set config 3 to fixed value
|
||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig3, 0x0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (i *ISL29125) getReading() (*Reading, error) {
|
||||
|
||||
glog.Info("Getting reading")
|
||||
red, err := i.Bus.ReadWordFromReg(sensorI2cAddr, RegisterRedLow)
|
||||
red, err := i.Bus.ReadWordFromReg(SensorAddr, RegisterRedLow)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
green, err := i.Bus.ReadWordFromReg(sensorI2cAddr, RegisterGreenLow)
|
||||
green, err := i.Bus.ReadWordFromReg(SensorAddr, RegisterGreenLow)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blue, err := i.Bus.ReadWordFromReg(sensorI2cAddr, RegisterBlueLow)
|
||||
blue, err := i.Bus.ReadWordFromReg(SensorAddr, RegisterBlueLow)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user