diff --git a/platforms/tinkerboard/adaptor.go b/platforms/tinkerboard/adaptor.go index 1a2f5913..5659df94 100644 --- a/platforms/tinkerboard/adaptor.go +++ b/platforms/tinkerboard/adaptor.go @@ -3,6 +3,7 @@ package tinkerboard import ( "errors" "fmt" + "sync" multierror "github.com/hashicorp/go-multierror" "gobot.io/x/gobot" @@ -22,12 +23,14 @@ type Adaptor struct { digitalPins map[int]*sysfs.DigitalPin pwmPins map[int]*sysfs.PWMPin i2cBuses [2]sysfs.I2cDevice + mutex *sync.Mutex } // NewAdaptor creates a Tinkerboard Adaptor func NewAdaptor() *Adaptor { c := &Adaptor{ - name: gobot.DefaultName("Tinker Board"), + name: gobot.DefaultName("Tinker Board"), + mutex: &sync.Mutex{}, } c.setPins() @@ -47,6 +50,9 @@ func (c *Adaptor) Connect() (err error) { // Finalize closes connection to board and pins func (c *Adaptor) Finalize() (err error) { + c.mutex.Lock() + defer c.mutex.Unlock() + for _, pin := range c.digitalPins { if pin != nil { if e := pin.Unexport(); e != nil { @@ -127,6 +133,9 @@ func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) { // DigitalPin returns matched digitalPin for specified values func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) { + c.mutex.Lock() + defer c.mutex.Unlock() + i, err := c.translatePin(pin) if err != nil { @@ -149,6 +158,9 @@ func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinn // PWMPin returns matched pwmPin for specified pin number func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) { + c.mutex.Lock() + defer c.mutex.Unlock() + i, err := c.translatePwmPin(pin) if err != nil { return nil, err @@ -181,6 +193,9 @@ func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) { // GetConnection returns a connection to a device on a specified bus. // Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1. func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) { + c.mutex.Lock() + defer c.mutex.Unlock() + if (bus < 0) || (bus > 1) { return nil, fmt.Errorf("Bus number %d out of range", bus) }