diff --git a/examples/up2_leds.go b/examples/up2_leds.go new file mode 100644 index 00000000..3676b73a --- /dev/null +++ b/examples/up2_leds.go @@ -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() +} diff --git a/platforms/upboard/up2/README.md b/platforms/upboard/up2/README.md index 130e53ec..4fd859c7 100644 --- a/platforms/upboard/up2/README.md +++ b/platforms/upboard/up2/README.md @@ -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: ``` diff --git a/platforms/upboard/up2/adaptor.go b/platforms/upboard/up2/adaptor.go index 111b0163..10fa9add 100644 --- a/platforms/upboard/up2/adaptor.go +++ b/platforms/upboard/up2/adaptor.go @@ -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 diff --git a/platforms/upboard/up2/adaptor_test.go b/platforms/upboard/up2/adaptor_test.go index 7d23e37c..b448d953 100644 --- a/platforms/upboard/up2/adaptor_test.go +++ b/platforms/upboard/up2/adaptor_test.go @@ -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) }