1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

core: Refactor Firmata platform for new Adaptor creation signature

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-09-25 20:49:20 +02:00
parent 12a5cec209
commit 6d4b9927a7
4 changed files with 72 additions and 71 deletions

View File

@ -22,14 +22,14 @@ import (
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/drivers/gpio"
) )
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0") firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13") led := gpio.NewLedDriver(firmataAdaptor, "13")
work := func() { work := func() {
gobot.Every(1*time.Second, func() { gobot.Every(1*time.Second, func() {

View File

@ -14,14 +14,14 @@ Example:
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/drivers/gpio"
) )
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0") firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13") led := gpio.NewLedDriver(firmataAdaptor, "13")
work := func() { work := func() {
gobot.Every(1*time.Second, func() { gobot.Every(1*time.Second, func() {

View File

@ -26,8 +26,8 @@ type firmataBoard interface {
Event(string) string Event(string) string
} }
// FirmataAdaptor is the Gobot Adaptor for Firmata based boards // Adaptor is the Gobot Adaptor for Firmata based boards
type FirmataAdaptor struct { type Adaptor struct {
name string name string
port string port string
board firmataBoard board firmataBoard
@ -36,18 +36,17 @@ type FirmataAdaptor struct {
gobot.Eventer gobot.Eventer
} }
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts: // NewAdaptor returns a new Firmata Adaptor which optionally accepts:
// //
// string: port the FirmataAdaptor uses to connect to a serial port with a baude rate of 57600 // string: port the Adaptor uses to connect to a serial port with a baude rate of 57600
// io.ReadWriteCloser: connection the FirmataAdaptor uses to communication with the hardware // io.ReadWriteCloser: connection the Adaptor uses to communication with the hardware
// //
// If an io.ReadWriteCloser is not supplied, the FirmataAdaptor will open a connection // If an io.ReadWriteCloser is not supplied, the Adaptor will open a connection
// to a serial port with a baude rate of 57600. If an io.ReadWriteCloser // to a serial port with a baude rate of 57600. If an io.ReadWriteCloser
// is supplied, then the FirmataAdaptor will use the provided io.ReadWriteCloser and use the // is supplied, then the Adaptor will use the provided io.ReadWriteCloser and use the
// string port as a label to be displayed in the log and api. // string port as a label to be displayed in the log and api.
func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor { func NewAdaptor(args ...interface{}) *Adaptor {
f := &FirmataAdaptor{ f := &Adaptor{
name: name,
port: "", port: "",
conn: nil, conn: nil,
board: client.New(), board: client.New(),
@ -70,7 +69,7 @@ func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
} }
// Connect starts a connection to the board. // Connect starts a connection to the board.
func (f *FirmataAdaptor) Connect() (errs []error) { func (f *Adaptor) Connect() (errs []error) {
if f.conn == nil { if f.conn == nil {
sp, err := f.openSP(f.Port()) sp, err := f.openSP(f.Port())
if err != nil { if err != nil {
@ -85,7 +84,7 @@ func (f *FirmataAdaptor) Connect() (errs []error) {
} }
// Disconnect closes the io connection to the board // Disconnect closes the io connection to the board
func (f *FirmataAdaptor) Disconnect() (err error) { func (f *Adaptor) Disconnect() (err error) {
if f.board != nil { if f.board != nil {
return f.board.Disconnect() return f.board.Disconnect()
} }
@ -93,21 +92,24 @@ func (f *FirmataAdaptor) Disconnect() (err error) {
} }
// Finalize terminates the firmata connection // Finalize terminates the firmata connection
func (f *FirmataAdaptor) Finalize() (errs []error) { func (f *Adaptor) Finalize() (errs []error) {
if err := f.Disconnect(); err != nil { if err := f.Disconnect(); err != nil {
return []error{err} return []error{err}
} }
return return
} }
// Port returns the FirmataAdaptors port // Port returns the Firmata Adaptors port
func (f *FirmataAdaptor) Port() string { return f.port } func (f *Adaptor) Port() string { return f.port }
// Name returns the FirmataAdaptors name // Name returns the Firmata Adaptors name
func (f *FirmataAdaptor) Name() string { return f.name } func (f *Adaptor) Name() string { return f.name }
// SetName sets the Firmata Adaptors name
func (f *Adaptor) SetName(n string) { f.name = n }
// ServoConfig sets the pulse width in microseconds for a pin attached to a servo // ServoConfig sets the pulse width in microseconds for a pin attached to a servo
func (f *FirmataAdaptor) ServoConfig(pin string, min, max int) error { func (f *Adaptor) ServoConfig(pin string, min, max int) error {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return err return err
@ -117,7 +119,7 @@ func (f *FirmataAdaptor) ServoConfig(pin string, min, max int) error {
} }
// ServoWrite writes the 0-180 degree angle to the specified pin. // ServoWrite writes the 0-180 degree angle to the specified pin.
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) { func (f *Adaptor) ServoWrite(pin string, angle byte) (err error) {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return err return err
@ -134,7 +136,7 @@ func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) {
} }
// PwmWrite writes the 0-254 value to the specified pin // PwmWrite writes the 0-254 value to the specified pin
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) (err error) { func (f *Adaptor) PwmWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return err return err
@ -151,7 +153,7 @@ func (f *FirmataAdaptor) PwmWrite(pin string, level byte) (err error) {
} }
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0. // DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) { func (f *Adaptor) DigitalWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return return
@ -170,7 +172,7 @@ func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) {
// DigitalRead retrieves digital value from specified pin. // DigitalRead retrieves digital value from specified pin.
// Returns -1 if the response from the board has timed out // Returns -1 if the response from the board has timed out
func (f *FirmataAdaptor) DigitalRead(pin string) (val int, err error) { func (f *Adaptor) DigitalRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return return
@ -191,7 +193,7 @@ func (f *FirmataAdaptor) DigitalRead(pin string) (val int, err error) {
// AnalogRead retrieves value from analog pin. // AnalogRead retrieves value from analog pin.
// Returns -1 if the response from the board has timed out // Returns -1 if the response from the board has timed out
func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) { func (f *Adaptor) AnalogRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin) p, err := strconv.Atoi(pin)
if err != nil { if err != nil {
return return
@ -214,18 +216,18 @@ func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) {
} }
// digitalPin converts pin number to digital mapping // digitalPin converts pin number to digital mapping
func (f *FirmataAdaptor) digitalPin(pin int) int { func (f *Adaptor) digitalPin(pin int) int {
return pin + 14 return pin + 14
} }
// I2cStart starts an i2c device at specified address // I2cStart starts an i2c device at specified address
func (f *FirmataAdaptor) I2cStart(address int) (err error) { func (f *Adaptor) I2cStart(address int) (err error) {
return f.board.I2cConfig(0) return f.board.I2cConfig(0)
} }
// I2cRead returns size bytes from the i2c device // I2cRead returns size bytes from the i2c device
// Returns an empty array if the response from the board has timed out // Returns an empty array if the response from the board has timed out
func (f *FirmataAdaptor) I2cRead(address int, size int) (data []byte, err error) { func (f *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
ret := make(chan []byte) ret := make(chan []byte)
if err = f.board.I2cRead(address, size); err != nil { if err = f.board.I2cRead(address, size); err != nil {
@ -242,6 +244,6 @@ func (f *FirmataAdaptor) I2cRead(address int, size int) (data []byte, err error)
} }
// I2cWrite writes data to i2c device // I2cWrite writes data to i2c device
func (f *FirmataAdaptor) I2cWrite(address int, data []byte) (err error) { func (f *Adaptor) I2cWrite(address int, data []byte) (err error) {
return f.board.I2cWrite(address, data) return f.board.I2cWrite(address, data)
} }

View File

@ -10,21 +10,21 @@ import (
"time" "time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/drivers/gpio"
"github.com/hybridgroup/gobot/drivers/i2c"
"github.com/hybridgroup/gobot/gobottest" "github.com/hybridgroup/gobot/gobottest"
"github.com/hybridgroup/gobot/platforms/firmata/client" "github.com/hybridgroup/gobot/platforms/firmata/client"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/i2c"
) )
var _ gobot.Adaptor = (*FirmataAdaptor)(nil) var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gpio.DigitalReader = (*FirmataAdaptor)(nil) var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*FirmataAdaptor)(nil) var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ gpio.AnalogReader = (*FirmataAdaptor)(nil) var _ gpio.AnalogReader = (*Adaptor)(nil)
var _ gpio.PwmWriter = (*FirmataAdaptor)(nil) var _ gpio.PwmWriter = (*Adaptor)(nil)
var _ gpio.ServoWriter = (*FirmataAdaptor)(nil) var _ gpio.ServoWriter = (*Adaptor)(nil)
var _ i2c.I2c = (*FirmataAdaptor)(nil) var _ i2c.I2c = (*Adaptor)(nil)
type readWriteCloser struct{} type readWriteCloser struct{}
@ -87,8 +87,8 @@ func (mockFirmataBoard) I2cWrite(int, []byte) error { return nil }
func (mockFirmataBoard) I2cConfig(int) error { return nil } func (mockFirmataBoard) I2cConfig(int) error { return nil }
func (mockFirmataBoard) ServoConfig(int, int, int) error { return nil } func (mockFirmataBoard) ServoConfig(int, int, int) error { return nil }
func initTestFirmataAdaptor() *FirmataAdaptor { func initTestAdaptor() *Adaptor {
a := NewFirmataAdaptor("board", "/dev/null") a := NewAdaptor("/dev/null")
a.board = newMockFirmataBoard() a.board = newMockFirmataBoard()
a.openSP = func(port string) (io.ReadWriteCloser, error) { a.openSP = func(port string) (io.ReadWriteCloser, error) {
return &readWriteCloser{}, nil return &readWriteCloser{}, nil
@ -97,78 +97,77 @@ func initTestFirmataAdaptor() *FirmataAdaptor {
return a return a
} }
func TestFirmataAdaptor(t *testing.T) { func TestAdaptor(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
gobottest.Assert(t, a.Name(), "board")
gobottest.Assert(t, a.Port(), "/dev/null") gobottest.Assert(t, a.Port(), "/dev/null")
} }
func TestFirmataAdaptorFinalize(t *testing.T) { func TestAdaptorFinalize(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
gobottest.Assert(t, len(a.Finalize()), 0) gobottest.Assert(t, len(a.Finalize()), 0)
a = initTestFirmataAdaptor() a = initTestAdaptor()
a.board.(*mockFirmataBoard).disconnectError = errors.New("close error") a.board.(*mockFirmataBoard).disconnectError = errors.New("close error")
gobottest.Assert(t, a.Finalize()[0], errors.New("close error")) gobottest.Assert(t, a.Finalize()[0], errors.New("close error"))
} }
func TestFirmataAdaptorConnect(t *testing.T) { func TestAdaptorConnect(t *testing.T) {
var openSP = func(port string) (io.ReadWriteCloser, error) { var openSP = func(port string) (io.ReadWriteCloser, error) {
return &readWriteCloser{}, nil return &readWriteCloser{}, nil
} }
a := NewFirmataAdaptor("board", "/dev/null") a := NewAdaptor("/dev/null")
a.openSP = openSP a.openSP = openSP
a.board = newMockFirmataBoard() a.board = newMockFirmataBoard()
gobottest.Assert(t, len(a.Connect()), 0) gobottest.Assert(t, len(a.Connect()), 0)
a = NewFirmataAdaptor("board", "/dev/null") a = NewAdaptor("/dev/null")
a.board = newMockFirmataBoard() a.board = newMockFirmataBoard()
a.openSP = func(port string) (io.ReadWriteCloser, error) { a.openSP = func(port string) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error") return nil, errors.New("connect error")
} }
gobottest.Assert(t, a.Connect()[0], errors.New("connect error")) gobottest.Assert(t, a.Connect()[0], errors.New("connect error"))
a = NewFirmataAdaptor("board", &readWriteCloser{}) a = NewAdaptor(&readWriteCloser{})
a.board = newMockFirmataBoard() a.board = newMockFirmataBoard()
gobottest.Assert(t, len(a.Connect()), 0) gobottest.Assert(t, len(a.Connect()), 0)
} }
func TestFirmataAdaptorServoWrite(t *testing.T) { func TestAdaptorServoWrite(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
a.ServoWrite("1", 50) a.ServoWrite("1", 50)
} }
func TestFirmataAdaptorPwmWrite(t *testing.T) { func TestAdaptorPwmWrite(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
a.PwmWrite("1", 50) a.PwmWrite("1", 50)
} }
func TestFirmataAdaptorDigitalWrite(t *testing.T) { func TestAdaptorDigitalWrite(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
a.DigitalWrite("1", 1) a.DigitalWrite("1", 1)
} }
func TestFirmataAdaptorDigitalRead(t *testing.T) { func TestAdaptorDigitalRead(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
val, err := a.DigitalRead("1") val, err := a.DigitalRead("1")
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, val, 1) gobottest.Assert(t, val, 1)
} }
func TestFirmataAdaptorAnalogRead(t *testing.T) { func TestAdaptorAnalogRead(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
val, err := a.AnalogRead("1") val, err := a.AnalogRead("1")
gobottest.Assert(t, val, 133) gobottest.Assert(t, val, 133)
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
} }
func TestFirmataAdaptorI2cStart(t *testing.T) { func TestAdaptorI2cStart(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
a.I2cStart(0x00) a.I2cStart(0x00)
} }
func TestFirmataAdaptorI2cRead(t *testing.T) { func TestAdaptorI2cRead(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
i := []byte{100} i := []byte{100}
i2cReply := client.I2cReply{Data: i} i2cReply := client.I2cReply{Data: i}
go func() { go func() {
@ -179,13 +178,13 @@ func TestFirmataAdaptorI2cRead(t *testing.T) {
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, data, i) gobottest.Assert(t, data, i)
} }
func TestFirmataAdaptorI2cWrite(t *testing.T) { func TestAdaptorI2cWrite(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
a.I2cWrite(0x00, []byte{0x00, 0x01}) a.I2cWrite(0x00, []byte{0x00, 0x01})
} }
func TestServoConfig(t *testing.T) { func TestServoConfig(t *testing.T) {
a := initTestFirmataAdaptor() a := initTestAdaptor()
err := a.ServoConfig("9", 0, 0) err := a.ServoConfig("9", 0, 0)
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)