Skip to content

Commit

Permalink
i2c: GrovePi digitalwrite implemented
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 13, 2018
1 parent 425fea3 commit c9276f4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 20 deletions.
91 changes: 71 additions & 20 deletions drivers/i2c/grovepi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ const (
CommandWriteDigital = 2
CommandReadAnalog = 3
CommandWriteAnalog = 4
CommandPinMode = 5
CommandReadDHT = 40
)

// GrovePiDriver is a driver for the GrovePi for I²C bus interface.
type GrovePiDriver struct {
name string
connector Connector
connection Connection
name string
digitalPins map[int]string
analogPins map[int]string
connector Connector
connection Connection
Config
}

Expand All @@ -36,9 +39,11 @@ type GrovePiDriver struct {
//
func NewGrovePiDriver(a Connector, options ...func(Config)) *GrovePiDriver {
d := &GrovePiDriver{
name: gobot.DefaultName("GrovePi"),
connector: a,
Config: NewConfig(),
name: gobot.DefaultName("GrovePi"),
digitalPins: make(map[int]string),
analogPins: make(map[int]string),
connector: a,
Config: NewConfig(),
}

for _, option := range options {
Expand Down Expand Up @@ -78,12 +83,18 @@ func (d *GrovePiDriver) Halt() (err error) { return }
func (d *GrovePiDriver) AnalogRead(pin string) (value int, err error) {
// TODO: strip off the leading "A"

var channel int
channel, err = strconv.Atoi(pin)
var pinNum int
pinNum, err = strconv.Atoi(pin)
if err != nil {
return
}
value, err = d.ReadAnalog(byte(channel))

if dir, ok := d.analogPins[pinNum]; !ok || dir != "input" {
d.PinMode(byte(pinNum), "input")
d.analogPins[pinNum] = "input"
}

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

return
}
Expand Down Expand Up @@ -114,23 +125,41 @@ func (d *GrovePiDriver) ReadAnalog(pin byte) (int, error) {
}

// ReadDigital reads digitally to the GrovePi
func (d *GrovePiDriver) ReadDigital(pin byte, size int) ([]byte, error) {
buf := []byte{CommandReadDigital, pin, 0, 0}
_, err := d.connection.Write(buf)
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 {
return nil, err
return
}

time.Sleep(100 * time.Millisecond)

d.connection.Write([]byte{1})
data := make([]byte, size)
_, err = d.connection.Read(data)
v, err := d.connection.ReadByte()
if err != nil {
return nil, err
return
}

return data, err
return int(v), err
}

// DigitalRead performs a read on a digital pin.
func (d *GrovePiDriver) DigitalRead(pin string) (val int, err error) {
// TODO: strip off the leading "D"
var pinNum int
pinNum, err = strconv.Atoi(pin)
if err != nil {
return
}

if dir, ok := d.digitalPins[pinNum]; !ok || dir != "input" {
d.PinMode(byte(pinNum), "input")
d.digitalPins[pinNum] = "input"
}

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

return
}

// WriteDigital writes digitally to the GrovePi
Expand All @@ -144,12 +173,18 @@ 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"
var channel int
channel, err = strconv.Atoi(pin)
var pinNum int
pinNum, err = strconv.Atoi(pin)
if err != nil {
return
}
err = d.WriteDigital(byte(channel), val)

if dir, ok := d.digitalPins[pinNum]; !ok || dir != "output" {
d.PinMode(byte(pinNum), "output")
d.digitalPins[pinNum] = "output"
}

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

return
}
Expand All @@ -162,6 +197,22 @@ func (d *GrovePiDriver) WriteAnalog(pin byte, val byte) error {
return err
}

// PinMode sets the pin mode to input or output.
func (d *GrovePiDriver) PinMode(pin byte, mode string) error {
var b []byte
if mode == "output" {
b = []byte{1, CommandPinMode, pin, 1, 0}
} else {
b = []byte{1, CommandPinMode, pin, 0, 0}
}
_, err := d.connection.Write(b)
time.Sleep(100 * time.Millisecond)
if err != nil {
return err
}
return nil
}

// ReadDHT returns temperature and humidity from DHT sensor
func (d *GrovePiDriver) ReadDHT(pin byte, size int) ([]byte, error) {
cmd := []byte{1, CommandReadDHT, pin, 0, 0}
Expand Down
3 changes: 3 additions & 0 deletions drivers/i2c/grovepi_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

var _ gobot.Driver = (*GrovePiDriver)(nil)

// must implement the DigitalReader interface
var _ gpio.DigitalReader = (*GrovePiDriver)(nil)

// must implement the DigitalWriter interface
var _ gpio.DigitalWriter = (*GrovePiDriver)(nil)

Expand Down
34 changes: 34 additions & 0 deletions examples/raspi_grove_pi_blink.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 (
"time"

"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)
led := gpio.NewLedDriver(gp, "2")

work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
}

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

robot.Start()
}

0 comments on commit c9276f4

Please sign in to comment.