Hi all,


 I'm new the BBB and have taken one college course in programming. Intro to 
programming using Python3. I wanted to program the BBB using Python3, but 
ran into the issue that Adafruit's module was Python 2.7. I struggled with 
this for a while, until I came across a post by Joran B. on another 
website. He provided a bit of code to simply use the GPIO directly in 
Python3. I'm sure it's obvious to the advanced users, but I found his post 
very useful. His code didn't work for me at first, but I was able to add a 
few lines and get it working. I've attached the file. Now I just simply 
import it (add “from gpio import SimpleGPIO” to the beginning of my python 
code) and it works great. I learned a ton about how to access and use the 
GPIO by going through the code in his post and getting it to work. Any 
other newbies wanted to do something similar, I've attached the file for 
reference as well as an example of where I used it during my test. Please 
note, I haven't tested the xxx.read() method. Just turned on and off an LED 
so far.

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
import os

class SimpleGPIO:
     def __init__(self,gpio_pin):
         self.gpio_pin = gpio_pin
         os.system('echo %d > /sys/class/gpio/export'%self.gpio_pin)
         self.gpio_path = "/sys/class/gpio/gpio%d/"%gpio_pin
         with open(self.gpio_path+"direction") as f:
             self.direction = f.read()
     def write(self,value):
         if self.direction != "out":
             os.system("echo out > %sdirection"%self.gpio_path)
             self.direction = "out"
         os.system("echo %s > %svalue"%(value, self.gpio_path))
     def read(self):
         if self.direction != "in":
             os.system("echo in > %sdirection"%self.gpio_path)
             self.direction = "in"
         with open(self.gpio_path+value) as f:
             return f.read()
gpio_67 = SimpleGPIO(67)
gpio_67.write('high')
from gpio import SimpleGPIO
gpio_67 = SimpleGPIO(67)
gpio_67.write('high')

Reply via email to