1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +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/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/drivers/gpio"
)
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "13")
work := func() {
gobot.Every(1*time.Second, func() {

View File

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

View File

@ -26,8 +26,8 @@ type firmataBoard interface {
Event(string) string
}
// FirmataAdaptor is the Gobot Adaptor for Firmata based boards
type FirmataAdaptor struct {
// Adaptor is the Gobot Adaptor for Firmata based boards
type Adaptor struct {
name string
port string
board firmataBoard
@ -36,18 +36,17 @@ type FirmataAdaptor struct {
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
// io.ReadWriteCloser: connection the FirmataAdaptor uses to communication with the hardware
// string: port the Adaptor uses to connect to a serial port with a baude rate of 57600
// 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
// 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.
func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
f := &FirmataAdaptor{
name: name,
func NewAdaptor(args ...interface{}) *Adaptor {
f := &Adaptor{
port: "",
conn: nil,
board: client.New(),
@ -70,7 +69,7 @@ func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
}
// Connect starts a connection to the board.
func (f *FirmataAdaptor) Connect() (errs []error) {
func (f *Adaptor) Connect() (errs []error) {
if f.conn == nil {
sp, err := f.openSP(f.Port())
if err != nil {
@ -85,7 +84,7 @@ func (f *FirmataAdaptor) Connect() (errs []error) {
}
// Disconnect closes the io connection to the board
func (f *FirmataAdaptor) Disconnect() (err error) {
func (f *Adaptor) Disconnect() (err error) {
if f.board != nil {
return f.board.Disconnect()
}
@ -93,21 +92,24 @@ func (f *FirmataAdaptor) Disconnect() (err error) {
}
// Finalize terminates the firmata connection
func (f *FirmataAdaptor) Finalize() (errs []error) {
func (f *Adaptor) Finalize() (errs []error) {
if err := f.Disconnect(); err != nil {
return []error{err}
}
return
}
// Port returns the FirmataAdaptors port
func (f *FirmataAdaptor) Port() string { return f.port }
// Port returns the Firmata Adaptors port
func (f *Adaptor) Port() string { return f.port }
// Name returns the FirmataAdaptors name
func (f *FirmataAdaptor) Name() string { return f.name }
// Name returns the Firmata Adaptors 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
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)
if err != nil {
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.
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)
if err != nil {
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
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)
if err != nil {
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.
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)
if err != nil {
return
@ -170,7 +172,7 @@ func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) {
// DigitalRead retrieves digital value from specified pin.
// 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)
if err != nil {
return
@ -191,7 +193,7 @@ func (f *FirmataAdaptor) DigitalRead(pin string) (val int, err error) {
// AnalogRead retrieves value from analog pin.
// 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)
if err != nil {
return
@ -214,18 +216,18 @@ func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) {
}
// digitalPin converts pin number to digital mapping
func (f *FirmataAdaptor) digitalPin(pin int) int {
func (f *Adaptor) digitalPin(pin int) int {
return pin + 14
}
// 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)
}
// I2cRead returns size bytes from the i2c device
// 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)
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
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)
}

View File

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