SEARCH
TOOLBOX
LANGUAGES
modified on 5 August 2010 at 16:03 ••• 44,211 views

Blinky and Button examples using Python

From Manuals

(Difference between revisions)
Jump to: navigation, search
(Examples using Python)
(Button Example)
Line 52: Line 52:
== Button Example==
== Button Example==
 +
In Electrum100, GPIO PA31 is used for the user Button.
  import sys
  import sys
  import time
  import time

Revision as of 17:15, 3 August 2010

Contents

Examples using Python

The following application note covers the use of Electrum100's peripherals using Python. SYSFS method is used to access the GPIO's. To create the interfaces for GPIO's under /sys/class/gpio/*, the following configuration has to be enabled in the board configuration file

CONFIG_GPIO_SYSFS=Y

Blinky Example

User LED is connected to GPIO PA30. An interface for the user LED is created in the kernel by adding the following code in board-electrum-100.c

/*
 * LEDs
 */
static struct gpio_led ek_leds[] = {
	{	/* led1, yellow */
		.name			= "ds1",
		.gpio			= AT91_PIN_PA25,
		.default_trigger	= "heartbeat",
	},
	{	/* led2, green */
		.name			= "ds2",
		.gpio			= AT91_PIN_PA30,
		//.active_low		= 1,
		.default_trigger	= "none",
	}
};

And add the following lines in ek_board_init()

/* LEDs */
	at91_gpio_leds(ek_leds, ARRAY_SIZE(ek_leds));

Python script for Blinky Example:

import sys
import time

print "Blinking User LED"
print "Enter CTRL+C to exit"

def ledon():
        value = open("/sys/class/leds/ds2/brightness","w")
        value.write(str(1))
        value.close()

def ledoff():
        value = open("/sys/class/leds/ds2/brightness","w")
        value.write(str(0))
        value.close()

while True:
        ledon()
        time.sleep(.5)
        ledoff()
        time.sleep(.5)

Button Example

In Electrum100, GPIO PA31 is used for the user Button.

import sys
import time

print "Button Example"
print "Press CTRL + C to exit"

def pins_export():
        try:
                pin1export = open("/sys/class/gpio/export","w")
                pin1export.write(str(63))
                pin1export.close()
        except IOError:
                print "INFO: GPIO 63 already Exists, skipping export gpio"

def read_button():
        fp1 = open("/sys/class/gpio/gpio63/value","r")
        value = fp1.read()
        return value
        fp1.close()

def write_led(value):
        fp2 =  open("/sys/class/leds/ds2/brightness","w")
        fp2.write(str(value))
        fp2.close()

pins_export()
while True:
        button_value =  read_button()
        write_led(button_value)