Skip to content

Commit

Permalink
core: Refactoring to allow 'metal' development using Gobot adaptors/d…
Browse files Browse the repository at this point in the history
…rivers.

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 12, 2016
1 parent e159613 commit c64a0d9
Show file tree
Hide file tree
Showing 24 changed files with 73 additions and 122 deletions.
7 changes: 5 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,13 @@ func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive")

device := a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device"))

if event := a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).(gobot.Eventer).
Event(req.URL.Query().Get(":event")); event != nil {
gobot.On(event, func(data interface{}) {
Event(req.URL.Query().Get(":event")); len(event) > 0 {
device.(gobot.Eventer).On(event, func(data interface{}) {
d, _ := json.Marshal(data)
dataChan <- string(d)
})
Expand Down
3 changes: 2 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ func TestRobotDeviceEvent(t *testing.T) {

go func() {
time.Sleep(time.Millisecond * 5)
gobot.Publish(event, "event-data")
a.gobot.Robot("Robot1").
Device("Device1").(gobot.Eventer).Publish(event, "event-data")
}()

done := false
Expand Down
2 changes: 1 addition & 1 deletion platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (a *ArdroneDriver) Halt() (errs []error) {

// TakeOff makes the drone start flying, and publishes `flying` event
func (a *ArdroneDriver) TakeOff() {
gobot.Publish(a.Event("flying"), a.adaptor().drone.Takeoff())
a.Publish(a.Event("flying"), a.adaptor().drone.Takeoff())
}

// Land causes the drone to land
Expand Down
8 changes: 4 additions & 4 deletions platforms/ble/ble_minidrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (b *BLEMinidroneDriver) Init() (err error) {

// subscribe to battery notifications
b.adaptor().Subscribe(DroneNotificationService, BatteryCharacteristic, func(data []byte, e error) {
gobot.Publish(b.Event(Battery), data[len(data)-1])
b.Publish(b.Event(Battery), data[len(data)-1])
})

// subscribe to flying status notifications
Expand All @@ -127,13 +127,13 @@ func (b *BLEMinidroneDriver) Init() (err error) {
fmt.Println(data)
return
}
gobot.Publish(b.Event(Status), data[6])
b.Publish(b.Event(Status), data[6])
if (data[6] == 1 || data[6] == 2) && !b.flying {
b.flying = true
gobot.Publish(b.Event(Flying), true)
b.Publish(b.Event(Flying), true)
} else if (data[6] == 0) && b.flying {
b.flying = false
gobot.Publish(b.Event(Landed), true)
b.Publish(b.Event(Landed), true)
}
})

Expand Down
2 changes: 1 addition & 1 deletion platforms/ble/ollie.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *SpheroOllieDriver) Start() (errs []error) {
packet := <-s.packetChannel
err := s.write(packet)
if err != nil {
gobot.Publish(s.Event(Error), err)
s.Publish(s.Event(Error), err)
}
}
}()
Expand Down
1 change: 0 additions & 1 deletion platforms/gpio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)

Expand Down
1 change: 0 additions & 1 deletion platforms/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)

Expand Down
10 changes: 5 additions & 5 deletions platforms/gpio/grove_drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func NewGrovePiezoVibrationSensorDriver(a AnalogReader, name string, pin string,

sensor.AddEvent(Vibration)

// sensor.On(sensor.Event(Data), func(data interface{}) {
// if data.(int) > 1000 {
// sensor.Publish(sensor.Event(Vibration), data)
// }
// })
sensor.On(sensor.Event(Data), func(data interface{}) {
if data.(int) > 1000 {
sensor.Publish(sensor.Event(Vibration), data)
}
})

return sensor
}
Expand Down
9 changes: 4 additions & 5 deletions platforms/gpio/makey_button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)

Expand Down Expand Up @@ -42,7 +41,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
return
}

