1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00
embd/sensor/watersensor/watersensor.go
Karan Misra 9ab49745bc - use glog instead of Debug flag
- use glog instead of log
- make code conform to the Go code guidelines
2014-03-31 18:46:04 +05:30

61 lines
1.0 KiB
Go

// Package watersensor allows interfacing with the water sensor.
package watersensor
import (
"sync"
"github.com/golang/glog"
"github.com/kidoman/embd"
)
// WaterSensor represents a water sensor.
type WaterSensor struct {
Pin embd.DigitalPin
initialized bool
mu sync.RWMutex
}
// New creates a new WaterSensor struct
func New(pin embd.DigitalPin) *WaterSensor {
return &WaterSensor{Pin: pin}
}
func (d *WaterSensor) setup() error {
d.mu.RLock()
if d.initialized {
d.mu.RUnlock()
return nil
}
d.mu.RUnlock()
d.mu.Lock()
defer d.mu.Unlock()
if err := d.Pin.SetDirection(embd.In); err != nil {
return err
}
d.initialized = true
return nil
}
// IsWet determines if there is water present on the sensor
func (d *WaterSensor) IsWet() (bool, error) {
if err := d.setup(); err != nil {
return false, err
}
glog.V(1).Infof("watersensor: reading")
value, err := d.Pin.Read()
if err != nil {
return false, err
}
if value == embd.High {
return true, nil
} else {
return false, nil
}
}