mirror of
https://github.com/kidoman/embd
synced 2024-11-10 19:48:53 +01:00
Water sensor Packaged
This commit is contained in:
parent
534e903925
commit
e9d5fc03ef
BIN
sensor/us020/.us020.go.swp
Normal file
BIN
sensor/us020/.us020.go.swp
Normal file
Binary file not shown.
@ -1,3 +1,4 @@
|
|||||||
|
// Package watersensor allows interfacing with the water sensor
|
||||||
package watersensor
|
package watersensor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -9,15 +10,20 @@ import (
|
|||||||
type watersensor struct {
|
type watersensor struct {
|
||||||
waterPinNumber int
|
waterPinNumber int
|
||||||
waterPin rpio.Pin
|
waterPin rpio.Pin
|
||||||
|
|
||||||
initialized bool
|
initialized bool
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
|
|
||||||
debug bool
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaterSensor implements access to a water sensor
|
||||||
type WaterSensor interface {
|
type WaterSensor interface {
|
||||||
|
// IsWet determines if there is water present on the sensor
|
||||||
IsWet() (b bool,err error)
|
IsWet() (b bool,err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new WaterSensor interface
|
||||||
func New(pinNumber int) WaterSensor {
|
func New(pinNumber int) WaterSensor {
|
||||||
return &watersensor{waterPinNumber: pinNumber, mu: new(sync.RWMutex)}
|
return &watersensor{waterPinNumber: pinNumber, mu: new(sync.RWMutex)}
|
||||||
}
|
}
|
||||||
@ -36,15 +42,15 @@ func (d *watersensor) Setup() (err error) {
|
|||||||
if err = rpio.Open(); err != nil {
|
if err = rpio.Open(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
d.waterPin = rpio.Pin(d.waterPinNumber)
|
d.waterPin = rpio.Pin(d.waterPinNumber)
|
||||||
|
|
||||||
d.waterPin.Input()
|
d.waterPin.Input()
|
||||||
|
|
||||||
d.initialized = true
|
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) {
|
func (d *watersensor) IsWet() (b bool, err error) {
|
||||||
if err = d.Setup(); err != nil {
|
if err = d.Setup(); err != nil {
|
||||||
return
|
return
|
||||||
@ -54,6 +60,7 @@ func (d *watersensor) IsWet() (b bool, err error) {
|
|||||||
log.Print("Getting reading")
|
log.Print("Getting reading")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read the pin value of the sensor
|
||||||
if d.waterPin.Read() == rpio.High {
|
if d.waterPin.Read() == rpio.High {
|
||||||
b=true
|
b=true
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user