up2: add support for built-in LEDs

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-09-07 21:26:45 +02:00
parent 518f4e921f
commit 38b019ff36
4 changed files with 91 additions and 2 deletions

49
examples/up2_leds.go Normal file
View File

@ -0,0 +1,49 @@
// +build example
//
// Do not build by default.
package main
import (
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/platforms/upboard/up2"
)
func main() {
b := up2.NewAdaptor()
red := gpio.NewLedDriver(b, "red")
blue := gpio.NewLedDriver(b, "blue")
green := gpio.NewLedDriver(b, "green")
yellow := gpio.NewLedDriver(b, "yellow")
work := func() {
red.Off()
blue.Off()
green.Off()
yellow.Off()
gobot.Every(1*time.Second, func() {
red.Toggle()
})
gobot.Every(2*time.Second, func() {
green.Toggle()
})
gobot.Every(4*time.Second, func() {
yellow.Toggle()
})
gobot.Every(8*time.Second, func() {
blue.Toggle()
})
}
robot := gobot.NewRobot("blinkBot",
[]gobot.Connection{b},
[]gobot.Device{red, blue, green, yellow},
work,
)
robot.Start()
}

View File

@ -35,6 +35,23 @@ sudo groupadd gpio
sudo adduser upsquared gpio
```
To use the built-in LEDs you will need to create a new group, add your user to the group, and then add a UDEV rule.
Add the following text as a new UDEV rule named `/etc/udev/rules.d/99-leds.rules`:
```
SUBSYSTEM=="leds*", PROGRAM="/bin/sh -c '\
chown -R root:leds /sys/class/leds && chmod -R 770 /sys/class/leds;\
chown -R root:leds /sys/devices/platform/up-pinctrl/leds && chmod -R 770 /sys/devices/platform/up-pinctrl/leds;\
chown -R root:leds /sys/devices/platform/AANT0F01:00/upboard-led.* && chmod -R 770 /sys/devices/platform/AANT0F01:00/upboard-led.*;\
'"
```
```
sudo groupadd leds
sudo adduser upsquared leds
```
To access the I2C subsystem, run the following command:
```

View File

@ -3,6 +3,8 @@ package up2
import (
"errors"
"fmt"
"os"
"strconv"
"sync"
multierror "github.com/hashicorp/go-multierror"
@ -21,6 +23,7 @@ type sysfsPin struct {
type Adaptor struct {
name string
pinmap map[string]sysfsPin
ledPath string
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBuses [6]i2c.I2cDevice
@ -35,8 +38,9 @@ type Adaptor struct {
// NewAdaptor creates a UP2 Adaptor
func NewAdaptor() *Adaptor {
c := &Adaptor{
name: gobot.DefaultName("UP2"),
mutex: &sync.Mutex{},
name: gobot.DefaultName("UP2"),
mutex: &sync.Mutex{},
ledPath: "/sys/class/leds/upboard:%s:/brightness",
}
c.setPins()
@ -105,6 +109,18 @@ func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
// DigitalWrite writes digital value to the specified pin.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
// is it one of the built-in LEDs?
if pin == "red" || pin == "blue" || pin == "green" || pin == "yellow" {
pinPath := fmt.Sprintf(c.ledPath, pin)
fi, e := sysfs.OpenFile(pinPath, os.O_WRONLY|os.O_APPEND, 0666)
defer fi.Close()
if e != nil {
return e
}
_, err = fi.WriteString(strconv.Itoa(int(val)))
return err
}
// one of the normal GPIO pins, then
sysfsPin, err := c.DigitalPin(pin, sysfs.OUT)
if err != nil {
return err

View File

@ -39,6 +39,7 @@ func initTestUP2Adaptor() (*Adaptor, *sysfs.MockFilesystem) {
"/sys/class/pwm/pwmchip0/pwm0/period",
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
"/sys/class/pwm/pwmchip0/pwm0/polarity",
"/sys/class/leds/upboard:green:/brightness",
})
sysfs.SetFilesystem(fs)
@ -63,6 +64,12 @@ func TestUP2AdaptorDigitalIO(t *testing.T) {
i, _ := a.DigitalRead("13")
gobottest.Assert(t, i, 1)
a.DigitalWrite("green", 1)
gobottest.Assert(t,
fs.Files["/sys/class/leds/upboard:green:/brightness"].Contents,
"1",
)
gobottest.Assert(t, a.DigitalWrite("99", 1), errors.New("Not a valid pin"))
gobottest.Assert(t, a.Finalize(), nil)
}