mirror of
https://github.com/kidoman/embd
synced 2025-01-03 10:31:36 +01:00
watersensor: upgrade to embd/gpio
This commit is contained in:
parent
4e67e7eb11
commit
1d33c3e3bb
@ -1,25 +1,38 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"github.com/kidoman/embd/gpio"
|
||||||
"github.com/kidoman/embd/sensor/watersensor"
|
"github.com/kidoman/embd/sensor/watersensor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fluidSensor := watersensor.New(7)
|
if err := gpio.Open(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer gpio.Close()
|
||||||
|
|
||||||
|
pin, err := gpio.NewDigitalPin(7)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer pin.Close()
|
||||||
|
|
||||||
|
fluidSensor := watersensor.New(pin)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
isWater, err := fluidSensor.IsWet()
|
wet, err := fluidSensor.IsWet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if isWater {
|
if wet {
|
||||||
log.Printf("Bot is dry")
|
glog.Info("bot is dry")
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Bot is Wet")
|
glog.Info("bot is Wet")
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,70 +2,62 @@
|
|||||||
package watersensor
|
package watersensor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"sync"
|
"sync"
|
||||||
"github.com/stianeikeland/go-rpio"
|
|
||||||
)
|
|
||||||
|
|
||||||
type watersensor struct {
|
"github.com/golang/glog"
|
||||||
waterPinNumber int
|
"github.com/kidoman/embd/gpio"
|
||||||
waterPin rpio.Pin
|
)
|
||||||
|
|
||||||
|
type WaterSensor struct {
|
||||||
|
Pin gpio.DigitalPin
|
||||||
|
|
||||||
initialized bool
|
initialized bool
|
||||||
mu *sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
|
||||||
debug bool
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaterSensor implements access to a water sensor
|
// New creates a new WaterSensor struct
|
||||||
type WaterSensor interface {
|
func New(pin gpio.DigitalPin) *WaterSensor {
|
||||||
// IsWet determines if there is water present on the sensor
|
return &WaterSensor{Pin: pin}
|
||||||
IsWet() (b bool,err error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new WaterSensor interface
|
func (d *WaterSensor) setup() error {
|
||||||
func New(pinNumber int) WaterSensor {
|
|
||||||
return &watersensor{waterPinNumber: pinNumber, mu: new(sync.RWMutex)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *watersensor) Setup() (err error) {
|
|
||||||
d.mu.RLock()
|
d.mu.RLock()
|
||||||
if d.initialized {
|
if d.initialized {
|
||||||
d.mu.RUnlock()
|
d.mu.RUnlock()
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
d.mu.RUnlock()
|
d.mu.RUnlock()
|
||||||
|
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
defer d.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|
||||||
if err = rpio.Open(); err != nil {
|
if err := d.Pin.SetDirection(gpio.In); err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.waterPin = rpio.Pin(d.waterPinNumber)
|
|
||||||
d.waterPin.Input()
|
|
||||||
d.initialized = true
|
d.initialized = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsWet determines if there is water present on the sensor
|
// IsWet determines if there is water present on the sensor
|
||||||
func (d *watersensor) IsWet() (b bool, err error) {
|
func (d *WaterSensor) IsWet() (bool, error) {
|
||||||
if err = d.Setup(); err != nil {
|
if err := d.setup(); err != nil {
|
||||||
return
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.debug {
|
if d.Debug {
|
||||||
log.Print("Getting reading")
|
glog.Infof("watersensor: reading")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the pin value of the sensor
|
value, err := d.Pin.Read()
|
||||||
if d.waterPin.Read() == rpio.High {
|
if err != nil {
|
||||||
b=true
|
return false, err
|
||||||
|
}
|
||||||
|
if value == gpio.High {
|
||||||
|
return true, nil
|
||||||
} else {
|
} else {
|
||||||
b=false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user