Using RPi.GPIO with the inkyphat library


#1

Hi,

I am using an inkyphat display with the inkyphat library installed via sudo apt-get install python3-inkyphat on a fresh install of the latest Raspbian Lite (2018-04-18 Stretch) on a Pi Zero W. I am intending to attach buttons, and update the display depending on what was pressed. I’m trying to use RPi.GPIO with the inkyphat library, but I’m having no luck. I’ve read the docs and looked at all the examples, neither of which mention GPIO, and looking at the library source shows that it is also using RPi.GPIO. Unfortunately I don’t know enough Python to go any further - I don’t know if it’s a quirk of the libraries or, more likely, simply me not knowing something obvious. I couldn’t find anything helpful via Google either, which is unusual.

It appears as though the inkyphat library is setting the GPIO mode to BCM (11), so it gives me an error when I try to set it myself, which I have to do in order to use the pins. I pared the script down to the absolute basics:

pi@raspberrypi:~ $ cat test.py
#!/usr/bin/env python3
import inkyphat
import RPi.GPIO as GPIO
print (GPIO.getmode())
#GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

pi@raspberrypi:~ $ ./test.py
11
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 132, in _display_exit
    self._display_fini()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 230, in _v1_fini
    self._busy_wait()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 378, in _busy_wait
    while(GPIO.input(self.busy_pin) != wait_for):
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

… which is fair enough. However:

pi@raspberrypi:~ $ cat test.py
#!/usr/bin/env python3
import inkyphat
import RPi.GPIO as GPIO
print (GPIO.getmode())
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

pi@raspberrypi:~ $ ./test.py
11
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 132, in _display_exit
    self._display_fini()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 230, in _v1_fini
    self._busy_wait()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 378, in _busy_wait
    while(GPIO.input(self.busy_pin) != wait_for):
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

What’s the correct way to be able to use RPi.GPIO with inkyphat?

Thanks,
Lee


#2

Don’t call GPIO.cleanup() you’re triggering the error by trashing the GPIO while inkyphat is still using it.

What’s happening:

  1. You import inkyphat, it sets up the GPIO (BCM mode to be precise)
  2. You import RPi.GPIO, this does nothing, it’s already imported so you just get a reference to it
  3. You GPIO.setmode(GPIO.BCM) which also does nothing because it’s already set
  4. You GPIO.cleanup() which cleans up the global state of the RPi.GPIO library (including that within the Inky library)
  5. The script exits, triggering the _atexit handler (Note the Error in atexit._run_exitfuncs:)
  6. The exit handler tries to use the GPIO, but you’ve run GPIO.cleanup() so it’s reset back to its default state and baulks

Arguably the inky library should call that itself, here:

    def _v1_fini(self):
        self._busy_wait()
        self._send_command(_VCOM_DATA_INTERVAL_SETTING, [0x00])
        self._send_command(_POWER_SETTING, [0x02, 0x00, 0x00, 0x00])
        self._send_command(_POWER_OFF)

#3

Well, that was obvious, and never occurred to me. Thanks for sorting that out!