gobot.io/x/gobot/v2@v2.1.0/platforms/intel-iot/joule/joule_adaptor.go (about)

     1  package joule
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	multierror "github.com/hashicorp/go-multierror"
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/platforms/adaptors"
    10  	"gobot.io/x/gobot/v2/system"
    11  )
    12  
    13  const defaultI2cBusNumber = 0
    14  
    15  type sysfsPin struct {
    16  	pin    int
    17  	pwmPin int
    18  }
    19  
    20  // Adaptor represents an Intel Joule
    21  type Adaptor struct {
    22  	name  string
    23  	sys   *system.Accesser
    24  	mutex sync.Mutex
    25  	*adaptors.DigitalPinsAdaptor
    26  	*adaptors.PWMPinsAdaptor
    27  	*adaptors.I2cBusAdaptor
    28  }
    29  
    30  // NewAdaptor returns a new Joule Adaptor
    31  //
    32  // Optional parameters:
    33  //
    34  //	adaptors.WithGpiodAccess():	use character device gpiod driver instead of sysfs
    35  //	adaptors.WithSpiGpioAccess(sclk, nss, mosi, miso):	use GPIO's instead of /dev/spidev#.#
    36  func NewAdaptor(opts ...func(adaptors.Optioner)) *Adaptor {
    37  	sys := system.NewAccesser()
    38  	c := &Adaptor{
    39  		name: gobot.DefaultName("Joule"),
    40  		sys:  sys,
    41  	}
    42  	c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin, opts...)
    43  	c.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, c.translatePWMPin, adaptors.WithPWMPinInitializer(pwmPinInitializer))
    44  	c.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, c.validateI2cBusNumber, defaultI2cBusNumber)
    45  	return c
    46  }
    47  
    48  // Name returns the Adaptors name
    49  func (c *Adaptor) Name() string { return c.name }
    50  
    51  // SetName sets the Adaptors name
    52  func (c *Adaptor) SetName(n string) { c.name = n }
    53  
    54  // Connect create new connection to board and pins.
    55  func (c *Adaptor) Connect() error {
    56  	c.mutex.Lock()
    57  	defer c.mutex.Unlock()
    58  
    59  	if err := c.I2cBusAdaptor.Connect(); err != nil {
    60  		return err
    61  	}
    62  
    63  	if err := c.PWMPinsAdaptor.Connect(); err != nil {
    64  		return err
    65  	}
    66  	return c.DigitalPinsAdaptor.Connect()
    67  }
    68  
    69  // Finalize releases all i2c devices and exported digital and pwm pins.
    70  func (c *Adaptor) Finalize() error {
    71  	c.mutex.Lock()
    72  	defer c.mutex.Unlock()
    73  
    74  	err := c.DigitalPinsAdaptor.Finalize()
    75  
    76  	if e := c.PWMPinsAdaptor.Finalize(); e != nil {
    77  		err = multierror.Append(err, e)
    78  	}
    79  
    80  	if e := c.I2cBusAdaptor.Finalize(); e != nil {
    81  		err = multierror.Append(err, e)
    82  	}
    83  
    84  	return err
    85  }
    86  
    87  func (c *Adaptor) validateI2cBusNumber(busNr int) error {
    88  	// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
    89  	if (busNr < 0) || (busNr > 2) {
    90  		return fmt.Errorf("Bus number %d out of range", busNr)
    91  	}
    92  	return nil
    93  }
    94  
    95  func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
    96  	if val, ok := sysfsPinMap[id]; ok {
    97  		return "", val.pin, nil
    98  	}
    99  	return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
   100  }
   101  
   102  func (c *Adaptor) translatePWMPin(id string) (string, int, error) {
   103  	sysPin, ok := sysfsPinMap[id]
   104  	if !ok {
   105  		return "", -1, fmt.Errorf("'%s' is not a valid id for a pin", id)
   106  	}
   107  	if sysPin.pwmPin == -1 {
   108  		return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
   109  	}
   110  	return "/sys/class/pwm/pwmchip0", sysPin.pwmPin, nil
   111  }
   112  
   113  func pwmPinInitializer(pin gobot.PWMPinner) error {
   114  	if err := pin.Export(); err != nil {
   115  		return err
   116  	}
   117  	if err := pin.SetPeriod(10000000); err != nil {
   118  		return err
   119  	}
   120  	return pin.SetEnabled(true)
   121  }