2016-08-25 18:10:52 +02:00
|
|
|
package joule
|
|
|
|
|
|
|
|
import (
|
2017-02-08 11:33:24 +01:00
|
|
|
"fmt"
|
2017-05-06 15:11:58 +02:00
|
|
|
"sync"
|
2016-08-25 18:10:52 +02:00
|
|
|
|
2016-11-07 19:23:48 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
2017-02-06 14:50:14 +01:00
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2022-12-01 17:33:33 +01:00
|
|
|
"gobot.io/x/gobot/platforms/adaptors"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2016-08-25 18:10:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type sysfsPin struct {
|
2016-08-25 21:14:43 +02:00
|
|
|
pin int
|
|
|
|
pwmPin int
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:59:25 +02:00
|
|
|
// Adaptor represents an Intel Joule
|
|
|
|
type Adaptor struct {
|
2022-12-01 17:33:33 +01:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
*adaptors.DigitalPinsAdaptor
|
2022-12-05 18:28:57 +01:00
|
|
|
*adaptors.PWMPinsAdaptor
|
2022-12-01 17:33:33 +01:00
|
|
|
i2cBuses [3]i2c.I2cDevice
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:59:25 +02:00
|
|
|
// NewAdaptor returns a new Joule Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
2022-12-01 17:33:33 +01:00
|
|
|
sys := system.NewAccesser()
|
|
|
|
c := &Adaptor{
|
2022-11-20 19:22:26 +01:00
|
|
|
name: gobot.DefaultName("Joule"),
|
2022-12-01 17:33:33 +01:00
|
|
|
sys: sys,
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin)
|
2022-12-05 18:28:57 +01:00
|
|
|
c.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, c.translatePWMPin, adaptors.WithPWMPinInitializer(pwmPinInitializer))
|
2022-12-01 17:33:33 +01:00
|
|
|
return c
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:59:25 +02:00
|
|
|
// Name returns the Adaptors name
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Name() string { return c.name }
|
2016-09-25 20:59:25 +02:00
|
|
|
|
|
|
|
// SetName sets the Adaptors name
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) SetName(n string) { c.name = n }
|
2016-08-25 18:10:52 +02:00
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
// Connect create new connection to board and pins.
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Connect() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2017-05-06 15:11:58 +02:00
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
if err := c.PWMPinsAdaptor.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return c.DigitalPinsAdaptor.Connect()
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize releases all i2c devices and exported digital and pwm pins.
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Finalize() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2017-05-06 15:11:58 +02:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
err := c.DigitalPinsAdaptor.Finalize()
|
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
if e := c.PWMPinsAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
2022-12-05 18:28:57 +01:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
for _, bus := range c.i2cBuses {
|
2017-02-06 14:50:14 +01:00
|
|
|
if bus != nil {
|
|
|
|
if errs := bus.Close(); errs != nil {
|
|
|
|
err = multierror.Append(err, errs)
|
|
|
|
}
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return err
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetConnection returns an i2c connection to a device on a specified bus.
|
2017-02-06 14:50:14 +01:00
|
|
|
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
|
2017-02-06 14:50:14 +01:00
|
|
|
if (bus < 0) || (bus > 2) {
|
|
|
|
return nil, fmt.Errorf("Bus number %d out of range", bus)
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
if c.i2cBuses[bus] == nil {
|
|
|
|
c.i2cBuses[bus], err = c.sys.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
|
2017-02-06 14:50:14 +01:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return i2c.NewConnection(c.i2cBuses[bus], address), err
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetDefaultBus returns the default i2c bus for this platform
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) GetDefaultBus() int {
|
2017-02-06 14:50:14 +01:00
|
|
|
return 0
|
2016-08-25 18:10:52 +02:00
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
|
|
|
|
if val, ok := sysfsPinMap[id]; ok {
|
|
|
|
return "", val.pin, nil
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|
2022-12-05 18:28:57 +01:00
|
|
|
|
|
|
|
func (c *Adaptor) translatePWMPin(id string) (string, int, error) {
|
|
|
|
sysPin, ok := sysfsPinMap[id]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a pin", id)
|
|
|
|
}
|
|
|
|
if sysPin.pwmPin == -1 {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
|
|
|
|
}
|
|
|
|
return "/sys/class/pwm/pwmchip0", sysPin.pwmPin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pwmPinInitializer(pin gobot.PWMPinner) error {
|
|
|
|
if err := pin.Export(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := pin.SetPeriod(10000000); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pin.SetEnabled(true)
|
|
|
|
}
|