On Fri, 15 Nov 2019 20:49:08 +0100, "R.Wieser"
declaimed the following:
>
>import RPi.GPIO as GPIO1
>GPIO1.setmode(GPIO1.BCM)
>import RPi.GPIO as GPIO2
>GPIO2.setmode(GPIO2.BOARD)
>
GPIO1 and GPIO2 are "local" names both bound to the same, single
RPi.GPIO module. The second import will find that RPi.GPIO has already been
imported and will use that copy. The above code is equivalent to
import RPi.GPIO #loads the module, creates local name RPi.GPIO
GPIO1 = RPi.GPIO
GPIO2 = RPi.GPIO #create two local names bound to the module
del RPi.GPIO #remove RPi.GPIO from local names -- module is
#still bound to other names, so is not garbage
#collected.
{Yes, the discussion on how Python name-binding differs from common
language assignment has spread here}
>, but that threw an error on the second ".setmode()"
What documentation I can find does not state if .setmode() is
one-time-only call; only states that one must call it before using any
pins. Unfortunately, this is one case where read the package source is not
readily viable. The Python RPi.GPIO imports a binary _GPIO library to
implement the actual operations.
Downloading the source code from SourceForge one finds:
"""
// python function setmode(mode)
static PyObject *py_setmode(PyObject *self, PyObject *args)
{
int new_mode;
if (!PyArg_ParseTuple(args, "i", &new_mode))
return NULL;
if (gpio_mode != MODE_UNKNOWN && new_mode != gpio_mode)
{
PyErr_SetString(PyExc_ValueError, "A different mode has already been
set!");
return NULL;
}
"""
Which confirms that setmode is a one-time operation, and can not be changed
later.
>
>So my question is:
>Can I use two different pinnumbering schemes at the same time ? And if so,
>how ?
>
>Maybe by instanciating RPi.GPIO as an object (making all settings local to
>it) ?
>
My suggestion... Don't use RPi.GPIO directly. GPIOzero seems to be the
current package pushed by the R-Pi Foundation. It wraps over various of the
GPIO libraries available.
https://gpiozero.readthedocs.io/en/stable/installing.html
https://gpiozero.readthedocs.io/en/stable/recipes.html#pin-numbering
"""
2.2. Pin Numbering
This library uses Broadcom (BCM) pin numbering for the GPIO pins, as
opposed to physical (BOARD) numbering. Unlike in the RPi.GPIO library, this
is not configurable. However, translation from other schemes can be used by
providing prefixes to pin numbers (see below).
"""
"""
If you wish to use physical (BOARD) numbering you can specify the pin
number as “BOARD11”. If you are familiar with the wiringPi pin numbers
(another physical layout) you could use “WPI0” instead. Finally, you can
specify pins as “header:number”, e.g. “J8:11” meaning physical pin 11
on
header J8 (the GPIO header on modern Pis). Hence, the following lines are
all equivalent:
"""
https://gpiozero.readthedocs.io/en/stable/migrating_from_rpigpio.html
"""
GPIO Zero was originally just a layer on top of RPi.GPIO, but we later
added support for various other underlying pin libraries. RPi.GPIO is
currently the default pin library used. Read more about this in Changing
the pin factory.
"""
It is recommended that you do not mix "pin factories" in the
application, but you can control if RPi.GPIO, pigpio, or RPIO is
used.
You likely won't have as many places where you need to specify the pin.
Instead of RPi.GPIO's functions that all need pin numbers (as in)
gpio.setup(pin, gpio.OUT)
gpio.output(pin, gpio.HIGH)
gpio.output(pin, not gpio.input(pin)) #toggle pin state
(where pin depends on what setmode had been used -- and the above uses pin
FOUR time)
GPIOzero would use
myOutput = gpiozero.LED(pin)
myOutput.on()
myOutput.toggle()
(where pin could be xx, "BCMxx", "GPIOxx", "BOARDyy", "J8:yy" for xx being
the BCM number, yy being the BOARD number -- and pin is only used ONCE)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)
|