2014-09-18 12:45:54 -07:00
|
|
|
package edison
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-10-29 17:51:47 -07:00
|
|
|
"io"
|
2014-09-18 12:45:54 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-10-29 17:51:47 -07:00
|
|
|
"github.com/hybridgroup/gobot/sysfs"
|
2014-09-18 12:45:54 -07:00
|
|
|
)
|
|
|
|
|
2014-11-03 19:02:33 -08:00
|
|
|
var i2cLocation = "/dev/i2c-6"
|
|
|
|
|
2014-09-18 12:45:54 -07:00
|
|
|
type mux struct {
|
|
|
|
pin int
|
2014-10-30 13:41:27 -07:00
|
|
|
value int
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
type sysfsPin struct {
|
|
|
|
pin int
|
|
|
|
resistor int
|
|
|
|
levelShifter int
|
|
|
|
pwmPin int
|
|
|
|
mux []mux
|
|
|
|
}
|
|
|
|
|
|
|
|
type EdisonAdaptor struct {
|
|
|
|
gobot.Adaptor
|
2014-11-03 19:02:33 -08:00
|
|
|
tristate sysfs.DigitalPin
|
|
|
|
digitalPins map[int]sysfs.DigitalPin
|
2014-09-18 12:45:54 -07:00
|
|
|
pwmPins map[int]*pwmPin
|
2014-10-29 17:51:47 -07:00
|
|
|
i2cDevice io.ReadWriteCloser
|
2014-09-18 12:45:54 -07:00
|
|
|
connect func(e *EdisonAdaptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sysfsPinMap = map[string]sysfsPin{
|
|
|
|
"0": sysfsPin{
|
|
|
|
pin: 130,
|
|
|
|
resistor: 216,
|
|
|
|
levelShifter: 248,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"1": sysfsPin{
|
|
|
|
pin: 131,
|
|
|
|
resistor: 217,
|
|
|
|
levelShifter: 249,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"2": sysfsPin{
|
|
|
|
pin: 128,
|
|
|
|
resistor: 218,
|
|
|
|
levelShifter: 250,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"3": sysfsPin{
|
|
|
|
pin: 12,
|
|
|
|
resistor: 219,
|
|
|
|
levelShifter: 251,
|
|
|
|
pwmPin: 0,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
|
|
|
|
"4": sysfsPin{
|
|
|
|
pin: 129,
|
|
|
|
resistor: 220,
|
|
|
|
levelShifter: 252,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"5": sysfsPin{
|
|
|
|
pin: 13,
|
|
|
|
resistor: 221,
|
|
|
|
levelShifter: 253,
|
|
|
|
pwmPin: 1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"6": sysfsPin{
|
|
|
|
pin: 182,
|
|
|
|
resistor: 222,
|
|
|
|
levelShifter: 254,
|
|
|
|
pwmPin: 2,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"7": sysfsPin{
|
|
|
|
pin: 48,
|
|
|
|
resistor: 223,
|
|
|
|
levelShifter: 255,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"8": sysfsPin{
|
|
|
|
pin: 49,
|
|
|
|
resistor: 224,
|
|
|
|
levelShifter: 256,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"9": sysfsPin{
|
|
|
|
pin: 183,
|
|
|
|
resistor: 225,
|
|
|
|
levelShifter: 257,
|
|
|
|
pwmPin: 3,
|
|
|
|
mux: []mux{},
|
|
|
|
},
|
|
|
|
"10": sysfsPin{
|
|
|
|
pin: 41,
|
|
|
|
resistor: 226,
|
|
|
|
levelShifter: 258,
|
|
|
|
pwmPin: 4,
|
|
|
|
mux: []mux{
|
2014-10-29 17:51:47 -07:00
|
|
|
mux{263, sysfs.HIGH},
|
|
|
|
mux{240, sysfs.LOW},
|
2014-09-18 12:45:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"11": sysfsPin{
|
|
|
|
pin: 43,
|
|
|
|
resistor: 227,
|
|
|
|
levelShifter: 259,
|
|
|
|
pwmPin: 5,
|
|
|
|
mux: []mux{
|
2014-10-29 17:51:47 -07:00
|
|
|
mux{262, sysfs.HIGH},
|
|
|
|
mux{241, sysfs.LOW},
|
2014-09-18 12:45:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"12": sysfsPin{
|
|
|
|
pin: 42,
|
|
|
|
resistor: 228,
|
|
|
|
levelShifter: 260,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{
|
2014-10-29 17:51:47 -07:00
|
|
|
mux{242, sysfs.LOW},
|
2014-09-18 12:45:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"13": sysfsPin{
|
|
|
|
pin: 40,
|
|
|
|
resistor: 229,
|
|
|
|
levelShifter: 261,
|
|
|
|
pwmPin: -1,
|
|
|
|
mux: []mux{
|
2014-10-29 17:51:47 -07:00
|
|
|
mux{243, sysfs.LOW},
|
2014-09-18 12:45:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// writeFile validates file existence and writes data into it
|
2014-11-03 19:02:33 -08:00
|
|
|
var writeFile = func(name, data string) error {
|
2014-09-18 12:45:54 -07:00
|
|
|
if _, err := os.Stat(name); err == nil {
|
|
|
|
err := ioutil.WriteFile(
|
|
|
|
name,
|
|
|
|
[]byte(data),
|
|
|
|
0644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("File doesn't exist: " + name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// changePinMode writes pin mode to current_pinmux file
|
2014-09-18 12:45:54 -07:00
|
|
|
func changePinMode(pin, mode string) {
|
|
|
|
err := writeFile(
|
|
|
|
"/sys/kernel/debug/gpio_debug/gpio"+pin+"/current_pinmux",
|
|
|
|
"mode"+mode,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// NewEditionAdaptor creates a EdisonAdaptor with specified name and
|
|
|
|
// creates connect function
|
2014-09-18 12:45:54 -07:00
|
|
|
func NewEdisonAdaptor(name string) *EdisonAdaptor {
|
|
|
|
return &EdisonAdaptor{
|
|
|
|
Adaptor: *gobot.NewAdaptor(
|
|
|
|
name,
|
|
|
|
"EdisonAdaptor",
|
|
|
|
),
|
|
|
|
connect: func(e *EdisonAdaptor) {
|
2014-10-30 15:26:31 -07:00
|
|
|
e.tristate = sysfs.NewDigitalPin(214)
|
2014-10-30 13:41:27 -07:00
|
|
|
e.tristate.Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
e.tristate.Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
e.tristate.Write(sysfs.LOW)
|
2014-09-18 12:45:54 -07:00
|
|
|
|
|
|
|
for _, i := range []int{263, 262} {
|
2014-10-30 15:26:31 -07:00
|
|
|
io := sysfs.NewDigitalPin(i)
|
2014-10-30 13:41:27 -07:00
|
|
|
io.Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
io.Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
io.Write(sysfs.HIGH)
|
|
|
|
io.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []int{240, 241, 242, 243} {
|
2014-10-30 15:26:31 -07:00
|
|
|
io := sysfs.NewDigitalPin(i)
|
2014-10-30 13:41:27 -07:00
|
|
|
io.Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
io.Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
io.Write(sysfs.LOW)
|
|
|
|
io.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []string{"111", "115", "114", "109"} {
|
|
|
|
changePinMode(i, "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []string{"131", "129", "40"} {
|
|
|
|
changePinMode(i, "0")
|
|
|
|
}
|
|
|
|
|
2014-10-29 17:51:47 -07:00
|
|
|
e.tristate.Write(sysfs.HIGH)
|
2014-09-18 12:45:54 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// Connect starts conection with board and creates
|
|
|
|
// digitalPins and pwmPins adaptor maps
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) Connect() bool {
|
2014-11-03 19:02:33 -08:00
|
|
|
e.digitalPins = make(map[int]sysfs.DigitalPin)
|
2014-09-18 12:45:54 -07:00
|
|
|
e.pwmPins = make(map[int]*pwmPin)
|
|
|
|
e.connect(e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// Finalize closes connection to board and pins
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) Finalize() bool {
|
2014-10-29 17:51:47 -07:00
|
|
|
e.tristate.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
for _, pin := range e.digitalPins {
|
|
|
|
if pin != nil {
|
2014-10-29 17:51:47 -07:00
|
|
|
pin.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, pin := range e.pwmPins {
|
|
|
|
if pin != nil {
|
|
|
|
pin.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.i2cDevice != nil {
|
2014-10-29 17:51:47 -07:00
|
|
|
e.i2cDevice.Close()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2014-10-20 13:17:47 -05:00
|
|
|
|
|
|
|
// digitalPin returns matched digitalPin for specified values
|
2014-11-03 19:02:33 -08:00
|
|
|
func (e *EdisonAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin {
|
2014-09-18 12:45:54 -07:00
|
|
|
i := sysfsPinMap[pin]
|
|
|
|
if e.digitalPins[i.pin] == nil {
|
2014-10-30 15:26:31 -07:00
|
|
|
e.digitalPins[i.pin] = sysfs.NewDigitalPin(i.pin)
|
2014-10-30 13:41:27 -07:00
|
|
|
e.digitalPins[i.pin].Export()
|
2014-10-30 15:26:31 -07:00
|
|
|
e.digitalPins[i.resistor] = sysfs.NewDigitalPin(i.resistor)
|
2014-10-30 13:41:27 -07:00
|
|
|
e.digitalPins[i.resistor].Export()
|
2014-10-30 15:26:31 -07:00
|
|
|
e.digitalPins[i.levelShifter] = sysfs.NewDigitalPin(i.levelShifter)
|
2014-10-30 13:41:27 -07:00
|
|
|
e.digitalPins[i.levelShifter].Export()
|
2014-09-18 12:45:54 -07:00
|
|
|
if len(i.mux) > 0 {
|
|
|
|
for _, mux := range i.mux {
|
2014-10-30 15:26:31 -07:00
|
|
|
e.digitalPins[mux.pin] = sysfs.NewDigitalPin(mux.pin)
|
2014-10-30 13:41:27 -07:00
|
|
|
e.digitalPins[mux.pin].Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
e.digitalPins[mux.pin].Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
e.digitalPins[mux.pin].Write(mux.value)
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 19:02:33 -08:00
|
|
|
if dir == "in" {
|
|
|
|
e.digitalPins[i.pin].Direction(sysfs.IN)
|
|
|
|
e.digitalPins[i.resistor].Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
e.digitalPins[i.resistor].Write(sysfs.LOW)
|
2014-11-03 19:02:33 -08:00
|
|
|
e.digitalPins[i.levelShifter].Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
e.digitalPins[i.levelShifter].Write(sysfs.LOW)
|
2014-11-03 19:02:33 -08:00
|
|
|
} else if dir == "out" {
|
|
|
|
e.digitalPins[i.pin].Direction(sysfs.OUT)
|
|
|
|
e.digitalPins[i.resistor].Direction(sysfs.IN)
|
|
|
|
e.digitalPins[i.levelShifter].Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
e.digitalPins[i.levelShifter].Write(sysfs.HIGH)
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
return e.digitalPins[i.pin]
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// DigitalRead reads digital value from pin
|
2014-10-30 13:41:27 -07:00
|
|
|
func (e *EdisonAdaptor) DigitalRead(pin string) (i int) {
|
|
|
|
i, _ = e.digitalPin(pin, "in").Read()
|
|
|
|
return
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// DigitalWrite writes digital value to specified pin
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) DigitalWrite(pin string, val byte) {
|
2014-10-30 13:41:27 -07:00
|
|
|
e.digitalPin(pin, "out").Write(int(val))
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// PwmWrite writes scaled pwm value to specified pin
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) PwmWrite(pin string, val byte) {
|
|
|
|
sysPin := sysfsPinMap[pin]
|
|
|
|
if sysPin.pwmPin != -1 {
|
|
|
|
if e.pwmPins[sysPin.pwmPin] == nil {
|
|
|
|
e.DigitalWrite(pin, 1)
|
|
|
|
changePinMode(strconv.Itoa(int(sysPin.pin)), "1")
|
|
|
|
e.pwmPins[sysPin.pwmPin] = newPwmPin(sysPin.pwmPin)
|
|
|
|
}
|
|
|
|
period, err := strconv.Atoi(e.pwmPins[sysPin.pwmPin].period())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
duty := gobot.FromScale(float64(val), 0, 255.0)
|
|
|
|
e.pwmPins[sysPin.pwmPin].writeDuty(strconv.Itoa(int(float64(period) * duty)))
|
|
|
|
} else {
|
|
|
|
fmt.Println("Not a PWM pin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// AnalogRead returns value from analog reading of specified pin
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) AnalogRead(pin string) int {
|
|
|
|
buf, err := ioutil.ReadFile(
|
|
|
|
"/sys/bus/iio/devices/iio:device1/in_voltage" + pin + "_raw",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
val, err := strconv.Atoi(string(buf[0 : len(buf)-1]))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// I2cStart initializes i2c device for addresss
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) I2cStart(address byte) {
|
2014-10-29 17:51:47 -07:00
|
|
|
e.tristate.Write(sysfs.LOW)
|
2014-09-18 12:45:54 -07:00
|
|
|
|
|
|
|
for _, i := range []int{14, 165, 212, 213} {
|
2014-10-30 15:26:31 -07:00
|
|
|
io := sysfs.NewDigitalPin(i)
|
2014-10-30 13:41:27 -07:00
|
|
|
io.Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
io.Direction(sysfs.IN)
|
2014-10-29 17:51:47 -07:00
|
|
|
io.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []int{236, 237, 204, 205} {
|
2014-10-30 15:26:31 -07:00
|
|
|
io := sysfs.NewDigitalPin(i)
|
2014-10-30 13:41:27 -07:00
|
|
|
io.Export()
|
2014-11-03 19:02:33 -08:00
|
|
|
io.Direction(sysfs.OUT)
|
2014-10-29 17:51:47 -07:00
|
|
|
io.Write(sysfs.LOW)
|
|
|
|
io.Unexport()
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range []string{"28", "27"} {
|
|
|
|
changePinMode(i, "1")
|
|
|
|
}
|
|
|
|
|
2014-10-29 17:51:47 -07:00
|
|
|
e.tristate.Write(sysfs.HIGH)
|
2014-09-18 12:45:54 -07:00
|
|
|
|
2014-11-03 19:02:33 -08:00
|
|
|
e.i2cDevice, _ = sysfs.NewI2cDevice(i2cLocation, address)
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// I2cWrite writes data to i2cDevice
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) I2cWrite(data []byte) {
|
2014-10-29 17:51:47 -07:00
|
|
|
e.i2cDevice.Write(data)
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 13:17:47 -05:00
|
|
|
// I2cRead reads data from i2cDevice
|
2014-09-18 12:45:54 -07:00
|
|
|
func (e *EdisonAdaptor) I2cRead(size uint) []byte {
|
2014-10-29 17:51:47 -07:00
|
|
|
b := make([]byte, size)
|
|
|
|
e.i2cDevice.Read(b)
|
|
|
|
return b
|
2014-09-18 12:45:54 -07:00
|
|
|
}
|