up2: useful constant values to access the built-in LEDs

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-09-08 14:25:20 +02:00
parent 38b019ff36
commit 7c50801753
3 changed files with 26 additions and 5 deletions

View File

@ -14,10 +14,10 @@ import (
func main() { func main() {
b := up2.NewAdaptor() b := up2.NewAdaptor()
red := gpio.NewLedDriver(b, "red") red := gpio.NewLedDriver(b, up2.LEDRed)
blue := gpio.NewLedDriver(b, "blue") blue := gpio.NewLedDriver(b, up2.LEDBlue)
green := gpio.NewLedDriver(b, "green") green := gpio.NewLedDriver(b, up2.LEDGreen)
yellow := gpio.NewLedDriver(b, "yellow") yellow := gpio.NewLedDriver(b, up2.LEDYellow)
work := func() { work := func() {
red.Off() red.Off()

View File

@ -80,6 +80,13 @@ r := up2.NewAdaptor()
led := gpio.NewLedDriver(r, "13") 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 ## How to Connect
### Compiling ### Compiling

View File

@ -14,6 +14,20 @@ import (
"gobot.io/x/gobot/sysfs" "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 { type sysfsPin struct {
pin int pin int
pwmPin 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. // DigitalWrite writes digital value to the specified pin.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) { func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
// is it one of the built-in LEDs? // 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) pinPath := fmt.Sprintf(c.ledPath, pin)
fi, e := sysfs.OpenFile(pinPath, os.O_WRONLY|os.O_APPEND, 0666) fi, e := sysfs.OpenFile(pinPath, os.O_WRONLY|os.O_APPEND, 0666)
defer fi.Close() defer fi.Close()