From e80d9a2bac9b984d10c7a307c0bc09f53d20a680 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Fri, 14 Apr 2023 07:44:14 +0200 Subject: [PATCH 1/4] added mode BOARD for usage with BOARD pin numbering --- rpi_rf/rpi_rf.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/rpi_rf/rpi_rf.py b/rpi_rf/rpi_rf.py index 5c93c25..a266015 100644 --- a/rpi_rf/rpi_rf.py +++ b/rpi_rf/rpi_rf.py @@ -30,10 +30,10 @@ class RFDevice: """Representation of a GPIO RF device.""" # pylint: disable=too-many-instance-attributes,too-many-arguments - def __init__(self, gpio, - tx_proto=1, tx_pulselength=None, tx_repeat=10, tx_length=24, rx_tolerance=80): + def __init__(self, pin, + mode = 'BCM', tx_proto = 1, tx_pulselength = None, tx_repeat = 10, tx_length = 24, rx_tolerance = 80): """Initialize the RF device.""" - self.gpio = gpio + self.pin = pin self.tx_enabled = False self.tx_proto = tx_proto if tx_pulselength: @@ -56,8 +56,14 @@ def __init__(self, gpio, self.rx_bitlength = None self.rx_pulselength = None - GPIO.setmode(GPIO.BCM) - _LOGGER.debug("Using GPIO " + str(gpio)) + if (mode.upper() == 'BCM'): + GPIO.setmode(GPIO.BCM) + _LOGGER.debug("Using GPIO(BCM) pin " + str(pin)) + elif (mode.upper() == 'BOARD'): + GPIO.setmode(GPIO.BOARD) + _LOGGER.debug("Using BOARD pin " + str(pin)) + else: + _LOGGER.error("Error: using wrong GPIO.mode, only BCM or BOARD allowed!") def cleanup(self): """Disable TX and RX and clean up GPIO.""" @@ -75,7 +81,7 @@ def enable_tx(self): return False if not self.tx_enabled: self.tx_enabled = True - GPIO.setup(self.gpio, GPIO.OUT) + GPIO.setup(self.pin, GPIO.OUT) _LOGGER.debug("TX enabled") return True @@ -83,7 +89,7 @@ def disable_tx(self): """Disable TX, reset GPIO.""" if self.tx_enabled: # set up GPIO pin as input for safety - GPIO.setup(self.gpio, GPIO.IN) + GPIO.setup(self.pin, GPIO.IN) self.tx_enabled = False _LOGGER.debug("TX disabled") return True @@ -172,9 +178,9 @@ def tx_waveform(self, highpulses, lowpulses): if not self.tx_enabled: _LOGGER.error("TX is not enabled, not sending data") return False - GPIO.output(self.gpio, GPIO.HIGH) + GPIO.output(self.pin, GPIO.HIGH) self._sleep((highpulses * self.tx_pulselength) / 1000000) - GPIO.output(self.gpio, GPIO.LOW) + GPIO.output(self.pin, GPIO.LOW) self._sleep((lowpulses * self.tx_pulselength) / 1000000) return True @@ -185,22 +191,22 @@ def enable_rx(self): return False if not self.rx_enabled: self.rx_enabled = True - GPIO.setup(self.gpio, GPIO.IN) - GPIO.add_event_detect(self.gpio, GPIO.BOTH) - GPIO.add_event_callback(self.gpio, self.rx_callback) + GPIO.setup(self.pin, GPIO.IN) + GPIO.add_event_detect(self.pin, GPIO.BOTH) + GPIO.add_event_callback(self.pin, self.rx_callback) _LOGGER.debug("RX enabled") return True def disable_rx(self): """Disable RX, remove GPIO event detection.""" if self.rx_enabled: - GPIO.remove_event_detect(self.gpio) + GPIO.remove_event_detect(self.pin) self.rx_enabled = False _LOGGER.debug("RX disabled") return True # pylint: disable=unused-argument - def rx_callback(self, gpio): + def rx_callback(self, pin): """RX callback for GPIO event detection. Handle basic signal detection.""" timestamp = int(time.perf_counter() * 1000000) duration = timestamp - self._rx_last_timestamp From 3b7a2363aa3e6395aac27680ab638b1c0db94785 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Fri, 14 Apr 2023 16:04:40 +0200 Subject: [PATCH 2/4] create function to set the GPIO mode --- rpi_rf/rpi_rf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rpi_rf/rpi_rf.py b/rpi_rf/rpi_rf.py index a266015..c33074f 100644 --- a/rpi_rf/rpi_rf.py +++ b/rpi_rf/rpi_rf.py @@ -56,6 +56,10 @@ def __init__(self, pin, self.rx_bitlength = None self.rx_pulselength = None + # set GPIO mode + setMode(mode, pin) + + def setMode(self, mode, pin): if (mode.upper() == 'BCM'): GPIO.setmode(GPIO.BCM) _LOGGER.debug("Using GPIO(BCM) pin " + str(pin)) @@ -65,6 +69,7 @@ def __init__(self, pin, else: _LOGGER.error("Error: using wrong GPIO.mode, only BCM or BOARD allowed!") + def cleanup(self): """Disable TX and RX and clean up GPIO.""" if self.tx_enabled: From d77cdcf6b96d4b9c347027dabaeeb9981b7e8ce3 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Fri, 14 Apr 2023 18:04:08 +0200 Subject: [PATCH 3/4] added self to call memberfunction --- rpi_rf/rpi_rf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpi_rf/rpi_rf.py b/rpi_rf/rpi_rf.py index c33074f..09961c3 100644 --- a/rpi_rf/rpi_rf.py +++ b/rpi_rf/rpi_rf.py @@ -57,7 +57,7 @@ def __init__(self, pin, self.rx_pulselength = None # set GPIO mode - setMode(mode, pin) + self.setMode(mode, pin) def setMode(self, mode, pin): if (mode.upper() == 'BCM'): From c5f9cd22e44c07e2426af91562afb6f3cc4ef911 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Mon, 17 Apr 2023 16:19:39 +0200 Subject: [PATCH 4/4] optimise method setMode() - remove parameter `pin` from set mode, because it is available in the class itself --- rpi_rf/rpi_rf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rpi_rf/rpi_rf.py b/rpi_rf/rpi_rf.py index 09961c3..7b37b8b 100644 --- a/rpi_rf/rpi_rf.py +++ b/rpi_rf/rpi_rf.py @@ -57,15 +57,15 @@ def __init__(self, pin, self.rx_pulselength = None # set GPIO mode - self.setMode(mode, pin) + self.setMode(mode) - def setMode(self, mode, pin): + def setMode(self, mode): if (mode.upper() == 'BCM'): GPIO.setmode(GPIO.BCM) - _LOGGER.debug("Using GPIO(BCM) pin " + str(pin)) + _LOGGER.debug("Using GPIO(BCM) pin " + str(self.pin)) elif (mode.upper() == 'BOARD'): GPIO.setmode(GPIO.BOARD) - _LOGGER.debug("Using BOARD pin " + str(pin)) + _LOGGER.debug("Using BOARD pin " + str(self.pin)) else: _LOGGER.error("Error: using wrong GPIO.mode, only BCM or BOARD allowed!")