Skip to content

Commit

Permalink
Making water flow meter to use detector interface, it looks like suit…
Browse files Browse the repository at this point in the history
…s well to this purpose since it produces pulses
  • Loading branch information
Guney Saramali committed Aug 2, 2024
1 parent 6bbecff commit 3dc177e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
4 changes: 0 additions & 4 deletions dev/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,3 @@ type Wireless interface {
Wakeup()
Close() error
}

type FlowSensor interface {
Flowing() bool
}
10 changes: 5 additions & 5 deletions dev/water_flow_meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package dev

import "github.com/stianeikeland/go-rpio/v4"

// WaterFlowSensor implements FlowSensor interface
type WaterFlowSensor struct {
// WaterFlowMeter implements Detector interface
type WaterFlowMeter struct {
pin rpio.Pin
}

// NewWaterFlowMeter ...
func NewWaterFlowSensor(pin uint8) *WaterFlowSensor {
w := &WaterFlowSensor{
func NewWaterFlowMeter(pin uint8) *WaterFlowMeter {
w := &WaterFlowMeter{
pin: rpio.Pin(pin),
}
w.pin.Input()
return w
}

func (w *WaterFlowSensor) Flowing() bool {
func (w *WaterFlowMeter) Detected() bool {
return w.pin.Read() == rpio.High
}
4 changes: 2 additions & 2 deletions example/water_flow_sensor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
const pulsesPerLiter float32 = 450

func main() {
w := dev.NewWaterFlowSensor(17)
w := dev.NewWaterFlowMeter(17)
var numberOfPulsesCounted int = 0

for {
if w.Flowing() {
if w.Detected() {
numberOfPulsesCounted++
fmt.Printf("MiliLiters Flowed: %f", (float32(1000*numberOfPulsesCounted) / pulsesPerLiter))
continue
Expand Down

0 comments on commit 3dc177e

Please sign in to comment.