Skip to content

Commit c917de9

Browse files
committed
Added remaining typing, formatted per pre-commit
Also added license to adafruit_motor/__init__.py since there wasn't one
1 parent 0bfd4d6 commit c917de9

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

adafruit_motor/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2021 Scott Shawcroft for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT

adafruit_motor/motor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ class DCMotor:
5555
:param ~pwmio.PWMOut negative_pwm: The motor input that causes the motor to spin backwards
5656
when high and the other is low."""
5757

58-
def __init__(self, positive_pwm: PWMOut, negative_pwm: PWMOut):
58+
def __init__(self, positive_pwm: PWMOut, negative_pwm: PWMOut) -> None:
5959
self._positive = positive_pwm
6060
self._negative = negative_pwm
6161
self._throttle = None
6262
self._decay_mode = FAST_DECAY
6363

6464
@property
65-
def throttle(self):
65+
def throttle(self) -> Optional[float]:
6666
"""Motor speed, ranging from -1.0 (full speed reverse) to 1.0 (full speed forward),
6767
or ``None`` (controller off).
6868
If ``None``, both PWMs are turned full off. If ``0.0``, both PWMs are turned full on.
6969
"""
7070
return self._throttle
7171

7272
@throttle.setter
73-
def throttle(self, value: Optional[float]):
73+
def throttle(self, value: Optional[float]) -> None:
7474
if value is not None and (value > 1.0 or value < -1.0):
7575
raise ValueError("Throttle must be None or between -1.0 and +1.0")
7676
self._throttle = value
@@ -98,28 +98,28 @@ def throttle(self, value: Optional[float]):
9898
self._negative.duty_cycle = 0
9999

100100
@property
101-
def decay_mode(self):
101+
def decay_mode(self) -> int:
102102
"""Motor controller recirculation current decay mode. A value of ``motor.FAST_DECAY``
103103
sets the motor controller to the default fast recirculation current decay mode
104104
(coasting); ``motor.SLOW_DECAY`` sets slow decay (braking) mode."""
105105
return self._decay_mode
106106

107107
@decay_mode.setter
108-
def decay_mode(self, mode: int = FAST_DECAY):
108+
def decay_mode(self, mode: int = FAST_DECAY) -> None:
109109
if mode in (FAST_DECAY, SLOW_DECAY):
110110
self._decay_mode = mode
111111
else:
112112
raise ValueError(
113113
"Decay mode value must be either motor.FAST_DECAY or motor.SLOW_DECAY"
114114
)
115115

116-
def __enter__(self):
116+
def __enter__(self) -> "DCMotor":
117117
return self
118118

119119
def __exit__(
120120
self,
121121
exception_type: Optional[Type[type]],
122122
exception_value: Optional[BaseException],
123123
traceback: Optional[TracebackType],
124-
):
124+
) -> None:
125125
self.throttle = None

adafruit_motor/servo.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ class _BaseServo: # pylint: disable-msg=too-few-public-methods
3232
:param int min_pulse: The minimum pulse length of the servo in microseconds.
3333
:param int max_pulse: The maximum pulse length of the servo in microseconds."""
3434

35-
def __init__(self, pwm_out: PWMOut, *, min_pulse: int = 750, max_pulse: int = 2250):
35+
def __init__(
36+
self, pwm_out: PWMOut, *, min_pulse: int = 750, max_pulse: int = 2250
37+
) -> None:
3638
self._pwm_out = pwm_out
3739
self.set_pulse_width_range(min_pulse, max_pulse)
3840

