Skip to content

Commit

Permalink
Merge pull request #37 from tekktrik/doc/add-typing
Browse files Browse the repository at this point in the history
Add type annotations
  • Loading branch information
tekktrik committed Feb 23, 2022
2 parents fc0878f + 3ddcac6 commit 67982ff
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions adafruit_servokit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
import board
from adafruit_pca9685 import PCA9685

try:
from typing import Optional
from busio import I2C
from adafruit_motor.servo import Servo, ContinuousServo
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ServoKit.git"

Expand All @@ -54,6 +61,8 @@ class ServoKit:
:param int channels: The number of servo channels available. Must be 8 or 16. The FeatherWing
has 8 channels. The Shield, HAT, and Bonnet have 16 channels.
:param ~I2C i2c: The I2C bus to use. If not provided, it will use generate the default I2C
bus singleton ``busio.I2C()`` and use that.
:param int address: The I2C address of the PCA9685. Default address is ``0x40``.
:param int reference_clock_speed: The frequency of the internal reference clock in Hertz.
Default reference clock speed is ``25000000``.
Expand All @@ -65,12 +74,12 @@ class ServoKit:
def __init__(
self,
*,
channels,
i2c=None,
address=0x40,
reference_clock_speed=25000000,
frequency=50
):
channels: int,
i2c: Optional[I2C] = None,
address: int = 0x40,
reference_clock_speed: int = 25000000,
frequency: int = 50
) -> None:
if channels not in [8, 16]:
raise ValueError("servo_channels must be 8 or 16!")
self._items = [None] * channels
Expand All @@ -86,7 +95,7 @@ def __init__(
self._continuous_servo = _ContinuousServo(self)

@property
def servo(self):
def servo(self) -> "_Servo":
""":class:`~adafruit_motor.servo.Servo` controls for standard servos.
This FeatherWing example rotates a servo on channel ``0`` to ``180`` degrees for one second,
Expand All @@ -107,7 +116,7 @@ def servo(self):
return self._servo

@property
def continuous_servo(self):
def continuous_servo(self) -> "_ContinuousServo":
""":class:`~adafruit_motor.servo.ContinuousServo` controls for continuous rotation
servos.
Expand All @@ -133,10 +142,10 @@ def continuous_servo(self):

class _Servo:
# pylint: disable=protected-access
def __init__(self, kit):
def __init__(self, kit: ServoKit) -> None:
self.kit = kit

def __getitem__(self, servo_channel):
def __getitem__(self, servo_channel: int) -> Servo:
import adafruit_motor.servo # pylint: disable=import-outside-toplevel

num_channels = self.kit._channels
Expand All @@ -151,16 +160,16 @@ def __getitem__(self, servo_channel):
return servo
raise ValueError("Channel {} is already in use.".format(servo_channel))

def __len__(self):
def __len__(self) -> int:
return len(self.kit._items)


class _ContinuousServo:
# pylint: disable=protected-access
def __init__(self, kit):
def __init__(self, kit: ServoKit) -> None:
self.kit = kit

def __getitem__(self, servo_channel):
def __getitem__(self, servo_channel: int) -> ContinuousServo:
import adafruit_motor.servo # pylint: disable=import-outside-toplevel

num_channels = self.kit._channels
Expand All @@ -179,5 +188,5 @@ def __getitem__(self, servo_channel):
return servo
raise ValueError("Channel {} is already in use.".format(servo_channel))

def __len__(self):
def __len__(self) -> int:
return len(self.kit._items)

0 comments on commit 67982ff

Please sign in to comment.