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

Blinky and Button examples using Python

From Manuals

(Difference between revisions)
Jump to: navigation, search
(Button Example)
(Examples using Python)
 
(6 intermediate revisions not shown)
Line 1: Line 1:
__TOC__
__TOC__
=Examples using Python=
=Examples using Python=
-
The following application note covers the use of Electrum100's peripherals using Python. SYSFS method has been used to access the GPIO's.
+
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
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
+
  CONFIG_GPIO_SYSFS=y
==Blinky Example==
==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 sys
  import time
  import time
Line 29: Line 52:
== Button Example==
== Button Example==
 +
In Electrum100, GPIO PA31 is used for the User Button.
  import sys
  import sys
  import time
  import time

Current revision as of 16:03, 5 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)