39-
def set_pulse_width_range(self, min_pulse: int = 750, max_pulse: int = 2250):
41+
def set_pulse_width_range(
42+
self, min_pulse: int = 750, max_pulse: int = 2250
43+
) -> None:
4044
"""Change min and max pulse widths."""
4145
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
4246
max_duty = (max_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF
4347
self._duty_range = int(max_duty - self._min_duty)
4448

4549
@property
46-
def fraction(self):
50+
def fraction(self) -> Optional[float]:
4751
"""Pulse width expressed as fraction between 0.0 (`min_pulse`) and 1.0 (`max_pulse`).
4852
For conventional servos, corresponds to the servo position as a fraction
4953
of the actuation range. Is None when servo is diabled (pulsewidth of 0ms).
@@ -53,7 +57,7 @@ def fraction(self):
5357
return (self._pwm_out.duty_cycle - self._min_duty) / self._duty_range
5458

5559
@fraction.setter
56-
def fraction(self, value: Optional[float]):
60+
def fraction(self, value: Optional[float]) -> None:
5761
if value is None:
5862
self._pwm_out.duty_cycle = 0 # disable the motor
5963
return
@@ -100,22 +104,22 @@ def __init__(
100104
actuation_range: int = 180,
101105
min_pulse: int = 750,
102106
max_pulse: int = 2250
103-
):
107+
) -> None:
104108
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
105109
self.actuation_range = actuation_range
106110
"""The physical range of motion of the servo in degrees."""
107111
self._pwm = pwm_out
108112

109113
@property
110-
def angle(self):
114+
def angle(self) -> Optional[float]:
111115
"""The servo angle in degrees. Must be in the range ``0`` to ``actuation_range``.
112116
Is None when servo is disabled."""
113117
if self.fraction is None: # special case for disabled servos
114118
return None
115119
return self.actuation_range * self.fraction
116120

117121
@angle.setter
118-
def angle(self, new_angle: Optional[int]):
122+
def angle(self, new_angle: Optional[int]) -> None:
119123
if new_angle is None: # disable the servo by sending 0 signal
120124
self.fraction = None
121125
return
@@ -131,27 +135,27 @@ class ContinuousServo(_BaseServo):
131135
:param int max_pulse: The maximum pulse width of the servo in microseconds."""
132136

133137
@property
134-
def throttle(self):
138+
def throttle(self) -> float:
135139
"""How much power is being delivered to the motor. Values range from ``-1.0`` (full
136140
throttle reverse) to ``1.0`` (full throttle forwards.) ``0`` will stop the motor from
137141
spinning."""
138142
return self.fraction * 2 - 1
139143

140144
@throttle.setter
141-
def throttle(self, value: float):
145+
def throttle(self, value: float) -> None:
142146
if value > 1.0 or value < -1.0:
143147
raise ValueError("Throttle must be between -1.0 and 1.0")
144148
if value is None:
145149
raise ValueError("Continuous servos cannot spin freely")
146150
self.fraction = (value + 1) / 2
147151

148-
def __enter__(self):
152+
def __enter__(self) -> "ContinuousServo":
149153
return self
150154

151155
def __exit__(
152156
self,
153157
exception_type: Optional[Type[type]],
154158
exception_value: Optional[BaseException],
155159
traceback: Optional[TracebackType],
156-
):
160+
) -> None:
157161
self.throttle = 0

adafruit_motor/stepper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(
9494
bin2: Union[PWMOut, DigitalInOut],
9595
*,
9696
microsteps: Optional[int] = 16
97-
):
97+
) -> None:
9898
if microsteps is None:
9999
#
100100
# Digital IO Pins
@@ -122,7 +122,7 @@ def __init__(
122122
self._microsteps = microsteps
123123
self._update_coils()
124124

125-
def _update_coils(self, *, microstepping: bool = False):
125+
def _update_coils(self, *, microstepping: bool = False) -> None:
126126
if self._microsteps is None:
127127
#
128128
# Digital IO Pins
@@ -159,7 +159,7 @@ def _update_coils(self, *, microstepping: bool = False):
159159
for i in range(4):
160160
self._coil[i].duty_cycle = duty_cycles[i]
161161

162-
def release(self):
162+
def release(self) -> None:
163163
"""Releases all the coils so the motor can free spin, also won't use any power"""
164164
# De-energize coils:
165165
for coil in self._coil:
@@ -168,9 +168,9 @@ def release(self):
168168
else:
169169
coil.duty_cycle = 0
170170

171-
def onestep(
171+
def onestep( # pylint: disable=too-many-branches
172172
self, *, direction: int = FORWARD, style: int = SINGLE
173-
): # pylint: disable=too-many-branches
173+
) -> None:
174174
"""Performs one step of a particular style. The actual rotation amount will vary by style.
175175
`SINGLE` and `DOUBLE` will normal cause a full step rotation. `INTERLEAVE` will normally
176176
do a half step rotation. `MICROSTEP` will perform the smallest configured step.

0 commit comments

Comments
 (0)