Wednesday, March 24, 2010

Python code to read GPIO

#!/usr/bin/python
import struct

Event_Device = "/dev/input/event0"
Event_Format = 'iihhi'

Event_File = open(Event_Device, "rb")
This_Event = Event_File.read(16)
while This_Event:
(stamp1, stamp2, type, code, value) = struct.unpack(Event_Format, This_Event)
if type == 1 and value == 1 and code == 275:
print " GPIO 133 button pressed! "
if code == 276:
Event_File.close()
print "exiting...."
exit(0)
This_Event = Event_File.read(16)
Event_File.close()

No comments:

Post a Comment