Skip to content

Commit c1074db

Browse files
docs: Add SoundEvent.CLAP (V2).
1 parent 7a4cfb5 commit c1074db

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

docs/microbit_micropython_api.rst

+5-3
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,9 +97,9 @@ 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`.
99-
# Returns`true` if sound was heard at least once since the last
100-
# call, otherwise `false`.
100+
# A sound event, such as `SoundEvent.LOUD`, `SoundEvent.QUIET`, or
101+
# `SoundEvent.CLAP`. Returns`true` if sound was heard at least once since
102+
# the last call, otherwise `false`.
101103
was_event(event)
102104
# Returns a tuple of the event history. The most recent is listed last.
103105
# Also clears the sound event history before returning.

docs/microphone.rst

+22-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ 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
=========
@@ -35,16 +37,17 @@ Functions
3537
3638
Get the last recorded sound event.
3739

38-
:return: The event, ``SoundEvent('loud')`` or ``SoundEvent('quiet')``.
40+
:return: The event, ``SoundEvent('loud')``, ``SoundEvent('quiet')`` or
41+
``SoundEvent('clap')``.
3942

4043
.. py:function:: was_event(event)
4144
4245
Check if a sound was heard at least once since the last call.
4346

4447
This call clears the sound history before returning.
4548

46-
:param event: The event to check for, such as ``SoundEvent.LOUD`` or
47-
``SoundEvent.QUIET``.
49+
:param event: The event to check for, such as ``SoundEvent.LOUD``,
50+
``SoundEvent.QUIET`` or ``SoundEvent.CLAP``.
4851
:return: ``True`` if sound was heard at least once since the last call,
4952
otherwise ``False``.
5053

@@ -54,8 +57,8 @@ Functions
5457

5558
This call does not clear the sound event history.
5659

57-
:param event: The event to check for, such as ``SoundEvent.LOUD`` or
58-
``SoundEvent.QUIET``
60+
:param event: The event to check for, such as ``SoundEvent.LOUD``,
61+
``SoundEvent.QUIET`` or ``SoundEvent.CLAP``.
5962
:return: ``True`` if sound was the most recent heard, ``False`` otherwise.
6063

6164
.. py:function:: get_events()
@@ -68,7 +71,7 @@ Functions
6871

6972
.. py:function:: set_threshold(event, value)
7073
71-
Set the threshold for a sound event.
74+
Set the threshold for the ``LOUD`` or ``QUIET`` sound events.
7275

7376
The ``SoundEvent.LOUD`` event will be triggered when the sound level
7477
crosses this threshold upwards (from "quiet" to "loud"),
@@ -80,8 +83,7 @@ Functions
8083
threshold. If the ``SoundEvent.QUIET`` value is set higher than
8184
``SoundEvent.LOUD``, then the "loud" threshold will be set one unit above.
8285

83-
:param event: A sound event, such as ``SoundEvent.LOUD`` or
84-
``SoundEvent.QUIET``.
86+
:param event: A ``SoundEvent.LOUD`` or ``SoundEvent.QUIET`` event.
8587
:param value: The threshold level in the range 0-255. Values outside this
8688
range will be clamped.
8789

@@ -111,7 +113,10 @@ An example that runs through some of the functions of the microphone API::
111113

112114
while True:
113115
if button_a.is_pressed():
114-
if microphone.current_event() == SoundEvent.LOUD:
116+
if microphone.current_event() == SoundEvent.CLAP:
117+
display.show(Image.DIAMOND)
118+
uart.write('isClap\n')
119+
elif microphone.current_event() == SoundEvent.LOUD:
115120
display.show(Image.SQUARE)
116121
uart.write('isLoud\n')
117122
elif microphone.current_event() == SoundEvent.QUIET:
@@ -120,7 +125,10 @@ An example that runs through some of the functions of the microphone API::
120125
sleep(500)
121126
display.clear()
122127
if button_b.is_pressed():
123-
if microphone.was_event(SoundEvent.LOUD):
128+
if microphone.was_event(SoundEvent.CLAP):
129+
display.show(Image.DIAMOND)
130+
uart.write('wasClap\n')
131+
elif microphone.was_event(SoundEvent.LOUD):
124132
display.show(Image.SQUARE)
125133
uart.write('wasLoud\n')
126134
elif microphone.was_event(SoundEvent.QUIET):
@@ -135,7 +143,9 @@ An example that runs through some of the functions of the microphone API::
135143
soundLevel = microphone.sound_level()
136144
print(soundLevel)
137145
for sound in sounds:
138-
if sound == SoundEvent.LOUD:
146+
if sound == SoundEvent.CLAP:
147+
display.show(Image.DIAMOND)
148+
elif sound == SoundEvent.LOUD:
139149
display.show(Image.SQUARE)
140150
elif sound == SoundEvent.QUIET:
141151
display.show(Image.SQUARE_SMALL)

0 commit comments

Comments
 (0)