mirror of
https://github.com/kidoman/embd
synced 2025-07-03 03:47:33 +02:00
simplify package structure
This commit is contained in:
parent
3cae4064dc
commit
36f2c0486d
41 changed files with 736 additions and 885 deletions
|
@ -6,7 +6,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
//accuracy = sensorValue/actualValue] (min = 0.96, typ = 1.2, max = 1.44
|
||||
|
@ -25,62 +25,42 @@ const (
|
|||
pollDelay = 150
|
||||
)
|
||||
|
||||
// A BH1750FVI interface implements access to the sensor.
|
||||
type BH1750FVI interface {
|
||||
// Run starts continuous sensor data acquisition loop.
|
||||
Run() error
|
||||
type BH1750FVI struct {
|
||||
Bus embd.I2CBus
|
||||
Poll int
|
||||
|
||||
// Lighting returns the ambient lighting in lx.
|
||||
Lighting() (lighting float64, err error)
|
||||
|
||||
// Close.
|
||||
Close()
|
||||
|
||||
// SetPollDelay sets the delay between run of data acquisition loop.
|
||||
SetPollDelay(delay int)
|
||||
}
|
||||
|
||||
type bh1750fvi struct {
|
||||
bus i2c.Bus
|
||||
mu *sync.RWMutex
|
||||
mu sync.RWMutex
|
||||
|
||||
lightingReadings chan float64
|
||||
quit chan bool
|
||||
|
||||
i2cAddr byte
|
||||
operationCode byte
|
||||
|
||||
poll int
|
||||
}
|
||||
|
||||
// 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).
|
||||
|
||||
// New creates a new BH1750FVI interface according to the mode passed.
|
||||
func New(mode string, bus i2c.Bus) BH1750FVI {
|
||||
func New(mode string, bus embd.I2CBus) *BH1750FVI {
|
||||
switch mode {
|
||||
case High:
|
||||
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, mu: new(sync.RWMutex)}
|
||||
return &BH1750FVI{Bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, Poll: pollDelay}
|
||||
case High2:
|
||||
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResMode2OpCode, mu: new(sync.RWMutex)}
|
||||
return &BH1750FVI{Bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResMode2OpCode, Poll: pollDelay}
|
||||
default:
|
||||
return &bh1750fvi{bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, mu: new(sync.RWMutex)}
|
||||
return &BH1750FVI{Bus: bus, i2cAddr: sensorI2cAddr, operationCode: highResOpCode, Poll: pollDelay}
|
||||
}
|
||||
}
|
||||
|
||||
// NewHighMode returns a BH1750FVI inteface on high resolution mode (1lx resolution)
|
||||
func NewHighMode(bus i2c.Bus) BH1750FVI {
|
||||
func NewHighMode(bus embd.I2CBus) *BH1750FVI {
|
||||
return New(High, bus)
|
||||
}
|
||||
|
||||
// NewHighMode returns a BH1750FVI inteface on high resolution mode2 (0.5lx resolution)
|
||||
func NewHigh2Mode(bus i2c.Bus) BH1750FVI {
|
||||
func NewHigh2Mode(bus embd.I2CBus) *BH1750FVI {
|
||||
return New(High2, bus)
|
||||
}
|
||||
|
||||
func (d *bh1750fvi) measureLighting() (lighting float64, err error) {
|
||||
err = d.bus.WriteByte(d.i2cAddr, d.operationCode)
|
||||
func (d *BH1750FVI) measureLighting() (lighting float64, err error) {
|
||||
err = d.Bus.WriteByte(d.i2cAddr, d.operationCode)
|
||||
if err != nil {
|
||||
log.Print("bh1750fvi: Failed to initialize sensor")
|
||||
return
|
||||
|
@ -88,7 +68,7 @@ func (d *bh1750fvi) measureLighting() (lighting float64, err error) {
|
|||
time.Sleep(180 * time.Millisecond)
|
||||
|
||||
var reading uint16
|
||||
if reading, err = d.bus.ReadWordFromReg(d.i2cAddr, defReadReg); err != nil {
|
||||
if reading, err = d.Bus.ReadWordFromReg(d.i2cAddr, defReadReg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -97,7 +77,7 @@ func (d *bh1750fvi) measureLighting() (lighting float64, err error) {
|
|||
}
|
||||
|
||||
// Lighting returns the ambient lighting in lx.
|
||||
func (d *bh1750fvi) Lighting() (lighting float64, err error) {
|
||||
func (d *BH1750FVI) Lighting() (lighting float64, err error) {
|
||||
select {
|
||||
case lighting = <-d.lightingReadings:
|
||||
return
|
||||
|
@ -107,11 +87,11 @@ func (d *bh1750fvi) Lighting() (lighting float64, err error) {
|
|||
}
|
||||
|
||||
// Run starts continuous sensor data acquisition loop.
|
||||
func (d *bh1750fvi) Run() (err error) {
|
||||
func (d *BH1750FVI) Run() (err error) {
|
||||
go func() {
|
||||
d.quit = make(chan bool)
|
||||
|
||||
timer := time.Tick(time.Duration(d.poll) * time.Millisecond)
|
||||
timer := time.Tick(time.Duration(d.Poll) * time.Millisecond)
|
||||
|
||||
var lighting float64
|
||||
|
||||
|
@ -135,14 +115,9 @@ func (d *bh1750fvi) Run() (err error) {
|
|||
}
|
||||
|
||||
// Close.
|
||||
func (d *bh1750fvi) Close() {
|
||||
func (d *BH1750FVI) Close() {
|
||||
if d.quit != nil {
|
||||
d.quit <- true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between run of data acquisition loop.
|
||||
func (d *bh1750fvi) SetPollDelay(delay int) {
|
||||
d.poll = delay
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -38,26 +38,12 @@ const (
|
|||
pollDelay = 250
|
||||
)
|
||||
|
||||
// A BMP085 implements access to the Bosch BMP085 sensor.
|
||||
type BMP085 interface {
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
SetPollDelay(delay int)
|
||||
type BMP085 struct {
|
||||
Bus embd.I2CBus
|
||||
Poll int
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
Temperature() (temp float64, err error)
|
||||
// Pressure returns the current pressure reading.
|
||||
Pressure() (pressure int, err error)
|
||||
// Altitude returns the current altitude reading.
|
||||
Altitude() (altitude float64, err error)
|
||||
Debug bool
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
Run() error
|
||||
// Close.
|
||||
Close()
|
||||
}
|
||||
|
||||
type bmp085 struct {
|
||||
bus i2c.Bus
|
||||
oss uint
|
||||
|
||||
ac1, ac2, ac3 int16
|
||||
|
@ -65,29 +51,19 @@ type bmp085 struct {
|
|||
b1, b2, mb, mc, md int16
|
||||
b5 int32
|
||||
calibrated bool
|
||||
cmu *sync.RWMutex
|
||||
cmu sync.RWMutex
|
||||
|
||||
poll int
|
||||
temps chan uint16
|
||||
pressures chan int32
|
||||
altitudes chan float64
|
||||
quit chan struct{}
|
||||
|
||||
debug bool
|
||||
}
|
||||
|
||||
// New creates a new BMP085 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(bus i2c.Bus) BMP085 {
|
||||
return &bmp085{bus: bus, cmu: new(sync.RWMutex), poll: pollDelay}
|
||||
func New(bus embd.I2CBus) *BMP085 {
|
||||
return &BMP085{Bus: bus, Poll: pollDelay}
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func (d *bmp085) SetPollDelay(delay int) {
|
||||
d.poll = delay
|
||||
}
|
||||
|
||||
func (d *bmp085) calibrate() (err error) {
|
||||
func (d *BMP085) calibrate() (err error) {
|
||||
d.cmu.RLock()
|
||||
if d.calibrated {
|
||||
d.cmu.RUnlock()
|
||||
|
@ -100,7 +76,7 @@ func (d *bmp085) calibrate() (err error) {
|
|||
|
||||
readInt16 := func(reg byte) (value int16, err error) {
|
||||
var v uint16
|
||||
if v, err = d.bus.ReadWordFromReg(address, reg); err != nil {
|
||||
if v, err = d.Bus.ReadWordFromReg(address, reg); err != nil {
|
||||
return
|
||||
}
|
||||
value = int16(v)
|
||||
|
@ -109,7 +85,7 @@ func (d *bmp085) calibrate() (err error) {
|
|||
|
||||
readUInt16 := func(reg byte) (value uint16, err error) {
|
||||
var v uint16
|
||||
if v, err = d.bus.ReadWordFromReg(address, reg); err != nil {
|
||||
if v, err = d.Bus.ReadWordFromReg(address, reg); err != nil {
|
||||
return
|
||||
}
|
||||
value = uint16(v)
|
||||
|
@ -163,7 +139,7 @@ func (d *bmp085) calibrate() (err error) {
|
|||
|
||||
d.calibrated = true
|
||||
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Print("bmp085: calibration data retrieved")
|
||||
log.Printf("bmp085: param AC1 = %v", d.ac1)
|
||||
log.Printf("bmp085: param AC2 = %v", d.ac2)
|
||||
|
@ -181,18 +157,18 @@ func (d *bmp085) calibrate() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (d *bmp085) readUncompensatedTemp() (temp uint16, err error) {
|
||||
if err = d.bus.WriteByteToReg(address, control, readTempCmd); err != nil {
|
||||
func (d *BMP085) readUncompensatedTemp() (temp uint16, err error) {
|
||||
if err = d.Bus.WriteByteToReg(address, control, readTempCmd); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(tempReadDelay)
|
||||
if temp, err = d.bus.ReadWordFromReg(address, tempData); err != nil {
|
||||
if temp, err = d.Bus.ReadWordFromReg(address, tempData); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *bmp085) calcTemp(utemp uint16) uint16 {
|
||||
func (d *BMP085) calcTemp(utemp uint16) uint16 {
|
||||
x1 := ((int(utemp) - int(d.ac6)) * int(d.ac5)) >> 15
|
||||
x2 := (int(d.mc) << 11) / (x1 + int(d.md))
|
||||
|
||||
|
@ -203,7 +179,7 @@ func (d *bmp085) calcTemp(utemp uint16) uint16 {
|
|||
return uint16((d.b5 + 8) >> 4)
|
||||
}
|
||||
|
||||
func (d *bmp085) measureTemp() (temp uint16, err error) {
|
||||
func (d *BMP085) measureTemp() (temp uint16, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -212,25 +188,25 @@ func (d *bmp085) measureTemp() (temp uint16, err error) {
|
|||
if utemp, err = d.readUncompensatedTemp(); err != nil {
|
||||
return
|
||||
}
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: uncompensated temp: %v", utemp)
|
||||
}
|
||||
temp = d.calcTemp(utemp)
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: compensated temp %v", temp)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
func (d *bmp085) Temperature() (temp float64, err error) {
|
||||
func (d *BMP085) Temperature() (temp float64, err error) {
|
||||
|
||||
select {
|
||||
case t := <-d.temps:
|
||||
temp = float64(t) / 10
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no temps available... measuring")
|
||||
}
|
||||
var t uint16
|
||||
|
@ -243,14 +219,14 @@ func (d *bmp085) Temperature() (temp float64, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *bmp085) readUncompensatedPressure() (pressure uint32, err error) {
|
||||
if err = d.bus.WriteByteToReg(address, control, byte(readPressureCmd+(d.oss<<6))); err != nil {
|
||||
func (d *BMP085) readUncompensatedPressure() (pressure uint32, err error) {
|
||||
if err = d.Bus.WriteByteToReg(address, control, byte(readPressureCmd+(d.oss<<6))); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Duration(2+(3<<d.oss)) * time.Millisecond)
|
||||
|
||||
data := make([]byte, 3)
|
||||
if err = d.bus.ReadFromReg(address, pressureData, data); err != nil {
|
||||
if err = d.Bus.ReadFromReg(address, pressureData, data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -259,11 +235,11 @@ func (d *bmp085) readUncompensatedPressure() (pressure uint32, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (d *bmp085) calcPressure(upressure uint32) (p int32) {
|
||||
func (d *BMP085) calcPressure(upressure uint32) (p int32) {
|
||||
var x1, x2, x3 int32
|
||||
|
||||
l := func(s string, v interface{}) {
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: %v = %v", s, v)
|
||||
}
|
||||
}
|
||||
|
@ -315,11 +291,11 @@ func (d *bmp085) calcPressure(upressure uint32) (p int32) {
|
|||
return
|
||||
}
|
||||
|
||||
func (d *bmp085) calcAltitude(pressure int32) float64 {
|
||||
func (d *BMP085) calcAltitude(pressure int32) float64 {
|
||||
return 44330 * (1 - math.Pow(float64(pressure)/p0, 0.190295))
|
||||
}
|
||||
|
||||
func (d *bmp085) measurePressureAndAltitude() (pressure int32, altitude float64, err error) {
|
||||
func (d *BMP085) measurePressureAndAltitude() (pressure int32, altitude float64, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -328,22 +304,22 @@ func (d *bmp085) measurePressureAndAltitude() (pressure int32, altitude float64,
|
|||
if upressure, err = d.readUncompensatedPressure(); err != nil {
|
||||
return
|
||||
}
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: uncompensated pressure: %v", upressure)
|
||||
}
|
||||
pressure = d.calcPressure(upressure)
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: compensated pressure %v", pressure)
|
||||
}
|
||||
altitude = d.calcAltitude(pressure)
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: calculated altitude %v", altitude)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Pressure returns the current pressure reading.
|
||||
func (d *bmp085) Pressure() (pressure int, err error) {
|
||||
func (d *BMP085) Pressure() (pressure int, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -353,7 +329,7 @@ func (d *bmp085) Pressure() (pressure int, err error) {
|
|||
pressure = int(p)
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no pressures available... measuring")
|
||||
}
|
||||
var p int32
|
||||
|
@ -367,7 +343,7 @@ func (d *bmp085) Pressure() (pressure int, err error) {
|
|||
}
|
||||
|
||||
// Altitude returns the current altitude reading.
|
||||
func (d *bmp085) Altitude() (altitude float64, err error) {
|
||||
func (d *BMP085) Altitude() (altitude float64, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -376,7 +352,7 @@ func (d *bmp085) Altitude() (altitude float64, err error) {
|
|||
case altitude = <-d.altitudes:
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no altitudes available... measuring")
|
||||
}
|
||||
_, altitude, err = d.measurePressureAndAltitude()
|
||||
|
@ -388,10 +364,10 @@ func (d *bmp085) Altitude() (altitude float64, err error) {
|
|||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func (d *bmp085) Run() (err error) {
|
||||
func (d *BMP085) Run() (err error) {
|
||||
go func() {
|
||||
d.quit = make(chan struct{})
|
||||
timer := time.Tick(time.Duration(d.poll) * time.Millisecond)
|
||||
timer := time.Tick(time.Duration(d.Poll) * time.Millisecond)
|
||||
|
||||
var temp uint16
|
||||
var pressure int32
|
||||
|
@ -432,7 +408,7 @@ func (d *bmp085) Run() (err error) {
|
|||
}
|
||||
|
||||
// Close.
|
||||
func (d *bmp085) Close() {
|
||||
func (d *BMP085) Close() {
|
||||
if d.quit != nil {
|
||||
d.quit <- struct{}{}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -38,26 +38,12 @@ const (
|
|||
pollDelay = 250
|
||||
)
|
||||
|
||||
// A BMP180 implements access to the Bosch BMP180 sensor.
|
||||
type BMP180 interface {
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
SetPollDelay(delay int)
|
||||
type BMP180 struct {
|
||||
Bus embd.I2CBus
|
||||
Poll int
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
Temperature() (temp float64, err error)
|
||||
// Pressure returns the current pressure reading.
|
||||
Pressure() (pressure int, err error)
|
||||
// Altitude returns the current altitude reading.
|
||||
Altitude() (altitude float64, err error)
|
||||
Debug bool
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
Run() error
|
||||
// Close.
|
||||
Close()
|
||||
}
|
||||
|
||||
type bmp180 struct {
|
||||
bus i2c.Bus
|
||||
oss uint
|
||||
|
||||
ac1, ac2, ac3 int16
|
||||
|
@ -65,29 +51,19 @@ type bmp180 struct {
|
|||
b1, b2, mb, mc, md int16
|
||||
b5 int32
|
||||
calibrated bool
|
||||
cmu *sync.RWMutex
|
||||
cmu sync.RWMutex
|
||||
|
||||
poll int
|
||||
temps chan uint16
|
||||
pressures chan int32
|
||||
altitudes chan float64
|
||||
quit chan struct{}
|
||||
|
||||
debug bool
|
||||
}
|
||||
|
||||
// New creates a new BMP180 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(bus i2c.Bus) BMP180 {
|
||||
return &bmp180{bus: bus, cmu: new(sync.RWMutex), poll: pollDelay}
|
||||
func New(bus embd.I2CBus) *BMP180 {
|
||||
return &BMP180{Bus: bus, Poll: pollDelay}
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func (d *bmp180) SetPollDelay(delay int) {
|
||||
d.poll = delay
|
||||
}
|
||||
|
||||
func (d *bmp180) calibrate() (err error) {
|
||||
func (d *BMP180) calibrate() (err error) {
|
||||
d.cmu.RLock()
|
||||
if d.calibrated {
|
||||
d.cmu.RUnlock()
|
||||
|
@ -100,7 +76,7 @@ func (d *bmp180) calibrate() (err error) {
|
|||
|
||||
readInt16 := func(reg byte) (value int16, err error) {
|
||||
var v uint16
|
||||
if v, err = d.bus.ReadWordFromReg(address, reg); err != nil {
|
||||
if v, err = d.Bus.ReadWordFromReg(address, reg); err != nil {
|
||||
return
|
||||
}
|
||||
value = int16(v)
|
||||
|
@ -109,7 +85,7 @@ func (d *bmp180) calibrate() (err error) {
|
|||
|
||||
readUInt16 := func(reg byte) (value uint16, err error) {
|
||||
var v uint16
|
||||
if v, err = d.bus.ReadWordFromReg(address, reg); err != nil {
|
||||
if v, err = d.Bus.ReadWordFromReg(address, reg); err != nil {
|
||||
return
|
||||
}
|
||||
value = uint16(v)
|
||||
|
@ -163,36 +139,36 @@ func (d *bmp180) calibrate() (err error) {
|
|||
|
||||
d.calibrated = true
|
||||
|
||||
if d.debug {
|
||||
log.Print("bmp180: calibration data retrieved")
|
||||
log.Printf("bmp180: param AC1 = %v", d.ac1)
|
||||
log.Printf("bmp180: param AC2 = %v", d.ac2)
|
||||
log.Printf("bmp180: param AC3 = %v", d.ac3)
|
||||
log.Printf("bmp180: param AC4 = %v", d.ac4)
|
||||
log.Printf("bmp180: param AC5 = %v", d.ac5)
|
||||
log.Printf("bmp180: param AC6 = %v", d.ac6)
|
||||
log.Printf("bmp180: param B1 = %v", d.b1)
|
||||
log.Printf("bmp180: param B2 = %v", d.b2)
|
||||
log.Printf("bmp180: param MB = %v", d.mb)
|
||||
log.Printf("bmp180: param MC = %v", d.mc)
|
||||
log.Printf("bmp180: param MD = %v", d.md)
|
||||
if d.Debug {
|
||||
log.Print("bmp085: calibration data retrieved")
|
||||
log.Printf("bmp085: param AC1 = %v", d.ac1)
|
||||
log.Printf("bmp085: param AC2 = %v", d.ac2)
|
||||
log.Printf("bmp085: param AC3 = %v", d.ac3)
|
||||
log.Printf("bmp085: param AC4 = %v", d.ac4)
|
||||
log.Printf("bmp085: param AC5 = %v", d.ac5)
|
||||
log.Printf("bmp085: param AC6 = %v", d.ac6)
|
||||
log.Printf("bmp085: param B1 = %v", d.b1)
|
||||
log.Printf("bmp085: param B2 = %v", d.b2)
|
||||
log.Printf("bmp085: param MB = %v", d.mb)
|
||||
log.Printf("bmp085: param MC = %v", d.mc)
|
||||
log.Printf("bmp085: param MD = %v", d.md)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *bmp180) readUncompensatedTemp() (temp uint16, err error) {
|
||||
if err = d.bus.WriteByteToReg(address, control, readTempCmd); err != nil {
|
||||
func (d *BMP180) readUncompensatedTemp() (temp uint16, err error) {
|
||||
if err = d.Bus.WriteByteToReg(address, control, readTempCmd); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(tempReadDelay)
|
||||
if temp, err = d.bus.ReadWordFromReg(address, tempData); err != nil {
|
||||
if temp, err = d.Bus.ReadWordFromReg(address, tempData); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *bmp180) calcTemp(utemp uint16) uint16 {
|
||||
func (d *BMP180) calcTemp(utemp uint16) uint16 {
|
||||
x1 := ((int(utemp) - int(d.ac6)) * int(d.ac5)) >> 15
|
||||
x2 := (int(d.mc) << 11) / (x1 + int(d.md))
|
||||
|
||||
|
@ -203,7 +179,7 @@ func (d *bmp180) calcTemp(utemp uint16) uint16 {
|
|||
return uint16((d.b5 + 8) >> 4)
|
||||
}
|
||||
|
||||
func (d *bmp180) measureTemp() (temp uint16, err error) {
|
||||
func (d *BMP180) measureTemp() (temp uint16, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -212,26 +188,26 @@ func (d *bmp180) measureTemp() (temp uint16, err error) {
|
|||
if utemp, err = d.readUncompensatedTemp(); err != nil {
|
||||
return
|
||||
}
|
||||
if d.debug {
|
||||
log.Printf("bcm180: uncompensated temp: %v", utemp)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: uncompensated temp: %v", utemp)
|
||||
}
|
||||
temp = d.calcTemp(utemp)
|
||||
if d.debug {
|
||||
log.Printf("bcm180: compensated temp %v", temp)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: compensated temp %v", temp)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
func (d *bmp180) Temperature() (temp float64, err error) {
|
||||
func (d *BMP180) Temperature() (temp float64, err error) {
|
||||
|
||||
select {
|
||||
case t := <-d.temps:
|
||||
temp = float64(t) / 10
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
log.Print("bcm180: no temps available... measuring")
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no temps available... measuring")
|
||||
}
|
||||
var t uint16
|
||||
t, err = d.measureTemp()
|
||||
|
@ -243,14 +219,14 @@ func (d *bmp180) Temperature() (temp float64, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *bmp180) readUncompensatedPressure() (pressure uint32, err error) {
|
||||
if err = d.bus.WriteByteToReg(address, control, byte(readPressureCmd+(d.oss<<6))); err != nil {
|
||||
func (d *BMP180) readUncompensatedPressure() (pressure uint32, err error) {
|
||||
if err = d.Bus.WriteByteToReg(address, control, byte(readPressureCmd+(d.oss<<6))); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Duration(2+(3<<d.oss)) * time.Millisecond)
|
||||
|
||||
data := make([]byte, 3)
|
||||
if err = d.bus.ReadFromReg(address, pressureData, data); err != nil {
|
||||
if err = d.Bus.ReadFromReg(address, pressureData, data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -259,12 +235,12 @@ func (d *bmp180) readUncompensatedPressure() (pressure uint32, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (d *bmp180) calcPressure(upressure uint32) (p int32) {
|
||||
func (d *BMP180) calcPressure(upressure uint32) (p int32) {
|
||||
var x1, x2, x3 int32
|
||||
|
||||
l := func(s string, v interface{}) {
|
||||
if d.debug {
|
||||
log.Printf("bcm180: %v = %v", s, v)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: %v = %v", s, v)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -315,11 +291,11 @@ func (d *bmp180) calcPressure(upressure uint32) (p int32) {
|
|||
return
|
||||
}
|
||||
|
||||
func (d *bmp180) calcAltitude(pressure int32) float64 {
|
||||
func (d *BMP180) calcAltitude(pressure int32) float64 {
|
||||
return 44330 * (1 - math.Pow(float64(pressure)/p0, 0.190295))
|
||||
}
|
||||
|
||||
func (d *bmp180) measurePressureAndAltitude() (pressure int32, altitude float64, err error) {
|
||||
func (d *BMP180) measurePressureAndAltitude() (pressure int32, altitude float64, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -328,22 +304,22 @@ func (d *bmp180) measurePressureAndAltitude() (pressure int32, altitude float64,
|
|||
if upressure, err = d.readUncompensatedPressure(); err != nil {
|
||||
return
|
||||
}
|
||||
if d.debug {
|
||||
log.Printf("bcm180: uncompensated pressure: %v", upressure)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: uncompensated pressure: %v", upressure)
|
||||
}
|
||||
pressure = d.calcPressure(upressure)
|
||||
if d.debug {
|
||||
log.Printf("bcm180: compensated pressure %v", pressure)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: compensated pressure %v", pressure)
|
||||
}
|
||||
altitude = d.calcAltitude(pressure)
|
||||
if d.debug {
|
||||
log.Printf("bcm180: calculated altitude %v", altitude)
|
||||
if d.Debug {
|
||||
log.Printf("bcm085: calculated altitude %v", altitude)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Pressure returns the current pressure reading.
|
||||
func (d *bmp180) Pressure() (pressure int, err error) {
|
||||
func (d *BMP180) Pressure() (pressure int, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -353,8 +329,8 @@ func (d *bmp180) Pressure() (pressure int, err error) {
|
|||
pressure = int(p)
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
log.Print("bcm180: no pressures available... measuring")
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no pressures available... measuring")
|
||||
}
|
||||
var p int32
|
||||
p, _, err = d.measurePressureAndAltitude()
|
||||
|
@ -367,7 +343,7 @@ func (d *bmp180) Pressure() (pressure int, err error) {
|
|||
}
|
||||
|
||||
// Altitude returns the current altitude reading.
|
||||
func (d *bmp180) Altitude() (altitude float64, err error) {
|
||||
func (d *BMP180) Altitude() (altitude float64, err error) {
|
||||
if err = d.calibrate(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -376,8 +352,8 @@ func (d *bmp180) Altitude() (altitude float64, err error) {
|
|||
case altitude = <-d.altitudes:
|
||||
return
|
||||
default:
|
||||
if d.debug {
|
||||
log.Print("bcm180: no altitudes available... measuring")
|
||||
if d.Debug {
|
||||
log.Print("bcm085: no altitudes available... measuring")
|
||||
}
|
||||
_, altitude, err = d.measurePressureAndAltitude()
|
||||
if err != nil {
|
||||
|
@ -388,10 +364,10 @@ func (d *bmp180) Altitude() (altitude float64, err error) {
|
|||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func (d *bmp180) Run() (err error) {
|
||||
func (d *BMP180) Run() (err error) {
|
||||
go func() {
|
||||
d.quit = make(chan struct{})
|
||||
timer := time.Tick(time.Duration(d.poll) * time.Millisecond)
|
||||
timer := time.Tick(time.Duration(d.Poll) * time.Millisecond)
|
||||
|
||||
var temp uint16
|
||||
var pressure int32
|
||||
|
@ -432,7 +408,7 @@ func (d *bmp180) Run() (err error) {
|
|||
}
|
||||
|
||||
// Close.
|
||||
func (d *bmp180) Close() {
|
||||
func (d *BMP180) Close() {
|
||||
if d.quit != nil {
|
||||
d.quit <- struct{}{}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -112,7 +112,7 @@ type Orientation struct {
|
|||
|
||||
// L3GD20 represents a L3GD20 3-axis gyroscope.
|
||||
type L3GD20 struct {
|
||||
Bus i2c.Bus
|
||||
Bus embd.I2CBus
|
||||
Range *Range
|
||||
|
||||
initialized bool
|
||||
|
@ -128,7 +128,7 @@ type L3GD20 struct {
|
|||
|
||||
// New creates a new L3GD20 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(bus i2c.Bus, Range *Range) *L3GD20 {
|
||||
func New(bus embd.I2CBus, Range *Range) *L3GD20 {
|
||||
return &L3GD20{
|
||||
Bus: bus,
|
||||
Range: Range,
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -42,101 +42,82 @@ const (
|
|||
pollDelay = 250
|
||||
)
|
||||
|
||||
// A LSM303 implements access to a LSM303 sensor.
|
||||
type LSM303 interface {
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
SetPollDelay(delay int)
|
||||
|
||||
// Heading returns the current heading [0, 360).
|
||||
Heading() (heading float64, err error)
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
Run() error
|
||||
// Close closes the sensor data acquisition loop and puts the LSM303 into sleep mode.
|
||||
Close() error
|
||||
}
|
||||
|
||||
type lsm303 struct {
|
||||
bus i2c.Bus
|
||||
type LSM303 struct {
|
||||
Bus embd.I2CBus
|
||||
Poll int
|
||||
|
||||
initialized bool
|
||||
mu *sync.RWMutex
|
||||
mu sync.RWMutex
|
||||
|
||||
headings chan float64
|
||||
|
||||
poll int
|
||||
quit chan struct{}
|
||||
|
||||
debug bool
|
||||
Debug bool
|
||||
}
|
||||
|
||||
// New creates a new LSM303 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(bus i2c.Bus) LSM303 {
|
||||
return &lsm303{bus: bus, mu: new(sync.RWMutex), poll: pollDelay}
|
||||
func New(bus embd.I2CBus) *LSM303 {
|
||||
return &LSM303{Bus: bus, Poll: pollDelay}
|
||||
}
|
||||
|
||||
// Initialize the device
|
||||
func (d *lsm303) setup() (err error) {
|
||||
func (d *LSM303) setup() error {
|
||||
d.mu.RLock()
|
||||
if d.initialized {
|
||||
d.mu.RUnlock()
|
||||
return
|
||||
return nil
|
||||
}
|
||||
d.mu.RUnlock()
|
||||
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
if err = d.bus.WriteByteToReg(magAddress, magConfigRegA, MagCRADefault); err != nil {
|
||||
return
|
||||
if err := d.Bus.WriteByteToReg(magAddress, magConfigRegA, MagCRADefault); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = d.bus.WriteByteToReg(magAddress, magModeReg, MagMRDefault); err != nil {
|
||||
return
|
||||
if err := d.Bus.WriteByteToReg(magAddress, magModeReg, MagMRDefault); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.initialized = true
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func (d *lsm303) SetPollDelay(delay int) {
|
||||
d.poll = delay
|
||||
}
|
||||
|
||||
func (d *lsm303) measureHeading() (heading float64, err error) {
|
||||
if err = d.setup(); err != nil {
|
||||
return
|
||||
func (d *LSM303) measureHeading() (float64, error) {
|
||||
if err := d.setup(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if _, err = d.bus.ReadByteFromReg(magAddress, magDataSignal); err != nil {
|
||||
return
|
||||
if _, err := d.Bus.ReadByteFromReg(magAddress, magDataSignal); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
data := make([]byte, 6)
|
||||
if err = d.bus.ReadFromReg(magAddress, magData, data); err != nil {
|
||||
return
|
||||
if err := d.Bus.ReadFromReg(magAddress, magData, data); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
x := int16(data[0])<<8 | int16(data[1])
|
||||
y := int16(data[2])<<8 | int16(data[3])
|
||||
|
||||
heading = math.Atan2(float64(y), float64(x)) / math.Pi * 180
|
||||
heading := math.Atan2(float64(y), float64(x)) / math.Pi * 180
|
||||
if heading < 0 {
|
||||
heading += 360
|
||||
}
|
||||
|
||||
return
|
||||
return heading, nil
|
||||
}
|
||||
|
||||
// Heading returns the current heading [0, 360).
|
||||
func (d *lsm303) Heading() (heading float64, err error) {
|
||||
func (d *LSM303) Heading() (float64, error) {
|
||||
select {
|
||||
case heading = <-d.headings:
|
||||
return
|
||||
case heading := <-d.headings:
|
||||
return heading, nil
|
||||
default:
|
||||
if d.debug {
|
||||
if d.Debug {
|
||||
log.Print("lsm303: no headings available... measuring")
|
||||
}
|
||||
return d.measureHeading()
|
||||
|
@ -144,11 +125,11 @@ func (d *lsm303) Heading() (heading float64, err error) {
|
|||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func (d *lsm303) Run() (err error) {
|
||||
func (d *LSM303) Run() (err error) {
|
||||
go func() {
|
||||
d.quit = make(chan struct{})
|
||||
|
||||
timer := time.Tick(time.Duration(d.poll) * time.Millisecond)
|
||||
timer := time.Tick(time.Duration(d.Poll) * time.Millisecond)
|
||||
|
||||
var heading float64
|
||||
|
||||
|
@ -175,10 +156,9 @@ func (d *lsm303) Run() (err error) {
|
|||
}
|
||||
|
||||
// Close the sensor data acquisition loop and put the LSM303 into sleep mode.
|
||||
func (d *lsm303) Close() (err error) {
|
||||
func (d *LSM303) Close() error {
|
||||
if d.quit != nil {
|
||||
d.quit <- struct{}{}
|
||||
}
|
||||
err = d.bus.WriteByteToReg(magAddress, magModeReg, MagSleep)
|
||||
return
|
||||
return d.Bus.WriteByteToReg(magAddress, magModeReg, MagSleep)
|
||||
}
|
||||
|
|
|
@ -8,8 +8,7 @@ import (
|
|||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/i2c"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -54,7 +53,7 @@ var (
|
|||
// TMP006 represents a TMP006 thermopile sensor.
|
||||
type TMP006 struct {
|
||||
// Bus to communicate over.
|
||||
Bus i2c.Bus
|
||||
Bus embd.I2CBus
|
||||
// Addr of the sensor.
|
||||
Addr byte
|
||||
// Debug turns on additional debug output.
|
||||
|
@ -71,7 +70,7 @@ type TMP006 struct {
|
|||
}
|
||||
|
||||
// New creates a new TMP006 sensor.
|
||||
func New(bus i2c.Bus, addr byte) *TMP006 {
|
||||
func New(bus embd.I2CBus, addr byte) *TMP006 {
|
||||
return &TMP006{
|
||||
Bus: bus,
|
||||
Addr: addr,
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd/gpio"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -29,7 +29,7 @@ var NullThermometer = &nullThermometer{}
|
|||
|
||||
// US020 represents a US020 ultrasonic range finder.
|
||||
type US020 struct {
|
||||
EchoPin, TriggerPin gpio.DigitalPin
|
||||
EchoPin, TriggerPin embd.DigitalPin
|
||||
|
||||
Thermometer Thermometer
|
||||
|
||||
|
@ -43,7 +43,7 @@ type US020 struct {
|
|||
|
||||
// New creates a new US020 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(echoPin, triggerPin gpio.DigitalPin, thermometer Thermometer) *US020 {
|
||||
func New(echoPin, triggerPin embd.DigitalPin, thermometer Thermometer) *US020 {
|
||||
return &US020{EchoPin: echoPin, TriggerPin: triggerPin, Thermometer: thermometer}
|
||||
}
|
||||
|
||||
|
@ -58,8 +58,8 @@ func (d *US020) setup() (err error) {
|
|||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
d.TriggerPin.SetDirection(gpio.Out)
|
||||
d.EchoPin.SetDirection(gpio.In)
|
||||
d.TriggerPin.SetDirection(embd.Out)
|
||||
d.EchoPin.SetDirection(embd.In)
|
||||
|
||||
if d.Thermometer == nil {
|
||||
d.Thermometer = NullThermometer
|
||||
|
@ -91,9 +91,9 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
}
|
||||
|
||||
// Generate a TRIGGER pulse
|
||||
d.TriggerPin.Write(gpio.High)
|
||||
d.TriggerPin.Write(embd.High)
|
||||
time.Sleep(pulseDelay)
|
||||
d.TriggerPin.Write(gpio.Low)
|
||||
d.TriggerPin.Write(embd.Low)
|
||||
|
||||
if d.Debug {
|
||||
log.Print("us020: waiting for echo to go high")
|
||||
|
@ -106,7 +106,7 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
return 0, err
|
||||
}
|
||||
|
||||
if v != gpio.Low {
|
||||
if v != embd.Low {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
return 0, err
|
||||
}
|
||||
|
||||
if v != gpio.High {
|
||||
if v != embd.High {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -139,5 +139,5 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
|
||||
// Close.
|
||||
func (d *US020) Close() {
|
||||
d.EchoPin.SetDirection(gpio.Out)
|
||||
d.EchoPin.SetDirection(embd.Out)
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/kidoman/embd/gpio"
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
type WaterSensor struct {
|
||||
Pin gpio.DigitalPin
|
||||
Pin embd.DigitalPin
|
||||
|
||||
initialized bool
|
||||
mu sync.RWMutex
|
||||
|
@ -18,7 +18,7 @@ type WaterSensor struct {
|
|||
}
|
||||
|
||||
// New creates a new WaterSensor struct
|
||||
func New(pin gpio.DigitalPin) *WaterSensor {
|
||||
func New(pin embd.DigitalPin) *WaterSensor {
|
||||
return &WaterSensor{Pin: pin}
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ func (d *WaterSensor) setup() error {
|
|||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
if err := d.Pin.SetDirection(gpio.In); err != nil {
|
||||
if err := d.Pin.SetDirection(embd.In); err != nil {
|
||||
return err
|
||||
}
|
||||
d.initialized = true
|
||||
|
@ -55,7 +55,7 @@ func (d *WaterSensor) IsWet() (bool, error) {
|
|||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if value == gpio.High {
|
||||
if value == embd.High {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue