Skip to content

Commit

Permalink
Update gpio platform and driver interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 4, 2014
1 parent 958d194 commit a6fdedc
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 132 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PACKAGES := "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/api" "github.com/hybridgroup/gobot/platforms/ardrone" "github.com/hybridgroup/gobot/platforms/beaglebone" "github.com/hybridgroup/gobot/platforms/digispark" "github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/platforms/i2c" "github.com/hybridgroup/gobot/platforms/leap" "github.com/hybridgroup/gobot/platforms/neurosky" "github.com/hybridgroup/gobot/platforms/pebble" "github.com/hybridgroup/gobot/platforms/spark" "github.com/hybridgroup/gobot/platforms/sphero" "github.com/hybridgroup/gobot/platforms/opencv" "github.com/hybridgroup/gobot/platforms/joystick"
PACKAGES := gobot gobot/api $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')

test:
for package in $(PACKAGES) ; do \
go test $$package ; \
go test github.com/hybridgroup/$$package ; \
done ; \

cover:
echo "mode: count" > profile.cov ; \
for package in $(PACKAGES) ; do \
go test -covermode=count -coverprofile=tmp.cov $$package ; \
go test -covermode=count -coverprofile=tmp.cov github.com/hybridgroup/$$package ; \
cat tmp.cov | grep -v "mode: count" >> profile.cov ; \
done ; \
rm tmp.cov ; \
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (a *api) executeCommand(res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
body := make(map[string]interface{})
json.Unmarshal(data, &body)
f := a.gobot.Commands()[command]
f := a.gobot.Command(command)

if f != nil {
data, _ = json.Marshal(f(body))
Expand All @@ -235,7 +235,7 @@ func (a *api) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
json.Unmarshal(data, &body)
d := a.gobot.Robot(robot).Device(device)
body["robot"] = robot
f := d.Commands()[command]
f := d.Command(command)

if f != nil {
data, _ = json.Marshal(f(body))
Expand All @@ -256,7 +256,7 @@ func (a *api) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
json.Unmarshal(data, &body)
r := a.gobot.Robot(robot)
body["robot"] = robot
f := r.Commands[command]
f := r.Command(command)

if f != nil {
data, _ = json.Marshal(f(body))
Expand Down
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (d devices) Start() error {
var err error
log.Println("Starting devices...")
for _, device := range d.devices {
log.Println("Starting device " + device.name() + "...")
log.Println("Starting device " + device.Name() + "...")
if device.Start() == false {
err = errors.New("Could not start device")
break
Expand Down
145 changes: 100 additions & 45 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,138 @@ import (
"time"
)

type Driver struct {
Adaptor AdaptorInterface
Interval time.Duration
Pin string
Name string
commands map[string]func(map[string]interface{}) interface{}
Events map[string]*Event
Type string
}

type DriverInterface interface {
Start() bool
Halt() bool
adaptor() AdaptorInterface
setInterval(time.Duration)
interval() time.Duration
setName(string)
name() string
Adaptor() AdaptorInterface
SetInterval(time.Duration)
Interval() time.Duration
SetName(string)
Name() string
Pin() string
SetPin(string)
Command(string) func(map[string]interface{}) interface{}
Commands() map[string]func(map[string]interface{}) interface{}
AddCommand(string, func(map[string]interface{}) interface{})
Events() map[string]*Event
Event(string) *Event
AddEvent(string)
Type() string
ToJSON() *JSONDevice
}

func (d *Driver) adaptor() AdaptorInterface {
return d.Adaptor
type Driver struct {
adaptor AdaptorInterface
interval time.Duration
pin string
name string
commands map[string]func(map[string]interface{}) interface{}
events map[string]*Event
driverType string
}

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)))
}

for i := range v {
switch v[i].(type) {
case string:
pin = v[i].(string)
case AdaptorInterface:
adaptor = v[i].(AdaptorInterface)
case time.Duration:
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,
}
}

func (d *Driver) Adaptor() AdaptorInterface {
return d.adaptor
}

func (d *Driver) SetInterval(t time.Duration) {
d.interval = t
}

func (d *Driver) setInterval(t time.Duration) {
d.Interval = t
func (d *Driver) Interval() time.Duration {
return d.interval
}

func (d *Driver) interval() time.Duration {
return d.Interval
func (d *Driver) SetName(s string) {
d.name = s
}

func (d *Driver) setName(s string) {
d.Name = s
func (d *Driver) Name() string {
return d.name
}

func (d *Driver) name() string {
return d.Name
func (d *Driver) Pin() string {
return d.pin
}

func (d *Driver) SetPin(pin string) {
d.pin = pin
}

func (d *Driver) Type() string {
return d.driverType
}

func (d *Driver) Events() map[string]*Event {
return d.events
}

func (d *Driver) Event(name string) *Event {
return d.events[name]
}

func (d *Driver) AddEvent(name string) {
d.events[name] = NewEvent()
}

func (d *Driver) Command(name string) func(map[string]interface{}) interface{} {
return d.commands[name]
}

func (d *Driver) Commands() map[string]func(map[string]interface{}) interface{} {
return d.commands
}

func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) {
d.Commands()[name] = f
}

func NewDriver(name string, t string, a AdaptorInterface) *Driver {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
return &Driver{
Type: t,
Name: name,
Interval: 10 * time.Millisecond,
commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a,
}
d.commands[name] = f
}

func (d *Driver) ToJSON() *JSONDevice {
jsonDevice := &JSONDevice{
Name: d.Name,
Driver: d.Type,
Name: d.Name(),
Driver: d.Type(),
Commands: []string{},
Connection: nil,
}

if d.adaptor() != nil {
jsonDevice.Connection = d.adaptor().ToJSON()
if d.Adaptor() != nil {
jsonDevice.Connection = d.Adaptor().ToJSON()
}

commands := d.Commands()
for command := range commands {
for command := range d.Commands() {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}

Expand Down
8 changes: 8 additions & 0 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ func NewGobot() *Gobot {
}
}

func (g *Gobot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
g.commands[name] = f
}

func (g *Gobot) Commands() map[string]func(map[string]interface{}) interface{} {
return g.commands
}

func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
return g.commands[name]
}

func (g *Gobot) Start() {
g.robots.Start()

Expand Down
2 changes: 1 addition & 1 deletion gobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestGobotRobot(t *testing.T) {
Expect(t, g.Robot("Robot 1").Name, "Robot 1")
Expect(t, g.Robot("Robot 4"), (*Robot)(nil))
Expect(t, g.Robot("Robot 1").Device("Device 4"), (DriverInterface)(nil))
Expect(t, g.Robot("Robot 1").Device("Device 1").name(), "Device 1")
Expect(t, g.Robot("Robot 1").Device("Device 1").Name(), "Device 1")
Expect(t, g.Robot("Robot 1").Devices().Len(), 3)
Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (AdaptorInterface)(nil))
Expect(t, g.Robot("Robot 1").Connections().Len(), 3)
Expand Down
17 changes: 10 additions & 7 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ type AnalogSensorDriver struct {

func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
d := &AnalogSensorDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Adaptor: a.(gobot.AdaptorInterface),
},
Driver: *gobot.NewDriver(
name,
"AnalogSensorDriver",
a.(gobot.AdaptorInterface),
pin,
),
}

d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
return d.Read()
})

return d
}

func (a *AnalogSensorDriver) adaptor() AnalogReader {
return a.Driver.Adaptor.(AnalogReader)
return a.Driver.Adaptor().(AnalogReader)
}

func (a *AnalogSensorDriver) Start() bool { return true }
func (a *AnalogSensorDriver) Init() bool { return true }
func (a *AnalogSensorDriver) Halt() bool { return true }

func (a *AnalogSensorDriver) Read() int {
return a.adaptor().AnalogRead(a.Pin)
return a.adaptor().AnalogRead(a.Pin())
}
32 changes: 17 additions & 15 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ type ButtonDriver struct {
}

func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
return &ButtonDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Events: map[string]*gobot.Event{
"push": gobot.NewEvent(),
"release": gobot.NewEvent(),
},
Adaptor: a.(gobot.AdaptorInterface),
},
b := &ButtonDriver{
Driver: *gobot.NewDriver(
name,
"ButtonDriver",
a.(gobot.AdaptorInterface),
pin,
),
Active: false,
}

b.Driver.AddEvent("push")
b.Driver.AddEvent("release")

return b
}

func (b *ButtonDriver) adaptor() DigitalReader {
return b.Driver.Adaptor.(DigitalReader)
return b.Driver.Adaptor().(DigitalReader)
}

func (b *ButtonDriver) Start() bool {
state := 0
gobot.Every(b.Interval, func() {
gobot.Every(b.Interval(), func() {
newValue := b.readState()
if newValue != state && newValue != -1 {
state = newValue
Expand All @@ -43,15 +45,15 @@ func (b *ButtonDriver) Halt() bool { return true }
func (b *ButtonDriver) Init() bool { return true }

func (b *ButtonDriver) readState() int {
return b.adaptor().DigitalRead(b.Pin)
return b.adaptor().DigitalRead(b.Pin())
}

func (b *ButtonDriver) update(newVal int) {
if newVal == 1 {
b.Active = true
gobot.Publish(b.Events["push"], newVal)
gobot.Publish(b.Event("push"), newVal)
} else {
b.Active = false
gobot.Publish(b.Events["release"], newVal)
gobot.Publish(b.Event("release"), newVal)
}
}
Loading

0 comments on commit a6fdedc

Please sign in to comment.