Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #356 from j5api/float-int-piezo
Browse files Browse the repository at this point in the history
Allow Piezo.buzz to use int and float
  • Loading branch information
trickeydan authored Jul 27, 2019
2 parents df0cc08 + a2e9303 commit cacf340
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions j5/components/piezo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ def identifier(self) -> int:
"""An integer to identify the component on a board."""
return self._identifier

def buzz(self, duration: timedelta, pitch: Pitch) -> None:
"""Queue a note to be played."""
def buzz(self, duration: Union[int, float, timedelta], pitch: Pitch) -> None:
"""
Queue a note to be played.
Float and integer durations are measured in seconds.
"""
if isinstance(duration, float) or isinstance(duration, int):
duration = timedelta(seconds=duration)
if type(pitch) is int:
pitch = float(pitch)

Expand Down
4 changes: 2 additions & 2 deletions tests/components/test_piezo.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def test_piezo_buzz_method() -> None:
piezo.buzz(timedelta(seconds=1), 2093)
piezo.buzz(timedelta(seconds=1), 2093.23)
piezo.buzz(timedelta(minutes=1), Note.D7)
piezo.buzz(4, 2093)
piezo.buzz(4.3, 2093)


def test_piezo_buzz_invalid_value() -> None:
Expand All @@ -55,8 +57,6 @@ def test_piezo_buzz_invalid_value() -> None:
piezo.buzz(timedelta(seconds=1), "j5") # type: ignore
with pytest.raises(ValueError):
piezo.buzz(timedelta(seconds=-2), Note.D7)
with pytest.raises(TypeError):
piezo.buzz(1, Note.D7) # type: ignore


def test_note_reversed() -> None:
Expand Down

0 comments on commit cacf340

Please sign in to comment.