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

Blinky and Button examples using Python

From Manuals

(Difference between revisions)
Jump to: navigation, search
(Blinky Example)
(Examples using Python)
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
Line 24: Line 24:
  };
  };
-
And add the following lines in ek_board_init
+
And add the following lines in ek_board_init()
  /* LEDs */
  /* LEDs */
  at91_gpio_leds(ek_leds, ARRAY_SIZE(ek_leds));
  at91_gpio_leds(ek_leds, ARRAY_SIZE(ek_leds));

Revision as of 17:13, 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

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)