brettmclean / pad4pi

Interrupt-based matrix keypad library for Raspberry Pi
GNU Lesser General Public License v3.0
51 stars 19 forks source link

Parametrize `GPIO.cleanup()` call in keypad cleanup #21

Open tomekzaw opened 3 years ago

tomekzaw commented 3 years ago

The current implementation of Keypad.cleanup() always calls GPIO.cleanup():

https://github.com/brettmclean/pad4pi/blob/0e560a1ab67866cbedf8df9237316765debab2f1/pad4pi/rpi_gpio.py#L165-L168

However, for some applications it may be necessary to still be able to use RPi.GPIO after keypad cleanup, for example if the keypad should be active only occasionally (i.e. instantiated and destroyed multiple number of times).

When using this keypad library simulatenously with any HD44780 LCD display library based on RPi.GPIO, currently it is necessary to perform keypad cleanup at the very end. Otherwise, the following error will appear when trying to clear the display with lcd.clear() after keypad cleanup:

RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

According to RPi.GPIO documentation, GPIO.cleanup() should be called at the end any program. This can be easily achieved by simply placing GPIO.cleanup() at the end of the script separately or using exit handlers (atexit) manually by the library user.

For convenience and compability purposes, the suggested solution is to call GPIO.cleanup() conditionally, depending on the value of a boolean parameter (enabled by default). Otherwise, GPIO.remove_event_detect() should be called to unregister all interrupts. This will allow developers to configure the behaviour depending on the application.