mirror of
https://github.com/kidoman/embd
synced 2025-07-03 11:57:38 +02:00
bring in the idea of a hardware abstraction layer
This commit is contained in:
parent
b4de382833
commit
b5e2d0acc7
35 changed files with 994 additions and 971 deletions
|
@ -53,9 +53,6 @@ type bh1750fvi struct {
|
|||
poll int
|
||||
}
|
||||
|
||||
// Default instance for BH1750FVI sensor
|
||||
var Default = New(High, i2c.Default)
|
||||
|
||||
// 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).
|
||||
|
@ -149,23 +146,3 @@ func (d *bh1750fvi) Close() {
|
|||
func (d *bh1750fvi) SetPollDelay(delay int) {
|
||||
d.poll = delay
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between run of data acquisition loop.
|
||||
func SetPollDelay(delay int) {
|
||||
Default.SetPollDelay(delay)
|
||||
}
|
||||
|
||||
// Lighting returns the ambient lighting in lx.
|
||||
func Lighting() (lighting float64, err error) {
|
||||
return Default.Lighting()
|
||||
}
|
||||
|
||||
// Run starts continuous sensor data acquisition loop.
|
||||
func Run() (err error) {
|
||||
return Default.Run()
|
||||
}
|
||||
|
||||
// Close.
|
||||
func Close() {
|
||||
Default.Close()
|
||||
}
|
||||
|
|
|
@ -76,9 +76,6 @@ type bmp085 struct {
|
|||
debug bool
|
||||
}
|
||||
|
||||
// Default instance of the BMP085 sensor.
|
||||
var Default = New(i2c.Default)
|
||||
|
||||
// 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 {
|
||||
|
@ -440,33 +437,3 @@ func (d *bmp085) Close() {
|
|||
d.quit <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func SetPollDelay(delay int) {
|
||||
Default.SetPollDelay(delay)
|
||||
}
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
func Temperature() (temp float64, err error) {
|
||||
return Default.Temperature()
|
||||
}
|
||||
|
||||
// Pressure returns the current pressure reading.
|
||||
func Pressure() (pressure int, err error) {
|
||||
return Default.Pressure()
|
||||
}
|
||||
|
||||
// Altitude returns the current altitude reading.
|
||||
func Altitude() (altitude float64, err error) {
|
||||
return Default.Altitude()
|
||||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func Run() (err error) {
|
||||
return Default.Run()
|
||||
}
|
||||
|
||||
// Close.
|
||||
func Close() {
|
||||
Default.Close()
|
||||
}
|
||||
|
|
|
@ -76,9 +76,6 @@ type bmp180 struct {
|
|||
debug bool
|
||||
}
|
||||
|
||||
// Default instance of the BMP180 sensor.
|
||||
var Default = New(i2c.Default)
|
||||
|
||||
// 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 {
|
||||
|
@ -440,33 +437,3 @@ func (d *bmp180) Close() {
|
|||
d.quit <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func SetPollDelay(delay int) {
|
||||
Default.SetPollDelay(delay)
|
||||
}
|
||||
|
||||
// Temperature returns the current temperature reading.
|
||||
func Temperature() (temp float64, err error) {
|
||||
return Default.Temperature()
|
||||
}
|
||||
|
||||
// Pressure returns the current pressure reading.
|
||||
func Pressure() (pressure int, err error) {
|
||||
return Default.Pressure()
|
||||
}
|
||||
|
||||
// Altitude returns the current altitude reading.
|
||||
func Altitude() (altitude float64, err error) {
|
||||
return Default.Altitude()
|
||||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func Run() (err error) {
|
||||
return Default.Run()
|
||||
}
|
||||
|
||||
// Close.
|
||||
func Close() {
|
||||
Default.Close()
|
||||
}
|
||||
|
|
|
@ -70,9 +70,6 @@ type lsm303 struct {
|
|||
debug bool
|
||||
}
|
||||
|
||||
// Default instance of the LSM303 sensor.
|
||||
var Default = New(i2c.Default)
|
||||
|
||||
// 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 {
|
||||
|
@ -185,23 +182,3 @@ func (d *lsm303) Close() (err error) {
|
|||
err = d.bus.WriteByteToReg(magAddress, magModeReg, MagSleep)
|
||||
return
|
||||
}
|
||||
|
||||
// SetPollDelay sets the delay between runs of the data acquisition loop.
|
||||
func SetPollDelay(delay int) {
|
||||
Default.SetPollDelay(delay)
|
||||
}
|
||||
|
||||
// Heading returns the current heading [0, 360).
|
||||
func Heading() (heading float64, err error) {
|
||||
return Default.Heading()
|
||||
}
|
||||
|
||||
// Run starts the sensor data acquisition loop.
|
||||
func Run() (err error) {
|
||||
return Default.Run()
|
||||
}
|
||||
|
||||
// Close closes the sensor data acquisition loop and put the LSM303 into sleep mode.
|
||||
func Close() (err error) {
|
||||
return Default.Close()
|
||||
}
|
||||
|
|
|
@ -6,7 +6,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/stianeikeland/go-rpio"
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/gpio"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -29,13 +30,10 @@ var NullThermometer = &nullThermometer{}
|
|||
|
||||
// US020 represents a US020 ultrasonic range finder.
|
||||
type US020 struct {
|
||||
EchoPinNumber, TriggerPinNumber int
|
||||
EchoPin, TriggerPin gpio.DigitalPin
|
||||
|
||||
Thermometer Thermometer
|
||||
|
||||
echoPin rpio.Pin
|
||||
triggerPin rpio.Pin
|
||||
|
||||
speedSound float64
|
||||
|
||||
initialized bool
|
||||
|
@ -46,8 +44,8 @@ type US020 struct {
|
|||
|
||||
// New creates a new US020 interface. The bus variable controls
|
||||
// the I2C bus used to communicate with the device.
|
||||
func New(e, t int, thermometer Thermometer) *US020 {
|
||||
return &US020{EchoPinNumber: e, TriggerPinNumber: t, Thermometer: thermometer}
|
||||
func New(echoPin, triggerPin gpio.DigitalPin, thermometer Thermometer) *US020 {
|
||||
return &US020{EchoPin: echoPin, TriggerPin: triggerPin, Thermometer: thermometer}
|
||||
}
|
||||
|
||||
func (d *US020) setup() (err error) {
|
||||
|
@ -61,11 +59,8 @@ func (d *US020) setup() (err error) {
|
|||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
d.echoPin = rpio.Pin(d.EchoPinNumber) // ECHO port on the US020
|
||||
d.triggerPin = rpio.Pin(d.TriggerPinNumber) // TRIGGER port on the US020
|
||||
|
||||
d.echoPin.Input()
|
||||
d.triggerPin.Output()
|
||||
d.TriggerPin.SetDir(embd.Out)
|
||||
d.EchoPin.SetDir(embd.In)
|
||||
|
||||
if d.Thermometer == nil {
|
||||
d.Thermometer = NullThermometer
|
||||
|
@ -97,16 +92,24 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
}
|
||||
|
||||
// Generate a TRIGGER pulse
|
||||
d.triggerPin.High()
|
||||
d.TriggerPin.Write(gpio.High)
|
||||
time.Sleep(pulseDelay)
|
||||
d.triggerPin.Low()
|
||||
d.TriggerPin.Write(gpio.Low)
|
||||
|
||||
if d.Debug {
|
||||
log.Print("us020: waiting for echo to go high")
|
||||
}
|
||||
|
||||
// Wait until ECHO goes high
|
||||
for d.echoPin.Read() == rpio.Low {
|
||||
for {
|
||||
v, err := d.EchoPin.Read()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if v != embd.Low {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
startTime := time.Now() // Record time when ECHO goes high
|
||||
|
@ -116,7 +119,15 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
}
|
||||
|
||||
// Wait until ECHO goes low
|
||||
for d.echoPin.Read() == rpio.High {
|
||||
for {
|
||||
v, err := d.EchoPin.Read()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if v != embd.High {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
duration := time.Since(startTime) // Calculate time lapsed for ECHO to transition from high to low
|
||||
|
@ -129,5 +140,5 @@ func (d *US020) Distance() (distance float64, err error) {
|
|||
|
||||
// Close.
|
||||
func (d *US020) Close() {
|
||||
d.echoPin.Output()
|
||||
d.EchoPin.SetDir(embd.Out)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue