Skip to content

Commit f5b4460

Browse files
Docs: Speaker update and play() pin arguments (bbcmicrobit#695)
* speaker pin changes * Docs: update speaker pin info * Add speaker page * grammar * Link to speaker page * add speaker to navigation * add speaker example * Update docs/speaker.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/speaker.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Docs: Update speaker pin * remove build * gitignore _build directory * update music * Update docs/music.rst * Update docs/speech.rst * Update docs/speech.rst * Update docs/speech.rst * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]>
1 parent 9d95dea commit f5b4460

10 files changed

+103
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.yotta.json
22
build/*
3+
_build/*
34
yotta_modules/*
45
yotta_targets/*
56
*.swp

docs/audio.rst

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ This module allows you to play your own sounds. If you are using a micro:bit
77
**V2**, ``audio`` is also part of the ``microbit`` module.
88

99
By default sound output will be via the edge connector on pin 0 and the
10-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
11-
pin 0 and GND on the edge connector to hear the sounds.
10+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
11+
a speaker to pin 0 and GND on the edge connector to hear the sounds.
1212

1313
Functions
1414
=========
1515

1616
.. py:function:: play(source, wait=True, pin=pin0, return_pin=None)
17-
play(source, wait=True, pin=(pin_speaker, pin0), return_pin=None)
1817
1918
Play the source to completion.
2019

@@ -27,12 +26,9 @@ Functions
2726
* **wait**: If ``wait`` is ``True``, this function will block until the
2827
source is exhausted.
2928

30-
* **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.
29+
* **pin**: An optional argument to specify the output pin can be used to
30+
override the default of ``pin0``. If we do not want any sound to play
31+
we can use ``pin=None``.
3632

3733
* **return_pin**: specifies a differential edge connector pin to connect
3834
to an external speaker instead of ground. This is ignored for the **V2**

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Projects related to MicroPython on the BBC micro:bit include:
9292
pin.rst
9393
radio.rst
9494
random.rst
95+
speaker.rst
9596
speech.rst
9697
spi.rst
9798
uart.rst

docs/microbit.rst

+1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ Modules
7979
display.rst
8080
i2c.rst
8181
microphone.rst
82+
speaker.rst
8283
spi.rst
8384
uart.rst

docs/microbit_micropython_api.rst

+15-7
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,7 @@ pin_logo::
154154

155155
pin_speaker:
156156

157-
As above ``MicroBitPin`` class, but does not include ``pin.is_touched()`` and
158-
includes::
159-
160-
# disable the built-in speaker
161-
pin_speaker.disable()
162-
# enable the built-in speaker
163-
pin_speaker.enable()
157+
As above ``MicroBitPin`` class, but does not include ``pin.is_touched()``.
164158

165159
Images
166160
------
@@ -350,6 +344,20 @@ accessed via the ``microbit`` module and played with the :doc:`audio <audio>` mo
350344
``Sound.TWINKLE``
351345
``Sound.YAWN``
352346

347+
Speaker **V2**
348+
--------------
349+
350+
The speaker is enabled by default and can be accessed using the ``speaker`` object. It
351+
can be turned off or on::
352+
353+
# disable the built-in speaker
354+
speaker.off()
355+
# enable the built-in speaker
356+
speaker.on()
357+
# returns True or False to indicate if the speaker is on or off
358+
speaker.is_on()
359+
360+
353361
UART
354362
----
355363

docs/music.rst

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Music
55
66
This is the ``music`` module and you can use it to create and play melodies.
77
By default sound output will be via the edge connector on pin 0 and the
8-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
9-
pin 0 and GND on the edge connector to hear the sound:
8+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
9+
a speaker to pin 0 and GND on the edge connector to hear the sound:
1010

1111
.. image:: music-pins.png
1212

@@ -80,7 +80,6 @@ Functions
8080
Gets the current tempo as a tuple of integers: ``(ticks, bpm)``.
8181

8282
.. py:function:: play(music, pin=pin0, wait=True, loop=False)
83-
play(music, pin=(pin_speaker, pin0) wait=True,loop=False)
8483
8584
Plays ``music`` containing the musical DSL defined above.
8685

@@ -95,36 +94,37 @@ Functions
9594
their defaults before the music (whatever it may be) is played.
9695

9796
An optional argument to specify the output pin can be used to override the
98-
default of ``microbit.pin0``. With micro:bit **V2** you can use
99-
``microbit.pin_speaker`` or a tuple of two pins, for example
100-
``pin=(pin_speaker, pin0)`` which would output sound on the built-in
101-
speaker and pin 0.
97+
default of ``microbit.pin0``. If we do not want any sound to play we can
98+
use ``pin=None``.
10299

103100
If ``wait`` is set to ``True``, this function is blocking.
104101

105102
If ``loop`` is set to ``True``, the tune repeats until ``stop`` is called
106103
(see below) or the blocking call is interrupted.
107104

108-
.. py:function:: pitch(frequency, duration=-1, pin=microbit.pin0, wait=True)
109-
pitch(frequency, duration=-1, pin=(pin_speaker, pin0), wait=True)
105+
.. py:function:: pitch(frequency, duration=-1, pin=pin0, wait=True)
110106
111107
Plays a pitch at the integer frequency given for the specified number of
112108
milliseconds. For example, if the frequency is set to 440 and the length to
113109
1000 then we hear a standard concert A for one second.
114110

115111
Note that you can only play one pitch on one pin at any one time.
116112

113+
An optional argument to specify the output pin can be used to override the
114+
default of ``microbit.pin0``. If we do not want any sound to play out of
115+
the pins we can use ``pin=None``.
116+
117117
If ``wait`` is set to ``True``, this function is blocking.
118118

119-
If ``duration`` is negative the pitch is played continuously until either the
120-
blocking call is interrupted or, in the case of a background call, a new
121-
frequency is set or ``stop`` is called (see below).
119+
If ``duration`` is negative the pitch is played continuously until either
120+
the blocking call is interrupted or, in the case of a background call, a
121+
new frequency is set or ``stop`` is called (see below).
122122

123123
.. py:function:: stop(pin=pin0)
124-
stop(pin=(pin_speaker, pin0))
125124
126-
Stops all music playback on a given pin, eg. ``music.stop(pin_speaker)``
127-
**V2**. If no pin is given, eg. ``music.stop()`` pin0 is assumed.
125+
Stops all music playback on the built-in speaker and any pin outputting
126+
sound. An optional argument can be provided to specify a pin, eg.
127+
``music.stop(pin1)``.
128128

129129
.. py:function:: reset()
130130

docs/pin.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ in MicroPython, but that are not available via the edge connector:
8181
* ``pin_logo`` - A touch sensitive logo pin on the front of the micro:bit,
8282
which by default is set to capacitive touch mode.
8383

84-
* ``pin_speaker`` - A pin to control the sound output of the micro:bit speaker.
84+
* ``pin_speaker`` - A pin to address the micro:bit speaker.
8585

8686

8787
Pulse-Width Modulation

docs/speaker.png

37.1 KB
Loading

docs/speaker.rst

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Speaker **V2**
2+
**************
3+
4+
.. py:module:: microbit.speaker
5+
6+
The micro:bit **V2** has a built-in speaker located on the rear of the board.
7+
8+
.. image:: speaker.png
9+
:width: 300px
10+
:align: center
11+
:height: 240px
12+
:alt: speaker on rear of micro:bit V2
13+
14+
By default sound output will be via the edge connector on pin 0 and the
15+
built-in speaker **V2**. You can connect wired headphones or a speaker to
16+
pin 0 and GND on the edge connector to hear the sounds.
17+
18+
The speaker can be turned off or on using the functions listed here.
19+
20+
Functions
21+
=========
22+
23+
.. py:function:: off()
24+
25+
Use off() to turn off the speaker. This does not disable sound output, so
26+
you will still hear sound on Pin 0 if you have headphones or a speaker
27+
connected to that pin.
28+
29+
.. py:function:: on()
30+
31+
Use ``on()`` to turn on the speaker.
32+
33+
.. py:function:: is_on()
34+
35+
Returns ``True`` if the speaker is on, otherwise returns ``False``.
36+
37+
Example
38+
=======
39+
40+
An example that runs through some of the functions of the speaker API. ::
41+
42+
from microbit import *
43+
import audio
44+
45+
# Check that the speaker is on
46+
print(speaker.is_on())
47+
# Play a sound
48+
audio.play(Sound.HELLO)
49+
sleep(2000)
50+
# Disable the speaker
51+
speaker.off()
52+
# Check that the speaker is off
53+
print(speaker.is_on())
54+
# Play a sound. This will not be heard on the speaker, but will be heard
55+
# on Pin 0 if you have headphones or a speaker connected.
56+
audio.play(Sound.HELLO)

docs/speech.rst

+8-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Speech
55
66
This module makes the micro:bit talk, sing and make other speech like sounds.
77
By default sound output will be via the edge connector on pin 0 and the
8-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
9-
pin 0 and GND on the edge connector to hear the sound:
8+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
9+
a speaker to pin 0 and GND on the edge connector to hear the sound:
1010

1111
.. image:: speech.png
1212

@@ -46,32 +46,27 @@ Functions
4646
emphasis.
4747

4848
.. py:function:: pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128)
49-
pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
49+
pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
5050
5151
Pronounce the phonemes in the string ``phonemes``. See below for details of
5252
how to use phonemes to finely control the output of the speech synthesiser.
5353
Override the optional pitch, speed, mouth and throat settings to change the
5454
timbre (quality) of the voice.
5555

56-
As with the music module, you can use an optional argument to specify the
57-
output pin can be used to override the default of ``microbit.pin0``.
58-
If you have the latest micro:bit **V2**, you can use
59-
``microbit.pin_speaker``.
60-
61-
The pin argument can also take a tuple of two pins, for example
62-
``pin=(pin_speaker, pin0)`` which would output sound on the built-in
63-
speaker and pin 0.
56+
For micro:bit **V2** an optional argument to specify the output pin can be
57+
used to override the default of ``pin0``. If we do not want any sound to
58+
play out of the pins can use ``pin=None``.
6459

6560
.. py:function:: say(words, \*, pitch=64, speed=72, mouth=128, throat=128)
66-
say(words, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
61+
say(words, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
6762
6863
Say the English words in the string ``words``. The result is semi-accurate
6964
for English. Override the optional pitch, speed, mouth and throat
7065
settings to change the timbre (quality) of the voice. This is a short-hand
7166
equivalent of: ``speech.pronounce(speech.translate(words))``
7267

7368
.. py:function:: sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128)
74-
sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
69+
sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
7570
7671
Sing the phonemes contained in the string ``phonemes``. Changing the pitch
7772
and duration of the note is described below. Override the optional pitch,

0 commit comments

Comments
 (0)