1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 04:40:04 +01:00

watersensor: upgrade to embd/gpio

This commit is contained in:
Karan Misra 2014-03-02 12:21:29 +05:30
parent 4e67e7eb11
commit 1d33c3e3bb
2 changed files with 63 additions and 58 deletions

View File

@ -1,25 +1,38 @@
package main
import (
"log"
"time"
"github.com/golang/glog"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/sensor/watersensor"
)
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 {
isWater, err := fluidSensor.IsWet()
wet, err := fluidSensor.IsWet()
if err != nil {
log.Panic(err)
panic(err)
}
if isWater {
log.Printf("Bot is dry")
if wet {
glog.Info("bot is dry")
} else {
log.Printf("Bot is Wet")
glog.Info("bot is Wet")
}
time.Sleep(500 * time.Millisecond)
}
}

View File

@ -2,70 +2,62 @@
package watersensor
import (
"log"
"sync"
"github.com/stianeikeland/go-rpio"
)
"sync"
type watersensor struct {
waterPinNumber int
waterPin rpio.Pin
"github.com/golang/glog"
"github.com/kidoman/embd/gpio"
)
initialized bool
mu *sync.RWMutex
type WaterSensor struct {
Pin gpio.DigitalPin
debug bool
initialized bool
mu sync.RWMutex
Debug bool
}
// WaterSensor implements access to a water sensor
type WaterSensor interface {
// IsWet determines if there is water present on the sensor
IsWet() (b bool,err error)
// New creates a new WaterSensor struct
func New(pin gpio.DigitalPin) *WaterSensor {
return &WaterSensor{Pin: pin}
}
// New creates a new WaterSensor interface
func New(pinNumber int) WaterSensor {
return &watersensor{waterPinNumber: pinNumber, mu: new(sync.RWMutex)}
}
func (d *WaterSensor) setup() error {
d.mu.RLock()
if d.initialized {
d.mu.RUnlock()
return nil
}
d.mu.RUnlock()
func (d *watersensor) Setup() (err error) {
d.mu.RLock()
if d.initialized {
d.mu.RUnlock()
return
}
d.mu.RUnlock()
d.mu.Lock()
defer d.mu.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
if err := d.Pin.SetDirection(gpio.In); err != nil {
return err
}
d.initialized = true
if err = rpio.Open(); err != nil {
return
}
d.waterPin = rpio.Pin(d.waterPinNumber)
d.waterPin.Input()
d.initialized = true
return nil
return nil
}
// IsWet determines if there is water present on the sensor
func (d *watersensor) IsWet() (b bool, err error) {
if err = d.Setup(); err != nil {
return
}
func (d *WaterSensor) IsWet() (bool, error) {
if err := d.setup(); err != nil {
return false, err
}
if d.debug {
log.Print("Getting reading")
}
if d.Debug {
glog.Infof("watersensor: reading")
}
// Read the pin value of the sensor
if d.waterPin.Read() == rpio.High {
b=true
} else {
b=false
}
return
value, err := d.Pin.Read()
if err != nil {
return false, err
}
if value == gpio.High {
return true, nil
} else {
return false, nil
}
}