From eaeb7fc2872f2b30b1647fa8b901e185b9a76be4 Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Sun, 3 Jan 2021 23:06:23 -0800 Subject: [PATCH 1/7] CP: more configuration options for the tap detection --- .../circuit_playground_base.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index febc396..d7ec133 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -184,6 +184,34 @@ def detect_taps(self, value): value, 60, time_limit=10, time_latency=50, time_window=255 ) + def configure_taps(self, value, range = adafruit_lis3dh.RANGE_8_G, theshold = None, time_limit = None, time_latency = 50, time_window = 255): + """ A way to customize the tap detection better. The default values don't work for all cases. + Higher default thresholds for the CPB + """ + if value < 0 or value > 2: + return + + if range not in [ adafruit_lis3dh.RANGE_2_G, adafruit_lis3dh.RANGE_4_G, adafruit_lis3dh.RANGE_8_G, adafruit_lis3dh.RANGE_16_G ]: + range = adafruit_lis3dh.RANGE_8_G + self._lis3dh.range = range + + if value == 1: + if theshold == None or theshold < 0 or theshold > 127: + theshold = 100 if "nRF52840" in os.uname().machine else 90 + if time_limit == None: + time_limit = 4 + elif value == 2: + if theshold == None or theshold < 0 or theshold > 127: + theshold = 70 if "nRF52840" in os.uname().machine else 60 + if time_limit == None: + time_limit = 10 + else: + # sane values for turning the tap detection off + theshold = 100 + time_limit = 1 + + self._lis3dh.set_tap(value, theshold, time_limit = time_limit, time_latency = time_latency, time_window = time_window) + @property def tapped(self): """True once after a detecting a tap. Requires ``cp.detect_taps``. From 7a3468190117ff0d254712ca42348524a41c0540 Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Sun, 3 Jan 2021 23:20:21 -0800 Subject: [PATCH 2/7] Change black-ified --- .../circuit_playground_base.py | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index d7ec133..ca69e73 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -184,14 +184,27 @@ def detect_taps(self, value): value, 60, time_limit=10, time_latency=50, time_window=255 ) - def configure_taps(self, value, range = adafruit_lis3dh.RANGE_8_G, theshold = None, time_limit = None, time_latency = 50, time_window = 255): + def configure_taps( + self, + value, + range=adafruit_lis3dh.RANGE_8_G, + theshold=None, + time_limit=None, + time_latency=50, + time_window=255, + ): """ A way to customize the tap detection better. The default values don't work for all cases. Higher default thresholds for the CPB """ if value < 0 or value > 2: return - if range not in [ adafruit_lis3dh.RANGE_2_G, adafruit_lis3dh.RANGE_4_G, adafruit_lis3dh.RANGE_8_G, adafruit_lis3dh.RANGE_16_G ]: + if range not in [ + adafruit_lis3dh.RANGE_2_G, + adafruit_lis3dh.RANGE_4_G, + adafruit_lis3dh.RANGE_8_G, + adafruit_lis3dh.RANGE_16_G, + ]: range = adafruit_lis3dh.RANGE_8_G self._lis3dh.range = range @@ -210,7 +223,13 @@ def configure_taps(self, value, range = adafruit_lis3dh.RANGE_8_G, theshold = No theshold = 100 time_limit = 1 - self._lis3dh.set_tap(value, theshold, time_limit = time_limit, time_latency = time_latency, time_window = time_window) + self._lis3dh.set_tap( + value, + theshold, + time_limit=time_limit, + time_latency=time_latency, + time_window=time_window, + ) @property def tapped(self): From 6baf8ef965cea2ec2c084010144810763822a00e Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Sun, 3 Jan 2021 23:44:53 -0800 Subject: [PATCH 3/7] Change pylint-ified --- .../circuit_playground_base.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index ca69e73..24afd69 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -184,39 +184,42 @@ def detect_taps(self, value): value, 60, time_limit=10, time_latency=50, time_window=255 ) + # pylint: disable-msg=too-many-arguments + # Many default arguments which will probably not need changing in most cases def configure_taps( self, value, - range=adafruit_lis3dh.RANGE_8_G, + accel_range=adafruit_lis3dh.RANGE_8_G, theshold=None, time_limit=None, time_latency=50, time_window=255, ): - """ A way to customize the tap detection better. The default values don't work for all cases. + """ A way to customize the tap detection better. The default values don't work + for all cases. Higher default thresholds for the CPB """ if value < 0 or value > 2: return - if range not in [ + if accel_range not in [ adafruit_lis3dh.RANGE_2_G, adafruit_lis3dh.RANGE_4_G, adafruit_lis3dh.RANGE_8_G, adafruit_lis3dh.RANGE_16_G, ]: - range = adafruit_lis3dh.RANGE_8_G - self._lis3dh.range = range + accel_range = adafruit_lis3dh.RANGE_8_G + self._lis3dh.range = accel_range if value == 1: - if theshold == None or theshold < 0 or theshold > 127: + if theshold is None or theshold < 0 or theshold > 127: theshold = 100 if "nRF52840" in os.uname().machine else 90 - if time_limit == None: + if time_limit is None: time_limit = 4 elif value == 2: - if theshold == None or theshold < 0 or theshold > 127: + if theshold is None or theshold < 0 or theshold > 127: theshold = 70 if "nRF52840" in os.uname().machine else 60 - if time_limit == None: + if time_limit is None: time_limit = 10 else: # sane values for turning the tap detection off @@ -230,6 +233,7 @@ def configure_taps( time_latency=time_latency, time_window=time_window, ) + # pylint: enable-msg=too-many-arguments @property def tapped(self): From 486cf99d98c0b0746c49343946581e2c0ae43785 Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Sun, 3 Jan 2021 23:47:17 -0800 Subject: [PATCH 4/7] Change black-ified --- adafruit_circuitplayground/circuit_playground_base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 24afd69..f383ae0 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -233,6 +233,7 @@ def configure_taps( time_latency=time_latency, time_window=time_window, ) + # pylint: enable-msg=too-many-arguments @property From 6a430fb95cc5fe7e990fe88fae5f7d796bf410f1 Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Mon, 4 Jan 2021 00:06:15 -0800 Subject: [PATCH 5/7] Pylint-ified two example files, I did not intend to change:-( --- examples/circuitplayground_ir_receive.py | 4 ++-- examples/circuitplayground_ir_transmit.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/circuitplayground_ir_receive.py b/examples/circuitplayground_ir_receive.py index bcac5dd..421d924 100644 --- a/examples/circuitplayground_ir_receive.py +++ b/examples/circuitplayground_ir_receive.py @@ -14,10 +14,10 @@ # Create a 'pulseio' input, to listen to infrared signals on the IR receiver try: pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True) -except AttributeError: +except AttributeError as e: raise NotImplementedError( "This example does not work with Circuit Playground Bluefruti!" - ) + ) from e # Create a decoder that will take pulses and turn them into numbers decoder = adafruit_irremote.GenericDecode() diff --git a/examples/circuitplayground_ir_transmit.py b/examples/circuitplayground_ir_transmit.py index cc9fa02..c5fdd31 100644 --- a/examples/circuitplayground_ir_transmit.py +++ b/examples/circuitplayground_ir_transmit.py @@ -15,10 +15,10 @@ # Create a 'pulseio' output, to send infrared signals from the IR transmitter try: pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15) -except AttributeError: +except AttributeError as e: raise NotImplementedError( "This example does not work with Circuit Playground Bluefruit!" - ) + ) from e pulseout = pulseio.PulseOut(pwm) # pylint: disable=no-member # Create an encoder that will take numbers and turn them into NEC IR pulses encoder = adafruit_irremote.GenericTransmit( From b770c0eac79df9542f04f2e472072d44e8a3176f Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Mon, 4 Jan 2021 15:39:11 -0800 Subject: [PATCH 6/7] Some suggested clarifications --- .../circuit_playground_base.py | 115 +++++++++++------- 1 file changed, 73 insertions(+), 42 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index f383ae0..ce760fb 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -160,48 +160,81 @@ def detect_taps(self): """ return self._detect_taps - @detect_taps.setter - def detect_taps(self, value): - self._detect_taps = value + @staticmethod + def _default_tap_threshold(tap): if ( "nRF52840" in os.uname().machine ): # If we're on a CPB, use a higher tap threshold - if value == 1: - self._lis3dh.set_tap( - value, 100, time_limit=4, time_latency=50, time_window=255 - ) - if value == 2: - self._lis3dh.set_tap( - value, 70, time_limit=10, time_latency=50, time_window=255 - ) - else: # If we're on a CPX - if value == 1: - self._lis3dh.set_tap( - value, 90, time_limit=4, time_latency=50, time_window=255 - ) - if value == 2: - self._lis3dh.set_tap( - value, 60, time_limit=10, time_latency=50, time_window=255 - ) - - # pylint: disable-msg=too-many-arguments - # Many default arguments which will probably not need changing in most cases - def configure_taps( + return 100 if tap == 1 else 70 + + # If we're on a CPX + return 90 if tap == 1 else 60 + + @detect_taps.setter + def detect_taps(self, value): + self._detect_taps = value + if value == 1: + self._lis3dh.set_tap( + value, + self._default_tap_threshold(value), + time_limit=4, + time_latency=50, + time_window=255, + ) + if value == 2: + self._lis3dh.set_tap( + value, + self._default_tap_threshold(value), + time_limit=10, + time_latency=50, + time_window=255, + ) + + def configure_tap( # pylint: disable-msg=too-many-arguments self, - value, + tap, accel_range=adafruit_lis3dh.RANGE_8_G, - theshold=None, + threshold=None, time_limit=None, time_latency=50, time_window=255, ): - """ A way to customize the tap detection better. The default values don't work - for all cases. - Higher default thresholds for the CPB + """ Granular configuration of tap parameters. Expose the power of the + adafruit_lis3dh module. + + :param int tap: 0 to disable tap detection, 1 to detect only single + taps, and 2 to detect only double taps. + :param int accel_range: Takes the defined values from the adafruit_lis3dh + module [ RANGE_2_G, RANGE_4_G, RANGE_8_G, RANGE_16_G ] + (default sets the same value as the *detect_taps* setter) + :param int threshold: A threshold for the tap detection. The higher the value + the less sensitive the detection. This changes based on the + accelerometer range. Good values are 5-10 for 16G, 10-20 + for 8G, 20-40 for 4G, and 40-80 for 2G. + (default sets the same value as the *detect_taps* setter) + :param int time_limit: TIME_LIMIT register value + (default sets the same value as the *detect_taps* setter) + :param int time_latency: TIME_LATENCY register value (default 50). + :param int time_window: TIME_WINDOW register value (default 255). + + To use with the Circuit Playground Express or Bluefruit: + + .. code-block:: python + + from adafruit_circuitplayground import cp + import adafruit_lis3dh + + cp.configure_tap(1, accel_range=adafruit_lis3dh.RANGE_2_G, threshold=50) + while True: + if cp.tapped: + print("Single tap detected!") + """ - if value < 0 or value > 2: + if tap < 0 or tap > 2: return + self._detect_taps = tap + if accel_range not in [ adafruit_lis3dh.RANGE_2_G, adafruit_lis3dh.RANGE_4_G, @@ -211,31 +244,29 @@ def configure_taps( accel_range = adafruit_lis3dh.RANGE_8_G self._lis3dh.range = accel_range - if value == 1: - if theshold is None or theshold < 0 or theshold > 127: - theshold = 100 if "nRF52840" in os.uname().machine else 90 + if tap == 1: + if threshold is None or threshold < 0 or threshold > 127: + threshold = self._default_tap_threshold(tap) if time_limit is None: time_limit = 4 - elif value == 2: - if theshold is None or theshold < 0 or theshold > 127: - theshold = 70 if "nRF52840" in os.uname().machine else 60 + elif tap == 2: + if threshold is None or threshold < 0 or threshold > 127: + threshold = self._default_tap_threshold(tap) if time_limit is None: time_limit = 10 else: - # sane values for turning the tap detection off - theshold = 100 + # reasonable values for turning the tap detection off + threshold = 100 time_limit = 1 self._lis3dh.set_tap( - value, - theshold, + tap, + threshold, time_limit=time_limit, time_latency=time_latency, time_window=time_window, ) - # pylint: enable-msg=too-many-arguments - @property def tapped(self): """True once after a detecting a tap. Requires ``cp.detect_taps``. From 6100110d882cd4e284be7d964371cbc72ca209d3 Mon Sep 17 00:00:00 2001 From: Marco Walther Date: Tue, 12 Jan 2021 13:51:19 -0800 Subject: [PATCH 7/7] New black rule --- adafruit_circuitplayground/circuit_playground_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 468aff0..1b1b1c9 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -181,7 +181,7 @@ def configure_tap( # pylint: disable-msg=too-many-arguments time_latency=50, time_window=255, ): - """ Granular configuration of tap parameters. Expose the power of the + """Granular configuration of tap parameters. Expose the power of the adafruit_lis3dh module. :param int tap: 0 to disable tap detection, 1 to detect only single