Skip to content

Commit 5133fd3

Browse files
docs: Update the docs to include the V2 API (#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 9c8d0ea commit 5133fd3

File tree

11 files changed

+370
-78
lines changed

11 files changed

+370
-78
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ BBC micro:bit MicroPython documentation
88

99
Welcome!
1010

11-
The BBC micro:bit is a small computing device for children. One of the
12-
languages it understands is the popular Python programming language. The
13-
version of Python that runs on the BBC micro:bit is called MicroPython.
11+
The `BBC micro:bit <https://microbit.org>`_ is a small computing device for
12+
children. One of the languages it understands is the popular Python programming
13+
language. The version of Python that runs on the BBC micro:bit is called
14+
MicroPython.
1415

1516
This documentation includes lessons for teachers
1617
and API documentation for developers (check out the index on the left). We hope
1718
you enjoy developing for the BBC micro:bit using MicroPython.
1819

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+
1927
If you're a new programmer, teacher or unsure where to start, begin with the
2028
:ref:`Tutorials <Tutorials>` and use the `micro:bit Python Editor <https://python.microbit.org>`_
2129
to program the micro:bit.
@@ -76,6 +84,7 @@ Projects related to MicroPython on the BBC micro:bit include:
7684
i2c.rst
7785
image.rst
7886
machine.rst
87+
microphone.rst
7988
micropython.rst
8089
music.rst
8190
neopixel.rst

docs/microbit.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Classes
6464
:maxdepth: 1
6565

6666
image.rst
67+
Sound <audio.rst>
6768

6869

6970
Modules
@@ -73,8 +74,10 @@ Modules
7374
:maxdepth: 1
7475

7576
accelerometer.rst
77+
Audio V2 <audio.rst>
7678
compass.rst
7779
display.rst
7880
i2c.rst
81+
microphone.rst
7982
spi.rst
8083
uart.rst

docs/microbit_micropython_api.rst

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ There are a few functions available directly::
2222
panic(error_code)
2323
# resets the micro:bit.
2424
reset()
25+
# sets the output volume (0-255) of the micro:bit speaker **V2** and
26+
# external speaker or headphones connected to the edge connector pins.
27+
set_volume(128) # V2
2528

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

@@ -70,10 +73,44 @@ The LED display is exposed via the `display` object::
7073
# written messages).
7174
display.scroll(string, delay=400)
7275

76+
SoundEvent **V2**
77+
-----------------
78+
Sound events describe changes in the sound heard by the microphone::
79+
80+
# Value to represent the transition of sound events, from `quiet` to `loud`
81+
# like clapping or shouting.
82+
SoundEvent.LOUD = SoundEvent('loud')
83+
# Value to represent the transition of sound events, from `loud` to `quiet`
84+
# like speaking or background music.
85+
SoundEvent.QUIET = SoundEvent('quiet')
86+
87+
Microphone **V2**
88+
-----------------
89+
90+
The Microphone is accessed via the `microphone` object::
91+
92+
# Returns the name of the last recorded sound event.
93+
current_event()
94+
# A sound event, such as `SoundEvent.LOUD` or `SoundEvent.QUIET`.
95+
# Returns`true` if sound was heard at least once since the last
96+
# call, otherwise `false`.
97+
was_event(event)
98+
# Returns a tuple of the event history. The most recent is listed last.
99+
# Also clears the sound event history before returning.
100+
get_events()
101+
# The threshold level in the range 0-255. For example,
102+
# `set_threshold(SoundEvent.LOUD, 250)` will only trigger if the
103+
# sound is very loud (>= 250).
104+
set_threshold(128)
105+
# Returns a representation of the sound pressure level in the range 0 to 255.
106+
sound_level()
107+
73108
Pins
74109
----
75110

76-
Provide digital and analog input and output functionality, for the pins in the connector. Some pins are connected internally to the I/O that drives the LED matrix and the buttons.
111+
Provide digital and analog input and output functionality, for the pins in the
112+
connector, the **V2** logo and the **V2** speaker. Some pins are connected
113+
internally to the I/O that drives the LED matrix and the buttons.
77114

78115
Each pin is provided as an object directly in the ``microbit`` module. This keeps the API relatively flat, making it very easy to use:
79116

@@ -85,6 +122,8 @@ Each pin is provided as an object directly in the ``microbit`` module. This kee
85122
* *Warning: P17-P18 (inclusive) are unavailable.*
86123
* pin19
87124
* pin20
125+
* pin_logo **V2**
126+
* pin_speaker **V2**
88127

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

@@ -102,9 +141,25 @@ Each of these pins are instances of the ``MicroBitPin`` class, which offers the
102141
# sets the period of the PWM output of the pin in microseconds
103142
# (see https://en.wikipedia.org/wiki/Pulse-width_modulation)
104143
pin.set_analog_period_microseconds(int)
105-
# returns boolean
144+
# Only available for touch pins 0, 1, and 2. Returns boolean if the pin
145+
# is touched
106146
pin.is_touched()
107147

148+
Except in the case of the pins marked **V2**, which offers the following API:
149+
150+
pin_logo::
151+
152+
# returns boolean for logo touch pin
153+
pin_logo.is_touched()
154+
155+
pin_speaker, as the above ``MicroBitPin`` class, but does not include
156+
``pin.is_touched()`` and includes::
157+
158+
# disable the built-in speaker
159+
pin_speaker.disable()
160+
# enable the built-in speaker
161+
pin_speaker.enable()
162+
108163
Images
109164
------
110165

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

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

docs/microphone.png

48.4 KB
Loading

0 commit comments

Comments
 (0)