Skip to content

Add typing, minor fixes #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 17, 2021
31 changes: 25 additions & 6 deletions adafruit_rtttl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
except ImportError:
pass

try:
from typing import Optional, Union, Tuple, List
from audioio import AudioOut
except ImportError:
pass

PIANO = {
"4c": 261.626,
"4c#": 277.183,
Expand Down Expand Up @@ -73,7 +79,7 @@
}


def _parse_note(note, duration=2, octave="6"):
def _parse_note(note: str, duration: int = 2, octave: int = 6) -> Tuple[str, float]:
note = note.strip()
piano_note = None
note_duration = duration
Expand All @@ -89,14 +95,14 @@ def _parse_note(note, duration=2, octave="6"):
note_duration *= 1.5
if "#" in note:
piano_note += "#"
note_octave = octave
note_octave = str(octave)
if note[-1].isdigit():
note_octave = note[-1]
piano_note = note_octave + piano_note
return piano_note, note_duration


def _get_wave(tune, octave):
def _get_wave(tune: str, octave: int) -> Tuple[List[int], float]:
"""Returns the proper waveform to play the song along with the minimum
frequency in the song.
"""
Expand All @@ -110,7 +116,14 @@ def _get_wave(tune, octave):


# pylint: disable-msg=too-many-arguments
def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
def _play_to_pin(
tune: str,
base_tone: Union[pwmio.PWMOut, AudioOut],
min_freq: float,
duration: int,
octave: int,
tempo: int,
) -> None:
"""Using the prepared input send the notes to the pin"""
pwm = isinstance(base_tone, pwmio.PWMOut)
for note in tune.split(","):
Expand Down Expand Up @@ -139,10 +152,16 @@ def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):


# pylint: disable-msg=too-many-arguments
def play(pin, rtttl, octave=None, duration=None, tempo=None):
def play(
pin,
rtttl: str,
octave: int = Optional[None],
duration: Optional[int] = None,
tempo: Optional[int] = None,
) -> None:
"""Play notes to a digialio pin using ring tone text transfer language (rtttl).
:param ~digitalio.DigitalInOut pin: the speaker pin
:param rtttl: string containing rtttl
:param str rtttl: string containing rtttl
:param int octave: represents octave number (default 6 starts at middle c)
:param int duration: length of notes (default 4 quarter note)
:param int tempo: how fast (default 63 beats per minute)
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sphinx.ext.viewcode",
]

autodoc_mock_imports = ["pulseio"]
autodoc_mock_imports = ["pulseio", "pwmio", "audioio"]

intersphinx_mapping = {
"python": ("https://docs.python.org/3.4", None),
Expand Down