|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Kattni Rembor for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, bluefruit OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +""" |
| 24 | +`adafruit_circuitplayground.bluefruit` |
| 25 | +==================================================== |
| 26 | +
|
| 27 | +CircuitPython subclass for Circuit Playground Bluefruit. |
| 28 | +
|
| 29 | +* Author(s): Kattni Rembor |
| 30 | +
|
| 31 | +Implementation Notes |
| 32 | +-------------------- |
| 33 | +
|
| 34 | +**Hardware:** |
| 35 | +
|
| 36 | +* `Circuit Playground Bluefruit <https://www.adafruit.com/product/4333>`_ |
| 37 | +
|
| 38 | +""" |
| 39 | + |
| 40 | +import array |
| 41 | +import math |
| 42 | +import time |
| 43 | +import audiocore |
| 44 | +import digitalio |
| 45 | +import board |
| 46 | +import audiopwmio |
| 47 | +from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase |
| 48 | + |
| 49 | + |
| 50 | +__version__ = "0.0.0-auto.0" |
| 51 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git" |
| 52 | + |
| 53 | + |
| 54 | +class Bluefruit(CircuitPlaygroundBase): |
| 55 | + """Represents a single CircuitPlayground Bluefruit.""" |
| 56 | + def __init__(self): |
| 57 | + # Only create the cpb module member when we aren't being imported by Sphinx |
| 58 | + if ("__module__" in dir(digitalio.DigitalInOut) and |
| 59 | + digitalio.DigitalInOut.__module__ == "sphinx.ext.autodoc"): |
| 60 | + return |
| 61 | + |
| 62 | + # Define audio: |
| 63 | + self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) |
| 64 | + self._speaker_enable.switch_to_output(value=False) |
| 65 | + self._sample = None |
| 66 | + self._sine_wave = None |
| 67 | + self._sine_wave_sample = None |
| 68 | + |
| 69 | + super().__init__() |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def _sine_sample(length): |
| 73 | + tone_volume = (2 ** 15) - 1 |
| 74 | + shift = 2 ** 15 |
| 75 | + for i in range(length): |
| 76 | + yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift) |
| 77 | + |
| 78 | + def _generate_sample(self, length=100): |
| 79 | + if self._sample is not None: |
| 80 | + return |
| 81 | + self._sine_wave = array.array("H", Bluefruit._sine_sample(length)) |
| 82 | + self._sample = audiopwmio.PWMAudioOut(board.SPEAKER) |
| 83 | + self._sine_wave_sample = audiocore.RawSample(self._sine_wave) |
| 84 | + |
| 85 | + def play_tone(self, frequency, duration): |
| 86 | + """ Produce a tone using the speaker. Try changing frequency to change |
| 87 | + the pitch of the tone. |
| 88 | +
|
| 89 | + :param int frequency: The frequency of the tone in Hz |
| 90 | + :param float duration: The duration of the tone in seconds |
| 91 | +
|
| 92 | + .. image :: ../docs/_static/speaker.jpg |
| 93 | + :alt: Onboard speaker |
| 94 | +
|
| 95 | + .. code-block:: python |
| 96 | +
|
| 97 | + from adafruit_circuitplayground.bluefruit import cpb |
| 98 | +
|
| 99 | + cpb.play_tone(440, 1) |
| 100 | + """ |
| 101 | + # Play a tone of the specified frequency (hz). |
| 102 | + self.start_tone(frequency) |
| 103 | + time.sleep(duration) |
| 104 | + self.stop_tone() |
| 105 | + |
| 106 | + def start_tone(self, frequency): |
| 107 | + """ Produce a tone using the speaker. Try changing frequency to change |
| 108 | + the pitch of the tone. |
| 109 | +
|
| 110 | + :param int frequency: The frequency of the tone in Hz |
| 111 | +
|
| 112 | + .. image :: ../docs/_static/speaker.jpg |
| 113 | + :alt: Onboard speaker |
| 114 | +
|
| 115 | + .. code-block:: python |
| 116 | +
|
| 117 | + from adafruit_circuitplayground.bluefruit import cpb |
| 118 | +
|
| 119 | + while True: |
| 120 | + if cpb.button_a: |
| 121 | + cpb.start_tone(262) |
| 122 | + elif cpb.button_b: |
| 123 | + cpb.start_tone(294) |
| 124 | + else: |
| 125 | + cpb.stop_tone() |
| 126 | + """ |
| 127 | + self._speaker_enable.value = True |
| 128 | + length = 100 |
| 129 | + if length * frequency > 350000: |
| 130 | + length = 350000 // frequency |
| 131 | + self._generate_sample(length) |
| 132 | + # Start playing a tone of the specified frequency (hz). |
| 133 | + self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency) |
| 134 | + if not self._sample.playing: |
| 135 | + self._sample.play(self._sine_wave_sample, loop=True) |
| 136 | + |
| 137 | + def stop_tone(self): |
| 138 | + """ Use with start_tone to stop the tone produced. |
| 139 | +
|
| 140 | + .. image :: ../docs/_static/speaker.jpg |
| 141 | + :alt: Onboard speaker |
| 142 | +
|
| 143 | + .. code-block:: python |
| 144 | +
|
| 145 | + from adafruit_circuitplayground.bluefruit import cpb |
| 146 | +
|
| 147 | + while True: |
| 148 | + if cpb.button_a: |
| 149 | + cpb.start_tone(262) |
| 150 | + elif cpb.button_b: |
| 151 | + cpb.start_tone(294) |
| 152 | + else: |
| 153 | + cpb.stop_tone() |
| 154 | + """ |
| 155 | + # Stop playing any tones. |
| 156 | + if self._sample is not None and self._sample.playing: |
| 157 | + self._sample.stop() |
| 158 | + self._sample.deinit() |
| 159 | + self._sample = None |
| 160 | + self._speaker_enable.value = False |
| 161 | + |
| 162 | + def play_file(self, file_name): |
| 163 | + """ Play a .wav file using the onboard speaker. |
| 164 | +
|
| 165 | + :param file_name: The name of your .wav file in quotation marks including .wav |
| 166 | +
|
| 167 | + .. image :: ../docs/_static/speaker.jpg |
| 168 | + :alt: Onboard speaker |
| 169 | +
|
| 170 | + .. code-block:: python |
| 171 | +
|
| 172 | + from adafruit_circuitplayground.bluefruit import cpb |
| 173 | +
|
| 174 | + while True: |
| 175 | + if cpb.button_a: |
| 176 | + cpb.play_file("laugh.wav") |
| 177 | + elif cpb.button_b: |
| 178 | + cpb.play_file("rimshot.wav") |
| 179 | + """ |
| 180 | + # Play a specified file. |
| 181 | + self.stop_tone() |
| 182 | + self._speaker_enable.value = True |
| 183 | + with audiopwmio.PWMAudioOut(board.SPEAKER) as audio: |
| 184 | + wavefile = audiocore.WaveFile(open(file_name, "rb")) |
| 185 | + audio.play(wavefile) |
| 186 | + while audio.playing: |
| 187 | + pass |
| 188 | + self._speaker_enable.value = False |
| 189 | + |
| 190 | + |
| 191 | +cpb = Bluefruit() # pylint: disable=invalid-name |
| 192 | +"""Object that is automatically created on import. |
| 193 | +
|
| 194 | + To use, simply import it from the module: |
| 195 | +
|
| 196 | + .. code-block:: python |
| 197 | +
|
| 198 | + from adafruit_circuitplayground.bluefruit import cpb |
| 199 | +""" |
0 commit comments