Skip to content

Commit

Permalink
up2: useful constant values to access the built-in LEDs
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed May 22, 2019
1 parent 38b019f commit 7c50801
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/up2_leds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

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")
red := gpio.NewLedDriver(b, up2.LEDRed)
blue := gpio.NewLedDriver(b, up2.LEDBlue)
green := gpio.NewLedDriver(b, up2.LEDGreen)
yellow := gpio.NewLedDriver(b, up2.LEDYellow)

work := func() {
red.Off()
Expand Down
7 changes: 7 additions & 0 deletions platforms/upboard/up2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ r := up2.NewAdaptor()
led := gpio.NewLedDriver(r, "13")
```

You can also use the values `up2.LEDRed`, `up2.LEDBlue`, `up2.LEDGreen`, and `up2.LEDYellow` as pin reference to access the 4 built-in LEDs. For example:

```go
r := up2.NewAdaptor()
led := gpio.NewLedDriver(r, up2.LEDRed)
```

## How to Connect

### Compiling
Expand Down
16 changes: 15 additions & 1 deletion platforms/upboard/up2/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import (
"gobot.io/x/gobot/sysfs"
)

const (
// LEDRed is the built-in red LED.
LEDRed = "red"

// LEDBlue is the built-in blue LED.
LEDBlue = "blue"

// LEDGreen is the built-in green LED.
LEDGreen = "green"

// LEDYellow is the built-in yellow LED.
LEDYellow = "yellow"
)

type sysfsPin struct {
pin int
pwmPin int
Expand Down Expand Up @@ -110,7 +124,7 @@ 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" {
if pin == LEDRed || pin == LEDBlue || pin == LEDGreen || pin == LEDYellow {
pinPath := fmt.Sprintf(c.ledPath, pin)
fi, e := sysfs.OpenFile(pinPath, os.O_WRONLY|os.O_APPEND, 0666)
defer fi.Close()
Expand Down

0 comments on commit 7c50801

Please sign in to comment.