-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (39 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"log"
"github.com/gnyblast/go-gpiocdev-eventhandlers/pkg/factory"
"github.com/gnyblast/go-gpiocdev-eventhandlers/pkg/sensors"
"github.com/warthog618/go-gpiocdev"
"github.com/warthog618/go-gpiocdev/device/rpi"
)
const inputMultiplier float64 = 0.002222222 // 0.002222222 is the multiplier value for a flow meter that has 450 pulses per liter = (1/450).
const chip string = "gpiochip0"
const pinName string = ""
func main() {
//Initialize the sensor handler
lfs := factory.InitializeEventHandlerFor(sensors.FLOW_SENSOR, chip, pinName, inputMultiplier)
// Get the pin for the device
pin, err := rpi.Pin(pinName)
if err != nil {
log.Fatalf("failed to get pin: %s", err.Error())
}
// Get the line by passing the handler from the sensor as WithEventHandler
line, err := gpiocdev.RequestLine(chip, pin, gpiocdev.WithPullUp, gpiocdev.WithBothEdges, gpiocdev.WithEventHandler(lfs.Measure))
if err != nil {
log.Fatalf("failed to request line: %s", err.Error())
}
// Close line and channel at the end
defer line.Close()
defer lfs.CloseChannels()
// Listen the measurement if you want to take action or print the values. Do whatever you want.
// Here shutdown when 100 liters are measured
for {
printMeasurement(<-lfs.Subscribe())
if lfs.GetMeasurement() > 100 {
break
}
}
}
func printMeasurement(measurement float64) {
log.Printf("%f Liters flowed", measurement)
}