From c377b39ba584cf03d734f228ec7a3855f5fb12eb Mon Sep 17 00:00:00 2001 From: hexthat Date: Tue, 1 Jan 2019 10:36:26 -0800 Subject: [PATCH 1/6] add sound sensor --- adafruit_circuitplayground/express.py | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index f9eeaaf..0de1a2b 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -51,6 +51,7 @@ import adafruit_thermistor import analogio import audioio +import audiobusio import board import busio import digitalio @@ -99,6 +100,12 @@ def __init__(self): # Define sensors: self._temp = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950) self._light = Photocell(board.LIGHT) + self._sound = audiobusio.PDMIn( + board.MICROPHONE_CLOCK, + board.MICROPHONE_DATA, + sample_rate=16000, + bit_depth=16 + ) # Define audio: self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) @@ -495,6 +502,34 @@ def switch(self): """ return self._switch.value + @property + def magnitude(self): + """The magnitude of sound pressure change within a sound wave. + + .. image :: ../docs/_static/circuitplayground_express.jpg + :alt: Sound sensor + + Try clapping to see it count claps. + + .. code-block:: python + + + from adafruit_circuitplayground.express import time, cpx + clapcount = 0 + + while True: + if cpx.magnitude > 500: + clapcount = clapcount + 1 + print("clap", clapcount) + time.sleep(0.3) + """ + samples = array.array('H', [0] * 80) + self._sound.record(samples, len(samples)) + minbuf = int(sum(samples) / len(samples)) + sum_of_samples = sum(float(sample - minbuf) * (sample - minbuf) + for sample in samples) + return math.sqrt(sum_of_samples / len(samples)) + @property def temperature(self): """The temperature of the CircuitPlayground in Celsius. From c66114f33298c4842d5258e101226b04d5751423 Mon Sep 17 00:00:00 2001 From: hexthat Date: Thu, 9 May 2019 12:19:27 -0700 Subject: [PATCH 2/6] Add files via upload --- adafruit_circuitplayground/express.py | 108 ++++++++++++++------------ 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index 0de1a2b..5021cf7 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -51,7 +51,6 @@ import adafruit_thermistor import analogio import audioio -import audiobusio import board import busio import digitalio @@ -100,12 +99,6 @@ def __init__(self): # Define sensors: self._temp = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950) self._light = Photocell(board.LIGHT) - self._sound = audiobusio.PDMIn( - board.MICROPHONE_CLOCK, - board.MICROPHONE_DATA, - sample_rate=16000, - bit_depth=16 - ) # Define audio: self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) @@ -502,34 +495,6 @@ def switch(self): """ return self._switch.value - @property - def magnitude(self): - """The magnitude of sound pressure change within a sound wave. - - .. image :: ../docs/_static/circuitplayground_express.jpg - :alt: Sound sensor - - Try clapping to see it count claps. - - .. code-block:: python - - - from adafruit_circuitplayground.express import time, cpx - clapcount = 0 - - while True: - if cpx.magnitude > 500: - clapcount = clapcount + 1 - print("clap", clapcount) - time.sleep(0.3) - """ - samples = array.array('H', [0] * 80) - self._sound.record(samples, len(samples)) - minbuf = int(sum(samples) / len(samples)) - sum_of_samples = sum(float(sample - minbuf) * (sample - minbuf) - for sample in samples) - return math.sqrt(sum_of_samples / len(samples)) - @property def temperature(self): """The temperature of the CircuitPlayground in Celsius. @@ -598,24 +563,26 @@ def red_led(self, value): self._led.value = value @staticmethod - def _sine_sample(length): - tone_volume = (2 ** 15) - 1 + def _sine_sample(length, volume=None): + if volume is None: + volume = 0 + tone_volume = (2 ** 15) + volume shift = 2 ** 15 for i in range(length): yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift) - def _generate_sample(self, length=100): + def _generate_sample(self, length=100, volume=None): if self._sample is not None: return - self._sine_wave = array.array("H", Express._sine_sample(length)) + self._sine_wave = array.array("H", Express._sine_sample(length, volume)) if sys.implementation.version[0] >= 3: self._sample = audioio.AudioOut(board.SPEAKER) self._sine_wave_sample = audioio.RawSample(self._sine_wave) else: - self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave) + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") - def play_tone(self, frequency, duration): + def play_tone(self, frequency, duration, volume=None): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -632,11 +599,11 @@ def play_tone(self, frequency, duration): cpx.play_tone(440, 1) """ # Play a tone of the specified frequency (hz). - self.start_tone(frequency) + self.start_tone(frequency, volume) time.sleep(duration) self.stop_tone() - def start_tone(self, frequency): + def start_tone(self, frequency, volume=None): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -661,16 +628,14 @@ def start_tone(self, frequency): length = 100 if length * frequency > 350000: length = 350000 // frequency - self._generate_sample(length) + self._generate_sample(length, volume) # Start playing a tone of the specified frequency (hz). if sys.implementation.version[0] >= 3: self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency) if not self._sample.playing: self._sample.play(self._sine_wave_sample, loop=True) else: - self._sample.frequency = int(len(self._sine_wave) * frequency) - if not self._sample.playing: - self._sample.play(loop=True) + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") def stop_tone(self): """ Use with start_tone to stop the tone produced. @@ -697,6 +662,50 @@ def stop_tone(self): self._sample = None self._speaker_enable.value = False + def play_melody(self, frequencies, durations, speed=10, volume=None): + """ Play a melody using notes and beats, rests are 0 frequency. + + :param int frequency: The frequency of the tone in Hz + :param float duration: The duration of the tone in seconds + :param float speed: Optional parameter, fine tune duration + + .. image :: ../docs/_static/speaker.jpg + :alt: Onboard speaker + + .. code-block:: python + + from adafruit_circuitplayground.express import cpx + + notes = [698, 880, 988, 698, 880, 988, 698, 880, 988, + 1319, 1175, 988, 1047, 988, 784, 659, 587, 659, 784, 659, + 698, 880, 988, 698, 880, 988, 698, 880, 988, 1319, + 1175, 988, 1047, 1319, 988, 784, 988, 784, 587, 659, + 587, 659, 698, 784, 880, 988, 1047, 988, 659, 698, + 784, 880, 988, 1047, 1175, 1319, 1397, 1568, + 587, 659, 698, 784, 880, 988, 1047, 988, 659, + 784, 698, 880, 784, 988, 880, 1047, 988, 1175, 1047, + 1319, 1175, 1397, 1319, 1319, 1397, 1175, 1319, + 0, 0, 0] + + beats= [2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 8, 2, 2, 2, + 10, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 8, 2, 2, 2, 10, + 2, 2, 4, 2, 2, 4, 2, 2, 8, 2, 2, 4, 2, 2, 4, 2, 2, 8, + 2, 2, 4, 2, 2, 4, 2, 2, 8, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 6, + 4, 2, 2] + + while True: + cpx.play_melody(notes, beats) + """ + # Play a melody. + if len(frequencies) == len(durations): + for idx, frequency in enumerate(frequencies): + if frequency != 0: + self.start_tone(frequency, volume) + time.sleep(durations[idx]/speed) + self._sample.stop() + self.stop_tone() + def play_file(self, file_name): """ Play a .wav file using the onboard speaker. @@ -725,10 +734,7 @@ def play_file(self, file_name): while audio.playing: pass else: - with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio: - audio.play() - while audio.playing: - pass + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") self._speaker_enable.value = False From 824e11c4392c54e429cc35b435185c849c4a084c Mon Sep 17 00:00:00 2001 From: hexthat Date: Thu, 9 May 2019 13:35:01 -0700 Subject: [PATCH 3/6] correct tab --- adafruit_circuitplayground/express.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index 5021cf7..5806303 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -698,7 +698,7 @@ def play_melody(self, frequencies, durations, speed=10, volume=None): cpx.play_melody(notes, beats) """ # Play a melody. - if len(frequencies) == len(durations): + if len(frequencies) == len(durations): for idx, frequency in enumerate(frequencies): if frequency != 0: self.start_tone(frequency, volume) From 0b713133e268912b7f3108e33fe38918aba59a1c Mon Sep 17 00:00:00 2001 From: hexthat Date: Thu, 9 May 2019 13:43:07 -0700 Subject: [PATCH 4/6] remove index len() match check --- adafruit_circuitplayground/express.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index 5806303..a51598f 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -698,13 +698,12 @@ def play_melody(self, frequencies, durations, speed=10, volume=None): cpx.play_melody(notes, beats) """ # Play a melody. - if len(frequencies) == len(durations): - for idx, frequency in enumerate(frequencies): - if frequency != 0: - self.start_tone(frequency, volume) - time.sleep(durations[idx]/speed) - self._sample.stop() - self.stop_tone() + for idx, frequency in enumerate(frequencies): + if frequency != 0: + self.start_tone(frequency, volume) + time.sleep(durations[idx]/speed) + self._sample.stop() + self.stop_tone() def play_file(self, file_name): """ Play a .wav file using the onboard speaker. From 51a1313360a9e268947641f4fb858273e8c8715f Mon Sep 17 00:00:00 2001 From: hexthat Date: Fri, 10 May 2019 19:34:11 -0700 Subject: [PATCH 5/6] remove melody --- adafruit_circuitplayground/express.py | 43 --------------------------- 1 file changed, 43 deletions(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index a51598f..28bc326 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -662,49 +662,6 @@ def stop_tone(self): self._sample = None self._speaker_enable.value = False - def play_melody(self, frequencies, durations, speed=10, volume=None): - """ Play a melody using notes and beats, rests are 0 frequency. - - :param int frequency: The frequency of the tone in Hz - :param float duration: The duration of the tone in seconds - :param float speed: Optional parameter, fine tune duration - - .. image :: ../docs/_static/speaker.jpg - :alt: Onboard speaker - - .. code-block:: python - - from adafruit_circuitplayground.express import cpx - - notes = [698, 880, 988, 698, 880, 988, 698, 880, 988, - 1319, 1175, 988, 1047, 988, 784, 659, 587, 659, 784, 659, - 698, 880, 988, 698, 880, 988, 698, 880, 988, 1319, - 1175, 988, 1047, 1319, 988, 784, 988, 784, 587, 659, - 587, 659, 698, 784, 880, 988, 1047, 988, 659, 698, - 784, 880, 988, 1047, 1175, 1319, 1397, 1568, - 587, 659, 698, 784, 880, 988, 1047, 988, 659, - 784, 698, 880, 784, 988, 880, 1047, 988, 1175, 1047, - 1319, 1175, 1397, 1319, 1319, 1397, 1175, 1319, - 0, 0, 0] - - beats= [2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 8, 2, 2, 2, - 10, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 8, 2, 2, 2, 10, - 2, 2, 4, 2, 2, 4, 2, 2, 8, 2, 2, 4, 2, 2, 4, 2, 2, 8, - 2, 2, 4, 2, 2, 4, 2, 2, 8, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 6, - 4, 2, 2] - - while True: - cpx.play_melody(notes, beats) - """ - # Play a melody. - for idx, frequency in enumerate(frequencies): - if frequency != 0: - self.start_tone(frequency, volume) - time.sleep(durations[idx]/speed) - self._sample.stop() - self.stop_tone() - def play_file(self, file_name): """ Play a .wav file using the onboard speaker. From 1e70f09209e28b99e11aa1a6d4d84db5b4b94fb6 Mon Sep 17 00:00:00 2001 From: hexthat Date: Fri, 10 May 2019 19:38:36 -0700 Subject: [PATCH 6/6] Update express.py --- adafruit_circuitplayground/express.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index 28bc326..ccd8e2c 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -581,7 +581,6 @@ def _generate_sample(self, length=100, volume=None): else: raise NotImplementedError("Please use CircuitPython 3.0 or higher.") - def play_tone(self, frequency, duration, volume=None): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -693,7 +692,6 @@ def play_file(self, file_name): raise NotImplementedError("Please use CircuitPython 3.0 or higher.") self._speaker_enable.value = False - cpx = Express() # pylint: disable=invalid-name """Object that is automatically created on import.