-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgpio_fake.go
146 lines (124 loc) · 3.57 KB
/
gpio_fake.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
/// Author: Bernhard Tittelbach, btittelbach@github (c) 2014
package bbhw
import (
"fmt"
"log"
)
// Use FakeGPIO for testing and debugging.
// Does not actually toogle GPIOs and works even on your normal computer.
type FakeGPIO struct {
name string
dir int
value bool
activelow bool
logTarget *log.Logger
connectedTo []*FakeGPIO
}
type FakeGPIONullWriter struct{}
func (n *FakeGPIONullWriter) Write(p []byte) (int, error) { return len(p), nil }
var FakeGPIODefaultLogTarget_ *log.Logger
func init() {
FakeGPIODefaultLogTarget_ = log.New(&FakeGPIONullWriter{}, "", 0)
}
// ----------- Fake GPIO for Testing ----------------
// same signature as all the other New*GPIO implementations.
// logs to FakeGPIODefaultLogTarget_ which is an exported field and thus you can set it to point to the log.Logger of your choice
func NewFakeGPIO(gpionum uint, direction int) (gpio *FakeGPIO) {
return NewFakeNamedGPIO(fmt.Sprintf("FakeGPIO(%d)", gpionum), direction, nil)
}
// slightly more fancy FakeGPIO for debugging.
// takes a name for easy recognition in debugging output and an optional logger (or nil) of your choice,
// thus you could route debug output of different GPIOs to different destinations
func NewFakeNamedGPIO(name string, direction int, logTarget *log.Logger) (gpio *FakeGPIO) {
gpio = &FakeGPIO{name: name, dir: direction, value: false, logTarget: logTarget}
return
}
func (gpio *FakeGPIO) CheckDirection() (direction int, err error) {
return gpio.dir, nil
}
func (gpio *FakeGPIO) SetDirection(direction int) error {
if !(direction == IN || direction == OUT || direction == IN_PULLDOWN || direction == IN_PULLUP) {
panic("direction value invalid")
}
gpio.dir = direction
return nil
}
func (gpio *FakeGPIO) GetState() (state bool, err error) {
return gpio.activelow != gpio.value, nil
}
func (gpio *FakeGPIO) SetState(state bool) error {
if gpio == nil {
panic("gpio == nil")
}
if gpio.dir == OUT {
gpio.value = gpio.activelow != state
gpio.log("set to virtual electrical state >%+v<", gpio.value)
if gpio.connectedTo != nil {
for _, othergpio := range gpio.connectedTo {
if othergpio == nil {
continue
}
othergpio.FakeInput(gpio.value)
}
}
} else {
panic("tried to set state on IN gpio")
}
return nil
}
//this inverts the meaning of virtual 0 and 1
func (gpio *FakeGPIO) SetActiveLow(activelow bool) error {
if gpio == nil {
panic("gpio == nil")
}
prev_state, err := gpio.GetState()
if err != nil {
return err
}
gpio.activelow = activelow
return gpio.SetState(prev_state)
}
func (gpio *FakeGPIO) SetStateNow(state bool) error { return gpio.SetState(state) }
func (gpio *FakeGPIO) Close() {
gpio = nil
}
func (gpio *FakeGPIO) ConnectTo(conn ...*FakeGPIO) {
gpio.connectedTo = conn
if gpio.connectedTo != nil {
var gpionames string
for _, othergpio := range gpio.connectedTo {
if othergpio == nil {
continue
}
dir := "IN"
if othergpio.dir == OUT {
dir = "OUT"
}
gpionames += " " + othergpio.name + "(" + dir + ")"
}
gpio.log("now connected to" + gpionames)
}
}
func (gpio *FakeGPIO) FakeInput(state bool) error {
if gpio == nil {
panic("gpio == nil")
}
if gpio.dir == IN {
gpio.log("faking input >%+v<", state)
gpio.value = state
} else {
panic("tried to fake input for output gpio")
}
return nil
}
func (gpio *FakeGPIO) log(fmt string, attr ...interface{}) {
logT := gpio.logTarget
if logT == nil {
logT = FakeGPIODefaultLogTarget_
}
dir := "IN"
if gpio.dir == OUT {
dir = "OUT"
}
logT.Printf("FakeGPIO %s(%s): "+fmt, append([]interface{}{gpio.name, dir}, attr...)...)
}