|
| 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 helper 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 digitalio |
| 43 | +import board |
| 44 | +import audiopwmio |
| 45 | +import audiobusio |
| 46 | +from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase |
| 47 | + |
| 48 | + |
| 49 | +__version__ = "0.0.0-auto.0" |
| 50 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git" |
| 51 | + |
| 52 | + |
| 53 | +class Bluefruit(CircuitPlaygroundBase): |
| 54 | + """Represents a single CircuitPlayground Bluefruit.""" |
| 55 | + |
| 56 | + _audio_out = audiopwmio.PWMAudioOut |
| 57 | + |
| 58 | + def __init__(self): |
| 59 | + # Only create the cpb module member when we aren't being imported by Sphinx |
| 60 | + if ("__module__" in dir(digitalio.DigitalInOut) and |
| 61 | + digitalio.DigitalInOut.__module__ == "sphinx.ext.autodoc"): |
| 62 | + return |
| 63 | + |
| 64 | + super().__init__() |
| 65 | + |
| 66 | + self._sample = None |
| 67 | + |
| 68 | + # Define mic/sound sensor: |
| 69 | + self._mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, |
| 70 | + sample_rate=16000, bit_depth=16) |
| 71 | + self._samples = None |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def _normalized_rms(values): |
| 75 | + mean_values = int(sum(values) / len(values)) |
| 76 | + return math.sqrt(sum(float(sample - mean_values) * (sample - mean_values) |
| 77 | + for sample in values) / len(values)) |
| 78 | + |
| 79 | + @property |
| 80 | + def sound_level(self): |
| 81 | + """Obtain the sound level from the microphone (sound sensor). |
| 82 | +
|
| 83 | + .. image :: ../docs/_static/microphone.jpg |
| 84 | + :alt: Microphone (sound sensor) |
| 85 | +
|
| 86 | + This example prints the sound levels. Try clapping or blowing on |
| 87 | + the microphone to see the levels change. |
| 88 | +
|
| 89 | + .. code-block:: python |
| 90 | +
|
| 91 | + from adafruit_circuitplayground.bluefruit import cpb |
| 92 | +
|
| 93 | + while True: |
| 94 | + print(cpb.sound_level) |
| 95 | + """ |
| 96 | + if self._sample is None: |
| 97 | + self._samples = array.array('H', [0] * 160) |
| 98 | + self._mic.record(self._samples, len(self._samples)) |
| 99 | + return self._normalized_rms(self._samples) |
| 100 | + |
| 101 | + def loud_sound(self, sound_threshold=200): |
| 102 | + """Utilise a loud sound as an input. |
| 103 | +
|
| 104 | + :param int sound_threshold: Threshold sound level must exceed to return true (Default: 200) |
| 105 | +
|
| 106 | + .. image :: ../docs/_static/microphone.jpg |
| 107 | + :alt: Microphone (sound sensor) |
| 108 | +
|
| 109 | + This example turns the LEDs red each time you make a loud sound. |
| 110 | + Try clapping or blowing onto the microphone to trigger it. |
| 111 | +
|
| 112 | + .. code-block:: python |
| 113 | +
|
| 114 | + from adafruit_circuitplayground.bluefruit import cpb |
| 115 | +
|
| 116 | + while True: |
| 117 | + if cpb.loud_sound(): |
| 118 | + cpb.pixels.fill((50, 0, 0)) |
| 119 | + else: |
| 120 | + cpb.pixels.fill(0) |
| 121 | +
|
| 122 | + You may find that the code is not responding how you would like. |
| 123 | + If this is the case, you can change the loud sound threshold to |
| 124 | + make it more or less responsive. Setting it to a higher number |
| 125 | + means it will take a louder sound to trigger. Setting it to a |
| 126 | + lower number will take a quieter sound to trigger. The following |
| 127 | + example shows the threshold being set to a higher number than |
| 128 | + the default. |
| 129 | +
|
| 130 | + .. code-block:: python |
| 131 | +
|
| 132 | + from adafruit_circuitplayground.bluefruit import cpb |
| 133 | +
|
| 134 | + while True: |
| 135 | + if cpb.loud_sound(sound_threshold=300): |
| 136 | + cpb.pixels.fill((50, 0, 0)) |
| 137 | + else: |
| 138 | + cpb.pixels.fill(0) |
| 139 | + """ |
| 140 | + |
| 141 | + return self.sound_level > sound_threshold |
| 142 | + |
| 143 | + |
| 144 | +cpb = Bluefruit() # pylint: disable=invalid-name |
| 145 | +"""Object that is automatically created on import. |
| 146 | +
|
| 147 | + To use, simply import it from the module: |
| 148 | +
|
| 149 | + .. code-block:: python |
| 150 | +
|
| 151 | + from adafruit_circuitplayground.bluefruit import cpb |
| 152 | +""" |
0 commit comments