From b87d1c3d338849fdb26fe243b6cf95b8bd1b0d7c Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Wed, 4 Aug 2021 10:56:13 +0100 Subject: [PATCH 01/12] Initial changes --- pitop/core/mixins/recreatable.py | 2 +- pitop/pma/adc_base.py | 7 +++++++ pitop/pma/button.py | 6 ++++++ pitop/pma/buzzer.py | 6 ++++++ pitop/pma/encoder_motor.py | 2 +- pitop/pma/led.py | 6 ++++++ pitop/pma/ultrasonic_sensor.py | 7 +++++++ 7 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pitop/core/mixins/recreatable.py b/pitop/core/mixins/recreatable.py index 3d71543b9..caeb91f19 100644 --- a/pitop/core/mixins/recreatable.py +++ b/pitop/core/mixins/recreatable.py @@ -8,7 +8,7 @@ class Recreatable: """Represents an object that keeps track of a set of parameters that will - allow to be recreate it in the future. + allow it to be recreated in the future. The values for each key provided in the :param:`config_dict` parameter can be a constant value or a reference to a function, diff --git a/pitop/pma/adc_base.py b/pitop/pma/adc_base.py index 7f9c3c040..5bf6185cd 100644 --- a/pitop/pma/adc_base.py +++ b/pitop/pma/adc_base.py @@ -6,6 +6,7 @@ ) from pitop.pma.plate_interface import PlateInterface from pitop.pma.common import get_pin_for_port +from pitop.pma.common.utils import Port class ADCBase(Stateful, Recreatable): @@ -22,6 +23,12 @@ class ADCBase(Stateful, Recreatable): """ def __init__(self, port_name, pin_number=1, name="adcbase"): + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is A0") + + if not port_name.startswith("A"): + raise ValueError(f"{port_name} is not a valid port type for an analog component. Try using an analog port, such as A0") + self._pma_port = port_name self.name = name diff --git a/pitop/pma/button.py b/pitop/pma/button.py index 2815751a2..8f37181c1 100644 --- a/pitop/pma/button.py +++ b/pitop/pma/button.py @@ -18,6 +18,12 @@ def __init__(self, port_name, name="button"): self._pma_port = port_name self.name = name + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + + if not port_name.startswith("D"): + raise ValueError(f"{port_name} is not a valid port type for a button. Try using a digital port, such as D0") + Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) gpiozero_Button.__init__(self, get_pin_for_port(self._pma_port)) diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 7ff3fe865..0f5b44a73 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -17,6 +17,12 @@ def __init__(self, port_name, name="buzzer"): self._pma_port = port_name self.name = name + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + + if not port_name.startswith("D"): + raise ValueError(f"{port_name} is not a valid port type for a buzzer. Try using a digital port, such as D0") + Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) gpiozero_Buzzer.__init__(self, get_pin_for_port(self._pma_port)) diff --git a/pitop/pma/encoder_motor.py b/pitop/pma/encoder_motor.py index 0e0828f0a..ea338d72e 100644 --- a/pitop/pma/encoder_motor.py +++ b/pitop/pma/encoder_motor.py @@ -26,7 +26,7 @@ class EncoderMotor(Stateful, Recreatable): The conversions between angle, rotations and RPM used by the motor to meters and meters/second are performed considering the :data:`wheel_diameter` parameter. This parameter defaults to the diameter of the wheel included with MMK. - If a wheel of different dimmensions is attached to the motor, you'll need to measure it's diameter, in order for these + If a wheel of different dimensions is attached to the motor, you'll need to measure it's diameter, in order for these methods to work properly. :type port_name: str diff --git a/pitop/pma/led.py b/pitop/pma/led.py index 4c9725dbb..7ab05b2c3 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -18,6 +18,12 @@ def __init__(self, port_name, name="led"): self._pma_port = port_name self.name = name + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + + if not port_name.startswith("D"): + raise ValueError(f"{port_name} is not a valid port type for an LED. Try using a digital port, such as D0") + Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) gpiozero_LED.__init__(self, get_pin_for_port(self._pma_port)) diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 6c86c15d4..edf0099c2 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -30,6 +30,13 @@ def __init__( self._pma_port = port_name self.name = name + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + + # todo: what ports do we allow for ultrasonic? Is the MCU now capable of handling ultrasonic, and if so do we unconditionally allow all D or A ports to be used? + #if not port_name.startswith("D"): + # raise ValueError(f"{port_name} is not a valid port type for an ultrasonic sensor. Try using a digital port, such as D0") + SmoothedInputDevice.__init__(self, get_pin_for_port(self._pma_port), pull_up=False, From 9106a90bb938986bacddda971a8bb800d82e3f9a Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Wed, 11 Aug 2021 17:46:37 +0100 Subject: [PATCH 02/12] Added ultrasonic check and missing port declarations --- pitop/pma/button.py | 1 + pitop/pma/buzzer.py | 1 + pitop/pma/led.py | 1 + pitop/pma/ultrasonic_sensor.py | 8 +++++++- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pitop/pma/button.py b/pitop/pma/button.py index 6de095267..2fd5aa18f 100644 --- a/pitop/pma/button.py +++ b/pitop/pma/button.py @@ -4,6 +4,7 @@ Recreatable, ) from pitop.pma.common import get_pin_for_port +from pitop.pma.common.utils import Port class Button(Stateful, Recreatable, gpiozero_Button): diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 6775e6fdf..d50507b6e 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -4,6 +4,7 @@ Recreatable, ) from pitop.pma.common import get_pin_for_port +from pitop.pma.common.utils import Port class Buzzer(Stateful, Recreatable, gpiozero_Buzzer): diff --git a/pitop/pma/led.py b/pitop/pma/led.py index 6b58d7af5..83bb4706a 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -4,6 +4,7 @@ Recreatable, ) from pitop.pma.common import get_pin_for_port +from pitop.pma.common.utils import Port class LED(Stateful, Recreatable, gpiozero_LED): diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 380d906c3..9277672e7 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -7,7 +7,7 @@ valid_analog_ports = ["A1", "A3"] - +invalid_analog_ports = ["A0", "A2"] class UltrasonicSensor(Stateful, Recreatable): def __init__(self, @@ -23,6 +23,12 @@ def __init__(self, self._pma_port = port_name self.name = name + if port_name not in Port.keys(): + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0 or A1") + + if port_name in invalid_analog_ports: + raise ValueError(f"Cannot use analog port {port_name} for ultrasonic sensor. Try A1, A3 or a digital port such as D0") + if port_name in valid_analog_ports: self.__ultrasonic_device = UltrasonicSensorMCU(port_name=port_name, queue_len=queue_len, From 2a60bb5820f404f6d54f1af13de151afeae5ea40 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Aug 2021 16:54:20 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pitop/pma/ultrasonic_sensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 9277672e7..1add29525 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -9,6 +9,7 @@ valid_analog_ports = ["A1", "A3"] invalid_analog_ports = ["A0", "A2"] + class UltrasonicSensor(Stateful, Recreatable): def __init__(self, port_name, From 39be2b36371815d03ad1203f61bb765c2a8710b6 Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Thu, 12 Aug 2021 13:56:27 +0100 Subject: [PATCH 04/12] Changed digital port check to mixin type --- examples/pma/button.py | 2 +- pitop/core/mixins/__init__.py | 3 ++- pitop/core/mixins/digital_component_checks.py | 14 ++++++++++++++ pitop/pma/button.py | 9 ++------- pitop/pma/buzzer.py | 8 ++------ pitop/pma/led.py | 8 ++------ pitop/pma/ultrasonic_sensor.py | 1 - 7 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 pitop/core/mixins/digital_component_checks.py diff --git a/examples/pma/button.py b/examples/pma/button.py index ae139573a..6229c7931 100644 --- a/examples/pma/button.py +++ b/examples/pma/button.py @@ -1,7 +1,7 @@ from pitop import Button from time import sleep -button = Button("D5") +button = Button("D8") def on_button_pressed(): diff --git a/pitop/core/mixins/__init__.py b/pitop/core/mixins/__init__.py index 76c47c239..1f7832545 100644 --- a/pitop/core/mixins/__init__.py +++ b/pitop/core/mixins/__init__.py @@ -1,5 +1,6 @@ from .componentable import Componentable from .recreatable import Recreatable from .stateful import Stateful +from .digital_component_checks import DigitalComponentChecks from .supports_battery import SupportsBattery -from .supports_miniscreen import SupportsMiniscreen +from .supports_miniscreen import SupportsMiniscreen \ No newline at end of file diff --git a/pitop/core/mixins/digital_component_checks.py b/pitop/core/mixins/digital_component_checks.py new file mode 100644 index 000000000..2ddadb4e8 --- /dev/null +++ b/pitop/core/mixins/digital_component_checks.py @@ -0,0 +1,14 @@ + +valid_digital_ports = ["D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7"] + +class DigitalComponentChecks: + """Performs basic checks on validity of user-specified port""" + + def __init__(self, port_name): + self.port_name = port_name + + if port_name not in valid_digital_ports: + raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + + if not port_name.startswith("D"): + raise ValueError(f"{port_name} is not a valid port type for a button. Try using a digital port, such as D0") diff --git a/pitop/pma/button.py b/pitop/pma/button.py index 3a5bc404d..2a17e55c4 100644 --- a/pitop/pma/button.py +++ b/pitop/pma/button.py @@ -2,9 +2,9 @@ from pitop.core.mixins import ( Stateful, Recreatable, + DigitalComponentChecks, ) from pitop.pma.common import get_pin_for_port -from pitop.pma.common.utils import Port class Button(Stateful, Recreatable, gpiozero_Button): @@ -19,14 +19,9 @@ def __init__(self, port_name, name="button"): self._pma_port = port_name self.name = name - if port_name not in Port.keys(): - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") - - if not port_name.startswith("D"): - raise ValueError(f"{port_name} is not a valid port type for a button. Try using a digital port, such as D0") - Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) + DigitalComponentChecks.__init__(self, self._pma_port) gpiozero_Button.__init__(self, get_pin_for_port(self._pma_port)) @property diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 09641a8f2..98e08f768 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -2,6 +2,7 @@ from pitop.core.mixins import ( Stateful, Recreatable, + DigitalComponentChecks, ) from pitop.pma.common import get_pin_for_port from pitop.pma.common.utils import Port @@ -18,14 +19,9 @@ def __init__(self, port_name, name="buzzer"): self._pma_port = port_name self.name = name - if port_name not in Port.keys(): - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") - - if not port_name.startswith("D"): - raise ValueError(f"{port_name} is not a valid port type for a buzzer. Try using a digital port, such as D0") - Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) + DigitalComponentChecks.__init__(self, self._pma_port) gpiozero_Buzzer.__init__(self, get_pin_for_port(self._pma_port)) @property diff --git a/pitop/pma/led.py b/pitop/pma/led.py index 5ed8b5550..38780d3d6 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -2,6 +2,7 @@ from pitop.core.mixins import ( Stateful, Recreatable, + DigitalComponentChecks, ) from pitop.pma.common import get_pin_for_port from pitop.pma.common.utils import Port @@ -19,14 +20,9 @@ def __init__(self, port_name, name="led"): self._pma_port = port_name self.name = name - if port_name not in Port.keys(): - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") - - if not port_name.startswith("D"): - raise ValueError(f"{port_name} is not a valid port type for an LED. Try using a digital port, such as D0") - Stateful.__init__(self) Recreatable.__init__(self, {"port_name": port_name, "name": self.name}) + DigitalComponentChecks.__init__(self, self._pma_port) gpiozero_LED.__init__(self, get_pin_for_port(self._pma_port)) @property diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 139e81c9a..59abb390e 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -9,7 +9,6 @@ valid_analog_ports = ["A1", "A3"] invalid_analog_ports = ["A0", "A2"] - class UltrasonicSensor(Stateful, Recreatable): def __init__(self, port_name, From 43c9fcdd2ced7934d0d0f6d51d519e430d4351e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 12 Aug 2021 12:56:57 +0000 Subject: [PATCH 05/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pitop/core/mixins/__init__.py | 2 +- pitop/core/mixins/digital_component_checks.py | 3 ++- pitop/pma/ultrasonic_sensor.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pitop/core/mixins/__init__.py b/pitop/core/mixins/__init__.py index 1f7832545..87071ae60 100644 --- a/pitop/core/mixins/__init__.py +++ b/pitop/core/mixins/__init__.py @@ -3,4 +3,4 @@ from .stateful import Stateful from .digital_component_checks import DigitalComponentChecks from .supports_battery import SupportsBattery -from .supports_miniscreen import SupportsMiniscreen \ No newline at end of file +from .supports_miniscreen import SupportsMiniscreen diff --git a/pitop/core/mixins/digital_component_checks.py b/pitop/core/mixins/digital_component_checks.py index 2ddadb4e8..ba450e31f 100644 --- a/pitop/core/mixins/digital_component_checks.py +++ b/pitop/core/mixins/digital_component_checks.py @@ -1,8 +1,9 @@ valid_digital_ports = ["D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7"] + class DigitalComponentChecks: - """Performs basic checks on validity of user-specified port""" + """Performs basic checks on validity of user-specified port.""" def __init__(self, port_name): self.port_name = port_name diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 59abb390e..139e81c9a 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -9,6 +9,7 @@ valid_analog_ports = ["A1", "A3"] invalid_analog_ports = ["A0", "A2"] + class UltrasonicSensor(Stateful, Recreatable): def __init__(self, port_name, From 12d0a8357ffaf191c05b9b2fa87765fda40aceaf Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Thu, 12 Aug 2021 14:33:56 +0100 Subject: [PATCH 06/12] Added missing DigitalComponentChecks --- pitop/pma/button.py | 2 +- pitop/pma/buzzer.py | 2 +- pitop/pma/led.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pitop/pma/button.py b/pitop/pma/button.py index 2a17e55c4..1bbaf5fdb 100644 --- a/pitop/pma/button.py +++ b/pitop/pma/button.py @@ -7,7 +7,7 @@ from pitop.pma.common import get_pin_for_port -class Button(Stateful, Recreatable, gpiozero_Button): +class Button(Stateful, Recreatable, DigitalComponentChecks, gpiozero_Button): """Encapsulates the behaviour of a push-button. A push-button is a simple switch mechanism for controlling some aspect of a circuit. diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 98e08f768..c336574fe 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -8,7 +8,7 @@ from pitop.pma.common.utils import Port -class Buzzer(Stateful, Recreatable, gpiozero_Buzzer): +class Buzzer(Stateful, Recreatable, DigitalComponentChecks, gpiozero_Buzzer): """Encapsulates the behaviour of a simple buzzer that can be turned on and off. diff --git a/pitop/pma/led.py b/pitop/pma/led.py index 38780d3d6..a25d2fa66 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -8,7 +8,7 @@ from pitop.pma.common.utils import Port -class LED(Stateful, Recreatable, gpiozero_LED): +class LED(Stateful, Recreatable, DigitalComponentChecks, gpiozero_LED): """Encapsulates the behaviour of an LED. An LED (Light Emitting Diode) is a simple light source that can be controlled directly. From 3dcd4a37bf9cc63c0a2788949d2efec71b1b36b9 Mon Sep 17 00:00:00 2001 From: Ryan Dunwoody Date: Mon, 23 Aug 2021 17:15:47 +0800 Subject: [PATCH 07/12] Remove redundant assert statement --- pitop/pma/ultrasonic_sensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 139e81c9a..5b61ae4a9 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -20,7 +20,6 @@ def __init__(self, name="ultrasonic" ): - assert (port_name in Port) self._pma_port = port_name self.name = name From cc758cb4881f822b6e67ee5787b3ddf1690cfbfe Mon Sep 17 00:00:00 2001 From: Ryan Dunwoody Date: Mon, 23 Aug 2021 17:16:16 +0800 Subject: [PATCH 08/12] line length fix --- pitop/pma/ultrasonic_sensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 5b61ae4a9..43a99f5e4 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -27,7 +27,8 @@ def __init__(self, raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0 or A1") if port_name in invalid_analog_ports: - raise ValueError(f"Cannot use analog port {port_name} for ultrasonic sensor. Try A1, A3 or a digital port such as D0") + raise ValueError(f"Cannot use analog port {port_name} for ultrasonic sensor. Try A1, A3 or a digital port " + f"such as D0") if port_name in valid_analog_ports: self.__ultrasonic_device = UltrasonicSensorMCU(port_name=port_name, From cdbdeb3da4c24ba810a18db2654357b25270391e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Aug 2021 11:37:50 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pitop/pma/ultrasonic_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 43a99f5e4..20cd8a77a 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -28,7 +28,7 @@ def __init__(self, if port_name in invalid_analog_ports: raise ValueError(f"Cannot use analog port {port_name} for ultrasonic sensor. Try A1, A3 or a digital port " - f"such as D0") + f"such as D0") if port_name in valid_analog_ports: self.__ultrasonic_device = UltrasonicSensorMCU(port_name=port_name, From 3168e0b9dce22a83e9e67853879a2d130d7562d7 Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Thu, 2 Sep 2021 17:13:40 +0300 Subject: [PATCH 10/12] Changed to use regex Now uses regex instead of dictionary list, as per Mike's suggestion. Needs proper testing though. --- examples/pma/button.py | 3 +-- pitop/core/mixins/digital_component_checks.py | 14 ++++++------- pitop/pma/adc_base.py | 16 ++++++++------- pitop/pma/ultrasonic_sensor.py | 20 +++++++++---------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/examples/pma/button.py b/examples/pma/button.py index 6229c7931..c069e718b 100644 --- a/examples/pma/button.py +++ b/examples/pma/button.py @@ -1,8 +1,7 @@ from pitop import Button from time import sleep -button = Button("D8") - +button = Button("D5") def on_button_pressed(): print("Pressed!") diff --git a/pitop/core/mixins/digital_component_checks.py b/pitop/core/mixins/digital_component_checks.py index ba450e31f..de9f55780 100644 --- a/pitop/core/mixins/digital_component_checks.py +++ b/pitop/core/mixins/digital_component_checks.py @@ -1,6 +1,4 @@ - -valid_digital_ports = ["D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7"] - +import re class DigitalComponentChecks: """Performs basic checks on validity of user-specified port.""" @@ -8,8 +6,10 @@ class DigitalComponentChecks: def __init__(self, port_name): self.port_name = port_name - if port_name not in valid_digital_ports: - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0") + #For the sake of a helpful error message, first check if the port is actually a valid port of any kind + if not re.search("^D[0-7]$|^A[0-3]$", self.port_name): + raise ValueError(f"{self.port_name} is not a valid port name. An example of a valid port name is D0") - if not port_name.startswith("D"): - raise ValueError(f"{port_name} is not a valid port type for a button. Try using a digital port, such as D0") + #Then, in this case, verify the port is digital not analog + if re.search("^A[0-3]$", self.port_name): + raise ValueError(f"Can't use analog port {self.port_name} for digital component. Try using a digital port, such as D0") diff --git a/pitop/pma/adc_base.py b/pitop/pma/adc_base.py index 5bf6185cd..47922c607 100644 --- a/pitop/pma/adc_base.py +++ b/pitop/pma/adc_base.py @@ -1,4 +1,5 @@ import time +import re from pitop.core.mixins import ( Stateful, @@ -6,7 +7,6 @@ ) from pitop.pma.plate_interface import PlateInterface from pitop.pma.common import get_pin_for_port -from pitop.pma.common.utils import Port class ADCBase(Stateful, Recreatable): @@ -23,15 +23,17 @@ class ADCBase(Stateful, Recreatable): """ def __init__(self, port_name, pin_number=1, name="adcbase"): - if port_name not in Port.keys(): - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is A0") - - if not port_name.startswith("A"): - raise ValueError(f"{port_name} is not a valid port type for an analog component. Try using an analog port, such as A0") - self._pma_port = port_name self.name = name + # For the sake of a helpful error message, first check if the port is actually a valid port of any kind + if not re.search("^D[0-7]$|^A[0-3]$", self._pma_port): + raise ValueError(f"{self._pma_port} is not a valid port name. An example of a valid port name is A0") + + # Then, in this case, verify the port is analog not digital + if re.search("^D[0-7]$", self._pma_port): + raise ValueError(f"Can't use digital port {self._pma_port} for analog component. Try using an analog port, such as A0") + self.is_current = False self.channel = get_pin_for_port(self._pma_port, pin_number) self.__adc_device = PlateInterface().get_device_mcu() diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 20cd8a77a..5184504c3 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -1,15 +1,10 @@ +import re from pitop.core.mixins import ( Stateful, Recreatable, ) -from pitop.pma.common.utils import Port from .ultrasonic_sensor_base import UltrasonicSensorMCU, UltrasonicSensorRPI - -valid_analog_ports = ["A1", "A3"] -invalid_analog_ports = ["A0", "A2"] - - class UltrasonicSensor(Stateful, Recreatable): def __init__(self, port_name, @@ -23,14 +18,17 @@ def __init__(self, self._pma_port = port_name self.name = name - if port_name not in Port.keys(): - raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0 or A1") + # For the sake of a helpful error message, first check if the port is actually a valid port of any kind + if not re.search("^D[0-7]$|^A[0-3]$", self._pma_port): + raise ValueError(f"{self._pma_port} is not a valid port name. An example of a valid port name is D0") - if port_name in invalid_analog_ports: - raise ValueError(f"Cannot use analog port {port_name} for ultrasonic sensor. Try A1, A3 or a digital port " + # Then, verify it's a valid port for the Ultrasonic sensor specifically + if not re.search("^D[0-7]$|^A1$|^A3$", self._pma_port): + raise ValueError(f"Can't use port {self._pma_port} for ultrasonic sensor. Try A1, A3 or a digital port " f"such as D0") - if port_name in valid_analog_ports: + #If port name is a valid analog port + if re.search("^A[0-3]$", self._pma_port): self.__ultrasonic_device = UltrasonicSensorMCU(port_name=port_name, queue_len=queue_len, max_distance=max_distance, From 32e3dc86b898124e77bccb58fce56cd850aea15e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Sep 2021 14:32:08 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/pma/button.py | 1 + pitop/core/mixins/__init__.py | 2 +- pitop/core/mixins/digital_component_checks.py | 13 +++++++++---- pitop/pma/adc_base.py | 10 +++++++--- pitop/pma/button.py | 3 ++- pitop/pma/buzzer.py | 3 ++- pitop/pma/led.py | 3 ++- pitop/pma/ultrasonic_sensor.py | 12 ++++++++---- 8 files changed, 32 insertions(+), 15 deletions(-) diff --git a/examples/pma/button.py b/examples/pma/button.py index 1a492a333..ab3e1c0e8 100644 --- a/examples/pma/button.py +++ b/examples/pma/button.py @@ -4,6 +4,7 @@ button = Button("D5") + def on_button_pressed(): print("Pressed!") diff --git a/pitop/core/mixins/__init__.py b/pitop/core/mixins/__init__.py index 87071ae60..1cbab99fd 100644 --- a/pitop/core/mixins/__init__.py +++ b/pitop/core/mixins/__init__.py @@ -1,6 +1,6 @@ from .componentable import Componentable +from .digital_component_checks import DigitalComponentChecks from .recreatable import Recreatable from .stateful import Stateful -from .digital_component_checks import DigitalComponentChecks from .supports_battery import SupportsBattery from .supports_miniscreen import SupportsMiniscreen diff --git a/pitop/core/mixins/digital_component_checks.py b/pitop/core/mixins/digital_component_checks.py index de9f55780..dc63ded2b 100644 --- a/pitop/core/mixins/digital_component_checks.py +++ b/pitop/core/mixins/digital_component_checks.py @@ -1,15 +1,20 @@ import re + class DigitalComponentChecks: """Performs basic checks on validity of user-specified port.""" def __init__(self, port_name): self.port_name = port_name - #For the sake of a helpful error message, first check if the port is actually a valid port of any kind + # For the sake of a helpful error message, first check if the port is actually a valid port of any kind if not re.search("^D[0-7]$|^A[0-3]$", self.port_name): - raise ValueError(f"{self.port_name} is not a valid port name. An example of a valid port name is D0") + raise ValueError( + f"{self.port_name} is not a valid port name. An example of a valid port name is D0" + ) - #Then, in this case, verify the port is digital not analog + # Then, in this case, verify the port is digital not analog if re.search("^A[0-3]$", self.port_name): - raise ValueError(f"Can't use analog port {self.port_name} for digital component. Try using a digital port, such as D0") + raise ValueError( + f"Can't use analog port {self.port_name} for digital component. Try using a digital port, such as D0" + ) diff --git a/pitop/pma/adc_base.py b/pitop/pma/adc_base.py index e183b2151..7b9fa4d8f 100644 --- a/pitop/pma/adc_base.py +++ b/pitop/pma/adc_base.py @@ -1,5 +1,5 @@ -import time import re +import time from pitop.core.mixins import Recreatable, Stateful from pitop.pma.common import get_pin_for_port @@ -25,11 +25,15 @@ def __init__(self, port_name, pin_number=1, name="adcbase"): # For the sake of a helpful error message, first check if the port is actually a valid port of any kind if not re.search("^D[0-7]$|^A[0-3]$", self._pma_port): - raise ValueError(f"{self._pma_port} is not a valid port name. An example of a valid port name is A0") + raise ValueError( + f"{self._pma_port} is not a valid port name. An example of a valid port name is A0" + ) # Then, in this case, verify the port is analog not digital if re.search("^D[0-7]$", self._pma_port): - raise ValueError(f"Can't use digital port {self._pma_port} for analog component. Try using an analog port, such as A0") + raise ValueError( + f"Can't use digital port {self._pma_port} for analog component. Try using an analog port, such as A0" + ) self.is_current = False self.channel = get_pin_for_port(self._pma_port, pin_number) diff --git a/pitop/pma/button.py b/pitop/pma/button.py index a1ffc8179..677b69893 100644 --- a/pitop/pma/button.py +++ b/pitop/pma/button.py @@ -1,5 +1,6 @@ from gpiozero import Button as gpiozero_Button -from pitop.core.mixins import Recreatable, Stateful, DigitalComponentChecks + +from pitop.core.mixins import DigitalComponentChecks, Recreatable, Stateful from pitop.pma.common import get_pin_for_port diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 207c03071..9adc45ab3 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -1,5 +1,6 @@ from gpiozero import Buzzer as gpiozero_Buzzer -from pitop.core.mixins import Recreatable, Stateful, DigitalComponentChecks + +from pitop.core.mixins import DigitalComponentChecks, Recreatable, Stateful from pitop.pma.common import get_pin_for_port from pitop.pma.common.utils import Port diff --git a/pitop/pma/led.py b/pitop/pma/led.py index 9a3729a52..d049ad697 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -1,5 +1,6 @@ from gpiozero import LED as gpiozero_LED -from pitop.core.mixins import Recreatable, Stateful, DigitalComponentChecks + +from pitop.core.mixins import DigitalComponentChecks, Recreatable, Stateful from pitop.pma.common import get_pin_for_port from pitop.pma.common.utils import Port diff --git a/pitop/pma/ultrasonic_sensor.py b/pitop/pma/ultrasonic_sensor.py index 25f729205..8d6b6155e 100644 --- a/pitop/pma/ultrasonic_sensor.py +++ b/pitop/pma/ultrasonic_sensor.py @@ -1,10 +1,10 @@ import re + from pitop.core.mixins import Recreatable, Stateful from .ultrasonic_sensor_base import UltrasonicSensorMCU, UltrasonicSensorRPI - class UltrasonicSensor(Stateful, Recreatable): def __init__( self, @@ -21,12 +21,16 @@ def __init__( # For the sake of a helpful error message, first check if the port is actually a valid port of any kind if not re.search("^D[0-7]$|^A[0-3]$", self._pma_port): - raise ValueError(f"{self._pma_port} is not a valid port name. An example of a valid port name is D0") + raise ValueError( + f"{self._pma_port} is not a valid port name. An example of a valid port name is D0" + ) # Then, verify it's a valid port for the Ultrasonic sensor specifically if not re.search("^D[0-7]$|^A1$|^A3$", self._pma_port): - raise ValueError(f"Can't use port {self._pma_port} for ultrasonic sensor. Try A1, A3 or a digital port " - f"such as D0") + raise ValueError( + f"Can't use port {self._pma_port} for ultrasonic sensor. Try A1, A3 or a digital port " + f"such as D0" + ) # If port name is a valid analog port if re.search("^A[0-3]$", self._pma_port): From 5f1cd5bddd81adc4b2d5ce0fe4f44c6a86db22e2 Mon Sep 17 00:00:00 2001 From: Wil Bennett Date: Thu, 2 Sep 2021 17:54:52 +0300 Subject: [PATCH 12/12] Removed unused imports --- pitop/pma/buzzer.py | 1 - pitop/pma/led.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pitop/pma/buzzer.py b/pitop/pma/buzzer.py index 9adc45ab3..4c6e47715 100644 --- a/pitop/pma/buzzer.py +++ b/pitop/pma/buzzer.py @@ -2,7 +2,6 @@ from pitop.core.mixins import DigitalComponentChecks, Recreatable, Stateful from pitop.pma.common import get_pin_for_port -from pitop.pma.common.utils import Port class Buzzer(Stateful, Recreatable, DigitalComponentChecks, gpiozero_Buzzer): diff --git a/pitop/pma/led.py b/pitop/pma/led.py index d049ad697..f2ff71102 100644 --- a/pitop/pma/led.py +++ b/pitop/pma/led.py @@ -2,7 +2,6 @@ from pitop.core.mixins import DigitalComponentChecks, Recreatable, Stateful from pitop.pma.common import get_pin_for_port -from pitop.pma.common.utils import Port class LED(Stateful, Recreatable, DigitalComponentChecks, gpiozero_LED):