Skip to content

Commit 9205f32

Browse files
committed
update i2c
1 parent 2e5b2c8 commit 9205f32

File tree

8 files changed

+163
-114
lines changed

8 files changed

+163
-114
lines changed

docs/audio.rst

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ Functions
2121
2222
Play the source to completion.
2323

24-
``source`` is an iterable, each element of which must be an ``AudioFrame``.
24+
``source`` is an iterable, each element of which must be an ``AudioFrame``
25+
or built-in ``Sound``.
2526

2627
If ``wait`` is ``True``, this function will block until the source is exhausted.
2728

28-
``pin`` specifies which pin the speaker is connected to.
29-
3029
As with the music module, you can use the optional ``pin`` argument to specify the
3130
output pin can be used to override the default of ``microbit.pin0``.
3231
If you have the latest micro:bit, you can use ``microbit.pin_speaker``
@@ -41,6 +40,15 @@ Functions
4140
``return_pin`` specifies a differential edge connector pin to connect
4241
to an exteernal speaker instead of ground.
4342

43+
.. py:function:: is_playing()
44+
45+
Return ``True`` if audio is playing, otherwise
46+
return ``False``.
47+
48+
.. py:function:: stop()
49+
50+
Stops all audio playback.
51+
4452
Classes
4553
=======
4654

@@ -55,9 +63,23 @@ Classes
5563
Using audio
5664
===========
5765

58-
You will need a sound source, as input to the ``play`` function. You can generate your own, like in
59-
``examples/waveforms.py``.
66+
You will need a sound source, as input to the ``play`` function. You can use
67+
the built-in sounds **V2** or generate your own, like in ``examples/waveforms.py``.
68+
69+
Built-in sounds **V2**
70+
---------------
71+
6072

73+
* ``Sound.GIGGLE``
74+
* ``Sound.HAPPY``
75+
* ``Sound.HELLO``
76+
* ``Sound.MYSTERIOUS``
77+
* ``Sound.SAD``
78+
* ``Sound.SLIDE``
79+
* ``Sound.SOARING``
80+
* ``Sound.SPRING``
81+
* ``Sound.TWINKLE``
82+
* ``Sound.YAWN``
6183

6284
Technical Details
6385
=================

docs/i2c.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ 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 <https://tech.microbit.org/hardware/i2c/>`_
35+
for the motion sensr and the edge connector.
36+
3337

3438

3539
.. py:function:: scan()

docs/microbit_micropython_api.rst

Lines changed: 28 additions & 5 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 heaphones 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

@@ -76,20 +79,20 @@ Microphone **V2**
7679
This Microphone is accessed via the `microphone` object::
7780

7881
# Value to represent loud sound events, like clapping or shouting.
79-
LOUD = "loud"
82+
SoundEvent.LOUD = "loud"
8083
# Value to represent quiet sound events, like speaking or background music.
81-
QUIET = "quiet"
84+
SoundEvent.QUIET = "quiet"
8285
# The name of the last recorded sound event, `loud` or `quiet`.
8386
current_sound()
84-
# A sound event, such as `microphone.LOUD` or `microphone.QUIET`.
87+
# A sound event, such as `SoundEvent.LOUD` or `SoundEvent.QUIET`.
8588
# Returns`true` if sound was heard at least once since the last
8689
# call, otherwise `false`.
8790
was_sound(event)
8891
# A tuple of the event history. The most recent is listed last.
8992
# Also clears the sound event history before returning.
9093
get_sounds()
9194
# The threshold level in the range 0-255. For example,
92-
# `set_threshold(microphone.LOUD, 250)` will only trigger if the
95+
# `set_threshold(SoundEvent.LOUD, 250)` will only trigger if the
9396
# sound is very loud (>= 250).
9497
set_threshold()
9598
# A representation of the sound pressure level in the range 0 to 255.
@@ -315,6 +318,26 @@ There is an I2C bus on the micro:bit that is exposed via the `i2c` object. It h
315318
# write buf to device with addr; repeat=True means a stop bit won't be sent.
316319
i2c.write(addr, buf, repeat=False)
317320

321+
Sounds **V2**
322+
------
323+
324+
A set of expressive sounds are available to the micro:bit **V2** and can be
325+
accessed via the ``audio`` module.
326+
327+
# built-in sounds
328+
329+
Sound.GIGGLE
330+
Sound.HAPPY
331+
Sound.HELLO
332+
Sound.MYSTERIOUS
333+
Sound.SAD
334+
Sound.SLIDE
335+
Sound.SOARING
336+
Sound.SPRING
337+
Sound.TWINKLE
338+
Sound.YAWN
339+
340+
318341
UART
319342
----
320343

@@ -334,4 +357,4 @@ Use ``uart`` to communicate with a serial device connected to the device's I/O p
334357
# read bytes into the referenced buffer.
335358
uart.readinto(buffer)
336359
# write bytes from the buffer to the connected device.
337-
uart.write(buffer)
360+
uart.write(buffer)

