Jan 2012
17

Blinking LEDs and Python

The BeagleBone has a row of four LEDs next to the ethernet connector that can be controlled easily. On a board like the Arduino, you’re writing to a memory location mapped to an output pin. For devices like the BeagleBone that run linux, it’s more like writing to a virtual file and letting the kernel do the rest. What’s nice about this setup is that any programming language that can write to files can control your LEDS

Since the BeagleBone comes with Python, controlling the LEDs with a Python script is the easiest way to demo the LEDs without the need to compile or cross-compile. If you’re board is connected to the internet, the easiest way to get the python script is to use wget. SSH into the board

root@beaglebone:$ wget http://www.meadios.com/download/bb-led/knightriderleds.py
root@beaglebone:$ wget http://www.meadios.com/download/bb-led/randomleds.py

Now the files should be downloaded and you can run the program.

root@beaglebone:$ python knightriderleds.py

You should see the LEDs blinking in a Knight Rider fashion. The code is pretty straight forward. A termainal loops until the ‘q’ key is pressed. You can change the speed by altering the time.sleep() function arguments. The random LEDs program is very similar but uses a random number generator.

These examples may seem trivial but compared to an AVR microcontroller it’s a substantial amount of power for $89. Python is a powerful scripting language a that doesn’t require a complicated development setup and has a bunch of libraries that are easy to figure out.

knightriderleds.py

import sys
import time
import random
import termios
import atexit
from select import select

#kbhit credits go to http://code.activestate.com/recipes/572182-how-to-implement-kbhit-on-linux/

print "Knight Rider style LEDs on the BeagleBone"
print "Press 'q' to exit"

leds = [
'/sys/class/leds/beaglebone::usr0/brightness',
'/sys/class/leds/beaglebone::usr1/brightness',
'/sys/class/leds/beaglebone::usr2/brightness',
'/sys/class/leds/beaglebone::usr3/brightness',
]

def ledon(n):
        value = open(leds[n],'w')
        value.write(str(1))
        value.close()

def ledoff(n):
        value = open(leds[n],'w')
        value.write(str(0))
        value.close()

def toggle(x):
	ledon(x)
        time.sleep(.1)
        ledoff(x)

for i, val in enumerate(leds):
	ledoff(i)

# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)

# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)

# switch to normal terminal
def set_normal_term():
    termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)

# switch to unbuffered terminal
def set_curses_term():
    termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)

def getch():
    return sys.stdin.read(1)

def kbhit():
    dr,dw,de = select([sys.stdin], [], [], 0)
    return dr <> []

if __name__ == '__main__':
    atexit.register(set_normal_term)
    set_curses_term()

    while 1:
        if kbhit():
            ch = getch()
	    if ch == 'q':
            	break
	for x in range(0, 4, 1):
		toggle(x)

	for x in range(2, 0, -1):
		toggle(x)
    print 'Exiting...'

randomleds.py

import sys
import time
import random
import termios
import atexit
from select import select

#kbhit credits go to http://code.activestate.com/recipes/572182-how-to-implement-kbhit-on-linux/

print "Random blinking LEDs on the BeagleBone"
print "Press 'q' to exit"

leds = [
'/sys/class/leds/beaglebone::usr0/brightness',
'/sys/class/leds/beaglebone::usr1/brightness',
'/sys/class/leds/beaglebone::usr2/brightness',
'/sys/class/leds/beaglebone::usr3/brightness',
]

def ledon(n):
        value = open(leds[n],'w')
        value.write(str(1))
        value.close()

def ledoff(n):
        value = open(leds[n],'w')
        value.write(str(0))
        value.close()

for i, val in enumerate(leds):
	ledoff(i)

# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)

# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)

# switch to normal terminal
def set_normal_term():
    termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)

# switch to unbuffered terminal
def set_curses_term():
    termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)

def getch():
    return sys.stdin.read(1)

def kbhit():
    dr,dw,de = select([sys.stdin], [], [], 0)
    return dr <> []

if __name__ == '__main__':
    atexit.register(set_normal_term)
    set_curses_term()

    while 1:
        if kbhit():
            ch = getch()
	    if ch == 'q':
            	break
        x = random.randrange(0, len(leds), 1) #range is 0 to 4 but 4 is excluded
        ledon(x)
        time.sleep(.1)
        ledoff(x)
        time.sleep(.5)

    print 'Exiting...'
Tags: ,

Please log in to post comments



| Register

Social Networking



User Name

Password
Remember Me
Forgot Password?

A password will be e-mailed to you.