Skip to content

Commit

Permalink
grovepi: add mutex to control transactionality of the device communic…
Browse files Browse the repository at this point in the history
…ation

Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed May 22, 2019
1 parent bcbcf66 commit 518f4e9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions drivers/i2c/grovepi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package i2c
import (
"strconv"
"strings"
"sync"
"time"

"gobot.io/x/gobot"
Expand Down Expand Up @@ -30,6 +31,7 @@ type GrovePiDriver struct {
name string
digitalPins map[int]string
analogPins map[int]string
mutex *sync.Mutex
connector Connector
connection Connection
Config
Expand All @@ -48,6 +50,7 @@ func NewGrovePiDriver(a Connector, options ...func(Config)) *GrovePiDriver {
name: gobot.DefaultName("GrovePi"),
digitalPins: make(map[int]string),
analogPins: make(map[int]string),
mutex: &sync.Mutex{},
connector: a,
Config: NewConfig(),
}
Expand Down Expand Up @@ -192,6 +195,9 @@ func getPin(pin string) string {

// readAnalog reads analog value from the GrovePi.
func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {
d.mutex.Lock()
defer d.mutex.Unlock()

b := []byte{CommandReadAnalog, pin, 0, 0}
_, err := d.connection.Write(b)
if err != nil {
Expand All @@ -213,6 +219,9 @@ func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {

// readDigital reads digitally from the GrovePi.
func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()

buf := []byte{CommandReadDigital, pin, 0, 0}
_, err = d.connection.Write(buf)
if err != nil {
Expand All @@ -232,6 +241,9 @@ func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {

// writeDigital writes digitally to the GrovePi.
func (d *GrovePiDriver) writeDigital(pin byte, val byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()

buf := []byte{CommandWriteDigital, pin, val, 0}
_, err := d.connection.Write(buf)

Expand Down

0 comments on commit 518f4e9

Please sign in to comment.