docs/microphone.png

200 KB
Loading

docs/microphone.rst

Lines changed: 51 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ is located on the front of the board alongside a microphone activity LED,
99
which is lit when the microphone is in use.
1010

1111
.. image:: microphone.png
12-
:width: 200px
12+
:width: 300px
1313
:align: center
14-
:height: 200px
14+
:height: 240px
1515
:alt: micro:bit with microphone LED on
1616

1717
Sound events
@@ -28,103 +28,56 @@ Classes
2828
.. py:class:: MicrobitMicrophone
2929
3030
.. note::
31-
The `MicrobitMicroPhone` class documented here can be accessed via the microbit module as
32-
`microbit.microphone`. It cannot be initialised on it's own.
33-
34-
Represents the microphone
35-
36-
#: Value to represent loud sound events, like clapping or shouting
37-
LOUD = "loud"
38-
#: Value to represent quiet sound events, like speaking or background music
39-
QUIET = "quiet"
40-
41-
def current_sound(self) -> string:
42-
"""
43-
:return: The name of the last recorded sound event, `loud` or `quiet`.
44-
"""
45-
pass
46-
47-
def was_sound(self, sound: string) -> bool:
48-
"""
49-
:param sound: A sound event, such as `microphone.LOUD` or
50-
`microphone.QUIET`.
51-
:return: `true` if sound was heard at least once since the last
52-
call, otherwise `false`.
53-
"""
54-
pass
55-
56-
def get_sounds(self):
57-
"""
58-
:return: A tuple of the event history. The most recent is listed last.
59-
Also clears the sound event history before returning
60-
"""
61-
pass
62-
63-
def set_threshold(self, sound: string, level: int) -> None:
64-
"""
65-
:param sound: A sound event, such as `microphone.LOUD` or
66-
`microphone.QUIET`.
67-
:param value: The threshold level in the range 0-255. For example,
68-
`set_threshold(microphone.LOUD, 250)` will only trigger if the
69-
sound is very loud (>= 250).
70-
"""
71-
pass
72-
73-
def sound_level(self) -> int:
74-
"""
75-
:return: A representation of the sound pressure level in the range 0 to
76-
20000.
77-
"""
78-
pass
31+
The ``MicrobitMicroPhone`` class documented here can be accessed via
32+
the microbit module as ``microbit.microphone``. It cannot be
33+
initialised on it's own.
34+
35+
Represents the microphone.
36+
37+
.. py:class:: SoundEvent
38+
39+
Value to represent loud sound events, like clapping or shouting
40+
``SoundEvent.LOUD`` = ``loud``.
41+
42+
Value to represent quiet sound events, like speaking or background music
43+
``SoundEvent.QUIET`` = ``quiet``.
44+
45+
.. py:function:: current_event()
46+
47+
Returns the name of the last recorded sound event, ``loud`` or ``quiet``.
48+
49+
.. py:function:: was_event(SoundEvent.LOUD)
50+
51+
Parameter: A sound event, such as ``SoundEvent.LOUD`` or ``SoundEvent.QUIET``.
52+
53+
Returns ``true`` if sound was heard at least once since the last call,
54+
otherwise ``false``.
55+
56+
.. py:function:: get_events():
57+
58+
Returns a tuple of the event history. The most recent is listed last.
59+
60+
Also clears the sound event history before returning.
61+
62+
.. py:function:: set_threshold()
63+
64+
Parameter: A sound event, such as ``SoundEvent.LOUD`` or ``SoundEvent.QUIET``.
65+
66+
Parameter: The threshold level in the range 0-255.
67+
68+
For example, ``set_threshold(SoundEvent.LOUD, 250)`` will only trigger if
69+
the sound is very loud (>= 250).
70+
71+
.. py:function:: sound_level()
72+
73+
Returns a representation of the sound pressure level in the range 0 to
74+
255.
75+
7976

8077
Example
8178
=======
8279

