/
firmata_adaptor.go
171 lines (141 loc) · 4.1 KB
/
firmata_adaptor.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
package firmata
import (
"fmt"
"strconv"
"time"
"github.com/hybridgroup/gobot"
"github.com/tarm/goserial"
)
type FirmataAdaptor struct {
gobot.Adaptor
board *board
i2cAddress byte
connect func(*FirmataAdaptor)
}
// NewFirmataAdaptor returns a new firmata adaptor with specified name
// Generates a connect function that opens serial communication in specified port
func NewFirmataAdaptor(name, port string) *FirmataAdaptor {
return &FirmataAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"FirmataAdaptor",
port,
),
connect: func(f *FirmataAdaptor) {
sp, err := serial.OpenPort(&serial.Config{Name: f.Port(), Baud: 57600})
if err != nil {
panic(err)
}
f.board = newBoard(sp)
},
}
}
// Connect returns true if connection to board is succesfull
func (f *FirmataAdaptor) Connect() bool {
f.connect(f)
f.board.connect()
f.SetConnected(true)
return true
}
// close finishes connection to serial port
// Prints error message on error
func (f *FirmataAdaptor) Disconnect() bool {
err := f.board.serial.Close()
if err != nil {
fmt.Println(err)
}
return true
}
// Finalize disconnects firmata adaptor
func (f *FirmataAdaptor) Finalize() bool { return f.Disconnect() }
// InitServo (not yet implemented)
func (f *FirmataAdaptor) InitServo() {}
// ServoWrite sets angle form 0 to 360 to specified servo pin
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) {
p, _ := strconv.Atoi(pin)
f.board.setPinMode(byte(p), servo)
f.board.analogWrite(byte(p), angle)
}
// PwmWrite writes analog value to specified pin
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) {
p, _ := strconv.Atoi(pin)
f.board.setPinMode(byte(p), pwm)
f.board.analogWrite(byte(p), level)
}
// DigitalWrite writes digital values to specified pin
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) {
p, _ := strconv.Atoi(pin)
f.board.setPinMode(byte(p), output)
f.board.digitalWrite(byte(p), level)
}
// DigitalRead retrieves digital value from specified pin
// Returns -1 if response from board is timed out
func (f *FirmataAdaptor) DigitalRead(pin string) int {
ret := make(chan int)
p, _ := strconv.Atoi(pin)
f.board.setPinMode(byte(p), input)
f.board.togglePinReporting(byte(p), high, reportDigital)
f.board.readAndProcess()
gobot.Once(f.board.events[fmt.Sprintf("digital_read_%v", pin)], func(data interface{}) {
ret <- int(data.([]byte)[0])
})
select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return -1
}
// AnalogRead retrieves value from analog pin.
// NOTE pins are numbered A0-A5, which translate to digital pins 14-19
func (f *FirmataAdaptor) AnalogRead(pin string) int {
ret := make(chan int)
p, _ := strconv.Atoi(pin)
p = f.digitalPin(p)
f.board.setPinMode(byte(p), analog)
f.board.togglePinReporting(byte(p), high, reportAnalog)
f.board.readAndProcess()
gobot.Once(f.board.events[fmt.Sprintf("analog_read_%v", pin)], func(data interface{}) {
b := data.([]byte)
ret <- int(uint(b[0])<<24 | uint(b[1])<<16 | uint(b[2])<<8 | uint(b[3]))
})
select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return -1
}
// AnalogWrite writes value to ananlog pin
func (f *FirmataAdaptor) AnalogWrite(pin string, level byte) {
f.PwmWrite(pin, level)
}
// digitalPin converts pin number to digital mapping
func (f *FirmataAdaptor) digitalPin(pin int) int {
return pin + 14
}
// I2cStart initializes board with i2c configuration
func (f *FirmataAdaptor) I2cStart(address byte) {
f.i2cAddress = address
f.board.i2cConfig([]byte{0})
}
// I2cRead reads from I2c specified size
// Returns empty byte array if response is timed out
func (f *FirmataAdaptor) I2cRead(size uint) []byte {
ret := make(chan []byte)
f.board.i2cReadRequest(f.i2cAddress, size)
f.board.readAndProcess()
gobot.Once(f.board.events["i2c_reply"], func(data interface{}) {
ret <- data.(map[string][]byte)["data"]
})
select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return []byte{}
}
// I2cWrite retrieves i2c data
func (f *FirmataAdaptor) I2cWrite(data []byte) {
f.board.i2cWriteRequest(f.i2cAddress, data)
}