forked from google/periph
/
polledge.go
117 lines (106 loc) · 2.42 KB
/
polledge.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package gpioutil
import (
"time"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/physic"
)
// pollEdge is a gpio.PinIO where edge detection is done manually.
type pollEdge struct {
// Immutable.
gpio.PinIO
// period is the delay between each poll.
period time.Duration
die chan struct{}
// Mutable.
// edge is the current edge detection.
edge gpio.Edge
}
// PollEdge returns a gpio.PinIO which implements edge detection via polling.
//
// Example of GPIOs without edge detection are GPIOs accessible over an I²C
// chip or over USB.
//
// freq must be above 0. A reasonable value is 20Hz reading. High rate
// essentially means a busy loop.
func PollEdge(p gpio.PinIO, freq physic.Frequency) gpio.PinIO {
return &pollEdge{PinIO: p, period: freq.Duration(), die: make(chan struct{}, 1)}
}
// In implements gpio.PinIO.
func (p *pollEdge) In(pull gpio.Pull, edge gpio.Edge) error {
p.edge = gpio.NoEdge
err := p.PinIO.In(pull, gpio.NoEdge)
if err == nil {
p.edge = edge
}
return err
}
// WaitForEdge implements gpio.PinIO.
func (p *pollEdge) WaitForEdge(timeout time.Duration) bool {
select {
case <-p.die:
default:
}
defer func() {
select {
case <-p.die:
default:
}
}()
curr := p.PinIO.Read()
// -1 means to wait indefinitely.
if timeout >= 0 {
defer time.AfterFunc(timeout, func() {
p.die <- struct{}{}
}).Stop()
}
// Sadly it's not possible to stop then restart a ticker, so we can't cache
// it in the object.
t := time.NewTicker(p.period)
defer t.Stop()
for {
select {
case <-t.C:
n := p.PinIO.Read()
if n != curr {
switch p.edge {
case gpio.RisingEdge:
if n == gpio.High {
return true
}
curr = n
case gpio.FallingEdge:
if n == gpio.Low {
return true
}
curr = n
case gpio.BothEdges:
return true
}
}
case <-p.die:
return false
}
}
}
// Halt implements gpio.PinIO.
//
// It unblocks any WaitForEdge loop.
func (p *pollEdge) Halt() error {
select {
// If a WaitForEdge was pending, it will be unblocked.
case p.die <- struct{}{}:
default:
}
return nil
}
// Real implements gpio.RealPin.
func (p *pollEdge) Real() gpio.PinIO {
if r, ok := p.PinIO.(gpio.RealPin); ok {
return r.Real()
}
return p.PinIO
}
var _ gpio.PinIO = &pollEdge{}