Skip to content

Commit 7acaba7

Browse files
authored
Merge pull request #25 from tekktrik/feature/add-typing
Add typing, minor fixes
2 parents 47af0d6 + 0174764 commit 7acaba7

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

adafruit_rtttl.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
except ImportError:
3232
pass
3333

34+
try:
35+
from typing import Optional, Union, Tuple, List
36+
from audioio import AudioOut
37+
except ImportError:
38+
pass
39+
3440
PIANO = {
3541
"4c": 261.626,
3642
"4c#": 277.183,
@@ -73,7 +79,7 @@
7379
}
7480

7581

76-
def _parse_note(note, duration=2, octave="6"):
82+
def _parse_note(note: str, duration: int = 2, octave: int = 6) -> Tuple[str, float]:
7783
note = note.strip()
7884
piano_note = None
7985
note_duration = duration
@@ -89,14 +95,14 @@ def _parse_note(note, duration=2, octave="6"):
8995
note_duration *= 1.5
9096
if "#" in note:
9197
piano_note += "#"
92-
note_octave = octave
98+
note_octave = str(octave)
9399
if note[-1].isdigit():
94100
note_octave = note[-1]
95101
piano_note = note_octave + piano_note
96102
return piano_note, note_duration
97103

98104

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

111117

112118
# pylint: disable-msg=too-many-arguments
113-
def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
119+
def _play_to_pin(
120+
tune: str,
121+
base_tone: Union[pwmio.PWMOut, AudioOut],
122+
min_freq: float,
123+
duration: int,
124+
octave: int,
125+
tempo: int,
126+
) -> None:
114127
"""Using the prepared input send the notes to the pin"""
115128
pwm = isinstance(base_tone, pwmio.PWMOut)
116129
for note in tune.split(","):
@@ -139,10 +152,16 @@ def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
139152

140153

141154
# pylint: disable-msg=too-many-arguments
142-
def play(pin, rtttl, octave=None, duration=None, tempo=None):
155+
def play(
156+
pin,
157+
rtttl: str,
158+
octave: int = Optional[None],
159+
duration: Optional[int] = None,
160+
tempo: Optional[int] = None,
161+
) -> None:
143162
"""Play notes to a digialio pin using ring tone text transfer language (rtttl).
144163
:param ~digitalio.DigitalInOut pin: the speaker pin
145-
:param rtttl: string containing rtttl
164+
:param str rtttl: string containing rtttl
146165
:param int octave: represents octave number (default 6 starts at middle c)
147166
:param int duration: length of notes (default 4 quarter note)
148167
:param int tempo: how fast (default 63 beats per minute)

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"sphinx.ext.viewcode",
2121
]
2222

23-
autodoc_mock_imports = ["pulseio"]
23+
autodoc_mock_imports = ["pulseio", "pwmio", "audioio"]
2424

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

0 commit comments

Comments
 (0)