Skip to content

Commit 669791d

Browse files
docs: Update the docs to include the V2 API (bbcmicrobit#692)
* Add microphone API * include V2 on index * update music and V2 pins * update speech * update audio * update i2c * add description to built-in sounds * format sounds * format pins * format pins * spelling * Update docs/audio.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/audio.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/i2c.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * changes based on initial feedback * update return pin * sound is in microbit module * Update docs/audio.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * more feedback updates * spacing * Update docs/audio.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * does this comment work? * Revert "does this comment work?" This reverts commit 4846113. * update audio * update audio * add Python Editor * add references to sound * update mic image * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * multiple fixes * use Param in docs * update SouneEvent * update microphone module * update param * update image list * update parameters * remove note * update param * add line break * add mic and make clases a-z * format parameters * Update docs/microbit_micropython_api.rst * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * more updates * tidy up * move SoundEvent * Revert "move SoundEvent" This reverts commit 8f52c97. * move soundEvent * update image list * remove param reference * more feedback updates * move audio to modules * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microphone.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microbit_micropython_api.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/microphone.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/audio.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * more updates Co-authored-by: Carlos Pereira Atencio <[email protected]>
1 parent 2093bbb commit 669791d

File tree

11 files changed

+361
-73
lines changed

11 files changed

+361
-73
lines changed

docs/audio.rst

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,47 @@ Audio
33

44
.. py:module:: audio
55
6-
This module allows you play sounds from a speaker attached to the micro:bit.
6+
This module allows you play sounds with the micro:bit.
7+
8+
By default sound output will be via the edge connector on pin 0 and the
9+
built-in speaker **V2**. You can connect a wired headphones or a speaker to
10+
pin 0 and GND on the edge connector to hear the sounds.
711

812
The ``audio`` module can be imported as ``import audio`` or accessed via
913
the ``microbit`` module as ``microbit.audio``.
1014

11-
In order to use the audio module you will need to provide a sound source.
12-
13-
A sound source is an iterable (sequence, like list or tuple, or a generator) of
14-
frames, each of 32 samples.
15-
The ``audio`` modules plays samples at the rate of 7812.5 samples per second,
16-
which means that it can reproduce frequencies up to 3.9kHz.
17-
1815
Functions
1916
=========
2017

2118
.. py:function:: play(source, wait=True, pin=pin0, return_pin=None)
19+
play(source, wait=True, pin=(pin_speaker, pin0), return_pin=None)
2220
2321
Play the source to completion.
2422

25-
:param source: An iterable sound source, each element of which must be
26-
an ``AudioFrame``.
23+
:param source: ``Sound``: The ``microbit`` module contains a list of
24+
built-in sounds that your can pass to ``audio.play()``.
25+
26+
``AudioFrame``: The source agrument can also be an iterable
27+
of ``AudioFrame`` elements as described below.
2728
:param wait: If ``wait`` is ``True``, this function will block until the
2829
source is exhausted.
29-
:param pin: Specifies which pin the speaker is connected to.
30-
:param return_pin: Specifies a differential pin to connect to the speaker
31-
instead of ground.
30+
:param pin: As with the music module, you can use the optional ``pin``
31+
argument to specify the output pin can be used to override the
32+
default of ``microbit.pin0``. If you have the latest micro:bit **V2**,
33+
you can use ``microbit.pin_speaker``. The pin argument can also take a
34+
tuple of two pins, for example ``pin=(pin_speaker, pin0)`` which would
35+
output sound on the built-in speaker and pin 0.
36+
:param return_pin: specifies a differential edge connector pin to connect
37+
to an external speaker instead of ground. This is ignored for the **V2**
38+
revision.
39+
40+
.. py:function:: is_playing()
41+
42+
:returns: ``True`` if audio is playing, otherwise returns ``False``.
43+
44+
.. py:function:: stop()
45+
46+
Stops all audio playback.
3247

3348
Classes
3449
=======
@@ -52,9 +67,25 @@ Classes
5267
Using audio
5368
===========
5469

55-
You will need a sound source, as input to the ``play`` function. You can generate your own, like in
56-
``examples/waveforms.py``.
57-
70+
You will need a sound source, as input to the ``play`` function. You can use
71+
the built-in sounds **V2** from the ``microbit`` module, ``microbit.Sound``, or
72+
generate your own, like in ``examples/waveforms.py``.
73+
74+
Built-in sounds **V2**
75+
----------------------
76+
77+
The built-in sounds can be called using ``audio.play(Sound.NAME)``.
78+
79+
* ``Sound.GIGGLE``
80+
* ``Sound.HAPPY``
81+
* ``Sound.HELLO``
82+
* ``Sound.MYSTERIOUS``
83+
* ``Sound.SAD``
84+
* ``Sound.SLIDE``
85+
* ``Sound.SOARING``
86+
* ``Sound.SPRING``
87+
* ``Sound.TWINKLE``
88+
* ``Sound.YAWN``
5889

5990
Technical Details
6091
=================

docs/i2c.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ Functions
2828

2929
.. warning::
3030

31-
Changing the I²C pins from defaults will make the accelerometer and
32-
compass stop working, as they are connected internally to those pins.
31+
On a micro:bit V1 board, changing the I²C pins from defaults will make
32+
the accelerometer and compass stop working, as they are connected
33+
internally to those pins. This warning does not apply to the **V2**
34+
revision of the micro:bit as this has `separate I²C lines
35+
<https://tech.microbit.org/hardware/i2c/>`_
36+
for the motion sensors and the edge connector.
37+
3338

3439

3540
.. py:function:: scan()

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ This documentation includes lessons for teachers
1717
and API documentation for developers (check out the index on the left). We hope
1818
you enjoy developing for the BBC micro:bit using MicroPython.
1919

20+
.. note::
21+
22+
The BBC micro:bit MicroPython documentation contains information for all
23+
versions of the micro:bit board. Where functionality is applicable only
24+
to the latest device, you will see a note or comment marking this as
25+
**V2**.
26+
2027
If you're a new programmer, teacher or unsure where to start, begin with the
2128
:ref:`Tutorials <Tutorials>` and use the `micro:bit Python Editor <https://python.microbit.org>`_
2229
to program the micro:bit.
@@ -77,6 +84,7 @@ Projects related to MicroPython on the BBC micro:bit include:
7784
i2c.rst
7885
image.rst
7986
machine.rst
87+
microphone.rst
8088
micropython.rst
8189
music.rst
8290
neopixel.rst

docs/microbit.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Classes
9090
:maxdepth: 1
9191

9292
image.rst
93+
Sound <audio.rst>
9394

9495

9596
Modules
@@ -99,8 +100,10 @@ Modules
99100
:maxdepth: 1
100101

101102
accelerometer.rst
103+
Audio V2 <audio.rst>
102104
compass.rst
103105
display.rst
104106
i2c.rst
107+
microphone.rst
105108
spi.rst
106109
uart.rst

docs/microbit_micropython_api.rst

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ There are a few functions available directly::
2626
panic(error_code)
2727
# resets the micro:bit.
2828
reset()
29+
# sets the output volume (0-255) of the micro:bit speaker **V2** and
30+
# external speaker or headphones connected to the edge connector pins.
31+
set_volume(128) # V2
2932

3033
The rest of the functionality is provided by objects and classes in the microbit module, as described below.
3134

@@ -74,11 +77,43 @@ The LED display is exposed via the `display` object::
7477
# written messages).
7578
display.scroll(string, delay=400)
7679

80+
SoundEvent **V2**
81+
-----------------
82+
Sound events describe changes in the sound heard by the microphone::
83+
84+
# Value to represent the transition of sound events, from `quiet` to `loud`
85+
# like clapping or shouting.
86+
SoundEvent.LOUD = SoundEvent('loud')
87+
# Value to represent the transition of sound events, from `loud` to `quiet`
88+
# like speaking or background music.
89+
SoundEvent.QUIET = SoundEvent('quiet')
90+
91+
Microphone **V2**
92+
-----------------
93+
94+
The Microphone is accessed via the `microphone` object::
95+
96+
# Returns the name of the last recorded sound event.
97+
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`.
101+
was_event(event)
102+
# Returns a tuple of the event history. The most recent is listed last.
103+
# Also clears the sound event history before returning.
104+
get_events()
105+
# The threshold level in the range 0-255. For example,
106+
# `set_threshold(SoundEvent.LOUD, 250)` will only trigger if the
107+
# sound is very loud (>= 250).
108+
set_threshold(128)
109+
# Returns a representation of the sound pressure level in the range 0 to 255.
110+
sound_level()
111+
77112
Pins
78113
----
79114

80115
Provide digital and analog input and output functionality, for the pins in the
81-
connector. Some pins are connected
116+
connector, the **V2** logo and the **V2** speaker. Some pins are connected
82117
internally to the I/O that drives the LED matrix and the buttons.
83118

84119
Each pin is provided as an object directly in the ``microbit`` module. This
@@ -92,6 +127,8 @@ keeps the API relatively flat, making it very easy to use:
92127
* *Warning: P17-P18 (inclusive) are unavailable.*
93128
* pin19
94129
* pin20
130+
* pin_logo **V2**
131+
* pin_speaker **V2**
95132

96133
Each of these pins are instances of the ``MicroBitPin`` class, which offers the following API::
97134

@@ -113,6 +150,21 @@ Each of these pins are instances of the ``MicroBitPin`` class, which offers the
113150
# is touched
114151
pin.is_touched()
115152

153+
Except in the case of the pins marked **V2**, which offers the following API:
154+
155+
pin_logo::
156+
157+
# returns boolean for logo touch pin
158+
pin_logo.is_touched()
159+
160+
pin_speaker, as the above ``MicroBitPin`` class, but does not include
161+
``pin.is_touched()`` and includes::
162+
163+
# disable the built-in speaker
164+
pin_speaker.disable()
165+
# enable the built-in speaker
166+
pin_speaker.enable()
167+
116168
Images
117169
------
118170

@@ -277,6 +329,25 @@ There is an I2C bus on the micro:bit that is exposed via the `i2c` object. It h
277329
# write buf to device with addr; repeat=True means a stop bit won't be sent.
278330
i2c.write(addr, buf, repeat=False)
279331

332+
Sound **V2**
333+
------------
334+
335+
A set of expressive sounds are available to the micro:bit **V2**. They can be
336+
accessed via the ``microbit`` module and played with the :doc:`audio <audio>` module.
337+
338+
**Built-in sounds**
339+
340+
``Sound.GIGGLE``
341+
``Sound.HAPPY``
342+
``Sound.HELLO``
343+
``Sound.MYSTERIOUS``
344+
``Sound.SAD``
345+
``Sound.SLIDE``
346+
``Sound.SOARING``
347+
``Sound.SPRING``
348+
``Sound.TWINKLE``
349+
``Sound.YAWN``
350+
280351
UART
281352
----
282353

docs/microphone.png

48.4 KB
Loading

docs/microphone.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
Microphone **V2**
2+
*****************
3+
4+
.. py:module:: microbit.microphone
5+
6+
This object lets you access the built-in microphone available on the
7+
micro:bit **V2**. It can be used to respond to sound. The microphone input
8+
is located on the front of the board alongside a microphone activity LED,
9+
which is lit when the microphone is in use.
10+
11+
.. image:: microphone.png
12+
:width: 300px
13+
:align: center
14+
:height: 240px
15+
:alt: micro:bit with microphone LED on
16+
17+
Sound events
18+
============
19+
The microphone can respond to a pre-defined set of sound events that are
20+
based on the amplitude and wavelength of the sound.
21+
22+
These sound events are represented by instances of the ``SoundEvent`` class,
23+
accessible via variables in ``microbit.SoundEvent``:
24+
25+
- ``microbit.SoundEvent.QUIET``: Represents the transition of sound events,
26+
from ``loud`` to ``quiet`` like speaking or background music.
27+
28+
- ``microbit.SoundEvent.LOUD``: Represents the transition of sound events,
29+
from ``quiet`` to ``loud`` like clapping or shouting.
30+
31+
Functions
32+
=========
33+
34+
.. py:function:: current_event()
35+
36+
* **return**: the name of the last recorded sound event,
37+
``SoundEvent('loud')`` or ``SoundEvent('quiet')``.
38+
39+
.. py:function:: was_event(event)
40+
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
44+
call, otherwise ``false``. ``was_event()`` also clears the sound
45+
event history before returning.
46+
47+
.. py:function:: is_event(event)
48+
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
52+
call, otherwise ``false``. It does not clear the sound event history.
53+
54+
.. py:function:: get_events()
55+
56+
* **return**: a tuple of the event history. The most recent is listed last.
57+
``get_events()`` also clears the sound event history before returning.
58+
59+
.. py:function:: set_threshold(event, value)
60+
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,
65+
``set_threshold(SoundEvent.LOUD, 250)`` will only trigger if the sound is
66+
very loud (>= 250).
67+
68+
.. py:function:: sound_level()
69+
70+
* **return**: a representation of the sound pressure level in the range 0 to
71+
255.
72+
73+
74+
Example
75+
=======
76+
77+
An example that runs through some of the functions of the microphone API::
78+
79+
# Basic test for microphone. This test should update the display when
80+
# Button A is pressed and a loud or quiet sound *is* heard, printing the
81+
# results. On Button B this test should update the display when a loud or
82+
# 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
84+
# and a quiet one before you shake.
85+
86+
from microbit import *
87+
88+
display.clear()
89+
sound = microphone.current_event()
90+
91+
while True:
92+
if button_a.is_pressed():
93+
if microphone.current_event() == SoundEvent.LOUD:
94+
display.show(Image.SQUARE)
95+
uart.write('isLoud\n')
96+
elif microphone.current_event() == SoundEvent.QUIET:
97+
display.show(Image.SQUARE_SMALL)
98+
uart.write('isQuiet\n')
99+
sleep(500)
100+
display.clear()
101+
if button_b.is_pressed():
102+
if microphone.was_event(SoundEvent.LOUD):
103+
display.show(Image.SQUARE)
104+
uart.write('wasLoud\n')
105+
elif microphone.was_event(SoundEvent.QUIET):
106+
display.show(Image.SQUARE_SMALL)
107+
uart.write('wasQuiet\n')
108+
else:
109+
display.clear()
110+
sleep(500)
111+
display.clear()
112+
if accelerometer.was_gesture('shake'):
113+
sounds = microphone.get_events()
114+
soundLevel = microphone.sound_level()
115+
print(soundLevel)
116+
for sound in sounds:
117+
if sound == SoundEvent.LOUD:
118+
display.show(Image.SQUARE)
119+
elif sound == SoundEvent.QUIET:
120+
display.show(Image.SQUARE_SMALL)
121+
else:
122+
display.clear()
123+
print(sound)
124+
sleep(500)

0 commit comments

Comments
 (0)