2016-01-30 18:00:27 +08:00
|
|
|
package chip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-01-11 08:39:23 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-11-28 04:56:09 +08:00
|
|
|
"strings"
|
2016-01-30 18:00:27 +08:00
|
|
|
|
2016-11-08 00:43:55 +08:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2017-02-02 22:57:45 +08:00
|
|
|
"gobot.io/x/gobot"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/sysfs"
|
2016-01-30 18:00:27 +08:00
|
|
|
)
|
|
|
|
|
2016-11-28 04:56:09 +08:00
|
|
|
// Adaptor represents a Gobot Adaptor for a C.H.I.P.
|
2016-09-26 02:19:19 +08:00
|
|
|
type Adaptor struct {
|
2016-01-30 18:00:27 +08:00
|
|
|
name string
|
|
|
|
digitalPins map[int]sysfs.DigitalPin
|
2016-11-28 04:56:09 +08:00
|
|
|
pinMap map[string]int
|
2016-01-30 18:00:27 +08:00
|
|
|
i2cDevice sysfs.I2cDevice
|
|
|
|
}
|
|
|
|
|
2017-01-11 08:39:23 +08:00
|
|
|
var fixedPins = map[string]int{
|
2017-01-11 08:49:27 +08:00
|
|
|
"PWM0": 34,
|
2017-01-11 08:39:23 +08:00
|
|
|
"AP-EINT3": 35,
|
|
|
|
|
|
|
|
"TWI1-SCK": 47,
|
|
|
|
"TWI1-SDA": 48,
|
|
|
|
"TWI2-SCK": 49,
|
|
|
|
"TWI2-SDA": 50,
|
|
|
|
|
2017-01-11 08:49:27 +08:00
|
|
|
"LCD-D2": 98,
|
|
|
|
"LCD-D3": 99,
|
|
|
|
"LCD-D4": 100,
|
|
|
|
"LCD-D5": 101,
|
|
|
|
"LCD-D6": 102,
|
|
|
|
"LCD-D7": 103,
|
|
|
|
"LCD-D10": 106,
|
|
|
|
"LCD-D11": 107,
|
|
|
|
"LCD-D12": 108,
|
|
|
|
"LCD-D13": 109,
|
|
|
|
"LCD-D14": 110,
|
|
|
|
"LCD-D15": 111,
|
|
|
|
"LCD-D18": 114,
|
|
|
|
"LCD-D19": 115,
|
|
|
|
"LCD-D20": 116,
|
|
|
|
"LCD-D21": 117,
|
|
|
|
"LCD-D22": 118,
|
|
|
|
"LCD-D23": 119,
|
|
|
|
"LCD-CLK": 120,
|
|
|
|
"LCD-DE": 121,
|
2017-01-11 08:39:23 +08:00
|
|
|
"LCD-HSYNC": 122,
|
|
|
|
"LCD-VSYNC": 123,
|
|
|
|
|
2017-01-11 08:49:27 +08:00
|
|
|
"CSIPCK": 128,
|
|
|
|
"CSICK": 129,
|
2017-01-11 08:39:23 +08:00
|
|
|
"CSIHSYNC": 130,
|
|
|
|
"CSIVSYNC": 131,
|
2017-01-11 08:49:27 +08:00
|
|
|
"CSID0": 132,
|
|
|
|
"CSID1": 133,
|
|
|
|
"CSID2": 134,
|
|
|
|
"CSID3": 135,
|
|
|
|
"CSID4": 136,
|
|
|
|
"CSID5": 137,
|
|
|
|
"CSID6": 138,
|
|
|
|
"CSID7": 139,
|
2017-01-11 08:39:23 +08:00
|
|
|
|
|
|
|
"AP-EINT1": 193,
|
|
|
|
|
|
|
|
"UART1-TX": 195,
|
|
|
|
"UART1-RX": 196,
|
2016-11-28 04:56:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:19:19 +08:00
|
|
|
// NewAdaptor creates a C.H.I.P. Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
c := &Adaptor{
|
2017-02-02 22:57:45 +08:00
|
|
|
name: gobot.DefaultName("CHIP"),
|
2016-01-30 18:00:27 +08:00
|
|
|
digitalPins: make(map[int]sysfs.DigitalPin),
|
|
|
|
}
|
2016-11-28 04:56:09 +08:00
|
|
|
|
|
|
|
c.setPins()
|
2016-01-30 18:00:27 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-09-26 02:19:19 +08:00
|
|
|
// Name returns the name of the Adaptor
|
|
|
|
func (c *Adaptor) Name() string { return c.name }
|
|
|
|
|
|
|
|
// SetName sets the name of the Adaptor
|
|
|
|
func (c *Adaptor) SetName(n string) { c.name = n }
|
2016-01-30 18:00:27 +08:00
|
|
|
|
|
|
|
// Connect initializes the board
|
2016-11-08 00:43:55 +08:00
|
|
|
func (c *Adaptor) Connect() (err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize closes connection to board and pins
|
2016-11-08 00:43:55 +08:00
|
|
|
func (c *Adaptor) Finalize() (err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
for _, pin := range c.digitalPins {
|
|
|
|
if pin != nil {
|
2016-11-08 00:43:55 +08:00
|
|
|
if e := pin.Unexport(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2016-01-30 18:00:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.i2cDevice != nil {
|
2016-11-08 00:43:55 +08:00
|
|
|
if e := c.i2cDevice.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2016-01-30 18:00:27 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 00:43:55 +08:00
|
|
|
return
|
2016-01-30 18:00:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-28 04:56:09 +08:00
|
|
|
func (c *Adaptor) setPins() {
|
2017-01-11 08:39:23 +08:00
|
|
|
c.pinMap = fixedPins
|
|
|
|
baseAddr, _ := getXIOBase()
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
pin := fmt.Sprintf("XIO-P%d", i)
|
|
|
|
c.pinMap[pin] = baseAddr + i
|
2016-11-28 04:56:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) translatePin(pin string) (i int, err error) {
|
2016-11-28 04:56:09 +08:00
|
|
|
if val, ok := c.pinMap[pin]; ok {
|
2016-01-30 18:00:27 +08:00
|
|
|
i = val
|
|
|
|
} else {
|
|
|
|
err = errors.New("Not a valid pin")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// digitalPin returns matched digitalPin for specified values
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
i, err := c.translatePin(pin)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.digitalPins[i] == nil {
|
|
|
|
c.digitalPins[i] = sysfs.NewDigitalPin(i)
|
|
|
|
if err = c.digitalPins[i].Export(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.digitalPins[i].Direction(dir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.digitalPins[i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalRead reads digital value from the specified pin.
|
2016-02-08 16:01:44 +08:00
|
|
|
// Valids pins are XIO-P0 through XIO-P7 (pins 13-20 on header 14).
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
sysfsPin, err := c.digitalPin(pin, sysfs.IN)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return sysfsPin.Read()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalWrite writes digital value to the specified pin.
|
2016-02-08 16:01:44 +08:00
|
|
|
// Valids pins are XIO-P0 through XIO-P7 (pins 13-20 on header 14).
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
sysfsPin, err := c.digitalPin(pin, sysfs.OUT)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sysfsPin.Write(int(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2cStart starts an i2c device in specified address.
|
|
|
|
// This assumes that the bus used is /dev/i2c-1, which corresponds to
|
2016-02-08 16:01:44 +08:00
|
|
|
// pins labeled TWI1-SDA and TW1-SCK (pins 9 and 11 on header 13).
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) I2cStart(address int) (err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
if c.i2cDevice == nil {
|
|
|
|
c.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2cWrite writes data to i2c device
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) I2cWrite(address int, data []byte) (err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
if err = c.i2cDevice.SetAddress(address); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = c.i2cDevice.Write(data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2cRead returns value from i2c device using specified size
|
2016-09-26 02:19:19 +08:00
|
|
|
func (c *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
|
2016-01-30 18:00:27 +08:00
|
|
|
if err = c.i2cDevice.SetAddress(address); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data = make([]byte, size)
|
|
|
|
_, err = c.i2cDevice.Read(data)
|
|
|
|
return
|
|
|
|
}
|
2016-11-28 04:56:09 +08:00
|
|
|
|
2017-01-11 08:39:23 +08:00
|
|
|
func getXIOBase() (baseAddr int, err error) {
|
|
|
|
// Default to original base from 4.3 kernel
|
|
|
|
baseAddr = 408
|
|
|
|
const expanderID = "pcf8574a"
|
|
|
|
|
|
|
|
labels, err := filepath.Glob("/sys/class/gpio/*/label")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, labelPath := range labels {
|
|
|
|
label, err := ioutil.ReadFile(labelPath)
|
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(string(label), expanderID) {
|
|
|
|
expanderPath, _ := filepath.Split(labelPath)
|
|
|
|
basePath := filepath.Join(expanderPath, "base")
|
|
|
|
base, err := ioutil.ReadFile(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
baseAddr, _ = strconv.Atoi(strings.TrimSpace(string(base)))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 04:56:09 +08:00
|
|
|
|
2017-01-11 08:39:23 +08:00
|
|
|
return baseAddr, nil
|
2016-11-28 04:56:09 +08:00
|
|
|
}
|