2014-04-28 10:56:18 +08:00
|
|
|
package firmata
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-11 04:42:18 +08:00
|
|
|
"strconv"
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/tarm/goserial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FirmataAdaptor struct {
|
|
|
|
gobot.Adaptor
|
|
|
|
Board *board
|
|
|
|
i2cAddress byte
|
2014-04-28 10:56:18 +08:00
|
|
|
connect func(*FirmataAdaptor)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 23:10:44 +08:00
|
|
|
func NewFirmataAdaptor(name, port string) *FirmataAdaptor {
|
2014-04-28 10:56:18 +08:00
|
|
|
return &FirmataAdaptor{
|
2014-06-24 11:33:59 +08:00
|
|
|
Adaptor: *gobot.NewAdaptor(
|
|
|
|
name,
|
|
|
|
"FirmataAdaptor",
|
2014-07-04 10:52:31 +08:00
|
|
|
port,
|
2014-06-24 11:33:59 +08:00
|
|
|
),
|
2014-04-28 10:56:18 +08:00
|
|
|
connect: func(f *FirmataAdaptor) {
|
2014-07-04 10:52:31 +08:00
|
|
|
sp, err := serial.OpenPort(&serial.Config{Name: f.Port(), Baud: 57600})
|
2014-04-28 10:56:18 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
f.Board = newBoard(sp)
|
|
|
|
},
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) Connect() bool {
|
|
|
|
f.connect(f)
|
|
|
|
f.Board.connect()
|
2014-07-04 10:52:31 +08:00
|
|
|
f.SetConnected(true)
|
2014-04-26 18:11:51 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) Disconnect() bool {
|
|
|
|
err := f.Board.Serial.Close()
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) Finalize() bool { return f.Disconnect() }
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) InitServo() {}
|
|
|
|
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) {
|
2014-04-26 18:11:51 +08:00
|
|
|
p, _ := strconv.Atoi(pin)
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
f.Board.setPinMode(byte(p), Servo)
|
2014-04-28 10:56:18 +08:00
|
|
|
f.Board.analogWrite(byte(p), angle)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) {
|
2014-04-26 18:11:51 +08:00
|
|
|
p, _ := strconv.Atoi(pin)
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
f.Board.setPinMode(byte(p), PWM)
|
|
|
|
f.Board.analogWrite(byte(p), level)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) {
|
2014-04-26 18:11:51 +08:00
|
|
|
p, _ := strconv.Atoi(pin)
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
f.Board.setPinMode(byte(p), Output)
|
2014-04-28 10:56:18 +08:00
|
|
|
f.Board.digitalWrite(byte(p), level)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) DigitalRead(pin string) int {
|
2014-04-26 18:11:51 +08:00
|
|
|
p, _ := strconv.Atoi(pin)
|
2014-06-11 06:16:11 +08:00
|
|
|
f.Board.setPinMode(byte(p), Input)
|
|
|
|
f.Board.togglePinReporting(byte(p), High, ReportDigital)
|
2014-04-28 10:56:18 +08:00
|
|
|
events := f.Board.findEvents(fmt.Sprintf("digital_read_%v", pin))
|
2014-04-26 18:11:51 +08:00
|
|
|
if len(events) > 0 {
|
|
|
|
return int(events[len(events)-1].Data[0])
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE pins are numbered A0-A5, which translate to digital pins 14-19
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) AnalogRead(pin string) int {
|
2014-04-26 18:11:51 +08:00
|
|
|
p, _ := strconv.Atoi(pin)
|
2014-04-28 10:56:18 +08:00
|
|
|
p = f.digitalPin(p)
|
2014-06-11 06:16:11 +08:00
|
|
|
f.Board.setPinMode(byte(p), Analog)
|
|
|
|
f.Board.togglePinReporting(byte(p), High, ReportAnalog)
|
2014-04-28 10:56:18 +08:00
|
|
|
events := f.Board.findEvents(fmt.Sprintf("analog_read_%v", pin))
|
2014-04-26 18:11:51 +08:00
|
|
|
if len(events) > 0 {
|
|
|
|
event := events[len(events)-1]
|
2014-07-11 04:42:18 +08:00
|
|
|
return int(uint(event.Data[0])<<24 |
|
|
|
|
uint(event.Data[1])<<16 |
|
|
|
|
uint(event.Data[2])<<8 |
|
|
|
|
uint(event.Data[3]))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2014-06-10 05:28:48 +08:00
|
|
|
func (f *FirmataAdaptor) AnalogWrite(pin string, level byte) {
|
|
|
|
f.PwmWrite(pin, level)
|
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) digitalPin(pin int) int {
|
2014-04-26 18:11:51 +08:00
|
|
|
return pin + 14
|
|
|
|
}
|
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
func (f *FirmataAdaptor) I2cStart(address byte) {
|
|
|
|
f.i2cAddress = address
|
2014-06-07 09:58:04 +08:00
|
|
|
f.Board.i2cConfig([]byte{0})
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 09:58:04 +08:00
|
|
|
func (f *FirmataAdaptor) I2cRead(size uint) []byte {
|
2014-04-28 10:56:18 +08:00
|
|
|
f.Board.i2cReadRequest(f.i2cAddress, size)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-04-28 10:56:18 +08:00
|
|
|
events := f.Board.findEvents("i2c_reply")
|
2014-04-26 18:11:51 +08:00
|
|
|
if len(events) > 0 {
|
|
|
|
return events[len(events)-1].I2cReply["data"]
|
|
|
|
}
|
2014-06-07 09:58:04 +08:00
|
|
|
return make([]byte, 0)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 09:58:04 +08:00
|
|
|
func (f *FirmataAdaptor) I2cWrite(data []byte) {
|
2014-04-28 10:56:18 +08:00
|
|
|
f.Board.i2cWriteRequest(f.i2cAddress, data)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|