#!/usr/bin/env python """ Demonstration of GPIO access under Python for Bifferboard modified by Sunspot to control lines 0 and 1 place in /usr/sbin 755 (www.bifferos.com) (www.sunspot.co.uk) """ import os, fcntl, struct, time, sys class GPIO: "Class to encapsulate communication with the driver" def __init__(self): self.fd = os.open("/dev/gpio_proxy", os.O_RDWR) def gpio_set_value(self, pin, val): if val: fcntl.ioctl(self.fd, 1, pin) else: fcntl.ioctl(self.fd, 2, pin) def gpio_direction_output(self, pin, val): if val: fcntl.ioctl(self.fd, 3, pin) else: fcntl.ioctl(self.fd, 4, pin) def status(self): fcntl.ioctl(self.fd, 5, 0) def gpio_direction_input(self, pin): fcntl.ioctl(self.fd, 6, pin) def gpio_get_value(self, pin): return fcntl.ioctl(self.fd, 7, pin) def flash1(g): g.gpio_direction_output(1,1) # Print the status of the GPIO register g.status() # Flash the red LED on and off ten times. for i in xrange(0,3): g.gpio_set_value(1,1) time.sleep(0.5) g.gpio_set_value(1,0) time.sleep(0.5) def flash0(g): g.gpio_direction_output(0,1) # Print the status of the GPIO register g.status() # Flash the red Green on and off ten times. for i in xrange(0,3): g.gpio_set_value(0,1) time.sleep(0.5) g.gpio_set_value(0,0) time.sleep(0.5) def flash5(g): g.gpio_direction_output(5,1) # Print the status of the GPIO register g.status() # Flash the red Green on and off ten times. for i in xrange(0,3): g.gpio_set_value(5,1) time.sleep(0.5) g.gpio_set_value(5,0) time.sleep(0.5) def led0_on(g): g.gpio_direction_output(0,1) # Print the status of the GPIO register g.status() # Turn the green led on print "The green LED 0 in ON" g.gpio_set_value(0,1) def led0_off(g): g.gpio_direction_output(0,1) # Print the status of the GPIO register g.status() # Turn the green led off print "The green LED 0 is OFF" g.gpio_set_value(0,0) def led1_on(g): g.gpio_direction_output(1,1) # Print the status of the GPIO register g.status() # Turn the green led on print "The red LED 1 is ON" g.gpio_set_value(1,1) def led1_off(g): g.gpio_direction_output(1,1) # Print the status of the GPIO register g.status() # Turn the green led off print "The red LED 1 is OFF" g.gpio_set_value(1,0) def led5_on(g): g.gpio_direction_output(5,1) # Print the status of the GPIO register g.status() # Turn the green led on print "The red LED 5 is OFF" g.gpio_set_value(5,1) def led5_off(g): g.gpio_direction_output(5,1) # Print the status of the GPIO register g.status() # Turn the green led off print "The red LED 5 is ON" g.gpio_set_value(5,0) def button(g): # Sample the button status # You must set the value of an input pin to '1' before using it as input g.gpio_set_value(4,1) g.gpio_direction_input(4) button = g.gpio_get_value(4) print "Button is now", button def Usage(): print "led_control.py - Control the green and red leds on gpio 0 and 1" print print "usage: led_control.py " print print " can be:" print "'flash0': Flash the green LED 0 on and off" print "'flash1': Flash the red LED 1 on and off" print "'button': Sample the button state" print "'led0_on or led0_off': Switch the green led 0" print "'led1_on or led1_off': Switch the red led 1" print "'led5_on or led5_off': Switch the surface mount red led 5" print if __name__ == "__main__": if sys.argv[1:]: cmd = sys.argv[1] if cmd == "flash1": flash1(GPIO()) elif cmd == "flash0": flash0(GPIO()) elif cmd == "flash5": flash5(GPIO()) elif cmd == "led0_on": led0_on(GPIO()) elif cmd == "led0_off": led0_off(GPIO()) elif cmd == "led1_on": led1_on(GPIO()) elif cmd == "led1_off": led1_off(GPIO()) elif cmd == "led5_on": led5_on(GPIO()) elif cmd == "led5_off": led5_off(GPIO()) elif cmd == "button": button(GPIO()) else: Usage() else: Usage()