func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i, err := b.translatePin(pin)
	if err != nil {
		return
	}
	if b.digitalPins[i] == nil {
		b.digitalPins[i] = sysfs.NewDigitalPin(i)
		err := b.digitalPins[i].Export()
		if err != nil {
			return nil, err
		}
	}
	if err = b.digitalPins[i].Direction(dir); err != nil {
		return
	}
	return b.digitalPins[i], nil
}
digitalPin retrieves digital pin value by name
package beaglebone
import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"github.com/hybridgroup/gobot"
	"github.com/hybridgroup/gobot/sysfs"
)
var slots = "/sys/devices/bone_capemgr.*"
var ocp = "/sys/devices/ocp.*"
var usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
var glob = func(pattern string) (matches []string, err error) {
	return filepath.Glob(pattern)
}
var pins = map[string]int{
	"P8_3":  38,
	"P8_4":  39,
	"P8_5":  34,
	"P8_6":  35,
	"P8_7":  66,
	"P8_8":  67,
	"P8_9":  69,
	"P8_10": 68,
	"P8_11": 45,
	"P8_12": 44,
	"P8_13": 23,
	"P8_14": 26,
	"P8_15": 47,
	"P8_16": 46,
	"P8_17": 27,
	"P8_18": 65,
	"P8_19": 22,
	"P8_20": 63,
	"P8_21": 62,
	"P8_22": 37,
	"P8_23": 36,
	"P8_24": 33,
	"P8_25": 32,
	"P8_26": 61,
	"P8_27": 86,
	"P8_28": 88,
	"P8_29": 87,
	"P8_30": 89,
	"P8_31": 10,
	"P8_32": 11,
	"P8_33": 9,
	"P8_34": 81,
	"P8_35": 8,
	"P8_36": 80,
	"P8_37": 78,
	"P8_38": 79,
	"P8_39": 76,
	"P8_40": 77,
	"P8_41": 74,
	"P8_42": 75,
	"P8_43": 72,
	"P8_44": 73,
	"P8_45": 70,
	"P8_46": 71,
	"P9_11": 30,
	"P9_12": 60,
	"P9_13": 31,
	"P9_14": 50,
	"P9_15": 48,
	"P9_16": 51,
	"P9_17": 5,
	"P9_18": 4,
	"P9_19": 13,
	"P9_20": 12,
	"P9_21": 3,
	"P9_22": 2,
	"P9_23": 49,
	"P9_24": 15,
	"P9_25": 117,
	"P9_26": 14,
	"P9_27": 115,
	"P9_28": 113,
	"P9_29": 111,
	"P9_30": 112,
	"P9_31": 110,
}
var pwmPins = map[string]string{
	"P9_14": "P9_14",
	"P9_21": "P9_21",
	"P9_22": "P9_22",
	"P9_29": "P9_29",
	"P9_42": "P9_42",
	"P8_13": "P8_13",
	"P8_34": "P8_34",
	"P8_45": "P8_45",
	"P8_46": "P8_46",
}
var analogPins = map[string]string{
	"P9_39": "AIN0",
	"P9_40": "AIN1",
	"P9_37": "AIN2",
	"P9_38": "AIN3",
	"P9_33": "AIN4",
	"P9_36": "AIN5",
	"P9_35": "AIN6",
}
type BeagleboneAdaptor struct {
	name        string
	digitalPins []sysfs.DigitalPin
	pwmPins     map[string]*pwmPin
	i2cDevice   sysfs.I2cDevice
	ocp         string
	helper      string
	slots       string
}
func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
	b := &BeagleboneAdaptor{
		name:        name,
		digitalPins: make([]sysfs.DigitalPin, 120),
		pwmPins:     make(map[string]*pwmPin),
	}
	g, _ := glob(ocp)
	b.ocp = g[0]
	g, _ = glob(slots)
	b.slots = fmt.Sprintf("%v/slots", g[0])
	return b
}
func (b *BeagleboneAdaptor) Name() string { return b.name }
func (b *BeagleboneAdaptor) Connect() (errs []error) {
	if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil {
		return []error{err}
	}
	if err := ensureSlot(b.slots, "am33xx_pwm"); err != nil {
		return []error{err}
	}
	g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp))
	if err != nil {
		return []error{err}
	}
	b.helper = g[0]
	return
}
func (b *BeagleboneAdaptor) Finalize() (errs []error) {
	for _, pin := range b.pwmPins {
		if pin != nil {
			if err := pin.release(); err != nil {
				errs = append(errs, err)
			}
		}
	}
	for _, pin := range b.digitalPins {
		if pin != nil {
			if err := pin.Unexport(); err != nil {
				errs = append(errs, err)
			}
		}
	}
	if b.i2cDevice != nil {
		if err := b.i2cDevice.Close(); err != nil {
			errs = append(errs, err)
		}
	}
	return
}
func (b *BeagleboneAdaptor) PwmWrite(pin string, val byte) (err error) {
	return b.pwmWrite(pin, val)
}
func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) (err error) {
	i, err := b.pwmPin(pin)
	if err != nil {
		return err
	}
	period := 16666666.0
	duty := (gobot.FromScale(float64(val), 0, 180.0) * 0.115) + 0.05
	return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
}
func (b *BeagleboneAdaptor) DigitalRead(pin string) (val int, err error) {
	sysfsPin, err := b.digitalPin(pin, sysfs.IN)
	if err != nil {
		return
	}
	return sysfsPin.Read()
}
func (b *BeagleboneAdaptor) DigitalWrite(pin string, val byte) (err error) {
	if strings.Contains(pin, "usr") {
		fi, err := sysfs.OpenFile(usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
		defer fi.Close()
		if err != nil {
			return err
		}
		_, err = fi.WriteString(strconv.Itoa(int(val)))
		return err
	}
	sysfsPin, err := b.digitalPin(pin, sysfs.OUT)
	if err != nil {
		return err
	}
	return sysfsPin.Write(int(val))
}
func (b *BeagleboneAdaptor) AnalogRead(pin string) (val int, err error) {
	analogPin, err := b.translateAnalogPin(pin)
	if err != nil {
		return
	}
	fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.helper, analogPin), os.O_RDONLY, 0644)
	defer fi.Close()
	if err != nil {
		return
	}
	var buf = make([]byte, 1024)
	_, err = fi.Read(buf)
	if err != nil {
		return
	}
	val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
	return
}
func (b *BeagleboneAdaptor) I2cStart(address int) (err error) {
	if b.i2cDevice == nil {
		b.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
	}
	return
}
func (b *BeagleboneAdaptor) I2cWrite(address int, data []byte) (err error) {
	if err = b.i2cDevice.SetAddress(address); err != nil {
		return
	}
	_, err = b.i2cDevice.Write(data)
	return
}
func (b *BeagleboneAdaptor) I2cRead(address int, size int) (data []byte, err error) {
	if err = b.i2cDevice.SetAddress(address); err != nil {
		return
	}
	data = make([]byte, size)
	_, err = b.i2cDevice.Read(data)
	return
}
func (b *BeagleboneAdaptor) translatePin(pin string) (value int, err error) {
	for key, value := range pins {
		if key == pin {
			return value, nil
		}
	}
	err = errors.New("Not a valid pin")
	return
}
func (b *BeagleboneAdaptor) translatePwmPin(pin string) (value string, err error) {
	for key, value := range pwmPins {
		if key == pin {
			return value, nil
		}
	}
	err = errors.New("Not a valid pin")
	return
}
func (b *BeagleboneAdaptor) translateAnalogPin(pin string) (value string, err error) {
	for key, value := range analogPins {
		if key == pin {
			return value, nil
		}
	}
	err = errors.New("Not a valid pin")
	return
}