Skip to content

Commit

Permalink
i2c: implemented DigitalRead, DigitalWrite, and AnalogRead for GrovePi
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 14, 2018
1 parent c9276f4 commit b8d26f0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
33 changes: 23 additions & 10 deletions drivers/i2c/grovepi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i2c

import (
"strconv"
"strings"
"time"

"gobot.io/x/gobot"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (d *GrovePiDriver) Halt() (err error) { return }

// AnalogRead returns value from analog pin implementing the AnalogReader interface.
func (d *GrovePiDriver) AnalogRead(pin string) (value int, err error) {
// TODO: strip off the leading "A"
pin = getPin(pin)

var pinNum int
pinNum, err = strconv.Atoi(pin)
Expand All @@ -94,13 +95,13 @@ func (d *GrovePiDriver) AnalogRead(pin string) (value int, err error) {
d.analogPins[pinNum] = "input"
}

value, err = d.ReadAnalog(byte(pinNum))
value, err = d.readAnalog(byte(pinNum))

return
}

// ReadAnalog reads analog value from the GrovePi
func (d *GrovePiDriver) ReadAnalog(pin byte) (int, error) {
func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {
b := []byte{1, CommandReadAnalog, pin, 0, 0}
_, err := d.connection.Write(b)
if err != nil {
Expand All @@ -125,7 +126,7 @@ func (d *GrovePiDriver) ReadAnalog(pin byte) (int, error) {
}

// ReadDigital reads digitally to the GrovePi
func (d *GrovePiDriver) ReadDigital(pin byte) (val int, err error) {
func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {
buf := []byte{1, CommandReadDigital, pin, 0, 0}
_, err = d.connection.Write(buf)
if err != nil {
Expand All @@ -145,7 +146,8 @@ func (d *GrovePiDriver) ReadDigital(pin byte) (val int, err error) {

// DigitalRead performs a read on a digital pin.
func (d *GrovePiDriver) DigitalRead(pin string) (val int, err error) {
// TODO: strip off the leading "D"
pin = getPin(pin)

var pinNum int
pinNum, err = strconv.Atoi(pin)
if err != nil {
Expand All @@ -157,13 +159,13 @@ func (d *GrovePiDriver) DigitalRead(pin string) (val int, err error) {
d.digitalPins[pinNum] = "input"
}

val, err = d.ReadDigital(byte(pinNum))
val, err = d.readDigital(byte(pinNum))

return
}

// WriteDigital writes digitally to the GrovePi
func (d *GrovePiDriver) WriteDigital(pin byte, val byte) error {
func (d *GrovePiDriver) writeDigital(pin byte, val byte) error {
buf := []byte{1, CommandWriteDigital, pin, val, 0}
_, err := d.connection.Write(buf)
time.Sleep(100 * time.Millisecond)
Expand All @@ -172,7 +174,8 @@ func (d *GrovePiDriver) WriteDigital(pin byte, val byte) error {

// DigitalWrite writes a value to a specific digital pin implementing the DigitalWriter interface.
func (d *GrovePiDriver) DigitalWrite(pin string, val byte) (err error) {
// TODO: strip off the leading "D"
pin = getPin(pin)

var pinNum int
pinNum, err = strconv.Atoi(pin)
if err != nil {
Expand All @@ -184,12 +187,12 @@ func (d *GrovePiDriver) DigitalWrite(pin string, val byte) (err error) {
d.digitalPins[pinNum] = "output"
}

err = d.WriteDigital(byte(pinNum), val)
err = d.writeDigital(byte(pinNum), val)

return
}

// WriteAnalog writes analog to the GrovePi
// WriteAnalog writes PWM aka analog to the GrovePi
func (d *GrovePiDriver) WriteAnalog(pin byte, val byte) error {
buf := []byte{1, CommandWriteAnalog, pin, val, 0}
_, err := d.connection.Write(buf)
Expand Down Expand Up @@ -236,3 +239,13 @@ func (d *GrovePiDriver) ReadDHT(pin byte, size int) ([]byte, error) {

return data, err
}

func getPin(pin string) string {
if len(pin) > 1 {
if strings.ToUpper(pin[0:1]) == "A" || strings.ToUpper(pin[0:1]) == "D" {
return pin[1:len(pin)]
}
}

return pin
}
20 changes: 20 additions & 0 deletions drivers/i2c/grovepi_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ func TestGrovePiDriverName(t *testing.T) {
gobottest.Refute(t, g.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(g.Name(), "GrovePi"), true)
}

func TestGrovePiDriverOptions(t *testing.T) {
g := NewGrovePiDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, g.GetBusOrDefault(1), 2)
}

// Methods
func TestGrovePiDriverStart(t *testing.T) {
g := initTestGrovePiDriver()

gobottest.Assert(t, g.Start(), nil)
}

func TestGrovePiDrivergetPin(t *testing.T) {
gobottest.Assert(t, getPin("a1"), "1")
gobottest.Assert(t, getPin("A16"), "16")
gobottest.Assert(t, getPin("D3"), "3")
gobottest.Assert(t, getPin("d22"), "22")
gobottest.Assert(t, getPin("22"), "22")
}
2 changes: 1 addition & 1 deletion examples/raspi_grove_pi_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func main() {
r := raspi.NewAdaptor()
gp := i2c.NewGrovePiDriver(r)
led := gpio.NewLedDriver(gp, "2")
led := gpio.NewLedDriver(gp, "D2")

work := func() {
gobot.Every(1*time.Second, func() {
Expand Down
41 changes: 41 additions & 0 deletions examples/raspi_grove_pi_button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// +build example
//
// Do not build by default.

package main

import (
"fmt"

"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)

func main() {
r := raspi.NewAdaptor()
gp := i2c.NewGrovePiDriver(r)
button := gpio.NewButtonDriver(gp, "D3")
led := gpio.NewLedDriver(gp, "D2")

work := func() {
button.On(gpio.ButtonPush, func(data interface{}) {
fmt.Println("button pressed")
led.On()
})

button.On(gpio.ButtonRelease, func(data interface{}) {
fmt.Println("button released")
led.Off()
})
}

robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{r},
[]gobot.Device{gp, button, led},
work,
)

robot.Start()
}
34 changes: 34 additions & 0 deletions examples/raspi_grove_pi_rotary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build example
//
// Do not build by default.

package main

import (
"fmt"

"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/aio"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)

func main() {
board := raspi.NewAdaptor()
gp := i2c.NewGrovePiDriver(board)
sensor := aio.NewGroveRotaryDriver(gp, "A1")

work := func() {
sensor.On(aio.Data, func(data interface{}) {
fmt.Println("sensor", data)
})
}

robot := gobot.NewRobot("sensorBot",
[]gobot.Connection{board},
[]gobot.Device{gp, sensor},
work,
)

robot.Start()
}

0 comments on commit b8d26f0

Please sign in to comment.