forked from google/periph
/
gpiotest.go
196 lines (168 loc) · 3.95 KB
/
gpiotest.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2016 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 gpiotest is meant to be used to test drivers using fake Pins.
package gpiotest
import (
"errors"
"fmt"
"log"
"sync"
"time"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/pin"
)
// Pin implements gpio.PinIO.
//
// Modify its members to simulate hardware events.
type Pin struct {
// These should be immutable.
N string
Num int
Fn string // TODO(maruel): pin.Func in v4.
// Grab the Mutex before accessing the following members.
sync.Mutex
L gpio.Level // Used for both input and output
P gpio.Pull
EdgesChan chan gpio.Level // Use it to fake edges
D gpio.Duty // PWM duty
F physic.Frequency // PWM period
}
// String implements conn.Resource.
func (p *Pin) String() string {
return fmt.Sprintf("%s(%d)", p.N, p.Num)
}
// Halt implements conn.Resource.
//
// It has no effect.
func (p *Pin) Halt() error {
return nil
}
// Name implements pin.Pin.
func (p *Pin) Name() string {
return p.N
}
// Number implements pin.Pin.
func (p *Pin) Number() int {
return p.Num
}
// Function implements pin.Pin.
func (p *Pin) Function() string {
return p.Fn
}
// Func implements pin.PinFunc.
func (p *Pin) Func() pin.Func {
return pin.Func(p.Fn)
}
// SupportedFuncs implements pin.PinFunc.
func (p *Pin) SupportedFuncs() []pin.Func {
return []pin.Func{gpio.IN, gpio.OUT}
}
// SetFunc implements pin.PinFunc.
func (p *Pin) SetFunc(f pin.Func) error {
return errors.New("gpiotest: not supported")
}
// In implements gpio.PinIn.
func (p *Pin) In(pull gpio.Pull, edge gpio.Edge) error {
p.Lock()
defer p.Unlock()
p.P = pull
if pull == gpio.PullDown {
p.L = gpio.Low
} else if pull == gpio.PullUp {
p.L = gpio.High
}
if edge != gpio.NoEdge && p.EdgesChan == nil {
return errors.New("gpiotest: please set p.EdgesChan first")
}
// Flush any buffered edges.
for {
select {
case <-p.EdgesChan:
default:
return nil
}
}
}
// Read implements gpio.PinIn.
func (p *Pin) Read() gpio.Level {
p.Lock()
defer p.Unlock()
return p.L
}
// WaitForEdge implements gpio.PinIn.
func (p *Pin) WaitForEdge(timeout time.Duration) bool {
if timeout == -1 {
_ = p.Out(<-p.EdgesChan)
return true
}
select {
case <-time.After(timeout):
return false
case l := <-p.EdgesChan:
_ = p.Out(l)
return true
}
}
// Pull implements gpio.PinIn.
func (p *Pin) Pull() gpio.Pull {
return p.P
}
// DefaultPull implements gpio.PinIn.
func (p *Pin) DefaultPull() gpio.Pull {
return p.P
}
// Out implements gpio.PinOut.
func (p *Pin) Out(l gpio.Level) error {
p.Lock()
defer p.Unlock()
p.L = l
return nil
}
// PWM implements gpio.PinOut.
func (p *Pin) PWM(duty gpio.Duty, f physic.Frequency) error {
p.Lock()
defer p.Unlock()
p.D = duty
p.F = f
return nil
}
// LogPinIO logs when its state changes.
type LogPinIO struct {
gpio.PinIO
}
// Real implements gpio.RealPin.
func (p *LogPinIO) Real() gpio.PinIO {
return p.PinIO
}
// In implements gpio.PinIn.
func (p *LogPinIO) In(pull gpio.Pull, edge gpio.Edge) error {
log.Printf("%s.In(%s, %s)", p, pull, edge)
return p.PinIO.In(pull, edge)
}
// Read implements gpio.PinIn.
func (p *LogPinIO) Read() gpio.Level {
l := p.PinIO.Read()
log.Printf("%s.Read() %s", p, l)
return l
}
// WaitForEdge implements gpio.PinIn.
func (p *LogPinIO) WaitForEdge(timeout time.Duration) bool {
s := time.Now()
r := p.PinIO.WaitForEdge(timeout)
log.Printf("%s.WaitForEdge(%s) -> %t after %s", p, timeout, r, time.Since(s))
return r
}
// Out implements gpio.PinOut.
func (p *LogPinIO) Out(l gpio.Level) error {
log.Printf("%s.Out(%s)", p, l)
return p.PinIO.Out(l)
}
// PWM implements gpio.PinOut.
func (p *LogPinIO) PWM(duty gpio.Duty, f physic.Frequency) error {
log.Printf("%s.PWM(%s, %s)", p, duty, f)
return p.PinIO.PWM(duty, f)
}
var _ gpio.PinIO = &Pin{}
var _ pin.PinFunc = &Pin{}