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

Blinky and Button examples using Python

From Manuals

(Difference between revisions)
Jump to: navigation, search
(Button Example)
(Blinky Example)
Line 6: Line 6:
==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 scrip for Blinky Example:
  import sys
  import sys
  import time
  import time

Revision as of 17:11, 3 August 2010

Contents

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. 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 scrip 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)