gobot.Once(d.Event(Push), func(data interface{}) {
d.Once(d.Event(Push), func(data interface{}) {
gobottest.Assert(t, d.Active, true)
sem <- true
})
Expand All @@ -58,7 +57,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
return
}

gobot.Once(d.Event(Release), func(data interface{}) {
d.Once(d.Event(Release), func(data interface{}) {
gobottest.Assert(t, d.Active, false)
sem <- true
})
Expand All @@ -74,7 +73,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
return
}

gobot.Once(d.Event(Error), func(data interface{}) {
d.Once(d.Event(Error), func(data interface{}) {
sem <- true
})

Expand All @@ -84,7 +83,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
t.Errorf("MakeyButton Event \"Error\" was not published")
}

gobot.Once(d.Event(Release), func(data interface{}) {
d.Once(d.Event(Release), func(data interface{}) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
Expand Down
10 changes: 5 additions & 5 deletions platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (j *JoystickDriver) Start() (errs []error) {
for {
for event := j.poll(); event != nil; event = j.poll() {
if err = j.handleEvent(event); err != nil {
gobot.Publish(j.Event("error"), err)
j.Publish(j.Event("error"), err)
}
}
select {
Expand Down Expand Up @@ -144,7 +144,7 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
if axis == "" {
return fmt.Errorf("Unknown Axis: %v", data.Axis)
}
gobot.Publish(j.Event(axis), data.Value)
j.Publish(j.Event(axis), data.Value)
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
Expand All @@ -153,17 +153,17 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
return fmt.Errorf("Unknown Button: %v", data.Button)
}
if data.State == 1 {
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
j.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
}
gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
j.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
}
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
return fmt.Errorf("Unknown Hat: %v %v", data.Hat, data.Value)
}
gobot.Publish(j.Event(hat), true)
j.Publish(j.Event(hat), true)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions platforms/joystick/joystick_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestJoystickDriverHandleEvent(t *testing.T) {
d.Start()

// left x stick
gobot.On(d.Event("left_x"), func(data interface{}) {
d.On(d.Event("left_x"), func(data interface{}) {
gobottest.Assert(t, int16(100), data.(int16))
sem <- true
})
Expand All @@ -62,7 +62,7 @@ func TestJoystickDriverHandleEvent(t *testing.T) {
}

// x button press
gobot.On(d.Event("x_press"), func(data interface{}) {
d.On(d.Event("x_press"), func(data interface{}) {
sem <- true
})
d.handleEvent(&sdl.JoyButtonEvent{
Expand All @@ -77,7 +77,7 @@ func TestJoystickDriverHandleEvent(t *testing.T) {
}

// x button release
gobot.On(d.Event("x_release"), func(data interface{}) {
d.On(d.Event("x_release"), func(data interface{}) {
sem <- true
})
d.handleEvent(&sdl.JoyButtonEvent{
Expand All @@ -92,7 +92,7 @@ func TestJoystickDriverHandleEvent(t *testing.T) {
}

// down button press
gobot.On(d.Event("down"), func(data interface{}) {
d.On(d.Event("down"), func(data interface{}) {
sem <- true
})
d.handleEvent(&sdl.JoyHatEvent{
Expand Down
2 changes: 1 addition & 1 deletion platforms/keyboard/keyboard_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewKeyboardDriver(name string) *KeyboardDriver {
break
}

gobot.Publish(k.Event("key"), Parse(keybuf))
k.Publish(k.Event("key"), Parse(keybuf))

}
},
Expand Down
6 changes: 3 additions & 3 deletions platforms/leap/leap_motion_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (l *LeapMotionDriver) Start() (errs []error) {
for {
receive(l.adaptor().ws, &msg)
frame = l.ParseFrame(msg)
gobot.Publish(l.Event("message"), frame)
l.Publish(l.Event("message"), frame)

for _, hand := range frame.Hands {
gobot.Publish(l.Event("hand"), hand)
l.Publish(l.Event("hand"), hand)
}

for _, gesture := range frame.Gestures {
gobot.Publish(l.Event("gesture"), gesture)
l.Publish(l.Event("gesture"), gesture)
}
}
}()
Expand Down
8 changes: 4 additions & 4 deletions platforms/mavlink/mavlink_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ func (m *MavlinkDriver) Start() (errs []error) {
for {
packet, err := common.ReadMAVLinkPacket(m.adaptor().sp)
if err != nil {
gobot.Publish(m.Event("errorIO"), err)
m.Publish(m.Event("errorIO"), err)
continue
}
gobot.Publish(m.Event("packet"), packet)
m.Publish(m.Event("packet"), packet)
message, err := packet.MAVLinkMessage()
if err != nil {
gobot.Publish(m.Event("errorMAVLink"), err)
m.Publish(m.Event("errorMAVLink"), err)
continue
}
gobot.Publish(m.Event("message"), message)
m.Publish(m.Event("message"), message)
<-time.After(m.interval)
}
}()
Expand Down
21 changes: 5 additions & 16 deletions platforms/mavlink/mavlink_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mavlink

import (
"errors"
"io"
"testing"
"time"
Expand Down Expand Up @@ -32,25 +31,24 @@ func TestMavlinkDriver(t *testing.T) {

d = NewMavlinkDriver(m, "myDriver", 100*time.Millisecond)
gobottest.Assert(t, d.interval, 100*time.Millisecond)

}

func TestMavlinkDriverStart(t *testing.T) {
d := initTestMavlinkDriver()
err := make(chan error, 0)
packet := make(chan *common.MAVLinkPacket, 0)
message := make(chan common.MAVLinkMessage, 0)

gobot.Once(d.Event("packet"), func(data interface{}) {
d.Once(d.Event("packet"), func(data interface{}) {
packet <- data.(*common.MAVLinkPacket)
})

gobot.Once(d.Event("message"), func(data interface{}) {
d.Once(d.Event("message"), func(data interface{}) {
message <- data.(common.MAVLinkMessage)
})
gobot.Once(d.Event("errorIO"), func(data interface{}) {
d.Once(d.Event("errorIO"), func(data interface{}) {
err <- data.(error)
})
gobot.Once(d.Event("errorMAVLink"), func(data interface{}) {
d.Once(d.Event("errorMAVLink"), func(data interface{}) {
err <- data.(error)
})

Expand All @@ -73,15 +71,6 @@ func TestMavlinkDriverStart(t *testing.T) {
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}

payload = []byte{0xFE, 0x09, 0x4E, 0x01, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
select {
case e := <-err:
gobottest.Assert(t, e, errors.New("Unknown Message ID: 255"))
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}

}

func TestMavlinkDriverHalt(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions platforms/neurosky/neurosky_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (n *NeuroskyDriver) Start() (errs []error) {
buff := make([]byte, 1024)
_, err := n.adaptor().sp.Read(buff[:])
if err != nil {
gobot.Publish(n.Event("error"), err)
n.Publish(n.Event("error"), err)
} else {
n.parse(bytes.NewBuffer(buff))
}
Expand Down Expand Up @@ -124,29 +124,29 @@ func (n *NeuroskyDriver) parsePacket(buf *bytes.Buffer) {
b, _ := buf.ReadByte()
switch b {
case CodeEx:
gobot.Publish(n.Event("extended"), nil)
n.Publish(n.Event("extended"), nil)
case CodeSignalQuality:
ret, _ := buf.ReadByte()
gobot.Publish(n.Event("signal"), ret)
n.Publish(n.Event("signal"), ret)
case CodeAttention:
ret, _ := buf.ReadByte()
gobot.Publish(n.Event("attention"), ret)
n.Publish(n.Event("attention"), ret)
case CodeMeditation:
ret, _ := buf.ReadByte()
gobot.Publish(n.Event("meditation"), ret)
n.Publish(n.Event("meditation"), ret)
case CodeBlink:
ret, _ := buf.ReadByte()
gobot.Publish(n.Event("blink"), ret)
n.Publish(n.Event("blink"), ret)
case CodeWave:
buf.Next(1)
var ret = make([]byte, 2)
buf.Read(ret)
gobot.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
n.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
case CodeAsicEEG:
ret := make([]byte, 25)
i, _ := buf.Read(ret)
if i == 25 {
gobot.Publish(n.Event("eeg"), n.parseEEG(ret))
n.Publish(n.Event("eeg"), n.parseEEG(ret))
}
}
}
Expand Down
Loading

0 comments on commit c64a0d9

Please sign in to comment.