2014-04-28 04:39:51 -07:00
|
|
|
package beaglebone
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-06-11 13:59:06 -06:00
|
|
|
"bufio"
|
2014-11-19 17:18:01 -08:00
|
|
|
"errors"
|
2014-06-11 13:59:06 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-04-26 03:11:51 -07:00
|
|
|
"strconv"
|
2014-06-11 13:59:06 -06:00
|
|
|
"strings"
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2016-11-07 17:33:11 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2014-10-16 10:59:23 -05:00
|
|
|
"github.com/hybridgroup/gobot"
|
2014-10-30 15:26:31 -07:00
|
|
|
"github.com/hybridgroup/gobot/sysfs"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
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)
|
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2014-04-26 03:11:51 -07:00
|
|
|
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",
|
|
|
|
}
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2014-04-26 03:11:51 -07:00
|
|
|
var analogPins = map[string]string{
|
|
|
|
"P9_39": "AIN0",
|
|
|
|
"P9_40": "AIN1",
|
|
|
|
"P9_37": "AIN2",
|
|
|
|
"P9_38": "AIN3",
|
|
|
|
"P9_33": "AIN4",
|
2015-08-27 19:59:26 -07:00
|
|
|
"P9_36": "AIN5",
|
|
|
|
"P9_35": "AIN6",
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// Adaptor is the gobot.Adaptor representation for the Beaglebone
|
|
|
|
type Adaptor struct {
|
2014-11-22 18:59:27 -08:00
|
|
|
name string
|
2014-11-03 19:41:34 -08:00
|
|
|
digitalPins []sysfs.DigitalPin
|
2014-04-26 03:11:51 -07:00
|
|
|
pwmPins map[string]*pwmPin
|
2015-07-03 18:57:29 -07:00
|
|
|
i2cDevice sysfs.I2cDevice
|
2014-11-09 14:35:36 -08:00
|
|
|
ocp string
|
|
|
|
helper string
|
|
|
|
slots string
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// NewAdaptor returns a new Beaglebone Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
b := &Adaptor{
|
2016-10-03 19:06:37 +02:00
|
|
|
name: "Beaglebone",
|
2014-11-09 14:35:36 -08:00
|
|
|
digitalPins: make([]sysfs.DigitalPin, 120),
|
|
|
|
pwmPins: make(map[string]*pwmPin),
|
2014-05-22 19:32:09 -07:00
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
|
|
|
g, _ := glob(ocp)
|
|
|
|
b.ocp = g[0]
|
|
|
|
g, _ = glob(slots)
|
|
|
|
b.slots = fmt.Sprintf("%v/slots", g[0])
|
|
|
|
|
|
|
|
return b
|
2014-04-28 04:39:51 -07:00
|
|
|
}
|
2014-12-31 06:46:47 -08:00
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// Name returns the Adaptor name
|
|
|
|
func (b *Adaptor) Name() string { return b.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor name
|
|
|
|
func (b *Adaptor) SetName(n string) { b.name = n }
|
2014-04-28 04:39:51 -07:00
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// Connect initializes the pwm and analog dts.
|
2016-11-07 17:33:11 +01:00
|
|
|
func (b *Adaptor) Connect() error {
|
2014-11-19 23:21:19 -08:00
|
|
|
if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
return err
|
2014-11-16 14:18:29 -08:00
|
|
|
}
|
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
if err := ensureSlot(b.slots, "am33xx_pwm"); err != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
return err
|
2014-11-16 14:18:29 -08:00
|
|
|
}
|
2014-11-10 12:48:08 -08:00
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp))
|
|
|
|
if err != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
return err
|
2014-11-16 14:18:29 -08:00
|
|
|
}
|
2014-11-10 12:48:08 -08:00
|
|
|
b.helper = g[0]
|
|
|
|
|
2016-11-07 17:33:11 +01:00
|
|
|
return nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// Finalize releases all i2c devices and exported analog, digital, pwm pins.
|
2016-11-07 17:33:11 +01:00
|
|
|
func (b *Adaptor) Finalize() (err error) {
|
2014-04-26 03:11:51 -07:00
|
|
|
for _, pin := range b.pwmPins {
|
|
|
|
if pin != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
if e := pin.release(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, pin := range b.digitalPins {
|
|
|
|
if pin != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
if e := pin.Unexport(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.i2cDevice != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
if e := b.i2cDevice.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// PwmWrite writes the 0-254 value to the specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
2014-11-16 14:18:29 -08:00
|
|
|
return b.pwmWrite(pin, val)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// ServoWrite writes the 0-180 degree val to the specified pin.
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) ServoWrite(pin string, val byte) (err error) {
|
2014-11-16 14:18:29 -08:00
|
|
|
i, err := b.pwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 18:35:45 -08:00
|
|
|
period := 16666666.0
|
|
|
|
duty := (gobot.FromScale(float64(val), 0, 180.0) * 0.115) + 0.05
|
2014-11-16 14:18:29 -08:00
|
|
|
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// DigitalRead returns a digital value from specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) DigitalRead(pin string) (val int, err error) {
|
2014-11-19 17:18:01 -08:00
|
|
|
sysfsPin, err := b.digitalPin(pin, sysfs.IN)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return sysfsPin.Read()
|
2014-06-28 12:05:17 -06:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// DigitalWrite writes a digital value to specified pin.
|
|
|
|
// valid usr pin values are usr0, usr1, usr2 and usr3
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) {
|
2014-09-17 15:20:08 -07:00
|
|
|
if strings.Contains(pin, "usr") {
|
2014-11-09 14:35:36 -08:00
|
|
|
fi, err := sysfs.OpenFile(usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
|
2014-10-30 15:26:31 -07:00
|
|
|
defer fi.Close()
|
2014-09-17 15:20:08 -07:00
|
|
|
if err != nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
return err
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.WriteString(strconv.Itoa(int(val)))
|
|
|
|
return err
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
sysfsPin, err := b.digitalPin(pin, sysfs.OUT)
|
2014-11-19 18:40:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
return sysfsPin.Write(int(val))
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// AnalogRead returns an analog value from specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
|
2014-11-19 17:18:01 -08:00
|
|
|
analogPin, err := b.translateAnalogPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.helper, analogPin), os.O_RDONLY, 0644)
|
2014-11-09 14:35:36 -08:00
|
|
|
defer fi.Close()
|
2014-11-16 14:18:29 -08:00
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
if err != nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-11-09 14:35:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf = make([]byte, 1024)
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// I2cStart starts a i2c device in specified address on i2c bus /dev/i2c-1
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) I2cStart(address int) (err error) {
|
2015-07-03 18:57:29 -07:00
|
|
|
if b.i2cDevice == nil {
|
|
|
|
b.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// I2cWrite writes data to i2c device
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) I2cWrite(address int, data []byte) (err error) {
|
2015-07-03 18:57:29 -07:00
|
|
|
if err = b.i2cDevice.SetAddress(address); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
_, err = b.i2cDevice.Write(data)
|
2015-07-03 18:57:29 -07:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// I2cRead returns size bytes from the i2c device
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
|
2015-07-03 18:57:29 -07:00
|
|
|
if err = b.i2cDevice.SetAddress(address); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
data = make([]byte, size)
|
|
|
|
_, err = b.i2cDevice.Read(data)
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translatePin converts digital pin name to pin position
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) translatePin(pin string) (value int, err error) {
|
2014-04-26 03:11:51 -07:00
|
|
|
for key, value := range pins {
|
|
|
|
if key == pin {
|
2014-11-19 17:18:01 -08:00
|
|
|
return value, nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
err = errors.New("Not a valid pin")
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translatePwmPin converts pwm pin name to pin position
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) translatePwmPin(pin string) (value string, err error) {
|
2014-04-26 03:11:51 -07:00
|
|
|
for key, value := range pwmPins {
|
|
|
|
if key == pin {
|
2014-11-19 17:18:01 -08:00
|
|
|
return value, nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
err = errors.New("Not a valid pin")
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translateAnalogPin converts analog pin name to pin position
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) translateAnalogPin(pin string) (value string, err error) {
|
2014-04-26 03:11:51 -07:00
|
|
|
for key, value := range analogPins {
|
|
|
|
if key == pin {
|
2014-11-19 17:18:01 -08:00
|
|
|
return value, nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
err = errors.New("Not a valid pin")
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// digitalPin retrieves digital pin value by name
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
|
2014-11-19 17:18:01 -08:00
|
|
|
i, err := b.translatePin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-30 15:26:31 -07:00
|
|
|
if b.digitalPins[i] == nil {
|
|
|
|
b.digitalPins[i] = sysfs.NewDigitalPin(i)
|
|
|
|
err := b.digitalPins[i].Export()
|
|
|
|
if err != nil {
|
2014-11-19 17:18:01 -08:00
|
|
|
return nil, err
|
2014-10-30 15:26:31 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
if err = b.digitalPins[i].Direction(dir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return b.digitalPins[i], nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// pwPin retrieves pwm pin value by name
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) pwmPin(pin string) (i string, err error) {
|
2014-11-19 17:18:01 -08:00
|
|
|
i, err = b.translatePwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
if b.pwmPins[i] == nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
err = ensureSlot(b.slots, fmt.Sprintf("bone_pwm_%v", pin))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.pwmPins[i], err = newPwmPin(i, b.ocp)
|
2014-11-19 17:18:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-06-28 12:05:17 -06:00
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// pwmWrite writes pwm value to specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) pwmWrite(pin string, val byte) (err error) {
|
2014-11-16 14:18:29 -08:00
|
|
|
i, err := b.pwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-28 12:05:17 -06:00
|
|
|
period := 500000.0
|
2014-12-23 18:35:45 -08:00
|
|
|
duty := gobot.FromScale(float64(val), 0, 255.0)
|
2014-11-16 14:18:29 -08:00
|
|
|
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
2014-06-28 12:05:17 -06:00
|
|
|
}
|
2014-06-11 13:59:06 -06:00
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
func ensureSlot(slots, item string) (err error) {
|
2014-11-09 14:35:36 -08:00
|
|
|
fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
|
2014-11-16 14:18:29 -08:00
|
|
|
defer fi.Close()
|
2014-06-11 13:59:06 -06:00
|
|
|
if err != nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-06-11 13:59:06 -06:00
|
|
|
}
|
|
|
|
|
2014-07-08 18:36:14 -07:00
|
|
|
// ensure the slot is not already written into the capemanager
|
|
|
|
// (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
|
2014-06-11 13:59:06 -06:00
|
|
|
scanner := bufio.NewScanner(fi)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Index(line, item) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.WriteString(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 18:35:45 -08:00
|
|
|
fi.Sync()
|
2014-06-11 13:59:06 -06:00
|
|
|
|
2014-06-30 19:50:28 -07:00
|
|
|
scanner = bufio.NewScanner(fi)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Index(line, item) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-06-11 13:59:06 -06:00
|
|
|
}
|