Created
March 6, 2012 15:49
-
-
Save mrichardson23/1986926 to your computer and use it in GitHub Desktop.
BeagleBone GPIO with Python Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# time module is needed for sleep function: | |
import time | |
# Open up the pins and set mode in/out | |
# TODO: Error handling | |
setupPin = file("/sys/class/gpio/export", "w") | |
setupPin.write("%d" % (38)) | |
setupPin.close() | |
setupPin = file("/sys/class/gpio/gpio38/direction", "w") | |
setupPin.write("out") | |
setupPin.close() | |
setupPin = file("/sys/class/gpio/export", "w") | |
setupPin.write("%d" % (70)) | |
setupPin.close() | |
setupPin = file("/sys/class/gpio/gpio70/direction", "w") | |
setupPin.write("in") | |
setupPin.close() | |
#Open outPin for writing and inPin for reading: | |
outPin = file("/sys/class/gpio/gpio38/value", "w") | |
inPin = file("/sys/class/gpio/gpio70/value", "r") | |
try: | |
while True: | |
# This method uses polling to read the state of the pin. | |
# It's not the way it should be done, but we'll make do | |
# with this for now. | |
inPin.seek(0) | |
if inPin.read() =="1\n": | |
print "Button Pressed!\n" | |
outPin.write("1") | |
outPin.flush() | |
time.sleep(1) | |
outPin.write("0") | |
outPin.flush() | |
# don't peg the processor: | |
time.sleep(.1) | |
# Control+C will get us out of that loop and close the pins so that other apps can use: | |
except KeyboardInterrupt: | |
inPin = file("/sys/class/gpio/unexport", "w") | |
inPin.write("%d" % (38)) | |
inPin.close() | |
outPin = file("/sys/class/gpio/unexport", "w") | |
outPin.write("%d" % (70)) | |
inPin.close() |
Okay, hello there! So, I'm quite in love with python. I started it about 4 months ago. I'm more than efficient in languages Java, VB.Net/C#, PHP, and some C/C++ and ASM. Python was a cinch to pick up.
I was wondering, in the likely event that I don't use the default BBB's OS, where can I get the python API files for this? From the original out-of-box OS and just copy the file over to the new /sys ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, the value file should be closed. This code is a little old and I abstracted these functions into a Python module: https://github.com/mrichardson23/mrBBIO