• Welcome to Randomland - The Forum. Please login or sign up.
 
May 03, 2024, 06:08 AM

News:

Get Out, See Green!


RPi.GPIO object oriented wrapper

Started by zourtney, Aug 07, 2013, 07:23 AM

Previous topic - Next topic

zourtney

Pi'ers,

I started a little project to wrap the Rasberry Pi RPi.GPIO library in a more object-oriented architecture (meat). It's all woefully incomplete, but lets you do things like this:


from gpiocrust import Header, OutputPin

with Header() as header:
    my_led = OutputPin(11)   # create pin set for output. Defaults to OFF
    my_led.value = True      # or 1

# Cleanup is automatic.


I also wrapped up the pulse width modulated output pin functionality, so you can (effectively) get variable output levels.


from gpiocrust import Header, PWMOutputPin

with Header() as header:
    # Create software PWM'ed pin @100Hz, half-brightness
    my_led = PWMOutputPin(11, frequency=100, value=0.5)
   
    # Nah, go for epilepsy mode!
    my_led.frequency = 10
    my_led.value = 0.9


(You don't have to use with statements, it's just good for examples)

We can build this out for inputs and whatever else I don't even know about yet. The goal is just something a little more "pythonic" than the thinly veiled C library. Let me know if you all are interested.

Brad

Very cool. I am definitely interested in playing around with this when I get me second non-pirate ship oi and camera.

zourtney

Aug 07, 2013, 08:15 PM #2 Last Edit: Aug 07, 2013, 08:21 PM by zourtney
Sa-weet, that's just what I was hoping for :) When you get your hardware, just pull down the code and hack away. Add your two cents, half-dollars, Cheez-Its -- whatever. The more minds on this, the better.



I'm dreaming about how we could leverage @decorator syntax in a meaningful way. With inputs and eventing (of which I know virtually nothing), maybe something like:


@mondo_button.on
def do_mondo_action():
  pass


The good news is that my pack on tiny buttons just arrived in the mail today. So now I can learn how to say hello to the other half of the I/O world. This is fun. I should've been doing this stuff a decade ago!

zourtney

Aug 08, 2013, 07:58 PM #3 Last Edit: Aug 08, 2013, 08:00 PM by zourtney
...well, that was easy. I played with my switches and updated the library accordingly. A simple non-polling input driven program would look something like this:


from gpiocrust import Header, InputPin

with Header() as header:
  switch = InputPin(11)   # pull-down by default

  @switch.change
  def take_action(value):
    if value:
      pass     # it is high
    else:
      pass     # it is low


And that is all.

zourtney

I updated this lil' library to fall back to mock classes when RPi.GPIO cannot be imported. That way one can build out and test the general I/O flow from any system. Then, deploy it to the Pi when you're ready to try it on real hardware.

This fallback logic is under __init__.py. It will be transparent to the calling code, expect for a warning message piped to the console:

--------------------------------------------------------------------
WARNING: RPi.GPIO library not found. Falling back to mock objects.
--------------------------------------------------------------------

Nick

Thats cool. This may turn unto a project that people at large might be interested in. PI people, anyway.