mirror of
https://github.com/kidoman/embd
synced 2025-07-01 11:08:36 +02:00
changes from PR comments
This commit is contained in:
parent
9166155d1d
commit
b26f74efe8
@ -94,7 +94,7 @@ const (
|
|||||||
CmdReset = 0x46
|
CmdReset = 0x46
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reading represents a single reading from an RGB light sensor
|
// Reading represents a single reading from an RGB light sensor.
|
||||||
type Reading struct {
|
type Reading struct {
|
||||||
Red uint16
|
Red uint16
|
||||||
Green uint16
|
Green uint16
|
||||||
@ -104,7 +104,7 @@ type Reading struct {
|
|||||||
Resolution int
|
Resolution int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ISL29125 represents an RGB light sensor
|
// ISL29125 represents an RGB light sensor.
|
||||||
type ISL29125 struct {
|
type ISL29125 struct {
|
||||||
Bus embd.I2CBus
|
Bus embd.I2CBus
|
||||||
Poll int
|
Poll int
|
||||||
@ -115,9 +115,9 @@ type ISL29125 struct {
|
|||||||
mode uint8
|
mode uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an ISL29125 for a given config
|
// New returns an ISL29125 for a given config.
|
||||||
func New(config uint8, bus embd.I2CBus) *ISL29125 {
|
func New(config uint8, bus embd.I2CBus) *ISL29125 {
|
||||||
glog.Info("Creating new ISL29125")
|
glog.V(1).Info("Creating new ISL29125")
|
||||||
return &ISL29125{Bus: bus, Poll: pollDelay, mode: config}
|
return &ISL29125{Bus: bus, Poll: pollDelay, mode: config}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,28 +147,25 @@ func (i *ISL29125) Init() error {
|
|||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
// verify status after reset is ready
|
// verify status after reset is ready
|
||||||
status, err := i.Bus.ReadByteFromReg(SensorAddr, CmdGetStatus)
|
if status, err := i.Bus.ReadByteFromReg(SensorAddr, CmdGetStatus); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if status != FlagReady {
|
if status != FlagReady {
|
||||||
return fmt.Errorf("Invalid device status. Expected [%x] but device reports [%x]", FlagReady, status)
|
return fmt.Errorf("Invalid device status. Expected [%x] but device reports [%x]", FlagReady, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set config 1 to user specified mode
|
// set config 1 to user specified mode
|
||||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig1, i.mode)
|
if err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig1, i.mode); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// set config 2 to fixed value
|
// set config 2 to fixed value
|
||||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig2, IRAdjustHigh)
|
if err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig2, IRAdjustHigh); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// set config 3 to fixed value
|
// set config 3 to fixed value
|
||||||
err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig3, 0x0)
|
if err = i.Bus.WriteByteToReg(SensorAddr, RegisterConfig3, 0x0); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +175,7 @@ func (i *ISL29125) Init() error {
|
|||||||
|
|
||||||
func (i *ISL29125) getReading() (*Reading, error) {
|
func (i *ISL29125) getReading() (*Reading, error) {
|
||||||
|
|
||||||
glog.Info("Getting reading")
|
glog.V(1).Info("Getting reading")
|
||||||
red, err := i.Bus.ReadWordFromReg(SensorAddr, RegisterRedLow)
|
red, err := i.Bus.ReadWordFromReg(SensorAddr, RegisterRedLow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -195,7 +192,7 @@ func (i *ISL29125) getReading() (*Reading, error) {
|
|||||||
return &Reading{Red: red, Green: green, Blue: blue}, nil
|
return &Reading{Red: red, Green: green, Blue: blue}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reading returns a single sensor reading
|
// Reading returns a single sensor reading.
|
||||||
func (i *ISL29125) Reading() (*Reading, error) {
|
func (i *ISL29125) Reading() (*Reading, error) {
|
||||||
select {
|
select {
|
||||||
case r := <-i.readings:
|
case r := <-i.readings:
|
||||||
@ -207,11 +204,12 @@ func (i *ISL29125) Reading() (*Reading, error) {
|
|||||||
|
|
||||||
// Run starts continuous sensor data acquisition loop.
|
// Run starts continuous sensor data acquisition loop.
|
||||||
func (i *ISL29125) Run() {
|
func (i *ISL29125) Run() {
|
||||||
glog.Info("Running sensor")
|
glog.V(1).Info("Running sensor")
|
||||||
go func() {
|
go func() {
|
||||||
i.quit = make(chan bool)
|
i.quit = make(chan bool)
|
||||||
i.readings = make(chan *Reading)
|
i.readings = make(chan *Reading)
|
||||||
timer := time.Tick(time.Duration(i.Poll) * time.Millisecond)
|
timer := time.NewTicker(time.Duration(i.Poll) * time.Millisecond)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
var reading *Reading
|
var reading *Reading
|
||||||
|
|
||||||
@ -232,9 +230,9 @@ func (i *ISL29125) Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close down sensor
|
// Close down sensor.
|
||||||
func (i *ISL29125) Close() {
|
func (i *ISL29125) Close() {
|
||||||
glog.Info("Closing sensor")
|
glog.V(1).Info("Closing sensor")
|
||||||
if i.quit != nil {
|
if i.quit != nil {
|
||||||
i.quit <- true
|
i.quit <- true
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user