Skip to content
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

Update sound meter code to work with CPB and CPX #93

Closed
wants to merge 3 commits into from
Closed
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
50 changes: 50 additions & 0 deletions adafruit_circuitplayground/circuit_playground_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import math
import array
import time
import os

try:
import audiocore
Expand Down Expand Up @@ -78,6 +79,9 @@ class CircuitPlaygroundBase: # pylint: disable=too-many-public-methods

_audio_out = None

# Circuit Playground specific board names as found in os.uname().machine
_CP_TYPES = ("Bluefruit", "Express")
Copy link
Member

Choose a reason for hiding this comment

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

@dhalbert would you think doing this consts make more sense?

Copy link
Contributor

@dhalbert dhalbert May 1, 2020

Choose a reason for hiding this comment

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

I suggested consts as a possibility but @kattni preferred strings. We need the strings anyway to search for them in the os.uname() field to distinguish the boards.

We could use strings that have class attribute names too.


def __init__(self):
self._a = digitalio.DigitalInOut(board.BUTTON_A)
self._a.switch_to_input(pull=digitalio.Pull.DOWN)
Expand Down Expand Up @@ -138,6 +142,52 @@ def __init__(self):
self._detect_taps = 1
self.detect_taps = 1

@property
def circuit_playground_type(self):
"""The Circuit Playground board type currently running the library. Returns the board name
shortened to the specific type, e.g. ``Express`` or ``Bluefruit``.

The following example prints the board type to the serial console.

To use with the Circuit Playground Express or Bluefruit:

.. code-block:: python

from adafruit_circuitplayground import cp

print(cp.circuit_playground_type)
"""
board_name = os.uname().machine
for board_type in self._CP_TYPES:
if board_type in board_name:
return board_type
raise ValueError("Board unsupported by this library.")

def circuit_playground_is_type(self, is_type):
"""Checks whether the board is the specified type of Circuit Playground, using the board
name shortened to the specific type, e.g. ``Express`` or ``Bluefruit``.

To use with the Circuit Playground Express or Bluefruit:

The following example prints the board type to the serial console, if the board type is a
Circuit Playground Express. It does nothing if the board is not a CPX. Change ``Express``
to ``Bluefruit`` to print the board type to the serial console, if the board type is a
Circuit Playground Bluefruit.

.. code-block:: python

from adafruit_circuitplayground import cp

if cp.circuit_playground_is_type("Express"):
print(cp.circuit_playground_type)

"""
if is_type not in self._CP_TYPES:
raise ValueError(
"Valid Circuit Playground boards are {}".format(self._CP_TYPES)
)
return self.circuit_playground_type == is_type

@property
def detect_taps(self):
"""Configure what type of tap is detected by ``cp.tapped``. Use ``1`` for single-tap
Expand Down
29 changes: 19 additions & 10 deletions examples/circuitplayground_sound_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
to see the NeoPixels light up!"""
import array
import math
import board
import audiobusio
import board
from adafruit_circuitplayground import cp


Expand All @@ -27,23 +27,32 @@ def normalized_rms(values):
)


mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
)
# Check to see if the board type is a Circuit Playground Express, and, if so, run the following:
if cp.circuit_playground_is_type("Express"):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if cp.circuit_playground_is_type("Express"):
if isinstance(cp, Express):

Copy link
Member

Choose a reason for hiding this comment

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

This will also need another import from adafruit_circuitplayground.express import Express and the `cp instantiate will need to be in init (otherwise this import will construct the cpx object.)

mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
)

samples = array.array("H", [0] * 160)
mic.record(samples, len(samples))
input_floor = normalized_rms(samples) + 10
samples = array.array("H", [0] * 160)
mic.record(samples, len(samples))
input_floor = normalized_rms(samples) + 10
# If it is not, run the following:
else:
input_floor = cp.sound_level + 10

# Lower number means more sensitive - more LEDs will light up with less sound.
sensitivity = 500
input_ceiling = input_floor + sensitivity

peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print((magnitude,))
if cp.circuit_playground_is_type("Express"): # Circuit Playground Express
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
else: # Not CPX
magnitude = cp.sound_level

print((magnitude,)) # Printed as a tuple for the Mu plotter.

c = log_scale(
constrain(magnitude, input_floor, input_ceiling),
Expand Down