-
Notifications
You must be signed in to change notification settings - Fork 11
/
goduino.go
170 lines (156 loc) · 3.85 KB
/
goduino.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
package goduino
import (
"fmt"
"github.com/argandas/goduino/firmata"
"github.com/tarm/serial"
"io"
"log"
"os"
"time"
)
const (
Input = firmata.Input
Output = firmata.Output
Analog = firmata.Analog
Pwm = firmata.Pwm
Servo = firmata.Servo
)
type firmataBoard interface {
Connect(io.ReadWriteCloser) error
Disconnect() error
Pins() []firmata.Pin
AnalogWrite(int, int) error
SetPinMode(int, int) error
ReportAnalog(int, int) error
ReportDigital(int, int) error
DigitalWrite(int, int) error
I2cRead(int, int) error
I2cWrite(int, []byte) error
I2cConfig(int) error
}
// Arduino Firmata client for golang
type Goduino struct {
name string
port string
board firmataBoard
conn io.ReadWriteCloser
openSP func(port string) (io.ReadWriteCloser, error)
logger *log.Logger
verbose bool
}
// Creates a new Goduino object and connects to the Arduino board
// over specified serial port. This function blocks till a connection is
// succesfullt established and pin mappings are retrieved.
func New(name string, args ...interface{}) *Goduino {
// Create new Goduino client
goduino := &Goduino{
name: name,
port: "",
conn: nil,
board: firmata.New(),
openSP: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
},
logger: log.New(os.Stdout, fmt.Sprintf("[%s] ", name), log.Ltime),
verbose: true,
}
// Parse variadic args
for _, arg := range args {
switch arg.(type) {
case string:
goduino.port = arg.(string)
case io.ReadWriteCloser:
goduino.conn = arg.(io.ReadWriteCloser)
}
}
return goduino
}
// Connect starts a connection to the firmata board.
func (ino *Goduino) Connect() error {
if ino.conn == nil {
// Try to connect to serial port
sp, err := ino.openSP(ino.Port())
if err != nil {
return err
}
// Serial connection was successful
ino.conn = sp
}
// Firmata connection
return ino.board.Connect(ino.conn)
}
// Disconnect closes the io connection to the firmata board
func (ino *Goduino) Disconnect() (err error) {
if ino.board != nil {
// Disconnect firmata board
return ino.board.Disconnect()
}
return nil
}
// Port returns the FirmataAdaptors port
func (ino *Goduino) Port() string { return ino.port }
// Name returns the FirmataAdaptors name
func (ino *Goduino) Name() string { return ino.name }
// PinMode configures the specified pin to behave either as an input or an output.
func (ino *Goduino) PinMode(pin, mode int) error {
// Check if pin is valid
if uint8(pin) < 0 || pin > len(ino.board.Pins()) {
return fmt.Errorf("Invalid pin number %v\n", pin)
}
switch mode {
// If mode == Input
case Input:
// Set pin mode
if err := ino.board.SetPinMode(pin, mode); err != nil {
return err
}
if err := ino.board.ReportDigital(pin, 1); err != nil {
return err
}
<-time.After(10 * time.Millisecond)
// If mode == Analog
case Analog:
pin = ino.digitalPin(pin)
// Set pin mode
if err := ino.board.SetPinMode(pin, mode); err != nil {
return err
}
if err := ino.board.ReportAnalog(pin, 1); err != nil {
return err
}
<-time.After(10 * time.Millisecond)
default:
// Set pin mode
if err := ino.board.SetPinMode(pin, mode); err != nil {
return err
}
}
// PinMode was successful
ino.logger.Printf("pinMode(%d, %s)\r\n", pin, PinMode(mode))
return nil
}
// Close the serial connection to properly clean up after ourselves
// Usage: defer client.Close()
func (ino *Goduino) Delay(duration time.Duration) {
time.Sleep(duration)
}
// digitalPin converts pin number to digital mapping
func (ino *Goduino) digitalPin(pin int) int {
return pin + 14
}
type PinMode uint8
func (m PinMode) String() string {
switch {
case m == Input:
return "INPUT"
case m == Output:
return "OUTPUT"
case m == Analog:
return "ANALOG"
case m == Pwm:
return "PWM"
case m == Servo:
return "SERVO"
}
return "UNKNOWN"
}