mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
grovepi: add mutex to control transactionality of the device communication
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
parent
bf8b1d241f
commit
3f65f1df37
@ -3,6 +3,7 @@ package i2c
|
|||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gobot.io/x/gobot"
|
"gobot.io/x/gobot"
|
||||||
@ -30,6 +31,7 @@ type GrovePiDriver struct {
|
|||||||
name string
|
name string
|
||||||
digitalPins map[int]string
|
digitalPins map[int]string
|
||||||
analogPins map[int]string
|
analogPins map[int]string
|
||||||
|
mutex *sync.Mutex
|
||||||
connector Connector
|
connector Connector
|
||||||
connection Connection
|
connection Connection
|
||||||
Config
|
Config
|
||||||
@ -48,6 +50,7 @@ func NewGrovePiDriver(a Connector, options ...func(Config)) *GrovePiDriver {
|
|||||||
name: gobot.DefaultName("GrovePi"),
|
name: gobot.DefaultName("GrovePi"),
|
||||||
digitalPins: make(map[int]string),
|
digitalPins: make(map[int]string),
|
||||||
analogPins: make(map[int]string),
|
analogPins: make(map[int]string),
|
||||||
|
mutex: &sync.Mutex{},
|
||||||
connector: a,
|
connector: a,
|
||||||
Config: NewConfig(),
|
Config: NewConfig(),
|
||||||
}
|
}
|
||||||
@ -192,6 +195,9 @@ func getPin(pin string) string {
|
|||||||
|
|
||||||
// readAnalog reads analog value from the GrovePi.
|
// readAnalog reads analog value from the GrovePi.
|
||||||
func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {
|
func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {
|
||||||
|
d.mutex.Lock()
|
||||||
|
defer d.mutex.Unlock()
|
||||||
|
|
||||||
b := []byte{CommandReadAnalog, pin, 0, 0}
|
b := []byte{CommandReadAnalog, pin, 0, 0}
|
||||||
_, err := d.connection.Write(b)
|
_, err := d.connection.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -213,6 +219,9 @@ func (d *GrovePiDriver) readAnalog(pin byte) (int, error) {
|
|||||||
|
|
||||||
// readDigital reads digitally from the GrovePi.
|
// readDigital reads digitally from the GrovePi.
|
||||||
func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {
|
func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {
|
||||||
|
d.mutex.Lock()
|
||||||
|
defer d.mutex.Unlock()
|
||||||
|
|
||||||
buf := []byte{CommandReadDigital, pin, 0, 0}
|
buf := []byte{CommandReadDigital, pin, 0, 0}
|
||||||
_, err = d.connection.Write(buf)
|
_, err = d.connection.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -232,6 +241,9 @@ func (d *GrovePiDriver) readDigital(pin byte) (val int, err error) {
|
|||||||
|
|
||||||
// writeDigital writes digitally to the GrovePi.
|
// writeDigital writes digitally to the GrovePi.
|
||||||
func (d *GrovePiDriver) writeDigital(pin byte, val byte) error {
|
func (d *GrovePiDriver) writeDigital(pin byte, val byte) error {
|
||||||
|
d.mutex.Lock()
|
||||||
|
defer d.mutex.Unlock()
|
||||||
|
|
||||||
buf := []byte{CommandWriteDigital, pin, val, 0}
|
buf := []byte{CommandWriteDigital, pin, val, 0}
|
||||||
_, err := d.connection.Write(buf)
|
_, err := d.connection.Write(buf)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user