From e1cdf0ab765f4ce60229452294057c375374f3c5 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 20 Mar 2022 14:03:54 -0400 Subject: [PATCH 1/5] Add type annotations --- adafruit_motorkit.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/adafruit_motorkit.py b/adafruit_motorkit.py index 752cf65..f081288 100644 --- a/adafruit_motorkit.py +++ b/adafruit_motorkit.py @@ -37,6 +37,13 @@ import board from adafruit_pca9685 import PCA9685 +try: + from typing import Optional, Tuple + from busio import I2C + import adafruit_motor +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MotorKit.git" @@ -50,8 +57,12 @@ class MotorKit: Alternately, if using with multiple I2C devices, you can specify the I2C bus.""" def __init__( - self, address=0x60, i2c=None, steppers_microsteps=16, pwm_frequency=1600 - ): + self, + address: int = 0x60, + i2c: Optional[I2C] = None, + steppers_microsteps: int = 16, + pwm_frequency: float = 1600, + ) -> None: self._motor1 = None self._motor2 = None self._motor3 = None @@ -67,7 +78,9 @@ def __init__( # We can save memory usage (~300 bytes) by deduplicating the construction of the objects for # each motor. This saves both code size and the number of raw strings (the error message) # stored. The same technique is a net loss for stepper because there is less duplication. - def _motor(self, motor_name, channels, stepper_name): + def _motor( + self, motor_name: int, channels: Tuple[int, int, int], stepper_name: int + ) -> adafruit_motor.motor.DCMotor: from adafruit_motor import motor # pylint: disable=import-outside-toplevel motor_name = "_motor" + str(motor_name) @@ -90,7 +103,7 @@ def _motor(self, motor_name, channels, stepper_name): return getattr(self, motor_name) @property - def motor1(self): + def motor1(self) -> adafruit_motor.motor.DCMotor: """:py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 1. The following image shows the location of the M1 terminal on the DC/Stepper FeatherWing. @@ -117,7 +130,7 @@ def motor1(self): return self._motor(1, (8, 9, 10), 1) @property - def motor2(self): + def motor2(self) -> adafruit_motor.motor.DCMotor: """:py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 2. The following image shows the location of the M2 terminal on the DC/Stepper FeatherWing. @@ -144,7 +157,7 @@ def motor2(self): return self._motor(2, (13, 11, 12), 1) @property - def motor3(self): + def motor3(self) -> adafruit_motor.motor.DCMotor: """:py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 3. The following image shows the location of the M2 terminal on the DC/Stepper FeatherWing. @@ -171,7 +184,7 @@ def motor3(self): return self._motor(3, (2, 3, 4), 2) @property - def motor4(self): + def motor4(self) -> adafruit_motor.motor.DCMotor: """:py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 4. .. image :: ../docs/_static/motor_featherwing/m4.jpg @@ -194,7 +207,7 @@ def motor4(self): return self._motor(4, (7, 5, 6), 2) @property - def stepper1(self): + def stepper1(self) -> adafruit_motor.stepper.StepperMotor: """:py:class:``~adafruit_motor.stepper.StepperMotor`` controls for one connected to stepper 1 (also labeled motor 1 and motor 2). @@ -238,7 +251,7 @@ def stepper1(self): return self._stepper1 @property - def stepper2(self): + def stepper2(self) -> adafruit_motor.stepper.StepperMotor: """:py:class:``~adafruit_motor.stepper.StepperMotor`` controls for one connected to stepper 2 (also labeled motor 3 and motor 4). @@ -282,10 +295,10 @@ def stepper2(self): return self._stepper2 @property - def frequency(self): + def frequency(self) -> float: """The overall PCA9685 PWM frequency in Hertz.""" return self._pca.frequency @frequency.setter - def frequency(self, pwm_frequency=1600): + def frequency(self, pwm_frequency: float = 1600) -> None: self._pca.frequency = pwm_frequency From 04fcfc2d65f7a0464f0425127452d7a175873c81 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 20 Mar 2022 14:10:48 -0400 Subject: [PATCH 2/5] Fix typing import of adafruit_motor.motor --- adafruit_motorkit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_motorkit.py b/adafruit_motorkit.py index f081288..e2e966f 100644 --- a/adafruit_motorkit.py +++ b/adafruit_motorkit.py @@ -40,7 +40,7 @@ try: from typing import Optional, Tuple from busio import I2C - import adafruit_motor + import adafruit_motor.motor except ImportError: pass From 9d42bfe485994d76b32f4110794f77382b9d4f65 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 20 Mar 2022 14:27:58 -0400 Subject: [PATCH 3/5] Add pwmio to mock imports --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 1b4f1c8..74b3df4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -# autodoc_mock_imports = ["digitalio", "busio"] +autodoc_mock_imports = ["pwmio"] intersphinx_mapping = { From a9344212e9b707bd1c570470db0e0d628f1a9f95 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 20 Mar 2022 14:32:34 -0400 Subject: [PATCH 4/5] Add import for adafruit_motor.stepper --- adafruit_motorkit.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_motorkit.py b/adafruit_motorkit.py index e2e966f..41b43ae 100644 --- a/adafruit_motorkit.py +++ b/adafruit_motorkit.py @@ -41,6 +41,7 @@ from typing import Optional, Tuple from busio import I2C import adafruit_motor.motor + import adafruit_motor.stepper except ImportError: pass From ff88c9723b50e2173c3c3c09f3560ec290966e6c Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 21 Mar 2022 13:28:46 -0400 Subject: [PATCH 5/5] Change frequency defaults to float --- adafruit_motorkit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_motorkit.py b/adafruit_motorkit.py index 41b43ae..61e4951 100644 --- a/adafruit_motorkit.py +++ b/adafruit_motorkit.py @@ -62,7 +62,7 @@ def __init__( address: int = 0x60, i2c: Optional[I2C] = None, steppers_microsteps: int = 16, - pwm_frequency: float = 1600, + pwm_frequency: float = 1600.0, ) -> None: self._motor1 = None self._motor2 = None @@ -301,5 +301,5 @@ def frequency(self) -> float: return self._pca.frequency @frequency.setter - def frequency(self, pwm_frequency: float = 1600) -> None: + def frequency(self, pwm_frequency: float = 1600.0) -> None: self._pca.frequency = pwm_frequency