Skip to content

Commit

Permalink
Merge pull request hybridgroup#158 from hybridgroup/gpio_interfaces
Browse files Browse the repository at this point in the history
Gpio and I2c interfaces
  • Loading branch information
deadprogram committed Nov 29, 2014
2 parents 91a0123 + 560ebaf commit d171342
Show file tree
Hide file tree
Showing 34 changed files with 278 additions and 394 deletions.
20 changes: 10 additions & 10 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ import (
"strings"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/sysfs"
)

var _ gobot.Adaptor = (*BeagleboneAdaptor)(nil)

var _ gpio.DigitalReader = (*BeagleboneAdaptor)(nil)
var _ gpio.DigitalWriter = (*BeagleboneAdaptor)(nil)
var _ gpio.AnalogReader = (*BeagleboneAdaptor)(nil)
var _ gpio.PwmWriter = (*BeagleboneAdaptor)(nil)
var _ gpio.ServoWriter = (*BeagleboneAdaptor)(nil)

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

var slots = "/sys/devices/bone_capemgr.*"
var ocp = "/sys/devices/ocp.*"
var usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
Expand Down Expand Up @@ -190,11 +200,6 @@ func (b *BeagleboneAdaptor) PwmWrite(pin string, val byte) (err error) {
return b.pwmWrite(pin, val)
}

// InitServo starts servo (not yet implemented)
func (b *BeagleboneAdaptor) InitServo() (err error) {
return errors.New("InitServo is not yet implemented")
}

