-
Notifications
You must be signed in to change notification settings - Fork 0
/
falling_edge.go
58 lines (50 loc) · 1.19 KB
/
falling_edge.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
// Copyright (c) 2021 acud
package flipflop
import (
"time"
)
type detector struct {
t time.Duration
worstCase time.Duration
buf chan struct{}
out chan struct{}
quit chan struct{}
}
// NewFallingEdge returns a new falling edge detector.
// bufferTime is the time to buffer, worstCase is buffertime*worstcase time to wait before writing
// to the output anyway.
func NewFallingEdge(bufferTime, worstCase time.Duration) (in chan<- struct{}, out <-chan struct{}, clean func()) {
d := &detector{
t: bufferTime,
worstCase: worstCase,
buf: make(chan struct{}, 1),
out: make(chan struct{}),
quit: make(chan struct{}),
}
go d.work()
return d.buf, d.out, func() { close(d.quit) }
}
func (d *detector) work() {
var waitWrite <-chan time.Time
var worstCase <-chan time.Time
for {
select {
case <-d.quit:
return
case <-d.buf:
// we have an item in the buffer, dont announce yet
waitWrite = time.After(d.t)
if worstCase == nil {
worstCase = time.After(d.worstCase)
}
case <-waitWrite:
d.out <- struct{}{}
worstCase = nil
waitWrite = nil
case <-worstCase:
d.out <- struct{}{}
worstCase = nil
waitWrite = nil
}
}
}