Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for use as a drop-in replacement for RPi.GPIO for gpiozero #10

Open
theyoyojo opened this issue May 8, 2020 · 12 comments
Open
Assignees
Labels
Software Engineering/Feature New feature or request

Comments

@theyoyojo
Copy link
Contributor

In response to: gpiozero/gpiozero#840

@theyoyojo theyoyojo self-assigned this May 8, 2020
@bennuttall
Copy link

In particular, to be a drop-in replacement, there would need to be a "pin factory" implementation, which normalises the functionality of a pin library into a standard set of methods. The RPi.GPIO one is here: https://github.com/gpiozero/gpiozero/blob/master/gpiozero/pins/rpigpio.py

More info: https://gpiozero.readthedocs.io/en/latest/api_pins.html

@theyoyojo
Copy link
Contributor Author

theyoyojo commented May 15, 2020

A test-driven way we could approach this feature would be to integrate a version of that file into our testing system as we move towards API-equivalence with RPi.GPIO.

@theyoyojo theyoyojo added the Software Engineering/Feature New feature or request label May 15, 2020
@theyoyojo
Copy link
Contributor Author

theyoyojo commented Jun 12, 2020

I think that we are getting close to full compatibility with #23. #25, and #26

@lurch
Copy link

lurch commented Jun 12, 2020

Nice, I'll try to have a look at it this weekend (if I remember!) 👍

@theyoyojo
Copy link
Contributor Author

theyoyojo commented Jun 19, 2020

Oops, looks like I forgot another constant:

>>> import gpiozero.pins.rpigpio as rpigpio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rpi/python3-libgpiod-rpi/lib/python3.7/site-packages/gpiozero/pins/rpigpio.py", line 91, in <module>
    class RPiGPIOPin(LocalPiPin):
  File "/home/rpi/python3-libgpiod-rpi/lib/python3.7/site-packages/gpiozero/pins/rpigpio.py", line 104, in RPiGPIOPin
    'serial':  GPIO.SERIAL,
AttributeError: module 'RPi.GPIO' has no attribute 'SERIAL'

@theyoyojo
Copy link
Contributor Author

Alright it imports correctly now, but gpiozero now throws the following when I try to evaluate PiFactory.pi_info()

>>> import gpiozero.pins.rpigpio as rpigpio
>>> fac = rpigpio.LocalPiFactory()
>>> fac.pi_info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rpi/.local/lib/python3.7/site-packages/gpiozero/pins/__init__.py", line 188, in <lambda>
    lambda self: self._get_pi_info(),
  File "/home/rpi/.local/lib/python3.7/site-packages/gpiozero/pins/pi.py", line 120, in _get_pi_info
    self._info = pi_info(self._get_revision())
  File "/home/rpi/.local/lib/python3.7/site-packages/gpiozero/pins/local.py", line 109, in _get_revision
    raise PinUnknownPi('unable to locate Pi revision in /proc/device-tree or /proc/cpuinfo')
gpiozero.exc.PinUnknownPi: unable to locate Pi revision in /proc/device-tree or /proc/cpuinfo

@bennuttall
Copy link

Are you able to get the revision code another way? We ought to patch that. @lurch any ideas for doing that outside of Raspbian?

@theyoyojo
Copy link
Contributor Author

mistakenly closed instead of #37

@theyoyojo theyoyojo reopened this Jun 19, 2020
@theyoyojo
Copy link
Contributor Author

theyoyojo commented Jun 19, 2020

About to look into revision code issue. Just released RPi.GPIO2 0.3.0a2 to PyPi so people can pip install a version that is importable by gpiozero

@theyoyojo
Copy link
Contributor Author

theyoyojo commented Jun 19, 2020

On one of the pigpio issues: joan2937/pigpio#340,
@guymcswain links to the following hack: https://www.raspberrypi.org/forums/viewtopic.php?p=1428987#p1428987

@theyoyojo
Copy link
Contributor Author

theyoyojo commented Jun 19, 2020

One way to do might be to have a table mapping values of cat /proc/device-tree/model to revision strings, perhaps something like:

diff --git a/gpiozero/pins/local.py b/gpiozero/pins/local.py
index 1c40c47..df4b678 100644
--- a/gpiozero/pins/local.py
+++ b/gpiozero/pins/local.py
@@ -61,6 +61,10 @@ from ..devices import Device, SharedMixin
 from ..output_devices import OutputDevice
 from ..exc import DeviceClosed, PinUnknownPi, SPIInvalidClockMode
 
+REVISION_TABLE = {
+    "Raspberry Pi 3 Model B": hex(0xa22082),
+}
+
 
 class LocalPiFactory(PiFactory):
     """
@@ -98,6 +102,14 @@ class LocalPiFactory(PiFactory):
         except IOError as e:
             if e.errno != errno.ENOENT:
                 raise e
+        try:
+            with io.open('/proc/device-tree/model', 'r') as f:
+                modelstr = f.readline()[:-1]
+                revision = REVISION_TABLE[modelstr]
+        except (IOError, KeyError) as e:
+            if not isinstance(e, IOError) or e.errno != errno.ENOENT:
+                raise e
             with io.open('/proc/cpuinfo', 'r') as f:
                 for line in f:
                     if line.startswith('Revision'):

@theyoyojo
Copy link
Contributor Author

Using this patch to gpiozero, I was able to get gpiozerocli.pinout.main() to work with RPi.GPIO2 0.3.0a2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Software Engineering/Feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants