Blinky and Button examples using Python
From Manuals
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
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, so 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)
