Skip to content

Commit

Permalink
go lint and documentation tweaks for the firmata package
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jan 2, 2015
1 parent d073a1c commit de71de8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions platforms/firmata/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Package firmata provides the Gobot adaptor for microcontrollers that support the
Installing:
go get github.com/hybridgroup/gobot/platforms/firmata
go get -d -u github.com/hybridgroup/gobot/... && go get github.com/hybridgroup/gobot/platforms/firmata
## Example
Example:
package main
Expand Down
5 changes: 2 additions & 3 deletions platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package firmata

import (
"bytes"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -51,7 +50,7 @@ const (
i2CModeStopReading byte = 0x03
)

var defaultInitTimeInterval time.Duration = 1 * time.Second
var defaultInitTimeInterval = 1 * time.Second

type board struct {
serial io.ReadWriteCloser
Expand Down Expand Up @@ -439,7 +438,7 @@ func (b *board) process(data []byte) (err error) {
str := currentBuffer[2:len(currentBuffer)]
gobot.Publish(b.events["string_data"], string(str[:len(str)]))
default:
return errors.New(fmt.Sprintf("bad byte: 0x%x", command))
return fmt.Errorf("bad byte: 0x%x", command)
}
}
}
Expand Down
40 changes: 22 additions & 18 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var _ gpio.ServoWriter = (*FirmataAdaptor)(nil)

var _ i2c.I2c = (*FirmataAdaptor)(nil)

// FirmataAdaptor is the Gobot Adaptor for Firmata based boards
type FirmataAdaptor struct {
name string
port string
Expand All @@ -32,7 +33,7 @@ type FirmataAdaptor struct {
connect func(string) (io.ReadWriteCloser, error)
}

// NewFirmataAdaptor returns a new firmata adaptor with specified name and optionally accepts:
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and 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
Expand Down Expand Up @@ -63,41 +64,43 @@ func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
return f
}

// Connect returns true if connection to board is succesfull
// Connect starts a connection to the board.
func (f *FirmataAdaptor) Connect() (errs []error) {
if f.conn == nil {
if sp, err := f.connect(f.Port()); err != nil {
sp, err := f.connect(f.Port())
if err != nil {
return []error{err}
} else {
f.conn = sp
}
f.conn = sp
}
f.board = newBoard(f.conn)
f.board.connect()
return
}

// close finishes connection to serial port
// Prints error message on error
// Disconnect closes the io connection to the board
func (f *FirmataAdaptor) Disconnect() (err error) {
if f.board != nil {
return f.board.serial.Close()
}
return errors.New("no board connected")
}

// Finalize disconnects firmata adaptor
// Finalize terminates the firmata connection
func (f *FirmataAdaptor) 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 }

// Name returns the FirmataAdaptors name
func (f *FirmataAdaptor) Name() string { return f.name }

// ServoWrite sets angle form 0 to 360 to specified servo pin
// ServoWrite writes the 0-180 degree angle to the specified pin.
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
Expand All @@ -112,7 +115,7 @@ func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) {
return
}

// PwmWrite writes analog value to specified pin
// PwmWrite writes the 0-254 value to the specified pin
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
Expand All @@ -127,7 +130,7 @@ func (f *FirmataAdaptor) PwmWrite(pin string, level byte) (err error) {
return
}

// DigitalWrite writes digital values to specified pin
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
Expand All @@ -143,8 +146,8 @@ func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) {
return
}

// DigitalRead retrieves digital value from specified pin
// Returns -1 if response from board is timed out
// 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) {
ret := make(chan int)

Expand Down Expand Up @@ -175,14 +178,15 @@ func (f *FirmataAdaptor) DigitalRead(pin string) (val int, err error) {
}

// AnalogRead retrieves value from analog pin.
// NOTE pins are numbered A0-A5, which translate to digital pins 14-19
// Returns -1 if the response from the board has timed out
func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) {
ret := make(chan int)

p, err := strconv.Atoi(pin)
if err != nil {
return
}
// NOTE pins are numbered A0-A5, which translate to digital pins 14-19
p = f.digitalPin(p)
if err = f.board.setPinMode(byte(p), analog); err != nil {
return
Expand Down Expand Up @@ -214,14 +218,14 @@ func (f *FirmataAdaptor) digitalPin(pin int) int {
return pin + 14
}

// I2cStart initializes board with i2c configuration
// I2cStart starts an i2c device at specified address
func (f *FirmataAdaptor) I2cStart(address byte) (err error) {
f.i2cAddress = address
return f.board.i2cConfig([]byte{0})
}

// I2cRead reads from I2c specified size
// Returns empty byte array if response is timed out
// 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(size uint) (data []byte, err error) {
ret := make(chan []byte)
if err = f.board.i2cReadRequest(f.i2cAddress, size); err != nil {
Expand All @@ -244,7 +248,7 @@ func (f *FirmataAdaptor) I2cRead(size uint) (data []byte, err error) {
return []byte{}, nil
}

// I2cWrite retrieves i2c data
// I2cWrite writes data to i2c device
func (f *FirmataAdaptor) I2cWrite(data []byte) (err error) {
return f.board.i2cWriteRequest(f.i2cAddress, data)
}
6 changes: 3 additions & 3 deletions platforms/firmata/firmata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}

var closeErr error = nil
var closeErr error

func (NullReadWriteCloser) Close() error {
return closeErr
Expand Down Expand Up @@ -150,11 +150,11 @@ func TestProcess(t *testing.T) {
}
//i2cReply
gobot.Once(b.events["i2c_reply"], func(data interface{}) {
i2c_reply := map[string][]byte{
i2cReply := map[string][]byte{
"slave_address": []byte{9},
"register": []byte{0},
"data": []byte{152, 1, 154}}
gobot.Assert(t, data.(map[string][]byte), i2c_reply)
gobot.Assert(t, data.(map[string][]byte), i2cReply)
sem <- true
})
b.process([]byte{240, 119, 9, 0, 0, 0, 24, 1, 1, 0, 26, 1, 247})
Expand Down

0 comments on commit de71de8

Please sign in to comment.