// ServoWrite writes scaled value to servo in specified pin
func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) (err error) {
i, err := b.pwmPin(pin)
Expand Down Expand Up @@ -257,11 +262,6 @@ func (b *BeagleboneAdaptor) AnalogRead(pin string) (val int, err error) {
return
}

// AnalogWrite writes an analog value to specified pin
func (b *BeagleboneAdaptor) AnalogWrite(pin string, val byte) (err error) {
return b.pwmWrite(pin, val)
}

// I2cStart starts a i2c device in specified address
func (b *BeagleboneAdaptor) I2cStart(address byte) (err error) {
b.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
Expand Down
15 changes: 0 additions & 15 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ func TestBeagleboneAdaptor(t *testing.T) {
"156862",
)

a.AnalogWrite("P9_14", 175)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"500000",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"156862",
)

a.ServoWrite("P9_14", 100)
gobot.Assert(
t,
Expand Down Expand Up @@ -117,7 +105,4 @@ func TestBeagleboneAdaptor(t *testing.T) {
gobot.Assert(t, data, []byte{0x00, 0x01})

gobot.Assert(t, len(a.Finalize()), 0)

gobot.Assert(t, a.InitServo(), errors.New("InitServo is not yet implemented"))

}
16 changes: 5 additions & 11 deletions platforms/digispark/digispark_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"strconv"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
)

var _ gobot.Adaptor = (*DigisparkAdaptor)(nil)

var _ gpio.DigitalWriter = (*DigisparkAdaptor)(nil)
var _ gpio.PwmWriter = (*DigisparkAdaptor)(nil)
var _ gpio.ServoWriter = (*DigisparkAdaptor)(nil)

type DigisparkAdaptor struct {
name string
littleWire lw
Expand Down Expand Up @@ -61,12 +66,6 @@ func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
return
}

// DigitalRead (not yet implemented)
func (d *DigisparkAdaptor) DigitalRead(pin string) (val int, err error) {
err = errors.New("DigitalRead is not yet implemented")
return
}

// PwmWrite updates pwm pin with sent value
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
if d.pwm == false {
Expand All @@ -84,11 +83,6 @@ func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
return
}

// InitServo (not yet implemented)
func (d *DigisparkAdaptor) InitServo() (err error) {
return errors.New("InitServo is not yet implemented")
}

// ServoWrite updates servo location with specified angle
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) {
if d.servo == false {
Expand Down
2 changes: 0 additions & 2 deletions platforms/digispark/digispark_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ func TestDigisparkAdaptorFinalize(t *testing.T) {

func TestDigisparkAdaptorIO(t *testing.T) {
a := initTestDigisparkAdaptor()
a.InitServo()
a.DigitalRead("1")
a.DigitalWrite("0", uint8(1))
gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))
Expand Down
21 changes: 10 additions & 11 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package firmata

import (
"errors"
"fmt"
"io"
"strconv"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/tarm/goserial"
)

var _ gobot.Adaptor = (*FirmataAdaptor)(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 _ i2c.I2c = (*FirmataAdaptor)(nil)

type FirmataAdaptor struct {
name string
port string
Expand Down Expand Up @@ -88,11 +97,6 @@ func (f *FirmataAdaptor) Finalize() (errs []error) {
func (f *FirmataAdaptor) Port() string { return f.port }
func (f *FirmataAdaptor) Name() string { return f.name }

// InitServo (not yet implemented)
func (f *FirmataAdaptor) InitServo() (err error) {
return errors.New("InitServo is not yet implemented")
}

// ServoWrite sets angle form 0 to 360 to specified servo pin
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) {
p, err := strconv.Atoi(pin)
Expand Down Expand Up @@ -205,11 +209,6 @@ func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) {
return -1, nil
}

// AnalogWrite writes value to ananlog pin
func (f *FirmataAdaptor) AnalogWrite(pin string, level byte) (err error) {
return f.PwmWrite(pin, level)
}

// digitalPin converts pin number to digital mapping
func (f *FirmataAdaptor) digitalPin(pin int) int {
return pin + 14
Expand Down
10 changes: 1 addition & 9 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ func TestFirmataAdaptorConnect(t *testing.T) {
gobot.Assert(t, a.connect(a), nil)
}

func TestFirmataAdaptorInitServo(t *testing.T) {
a := initTestFirmataAdaptor()
a.InitServo()
}

func TestFirmataAdaptorServoWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.ServoWrite("1", 50)
Expand Down Expand Up @@ -106,10 +101,7 @@ func TestFirmataAdaptorAnalogRead(t *testing.T) {
val, _ = a.AnalogRead(pinNumber)
gobot.Assert(t, val, 133)
}
func TestFirmataAdaptorAnalogWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.AnalogWrite("1", 50)
}

func TestFirmataAdaptorI2cStart(t *testing.T) {
a := initTestFirmataAdaptor()
a.I2cStart(0x00)
Expand Down
21 changes: 9 additions & 12 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type AnalogSensorDriver struct {
name string
pin string
interval time.Duration
connection gobot.Connection
connection AnalogReader
gobot.Eventer
gobot.Commander
}
Expand All @@ -25,7 +25,7 @@ type AnalogSensorDriver struct {
func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *AnalogSensorDriver {
d := &AnalogSensorDriver{
name: name,
connection: a.(gobot.Connection),
connection: a,
pin: pin,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
Expand All @@ -36,8 +36,9 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Du
d.interval = v[0]
}

d.AddEvent("data")
d.AddEvent("error")
d.AddEvent(Data)
d.AddEvent(Error)

d.AddCommand("Read", func(params map[string]interface{}) interface{} {
val, err := d.Read()
return map[string]interface{}{"val": val, "err": err}
Expand All @@ -46,10 +47,6 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Du
return d
}

func (a *AnalogSensorDriver) conn() AnalogReader {
return a.Connection().(AnalogReader)
}

// Starts the AnalogSensorDriver and reads the Analog Sensor at the given Driver.Interval().
// Returns true on successful start of the driver.
// Emits the Events:
Expand All @@ -60,10 +57,10 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
for {
newValue, err := a.Read()
if err != nil {
gobot.Publish(a.Event("error"), err)
gobot.Publish(a.Event(Error), err)
} else if newValue != value && newValue != -1 {
value = newValue
gobot.Publish(a.Event("data"), value)
gobot.Publish(a.Event(Data), value)
}
<-time.After(a.interval)
}
Expand All @@ -75,9 +72,9 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
func (a *AnalogSensorDriver) Halt() (errs []error) { return }
func (a *AnalogSensorDriver) Name() string { return a.name }
func (a *AnalogSensorDriver) Pin() string { return a.pin }
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection }
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) }

// Read returns the current reading from the Analog Sensor
func (a *AnalogSensorDriver) Read() (val int, err error) {
return a.conn().AnalogRead(a.Pin())
return a.connection.AnalogRead(a.Pin())
}
28 changes: 10 additions & 18 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ type ButtonDriver struct {
pin string
name string
interval time.Duration
connection gobot.Connection
connection DigitalReader
gobot.Eventer
}

// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duration) *ButtonDriver {
b := &ButtonDriver{
name: name,
connection: a.(gobot.Connection),
connection: a,
pin: pin,
Active: false,
Eventer: gobot.NewEventer(),
Expand All @@ -33,17 +33,13 @@ func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duratio
b.interval = v[0]
}

b.AddEvent("push")
b.AddEvent("release")
b.AddEvent("error")
b.AddEvent(Push)
b.AddEvent(Release)
b.AddEvent(Error)

return b
}

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

// Starts the ButtonDriver and reads the state of the button at the given Driver.Interval().
// Returns true on successful start of the driver.
//
Expand All @@ -55,9 +51,9 @@ func (b *ButtonDriver) Start() (errs []error) {
state := 0
go func() {
for {
newValue, err := b.readState()
newValue, err := b.connection.DigitalRead(b.Pin())
if err != nil {
gobot.Publish(b.Event("error"), err)
gobot.Publish(b.Event(Error), err)
} else if newValue != state && newValue != -1 {
state = newValue
b.update(newValue)
Expand All @@ -73,18 +69,14 @@ func (b *ButtonDriver) Halt() (errs []error) { return }

func (b *ButtonDriver) Name() string { return b.name }
func (b *ButtonDriver) Pin() string { return b.pin }
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection }

func (b *ButtonDriver) readState() (val int, err error) {
return b.adaptor().DigitalRead(b.Pin())
}
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }

func (b *ButtonDriver) update(newValue int) {
if newValue == 1 {
b.Active = true
gobot.Publish(b.Event("push"), newValue)
gobot.Publish(b.Event(Push), newValue)
} else {
b.Active = false
gobot.Publish(b.Event("release"), newValue)
gobot.Publish(b.Event(Release), newValue)
}
}
6 changes: 0 additions & 6 deletions platforms/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ func TestButtonDriverHalt(t *testing.T) {
gobot.Assert(t, len(d.Halt()), 0)
}

func TestButtonDriverReadState(t *testing.T) {
d := initTestButtonDriver()
val, _ := d.readState()
gobot.Assert(t, val, 1)
}

func TestButtonDriverActive(t *testing.T) {
d := initTestButtonDriver()
d.update(1)
Expand Down
Loading

0 comments on commit d171342

Please sign in to comment.