picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

using RPi.GPIO with defined classes

Sat Nov 30, 2019 10:00 am

I'm using straight code to run my I/O (GPIO.output(blah), GPIO.input(blah), etc)...that works ok.
However, I'm interested in defining and using classes..are there some good I/O examples/tutorials that use classes?

I want to have a class called MyPumps in module pumpcontrol
with some pump names defined (really they are just I/O pins), such as:

PumpCold is gpio17
PumpHOT is gpio27
PumpMix is gpio22

Then I want to have some methods to run the pump & do some calculations:
RunCalcs(pumpname)
io control & calcs go here
and one to ramp the pump
RampPump(pumpname)
io, timers

So I'd use it something like:
import pumpcontrol
mypump=pumpcontrol.MyPumps
mypump.RunCalcs(PumpCold)
mypump.RampPump(PumpHot)
etc
This is what I'm hoping to set up; I've got the general idea, but lack some details.

User avatar
B.Goode
Posts: 9826
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: using RPi.GPIO with defined classes

Sat Nov 30, 2019 10:26 am

The gpiozero Python library demonstrates some quite advanced and sophisticated use of python classes used with RPi.GPIO

Otherwise you probably just need a basic object-oriented python tutorial?

There is one in MagPi #54.

Or the Raspberry Pi organisation has a FutureLearn lesson.

Or almost any python textbook...

picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

Re: using RPi.GPIO with defined classes

Sat Nov 30, 2019 4:57 pm

I've seen a bunch of those articles with setting up classes, but none on any examples of using GPIO...maybe it is not done that way?
I looked at gpiozero Python library, but it seemed to be a list of commands, and not some real setup tutorial

User avatar
joan
Posts: 14841
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: using RPi.GPIO with defined classes

Sat Nov 30, 2019 5:32 pm

Have a look at the pigpio Python examples. Should be quite a few classes.

picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

Re: using RPi.GPIO with defined classes

Sat Nov 30, 2019 7:59 pm

Thanks, I'm looking for an article rather than just a listing (or a listing that uses RPi.GPIO) ...that shows both sides, the class and then using it...I can't tell where the errors occurs (whether the class is made up wrong (such as where to define the pin names & whether self is needed everywhere), or using it wrong).

picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

Re: using RPi.GPIO with defined classes

Sun Dec 01, 2019 9:46 am

After ten tons of mucking around I found the issue I was having:

Code: Select all

So I'd use it something like:
import pumpcontrol
mypump=pumpcontrol.MyPumps
should be:

Code: Select all

So I'd use it something like:
import pumpcontrol
mypump=pumpcontrol.MyPumps()
The original statement "worked" without generating an error, however lacking the () put me in a world of hurt, with strange error messages that sent me on goose chases whenever I tried to use the methods & initializations of the class (thinking I had screwed something in making the classes) . Also, lacking self, in a critical spot made it a double-error, even worse to diagnose.

Return to “Python”