diff --git a/examples/up2_leds.go b/examples/up2_leds.go index 3676b73a..2994837b 100644 --- a/examples/up2_leds.go +++ b/examples/up2_leds.go @@ -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() diff --git a/platforms/upboard/up2/README.md b/platforms/upboard/up2/README.md index 4fd859c7..950f34fc 100644 --- a/platforms/upboard/up2/README.md +++ b/platforms/upboard/up2/README.md @@ -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 diff --git a/platforms/upboard/up2/adaptor.go b/platforms/upboard/up2/adaptor.go index 10fa9add..51423589 100644 --- a/platforms/upboard/up2/adaptor.go +++ b/platforms/upboard/up2/adaptor.go @@ -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 @@ -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()