83-
An example that runs through all the functions of the microphone API
84-
85-
.. code::
86-
87-
# Basic test for microphone. This test should update the display when Button A is pressed and a loud or quiet sound *is* heard,
88-
# printing the results. On Button B This test should update the display when a loud or quiet sound *was* heard,
89-
# printing the results. On shake this should print the last sounds heard, you shoul try this test whilst
90-
# making a loud sound and a quiet one before you shake.
91-
92-
from microbit import *
93-
94-
icons = {
95-
microphone.LOUD: Image.SQUARE,
96-
microphone.QUIET: Image.SQUARE_SMALL
97-
}
98-
99-
display.clear()
100-
sound = microphone.current_sound()
101-
102-
while True:
103-
if button_a.is_pressed():
104-
if microphone.current_sound() == microphone.LOUD:
105-
display.show(Image.SQUARE)
106-
uart.write('isLoud\n')
107-
elif microphone.current_sound() == microphone.QUIET:
108-
display.show(Image.SQUARE_SMALL)
109-
uart.write('isQuiet\n')
110-
sleep(500)
111-
display.clear()
112-
if button_b.is_pressed():
113-
if microphone.was_sound(microphone.LOUD):
114-
display.show(Image.SQUARE)
115-
uart.write('wasLoud\n')
116-
elif microphone.was_sound(microphone.QUIET):
117-
display.show(Image.SQUARE_SMALL)
118-
uart.write('wasQuiet\n')
119-
else:
120-
display.clear()
121-
sleep(500)
122-
display.clear()
123-
if accelerometer.was_gesture('shake'):
124-
sounds = microphone.get_sounds()
125-
soundLevel = microphone.sound_level()
126-
print(soundLevel)
127-
for sound in sounds:
128-
display.show(icons[sound])
129-
print(sound)
130-
sleep(500)
80+
An example that runs through some of the functions of the microphone API
81+
82+
.. include:: ../examples/microphone.py
83+
:code: python

docs/music.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Functions
9595

9696
An optional argument to specify the output pin can be used to override the
9797
default of ``microbit.pin0``. If you have the latest micro:bit, you can
98-
use ``microbit.pin_speaker``**V2**.
98+
use ``microbit.pin_speaker`` **V2**.
9999

100100
.. note::
101101
Using this argument will disable the default functionality on the

docs/pin.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ depending upon the digital reading on pin 0::
2424
Pin Functions
2525
=============
2626

27-
`Edge connector and pinout <https://tech.microbit.org/hardware/edgeconnector/#edge-connector-pins)>`_
27+
`Edge connector and pinout <https://tech.microbit.org/hardware/edgeconnector/#edge-connector-pins>`_
2828

2929
Those pins are available as attributes on the ``microbit``
3030
module:``microbit.pin0`` - ``microbit.pin20``.
@@ -80,6 +80,7 @@ in MicroPython, but that are not available via the edge connector:
8080

8181
- ``pin_logo`` - A touch sensitive logo pin on the front of the micro:bit. which by
8282
default is set to capacative touch mode.
83+
8384
- ``pin_speaker`` - A pin to control the sound output of the micro:bit speaker.
8485

8586

@@ -209,7 +210,7 @@ its own to that.
209210

210211
.. note::
211212
The default touch mode for the pins on the edge connector is
212-
`resistive`. The default for the logo pin **V2** is capacative.
213+
`resistive`. The default for the logo pin **V2** is `capacative`.
213214

214215
**Resitive touch**
215216
This test is done by measuring how much resistance there is between the
@@ -219,7 +220,7 @@ its own to that.
219220

220221
**Capacative touch**
221222
This test is done by interacting with the electric field of a capacitor
222-
using the a finger as a conductor.`Capacative touch
223+
using the a finger as a conductor.`Capacative touch
223224
<https://www.allaboutcircuits.com/technical-articles/introduction-to-capacitive-touch-sensing/>`_
224225
does not require you to make a ground connection as part of a circuit.
225226

examples/microphone.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Basic test for microphone. This test should update the display when
2+
# Button A is pressed and a loud or quiet sound *is* heard, printing the
3+
# results. On Button B this test should update the display when a loud or
4+
# quiet sound *was* heard, printing the results. On shake this should print
5+
# the last sounds heard, you shoul try this test whilst making a loud sound
6+
# and a quiet one before you shake.
7+
8+
from microbit import *
9+
10+
icons = {
11+
SoundEvent.LOUD: Image.SQUARE,
12+
SoundEvent.QUIET: Image.SQUARE_SMALL
13+
}
14+
15+
display.clear()
16+
sound = microphone.current_event()
17+
18+
while True:
19+
if button_a.is_pressed():
20+
if microphone.current_event() == SoundEvent.LOUD:
21+
display.show(Image.SQUARE)
22+
uart.write('isLoud\n')
23+
elif microphone.current_event() == SoundEvent.QUIET:
24+
display.show(Image.SQUARE_SMALL)
25+
uart.write('isQuiet\n')
26+
sleep(500)
27+
display.clear()
28+
if button_b.is_pressed():
29+
if microphone.was_event(SoundEvent.LOUD):
30+
display.show(Image.SQUARE)
31+
uart.write('wasLoud\n')
32+
elif microphone.was_event(SoundEvent.QUIET):
33+
display.show(Image.SQUARE_SMALL)
34+
uart.write('wasQuiet\n')
35+
else:
36+
display.clear()
37+
sleep(500)
38+
display.clear()
39+
if accelerometer.was_gesture('shake'):
40+
sounds = microphone.get_events()
41+
soundLevel = microphone.sound_level()
42+
print(soundLevel)
43+
for sound in sounds:
44+
display.show(icons[sound])
45+
print(sound)
46+
sleep(500)

0 commit comments

Comments
 (0)