hybridgroup.gobot/platforms/firmata/firmata_adaptor.go

127 lines
2.8 KiB
Go
Raw Normal View History

2014-04-28 10:56:18 +08:00
package firmata
import (
"fmt"
"strconv"
"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-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",
port,
2014-06-24 11:33:59 +08:00
),
2014-04-28 10:56:18 +08:00
connect: func(f *FirmataAdaptor) {
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-28 10:56:18 +08:00
func (f *FirmataAdaptor) Connect() bool {
f.connect(f)
f.Board.connect()
f.SetConnected(true)
return true
}
2014-04-28 10:56:18 +08:00
func (f *FirmataAdaptor) Disconnect() bool {
err := f.Board.Serial.Close()
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-28 10:56:18 +08:00
func (f *FirmataAdaptor) InitServo() {}
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) {
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-28 10:56:18 +08:00
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) {
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-28 10:56:18 +08:00
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) {
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-28 10:56:18 +08:00
func (f *FirmataAdaptor) DigitalRead(pin string) int {
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))
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 {
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))
if len(events) > 0 {
event := events[len(events)-1]
return int(uint(event.Data[0])<<24 |
uint(event.Data[1])<<16 |
uint(event.Data[2])<<8 |
uint(event.Data[3]))
}
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 {
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-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-28 10:56:18 +08:00
events := f.Board.findEvents("i2c_reply")
if len(events) > 0 {
return events[len(events)-1].I2cReply["data"]
}
2014-06-07 09:58:04 +08:00
return make([]byte, 0)
}
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)
}