Skip to content

Commit

Permalink
Merge pull request adafruit#88 from pdp7/master
Browse files Browse the repository at this point in the history
Add support for on-board LEDs (USR0-USR3)
  • Loading branch information
ladyada committed Jan 3, 2016
2 parents 6dad329 + b620b3f commit 93f3558
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ Setup the pin for output, and write GPIO.HIGH or GPIO.LOW. Or you can use 1 or 0

import Adafruit_BBIO.GPIO as GPIO
GPIO.setup("P8_14", GPIO.OUT) GPIO.output("P8_14", GPIO.HIGH)

**On-Board LEDs**

On-board LEDs (USR0-USR3) are handled by LED class driver rather than the GPIO pin driver.

They have a different path in the /sys/ filesystem.

Setup the pin for output and write GPIO.HIGH or GPIO.LOW.

import Adafruit_BBIO.GPIO as GPIO
import time

for i in range(4):
GPIO.setup("USR%d" % i, GPIO.OUT)

while True:
for i in range(4):
GPIO.output("USR%d" % i, GPIO.HIGH)
time.sleep(1)
for i in range(4):
GPIO.output("USR%d" % i, GPIO.LOW)
time.sleep(1)

**GPIO Input**

Expand Down
19 changes: 14 additions & 5 deletions source/event_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,17 @@ int add_fd_list(unsigned int gpio, int fd)
int open_value_file(unsigned int gpio)
{
int fd;
char filename[40];
char filename[MAX_FILENAME];

// create file descriptor of value file
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
if ((gpio >= USR_LED_GPIO_MIN) && (gpio <= USR_LED_GPIO_MAX)) {
snprintf(filename, sizeof(filename), "/sys/class/leds/beaglebone:green:usr%d/brightness", gpio - USR_LED_GPIO_MIN);
} else {
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
}

if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
return -1;
return -1;
add_fd_list(gpio, fd);
return fd;
}
Expand Down Expand Up @@ -262,10 +267,14 @@ int gpio_get_direction(unsigned int gpio, unsigned int *value)
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd;
char filename[40];
char filename[MAX_FILENAME];
char vstr[10];

snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
if ((gpio >= USR_LED_GPIO_MIN) && (gpio <= USR_LED_GPIO_MAX)) {
snprintf(filename, sizeof(filename), "/sys/class/leds/beaglebone:green:usr%d/brightness", gpio - USR_LED_GPIO_MIN);
} else {
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
}

if ((fd = open(filename, O_WRONLY)) < 0)
return -1;
Expand Down
5 changes: 5 additions & 0 deletions source/event_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ SOFTWARE.
#define HIGH 1
#define LOW 0

#define MAX_FILENAME 50

#define USR_LED_GPIO_MIN 53
#define USR_LED_GPIO_MAX 56

#define PUD_OFF 0
#define PUD_DOWN 1
#define PUD_UP 2
Expand Down
22 changes: 22 additions & 0 deletions test/test_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
import os

import Adafruit_BBIO.GPIO as GPIO

def teardown_module(module):
GPIO.cleanup()

class TestLED:
def test_brightness_high(self):
GPIO.setup("USR0", GPIO.OUT)
GPIO.output("USR0", GPIO.HIGH)
value = open('/sys/class/leds/beaglebone:green:usr0/brightness').read()
assert int(value) > 0
GPIO.cleanup()

def test_brightness_low(self):
GPIO.setup("USR0", GPIO.OUT)
GPIO.output("USR0", GPIO.LOW)
value = open('/sys/class/leds/beaglebone:green:usr0/brightness').read()
assert int(value) == 0
GPIO.cleanup()

0 comments on commit 93f3558

Please sign in to comment.