2013-12-20 00:43:40 +05:30
|
|
|
// Package BH1750FVI allows interfacing with the BH1750FVI ambient light sensor through I2C protocol.
|
2013-12-21 01:52:36 +05:30
|
|
|
package bh1750fvi
|
2013-12-17 20:37:58 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kid0m4n/go-rpi/i2c"
|
|
|
|
)
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
//accuracy = sensorValue/actualValue] (min = 0.96, typ = 1.2, max = 1.44
|
2013-12-17 20:37:58 +05:30
|
|
|
const (
|
2013-12-20 00:43:40 +05:30
|
|
|
High = "H"
|
|
|
|
High2 = "H2"
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
measurementAcuuracy = 1.2
|
|
|
|
defReadReg = 0x00
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
sensorI2cAddr = 0x23
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
highResOpCode = 0x10
|
|
|
|
highResMode2OpCode = 0x11
|
2013-12-17 20:37:58 +05:30
|
|
|
|
|
|
|
pollDelay = 150
|
|
|
|
)
|
|
|
|
|
2013-12-21 01:52:36 +05:30
|
|
|
// A BH1750FVI interface implements access to the sensor.
|
|
|
|
type BH1750FVI interface {
|
2013-12-20 00:43:40 +05:30
|
|
|
// Run starts continuous sensor data acquisition loop.
|
|
|
|
Run() error
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Lighting returns the ambient lighting in lx.
|
2013-12-17 20:37:58 +05:30
|
|
|
Lighting() (lighting float64, err error)
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Close.
|
2013-12-17 20:37:58 +05:30
|
|
|
Close()
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// SetPollDelay sets the delay between run of data acquisition loop.
|
2013-12-17 20:37:58 +05:30
|
|
|
SetPollDelay(delay int)
|
|
|
|
}
|
|
|
|
|
2013-12-21 01:52:36 +05:30
|
|
|
type bh1750fvi struct {
|
2013-12-17 20:37:58 +05:30
|
|
|
bus i2c.Bus
|
|
|
|
mu *sync.RWMutex
|
|
|
|
|
|
|
|
lightingReadings chan float64
|
|
|
|
quit chan bool
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
i2cAddr byte
|
|
|
|
operationCode byte
|
2013-12-17 20:37:58 +05:30
|
|
|
|
|
|
|
poll int
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Default instance for BH1750FVI sensor
|
|
|
|
var Default = New(High, i2c.Default)
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Supports three modes:
|
|
|
|
// "H" -> High resolution mode (1lx), takes 120ms (recommended).
|
|
|
|
// "H2" -> High resolution mode 2 (0.5lx), takes 120ms (only use for low light).
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// New creates a new BH1750FVI interface according to the mode passed.
|
2013-12-21 01:52:36 +05:30
|
|
|
func New(mode string, bus i2c.Bus) BH1750FVI {
|
2013-12-17 20:37:58 +05:30
|
|
|
switch mode {
|
2013-12-20 00:43:40 +05:30
|
|
|
case High:
|
2013-12-21 01:52:36 +05:30
|
|
|
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, mu: new(sync.RWMutex)}
|
2013-12-20 00:43:40 +05:30
|
|
|
case High2:
|
2013-12-21 01:52:36 +05:30
|
|
|
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResMode2OpCode, mu: new(sync.RWMutex)}
|
2013-12-17 20:37:58 +05:30
|
|
|
default:
|
2013-12-21 01:52:36 +05:30
|
|
|
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, mu: new(sync.RWMutex)}
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// NewHighMode returns a BH1750FVI inteface on high resolution mode (1lx resolution)
|
2013-12-21 01:52:36 +05:30
|
|
|
func NewHighMode(bus i2c.Bus) BH1750FVI {
|
2013-12-20 00:43:40 +05:30
|
|
|
return New(High, bus)
|
|
|
|
}
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// NewHighMode returns a BH1750FVI inteface on high resolution mode2 (0.5lx resolution)
|
2013-12-21 01:52:36 +05:30
|
|
|
func NewHigh2Mode(bus i2c.Bus) BH1750FVI {
|
2013-12-20 00:43:40 +05:30
|
|
|
return New(High2, bus)
|
|
|
|
}
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-21 01:52:36 +05:30
|
|
|
func (d *bh1750fvi) measureLighting() (lighting float64, err error) {
|
2013-12-20 00:43:40 +05:30
|
|
|
err = d.bus.WriteByte(d.i2cAddr, d.operationCode)
|
2013-12-17 20:37:58 +05:30
|
|
|
if err != nil {
|
2013-12-21 01:52:36 +05:30
|
|
|
log.Print("bh1750fvi: Failed to initialize sensor")
|
2013-12-17 20:37:58 +05:30
|
|
|
return
|
|
|
|
}
|
2013-12-20 00:43:40 +05:30
|
|
|
time.Sleep(180 * time.Millisecond)
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
var sensorReading int
|
|
|
|
if sensorReading, err = d.bus.ReadInt(d.i2cAddr, defReadReg); err != nil {
|
2013-12-17 20:37:58 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lighting = float64(sensorReading) / measurementAcuuracy
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Lighting returns the ambient lighting in lx.
|
2013-12-21 01:52:36 +05:30
|
|
|
func (d *bh1750fvi) Lighting() (lighting float64, err error) {
|
2013-12-17 20:37:58 +05:30
|
|
|
select {
|
2013-12-20 00:43:40 +05:30
|
|
|
case lighting = <-d.lightingReadings:
|
2013-12-17 20:37:58 +05:30
|
|
|
return
|
|
|
|
default:
|
2013-12-20 00:43:40 +05:30
|
|
|
return d.measureLighting()
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Run starts continuous sensor data acquisition loop.
|
2013-12-21 01:52:36 +05:30
|
|
|
func (d *bh1750fvi) Run() (err error) {
|
2013-12-17 20:37:58 +05:30
|
|
|
go func() {
|
2013-12-20 00:43:40 +05:30
|
|
|
d.quit = make(chan bool)
|
2013-12-17 20:37:58 +05:30
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
timer := time.Tick(time.Duration(d.poll) * time.Millisecond)
|
2013-12-17 20:37:58 +05:30
|
|
|
|
|
|
|
var lighting float64
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2013-12-20 00:43:40 +05:30
|
|
|
case d.lightingReadings <- lighting:
|
2013-12-17 20:37:58 +05:30
|
|
|
case <-timer:
|
2013-12-20 00:43:40 +05:30
|
|
|
if l, err := d.measureLighting(); err == nil {
|
2013-12-17 20:37:58 +05:30
|
|
|
lighting = l
|
|
|
|
}
|
2013-12-20 00:43:40 +05:30
|
|
|
if err == nil && d.lightingReadings == nil {
|
|
|
|
d.lightingReadings = make(chan float64)
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
2013-12-20 00:43:40 +05:30
|
|
|
case <-d.quit:
|
|
|
|
d.lightingReadings = nil
|
2013-12-17 20:37:58 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Close.
|
2013-12-21 01:52:36 +05:30
|
|
|
func (d *bh1750fvi) Close() {
|
2013-12-20 00:43:40 +05:30
|
|
|
if d.quit != nil {
|
|
|
|
d.quit <- true
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// SetPollDelay sets the delay between run of data acquisition loop.
|
2013-12-21 01:52:36 +05:30
|
|
|
func (d *bh1750fvi) SetPollDelay(delay int) {
|
2013-12-20 00:43:40 +05:30
|
|
|
d.poll = delay
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// SetPollDelay sets the delay between run of data acquisition loop.
|
2013-12-17 20:37:58 +05:30
|
|
|
func SetPollDelay(delay int) {
|
|
|
|
Default.SetPollDelay(delay)
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Lighting returns the ambient lighting in lx.
|
2013-12-17 20:37:58 +05:30
|
|
|
func Lighting() (lighting float64, err error) {
|
|
|
|
return Default.Lighting()
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Run starts continuous sensor data acquisition loop.
|
|
|
|
func Run() (err error) {
|
|
|
|
return Default.Run()
|
2013-12-17 20:37:58 +05:30
|
|
|
}
|
|
|
|
|
2013-12-20 00:43:40 +05:30
|
|
|
// Close.
|
2013-12-17 20:37:58 +05:30
|
|
|
func Close() {
|
|
|
|
Default.Close()
|
|
|
|
}
|