Skip to content

Added speaker tones to Circuit Playground Express CircuitPython API. #2

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 3 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added _static/speaker.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 45 additions & 4 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import math

import board
import adafruit_thermistor
import analogio
import array
import audioio
import board
import digitalio
import math
import neopixel
import adafruit_thermistor
import time


class Photocell:
def __init__(self, pin):
Expand Down Expand Up @@ -63,6 +66,10 @@ def __init__(self):
self._temp = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950)
self._light = Photocell(board.LIGHT)

# Define audio:
self.speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
self.speaker_enable.switch_to_output(value=False)

@property
def button_a(self):
"""``True`` when Button A is pressed. ``False`` if not.
Expand Down Expand Up @@ -184,6 +191,40 @@ def red_led(self):
def red_led(self, value):
self._led.value = value

def play_tone(self, frequency, duration):
""" Produce a tone using the speaker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the two arguments frequency and duration using the param style here: https://github.com/adafruit/Adafruit_CircuitPython_DotStar/blob/master/adafruit_dotstar.py#L39

:param int frequency: The frequency of the tone in Hz
:param float duration: The duration of the tone in seconds

.. image :: /_static/speaker.jpg

The two numbers are frequency and duration. Duration is how long it
plays in seconds. Try changing frequency to change the pitch of the
tone.

..code-block:python

from adafruit_circuitplayground.express import circuit

circuit.play_tone(440, 1)
"""
length = 8000 // frequency
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)

sample = audioio.AudioOut(board.SPEAKER, sine_wave)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the speaker enable toggle here and after stop.

self.speaker_enable.value = True

sample.play(loop=True)
time.sleep(duration)
sample.stop()

self.speaker_enable.value = False


circuit = Express()
"""Object that is automatically created on import.

Expand Down