Skip to content

Commit

Permalink
Update firmata platform and adaptor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 4, 2014
1 parent a6fdedc commit d8154ce
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 81 deletions.
92 changes: 61 additions & 31 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,84 @@ package gobot
import "fmt"

type Adaptor struct {
Name string
Port string
Connected bool
Params map[string]interface{}
Type string
name string
port string
connected bool
params map[string]interface{}
adaptorType string
}

type AdaptorInterface interface {
Finalize() bool
Connect() bool
port() string
name() string
setName(string)
params() map[string]interface{}
Port() string
Name() string
Type() string
Connected() bool
SetConnected(bool)
SetName(string)
Params() map[string]interface{}
ToJSON() *JSONConnection
}

func (a *Adaptor) port() string {
return a.Port
func NewAdaptor(name string, adaptorType string, v ...interface{}) *Adaptor {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}

a := &Adaptor{
adaptorType: adaptorType,
name: name,
port: "",
params: make(map[string]interface{}),
}

for i := range v {
switch v[i].(type) {
case string:
a.port = v[i].(string)
case map[string]interface{}:
a.params = v[i].(map[string]interface{})
default:
fmt.Println("Unknown argument passed to NewAdaptor")
}
}

return a
}

func (a *Adaptor) name() string {
return a.Name
func (a *Adaptor) Port() string {
return a.port
}

func (a *Adaptor) setName(s string) {
a.Name = s
func (a *Adaptor) Name() string {
return a.name
}

func (a *Adaptor) params() map[string]interface{} {
return a.Params
func (a *Adaptor) SetName(s string) {
a.name = s
}

func (a *Adaptor) ToJSON() *JSONConnection {
return &JSONConnection{
Name: a.Name,
Port: a.Port,
Adaptor: a.Type,
}
func (a *Adaptor) Type() string {
return a.adaptorType
}

func NewAdaptor(name, port, t string) *Adaptor {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
return &Adaptor{
Type: t,
Name: name,
Port: port,
func (a *Adaptor) Connected() bool {
return a.connected
}

func (a *Adaptor) SetConnected(b bool) {
a.connected = b
}

func (a *Adaptor) Params() map[string]interface{} {
return a.params
}

func (a *Adaptor) ToJSON() *JSONConnection {
return &JSONConnection{
Name: a.Name(),
Port: a.Port(),
Adaptor: a.Type(),
}
}
6 changes: 5 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func (c connections) Start() error {
var err error
log.Println("Starting connections...")
for _, connection := range c.connections {
log.Println("Starting connection " + connection.name() + "...")
info := "Starting connection " + connection.Name()
if connection.Port() != "" {
info = info + " on Port " + connection.Port()
}
log.Println(info + "...")
if connection.Connect() == false {
err = errors.New("Could not start connection")
break
Expand Down
30 changes: 14 additions & 16 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,34 @@ type Driver struct {
}

func NewDriver(name string, driverType string, v ...interface{}) *Driver {
interval := 10 * time.Millisecond
pin := ""
var adaptor AdaptorInterface

if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}

d := &Driver{
driverType: driverType,
name: name,
interval: 10 * time.Millisecond,
commands: make(map[string]func(map[string]interface{}) interface{}),
events: make(map[string]*Event),
adaptor: nil,
pin: "",
}

for i := range v {
switch v[i].(type) {
case string:
pin = v[i].(string)
d.pin = v[i].(string)
case AdaptorInterface:
adaptor = v[i].(AdaptorInterface)
d.adaptor = v[i].(AdaptorInterface)
case time.Duration:
interval = v[i].(time.Duration)
d.interval = v[i].(time.Duration)
default:
fmt.Println("Unknown argument passed to NewDriver")
}
}

return &Driver{
driverType: driverType,
name: name,
interval: interval,
commands: make(map[string]func(map[string]interface{}) interface{}),
events: make(map[string]*Event),
adaptor: adaptor,
pin: pin,
}
return d
}

func (d *Driver) Adaptor() AdaptorInterface {
Expand Down
6 changes: 3 additions & 3 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func NewFirmataAdaptor(name, port string) *FirmataAdaptor {
return &FirmataAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
port,
"FirmataAdaptor",
port,
),
connect: func(f *FirmataAdaptor) {
sp, err := serial.OpenPort(&serial.Config{Name: f.Port, Baud: 57600})
sp, err := serial.OpenPort(&serial.Config{Name: f.Port(), Baud: 57600})
if err != nil {
panic(err)
}
Expand All @@ -34,7 +34,7 @@ func NewFirmataAdaptor(name, port string) *FirmataAdaptor {
func (f *FirmataAdaptor) Connect() bool {
f.connect(f)
f.Board.connect()
f.Connected = true
f.SetConnected(true)
return true
}

Expand Down
7 changes: 4 additions & 3 deletions platforms/gpio/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func (t *gpioTestAdaptor) Finalize() bool { return true }

func newGpioTestAdaptor(name string) *gpioTestAdaptor {
return &gpioTestAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
},
Adaptor: *gobot.NewAdaptor(
name,
"/dev/null",
),
}
}
48 changes: 23 additions & 25 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,38 @@ func NewRobot(name string, v ...interface{}) *Robot {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}

r := &Robot{
Name: name,
commands: make(map[string]func(map[string]interface{}) interface{}),
connections: &connections{},
devices: &devices{},
Work: nil,
}

log.Println("Initializing Robot", r.Name, "...")
if len(v) > 0 {
if v[0] == nil {
v[0] = connections{}
}
log.Println("Initializing connections...")
for _, connection := range v[0].([]Connection) {
c := r.Connections().Add(connection)
log.Println("Initializing connection", c.name(), "...")
}
}
if len(v) > 1 {
if v[1] == nil {
v[1] = devices{}
}
log.Println("Initializing devices...")
for _, device := range v[1].([]Device) {
d := r.Devices().Add(device)
log.Println("Initializing device", d.Name(), "...")
}
}
if len(v) > 2 {
if v[2] == nil {
v[2] = func() {}

for i := range v {
switch v[i].(type) {
case []Connection:
log.Println("Initializing connections...")
for _, connection := range v[i].([]Connection) {
c := r.Connections().Add(connection)
log.Println("Initializing connection", c.Name(), "...")
}
case []Device:
log.Println("Initializing devices...")
for _, device := range v[i].([]Device) {
d := r.Devices().Add(device)
log.Println("Initializing device", d.Name(), "...")
}
case func():
r.Work = v[i].(func())
default:
fmt.Println("Unknown argument passed to NewRobot")
}
r.Work = v[2].(func())
}

return r
}

Expand Down Expand Up @@ -136,7 +134,7 @@ func (r *Robot) Connection(name string) Connection {
return nil
}
for _, connection := range r.connections.connections {
if connection.name() == name {
if connection.Name() == name {
return connection
}
}
Expand Down
4 changes: 2 additions & 2 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (t *testAdaptor) Connect() bool { return true }
func NewTestAdaptor(name string) *testAdaptor {
return &testAdaptor{
Adaptor: Adaptor{
Name: name,
Params: map[string]interface{}{
name: name,
params: map[string]interface{}{
"param1": "1",
"param2": 2,
},
Expand Down

0 comments on commit d8154ce

Please sign in to comment.