Skip to content

Commit c182652

Browse files
docs: Add SoundEvent.CLAP (V2).
1 parent 87232b7 commit c182652

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

docs/microbit_micropython_api.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Sound events describe changes in the sound heard by the microphone::
8787
# Value to represent the transition of sound events, from `loud` to `quiet`
8888
# like speaking or background music.
8989
SoundEvent.QUIET = SoundEvent('quiet')
90+
# Value to represent a loud event similar to a clap.
91+
SoundEvent.CLAP = SoundEvent('clap')
9092

9193
Microphone **V2**
9294
-----------------
@@ -95,7 +97,7 @@ The Microphone is accessed via the `microphone` object::
9597

9698
# Returns the name of the last recorded sound event.
9799
current_event()
98-
# A sound event, such as `SoundEvent.LOUD` or `SoundEvent.QUIET`.
100+
# A sound event, such as `SoundEvent.LOUD`, `SoundEvent.QUIET`, or `SoundEvent.CLAP`.
99101
# Returns`true` if sound was heard at least once since the last
100102
# call, otherwise `false`.
101103
was_event(event)

docs/microphone.rst

+28-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ which is lit when the microphone is in use.
1717
Sound events
1818
============
1919
The microphone can respond to a pre-defined set of sound events that are
20-
based on the amplitude and wavelength of the sound.
20+
based on the amplitude and wavelength of the sound.
2121

2222
These sound events are represented by instances of the ``SoundEvent`` class,
2323
accessible via variables in ``microbit.SoundEvent``:
@@ -26,48 +26,48 @@ accessible via variables in ``microbit.SoundEvent``:
2626
from ``loud`` to ``quiet`` like speaking or background music.
2727

2828
- ``microbit.SoundEvent.LOUD``: Represents the transition of sound events,
29-
from ``quiet`` to ``loud`` like clapping or shouting.
29+
from ``quiet`` to ``loud`` like shouting.
30+
31+
- ``microbit.SoundEvent.CLAP``: Detects a loud event similar to a clap.
3032

3133
Functions
3234
=========
3335

3436
.. py:function:: current_event()
3537
36-
* **return**: the name of the last recorded sound event,
37-
``SoundEvent('loud')`` or ``SoundEvent('quiet')``.
38+
:returns: The name of the last recorded sound event,
39+
``SoundEvent('loud')``, ``SoundEvent('quiet')``, ``SoundEvent('clap')``.
3840

3941
.. py:function:: was_event(event)
4042
41-
* **event**: a sound event, such as ``SoundEvent.LOUD`` or
42-
``SoundEvent.QUIET``.
43-
* **return**: ``true`` if sound was heard at least once since the last
43+
:param event: A sound event, such as ``SoundEvent.LOUD``,
44+
``SoundEvent.QUIET``, or ``SoundEvent.CLAP``.
45+
:returns: ``true`` if sound was heard at least once since the last
4446
call, otherwise ``false``. ``was_event()`` also clears the sound
4547
event history before returning.
4648

4749
.. py:function:: is_event(event)
4850
49-
* **event**: a sound event, such as ``SoundEvent.LOUD`` or
50-
``SoundEvent.QUIET``.
51-
* **return**: ``true`` if sound event is the most recent since the last
51+
:param event: A sound event, such as ``SoundEvent.LOUD`` or
52+
``SoundEvent.QUIET``, or ``SoundEvent.CLAP``.
53+
:returns: ``true`` if sound event is the most recent since the last
5254
call, otherwise ``false``. It does not clear the sound event history.
5355

5456
.. py:function:: get_events()
5557
56-
* **return**: a tuple of the event history. The most recent is listed last.
58+
:returns: A tuple of the event history. The most recent is listed last.
5759
``get_events()`` also clears the sound event history before returning.
5860

5961
.. py:function:: set_threshold(event, value)
6062
61-
* **event**: a sound event, such as ``SoundEvent.LOUD`` or
62-
``SoundEvent.QUIET``.
63-
64-
* **value**: The threshold level in the range 0-255. For example,
63+
:param event: A ``SoundEvent.LOUD`` or ``SoundEvent.QUIET`` sound event.
64+
:param value: The threshold level in the range 0-255. For example,
6565
``set_threshold(SoundEvent.LOUD, 250)`` will only trigger if the sound is
6666
very loud (>= 250).
6767

6868
.. py:function:: sound_level()
6969
70-
* **return**: a representation of the sound pressure level in the range 0 to
70+
:returns: A representation of the sound pressure level in the range 0 to
7171
255.
7272

7373

@@ -80,7 +80,7 @@ An example that runs through some of the functions of the microphone API::
8080
# Button A is pressed and a loud or quiet sound *is* heard, printing the
8181
# results. On Button B this test should update the display when a loud or
8282
# quiet sound *was* heard, printing the results. On shake this should print
83-
# the last sounds heard, you should try this test whilst making a loud sound
83+
# the last sounds heard, you should try this test whilst making a loud sound
8484
# and a quiet one before you shake.
8585

8686
from microbit import *
@@ -90,7 +90,10 @@ An example that runs through some of the functions of the microphone API::
9090

9191
while True:
9292
if button_a.is_pressed():
93-
if microphone.current_event() == SoundEvent.LOUD:
93+
if microphone.current_event() == SoundEvent.CLAP:
94+
display.show(Image.DIAMOND)
95+
uart.write('isClap\n')
96+
elif microphone.current_event() == SoundEvent.LOUD:
9497
display.show(Image.SQUARE)
9598
uart.write('isLoud\n')
9699
elif microphone.current_event() == SoundEvent.QUIET:
@@ -99,7 +102,10 @@ An example that runs through some of the functions of the microphone API::
99102
sleep(500)
100103
display.clear()
101104
if button_b.is_pressed():
102-
if microphone.was_event(SoundEvent.LOUD):
105+
if microphone.was_event(SoundEvent.CLAP):
106+
display.show(Image.DIAMOND)
107+
uart.write('wasClap\n')
108+
elif microphone.was_event(SoundEvent.LOUD):
103109
display.show(Image.SQUARE)
104110
uart.write('wasLoud\n')
105111
elif microphone.was_event(SoundEvent.QUIET):
@@ -114,7 +120,9 @@ An example that runs through some of the functions of the microphone API::
114120
soundLevel = microphone.sound_level()
115121
print(soundLevel)
116122
for sound in sounds:
117-
if sound == SoundEvent.LOUD:
123+
if sound == SoundEvent.CLAP:
124+
display.show(Image.DIAMOND)
125+
elif sound == SoundEvent.LOUD:
118126
display.show(Image.SQUARE)
119127
elif sound == SoundEvent.QUIET:
120128
display.show(Image.SQUARE_SMALL)

0 commit comments

Comments
 (0)