forked from hybridgroup/gobot
/
io_pin_driver.go
250 lines (205 loc) · 5.56 KB
/
io_pin_driver.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package microbit
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
)
// IOPinDriver is the Gobot driver for the Microbit's built-in digital and
// analog I/O
type IOPinDriver struct {
name string
adMask int
ioMask int
connection gobot.Connection
gobot.Eventer
}
const (
// BLE services
ioPinService = "e95d127b251d470aa062fa1922dfa9a8"
// BLE characteristics
pinDataCharacteristic = "e95d8d00251d470aa062fa1922dfa9a8"
pinADConfigCharacteristic = "e95d5899251d470aa062fa1922dfa9a8"
pinIOConfigCharacteristic = "e95db9fe251d470aa062fa1922dfa9a8"
)
// PinData has the read data for a specific digital pin
type PinData struct {
pin uint8
value uint8
}
// NewIOPinDriver creates a Microbit IOPinDriver
func NewIOPinDriver(a ble.BLEConnector) *IOPinDriver {
n := &IOPinDriver{
name: gobot.DefaultName("Microbit IO Pins"),
connection: a,
Eventer: gobot.NewEventer(),
}
return n
}
// Connection returns the BLE connection
func (b *IOPinDriver) Connection() gobot.Connection { return b.connection }
// Name returns the Driver Name
func (b *IOPinDriver) Name() string { return b.name }
// SetName sets the Driver Name
func (b *IOPinDriver) SetName(n string) { b.name = n }
// adaptor returns BLE adaptor
func (b *IOPinDriver) adaptor() ble.BLEConnector {
return b.Connection().(ble.BLEConnector)
}
// Start tells driver to get ready to do work
func (b *IOPinDriver) Start() (err error) {
_, err = b.ReadPinADConfig()
if err != nil {
return
}
_, err = b.ReadPinIOConfig()
return
}
// Halt stops driver (void)
func (b *IOPinDriver) Halt() (err error) {
return
}
// ReadAllPinData reads and returns the pin data for all pins
func (b *IOPinDriver) ReadAllPinData() (pins []PinData) {
c, _ := b.adaptor().ReadCharacteristic(pinDataCharacteristic)
buf := bytes.NewBuffer(c)
pinsData := make([]PinData, buf.Len()/2)
for i := 0; i < buf.Len()/2; i++ {
pinData := PinData{}
pinData.pin, _ = buf.ReadByte()
pinData.value, _ = buf.ReadByte()
pinsData[i] = pinData
}
return pinsData
}
// WritePinData writes the pin data for a single pin
func (b *IOPinDriver) WritePinData(pin string, data byte) (err error) {
i, err := strconv.Atoi(pin)
if err != nil {
return
}
buf := []byte{byte(i), data}
err = b.adaptor().WriteCharacteristic(pinDataCharacteristic, buf)
return err
}
// ReadPinADConfig reads and returns the pin A/D config mask for all pins
func (b *IOPinDriver) ReadPinADConfig() (config int, err error) {
c, e := b.adaptor().ReadCharacteristic(pinADConfigCharacteristic)
if e != nil {
return 0, e
}
var result byte
for i := 0; i < 4; i++ {
result |= c[i] << uint(i)
}
b.adMask = int(result)
return int(result), nil
}
// WritePinADConfig writes the pin A/D config mask for all pins
func (b *IOPinDriver) WritePinADConfig(config int) (err error) {
b.adMask = config
data := &bytes.Buffer{}
binary.Write(data, binary.LittleEndian, uint32(config))
err = b.adaptor().WriteCharacteristic(pinADConfigCharacteristic, data.Bytes())
return
}
// ReadPinIOConfig reads and returns the pin IO config mask for all pins
func (b *IOPinDriver) ReadPinIOConfig() (config int, err error) {
c, e := b.adaptor().ReadCharacteristic(pinIOConfigCharacteristic)
if e != nil {
return 0, e
}
var result byte
for i := 0; i < 4; i++ {
result |= c[i] << uint(i)
}
b.ioMask = int(result)
return int(result), nil
}
// WritePinIOConfig writes the pin I/O config mask for all pins
func (b *IOPinDriver) WritePinIOConfig(config int) (err error) {
b.ioMask = config
data := &bytes.Buffer{}
binary.Write(data, binary.LittleEndian, uint32(config))
err = b.adaptor().WriteCharacteristic(pinIOConfigCharacteristic, data.Bytes())
return
}
// DigitalRead reads from a pin
func (b *IOPinDriver) DigitalRead(pin string) (val int, err error) {
p, err := validatedPin(pin)
if err != nil {
return
}
b.ensureDigital(p)
b.ensureInput(p)
pins := b.ReadAllPinData()
return int(pins[p].value), nil
}
// DigitalWrite writes to a pin
func (b *IOPinDriver) DigitalWrite(pin string, level byte) (err error) {
p, err := validatedPin(pin)
if err != nil {
return
}
b.ensureDigital(p)
b.ensureOutput(p)
return b.WritePinData(pin, level)
}
// AnalogRead reads from a pin
func (b *IOPinDriver) AnalogRead(pin string) (val int, err error) {
p, err := validatedPin(pin)
if err != nil {
return
}
b.ensureAnalog(p)
b.ensureInput(p)
pins := b.ReadAllPinData()
return int(pins[p].value), nil
}
func (b *IOPinDriver) ensureDigital(pin int) {
if hasBit(b.adMask, pin) {
b.WritePinADConfig(clearBit(b.adMask, pin))
}
}
func (b *IOPinDriver) ensureAnalog(pin int) {
if !hasBit(b.adMask, pin) {
b.WritePinADConfig(setBit(b.adMask, pin))
}
}
func (b *IOPinDriver) ensureInput(pin int) {
if !hasBit(b.ioMask, pin) {
b.WritePinIOConfig(setBit(b.ioMask, pin))
}
}
func (b *IOPinDriver) ensureOutput(pin int) {
if hasBit(b.ioMask, pin) {
b.WritePinIOConfig(clearBit(b.ioMask, pin))
}
}
func validatedPin(pin string) (int, error) {
i, err := strconv.Atoi(pin)
if err != nil {
return 0, err
}
if i < 0 || i > 2 {
return 0, errors.New("Invalid pin.")
}
return i, nil
}
// via http://stackoverflow.com/questions/23192262/how-would-you-set-and-clear-a-single-bit-in-go
// Sets the bit at pos in the integer n.
func setBit(n int, pos int) int {
n |= (1 << uint(pos))
return n
}
// Test if the bit at pos is set in the integer n.
func hasBit(n int, pos int) bool {
val := n & (1 << uint(pos))
return (val > 0)
}
// Clears the bit at pos in n.
func clearBit(n int, pos int) int {
return n &^ (1 << uint(pos))
}