1212* Author(s): Scott Shawcroft
1313"""
1414
15+ try :
16+ from typing import Optional , Type
17+ from types import TracebackType
18+ from pwmio import PWMOut
19+ except ImportError :
20+ pass
21+
22+
1523__version__ = "0.0.0-auto.0"
1624__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"
1725
@@ -24,18 +32,22 @@ class _BaseServo: # pylint: disable-msg=too-few-public-methods
2432 :param int min_pulse: The minimum pulse length of the servo in microseconds.
2533 :param int max_pulse: The maximum pulse length of the servo in microseconds."""
2634
27- def __init__ (self , pwm_out , * , min_pulse = 750 , max_pulse = 2250 ):
35+ def __init__ (
36+ self , pwm_out : PWMOut , * , min_pulse : int = 750 , max_pulse : int = 2250
37+ ) -> None :
2838 self ._pwm_out = pwm_out
2939 self .set_pulse_width_range (min_pulse , max_pulse )
3040
31- def set_pulse_width_range (self , min_pulse = 750 , max_pulse = 2250 ):
41+ def set_pulse_width_range (
42+ self , min_pulse : int = 750 , max_pulse : int = 2250
43+ ) -> None :
3244 """Change min and max pulse widths."""
3345 self ._min_duty = int ((min_pulse * self ._pwm_out .frequency ) / 1000000 * 0xFFFF )
3446 max_duty = (max_pulse * self ._pwm_out .frequency ) / 1000000 * 0xFFFF
3547 self ._duty_range = int (max_duty - self ._min_duty )
3648
3749 @property
38- def fraction (self ):
50+ def fraction (self ) -> Optional [ float ] :
3951 """Pulse width expressed as fraction between 0.0 (`min_pulse`) and 1.0 (`max_pulse`).
4052 For conventional servos, corresponds to the servo position as a fraction
4153 of the actuation range. Is None when servo is diabled (pulsewidth of 0ms).
@@ -45,7 +57,7 @@ def fraction(self):
4557 return (self ._pwm_out .duty_cycle - self ._min_duty ) / self ._duty_range
4658
4759 @fraction .setter
48- def fraction (self , value ) :
60+ def fraction (self , value : Optional [ float ]) -> None :
4961 if value is None :
5062 self ._pwm_out .duty_cycle = 0 # disable the motor
5163 return
@@ -85,22 +97,29 @@ class Servo(_BaseServo):
8597 Test carefully to find the safe minimum and maximum.
8698 """
8799
88- def __init__ (self , pwm_out , * , actuation_range = 180 , min_pulse = 750 , max_pulse = 2250 ):
100+ def __init__ (
101+ self ,
102+ pwm_out : PWMOut ,
103+ * ,
104+ actuation_range : int = 180 ,
105+ min_pulse : int = 750 ,
106+ max_pulse : int = 2250
107+ ) -> None :
89108 super ().__init__ (pwm_out , min_pulse = min_pulse , max_pulse = max_pulse )
90109 self .actuation_range = actuation_range
91110 """The physical range of motion of the servo in degrees."""
92111 self ._pwm = pwm_out
93112
94113 @property
95- def angle (self ):
114+ def angle (self ) -> Optional [ float ] :
96115 """The servo angle in degrees. Must be in the range ``0`` to ``actuation_range``.
97116 Is None when servo is disabled."""
98117 if self .fraction is None : # special case for disabled servos
99118 return None
100119 return self .actuation_range * self .fraction
101120
102121 @angle .setter
103- def angle (self , new_angle ) :
122+ def angle (self , new_angle : Optional [ int ]) -> None :
104123 if new_angle is None : # disable the servo by sending 0 signal
105124 self .fraction = None
106125 return
@@ -116,22 +135,27 @@ class ContinuousServo(_BaseServo):
116135 :param int max_pulse: The maximum pulse width of the servo in microseconds."""
117136
118137 @property
119- def throttle (self ):
138+ def throttle (self ) -> float :
120139 """How much power is being delivered to the motor. Values range from ``-1.0`` (full
121140 throttle reverse) to ``1.0`` (full throttle forwards.) ``0`` will stop the motor from
122141 spinning."""
123142 return self .fraction * 2 - 1
124143
125144 @throttle .setter
126- def throttle (self , value ) :
145+ def throttle (self , value : float ) -> None :
127146 if value > 1.0 or value < - 1.0 :
128147 raise ValueError ("Throttle must be between -1.0 and 1.0" )
129148 if value is None :
130149 raise ValueError ("Continuous servos cannot spin freely" )
131150 self .fraction = (value + 1 ) / 2
132151
133- def __enter__ (self ):
152+ def __enter__ (self ) -> "ContinuousServo" :
134153 return self
135154
136- def __exit__ (self , exception_type , exception_value , traceback ):
155+ def __exit__ (
156+ self ,
157+ exception_type : Optional [Type [type ]],
158+ exception_value : Optional [BaseException ],
159+ traceback : Optional [TracebackType ],
160+ ) -> None :
137161 self .throttle = 0
0 commit comments