Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
97af83a
Create speaker_tune.py
tracygardner Mar 1, 2022
5e188df
Create speaker_notes.py
tracygardner Mar 1, 2022
8d2d21e
Create rgb_blink.py
tracygardner Mar 1, 2022
32f3525
Create rgb_pulse.py
tracygardner Mar 1, 2022
5019792
Create rgb_cycle.py
tracygardner Mar 1, 2022
de98932
Updates to docs and examples.
tracygardner Mar 1, 2022
7adfa6d
Merge pull request #16 from RaspberryPiFoundation/master
Mar 2, 2022
84f3255
Update picozero.py
tracygardner Mar 3, 2022
9323e4c
Update speaker.py
tracygardner Mar 3, 2022
ddb1a53
Update speaker_notes.py
tracygardner Mar 3, 2022
7a6ebe4
Update speaker_tune.py
tracygardner Mar 3, 2022
985e0d3
resolve wait issue
Mar 3, 2022
0c2cb91
Update speaker_tune.py
beckyfranks Mar 8, 2022
8d6765e
Update speaker_notes.py
beckyfranks Mar 8, 2022
08ae4f7
Added docstrings for RGBLED
tracygardner Mar 8, 2022
17d93d0
Update picozero.py
tracygardner Mar 10, 2022
263f43e
pwm message
Mar 11, 2022
7b8f03e
update PWMLED.blink parameters
Mar 11, 2022
43d294e
schedule callbacks
Mar 11, 2022
a991bc9
added Button.is_active
Mar 11, 2022
8c9a4e3
fixed docs
Mar 11, 2022
afff194
fixed urls
Mar 11, 2022
ab97e6c
added license
Mar 11, 2022
ea3c326
organised classes by output and input
Mar 11, 2022
1eedd1f
added docstrings
Mar 11, 2022
599f44b
Update picozero.py
tracygardner Mar 14, 2022
85dbd65
refactored PWMBuzzer and Speaker
Mar 14, 2022
9771907
Update picozero.py
tracygardner Mar 14, 2022
ff4c3ba
tune generator
Mar 15, 2022
f99235f
removed debug print
Mar 16, 2022
e4b99be
documentation updates
Mar 16, 2022
0014b8a
Update picozero.py
tracygardner Mar 16, 2022
02e7b08
moved blink and pulse to PWMOutputDevice
Mar 16, 2022
cb0e5c8
updated duty factor
Mar 16, 2022
2932ec4
updated duty factor
Mar 16, 2022
485be47
Merge branch 'speaker' into dev
Mar 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ A beginner-friendly library for using common electronics components with the Ras

Full documentation is available at `picozero.readthedocs.io <https://picozero.readthedocs.io>`_ :

- `Installation and getting started guide <https://picozero.readthedocs.ioen/latest/gettingstarted.html>`_
- `Recipes and how-to's <https://picozero.readthedocs.ioen/latest/recipes.html>`_
- `API <https://picozero.readthedocs.ioen/latest/api.html>`_
- `Installation and getting started guide <https://picozero.readthedocs.io/en/latest/gettingstarted.html>`_
- `Recipes and how-to's <https://picozero.readthedocs.io/en/latest/recipes.html>`_
- `API <https://picozero.readthedocs.io/en/latest/api.html>`_

picozero is inspired by `gpiozero <https://gpiozero.readthedocs.io/en/stable/>`_ (and reuses some of its underlying struture), but is by design lighter weight and aligned with the Raspberry Pi Pico.
picozero is inspired by `gpiozero <https://gpiozero.readthedocs.io/en/stable/>`_ (and reuses some of its underlying structure), but is by design lighter weight and aligned with the Raspberry Pi Pico.

8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ PWMLED
:inherited-members:
:members:

RGBLED
----------

.. autoclass:: RGBLED
:show-inheritance:
:inherited-members:
:members:

Button
------

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __getattr__(cls, name):
return Mock()

sys.modules['machine'] = Mock()
sys.modules['micropython'] = Mock()

# add the ticks_ms function to time (as it is in micropython)
import time
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/button_function.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from picozero import Button, pico_led
from time import sleep

button = Button(17)
button = Button(18)

def led_on_off():
pico_led.on()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/button_is_pressed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from picozero import Button
from time import sleep

button = Button(17)
button = Button(18)

while True:
if button.is_pressed:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/button_led.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from picozero import Button, pico_led

button = Button(17)
button = Button(18)

button.when_pressed = pico_led.on
button.when_released = pico_led.off
2 changes: 1 addition & 1 deletion docs/examples/buzzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Active Buzzer that plays a not when powered
# Active Buzzer that plays a note when powered
from time import sleep
from picozero import Buzzer

Expand Down
19 changes: 19 additions & 0 deletions docs/examples/rgb_blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from picozero import RGBLED
from time import sleep

rgb = RGBLED(1, 2, 3)

rgb.blink() # does not wait
sleep(6)
rgb.off()
sleep(1)

# blink purple 2 seconds, off 0.5 seconds
rgb.blink(on_times=(2, 0.5), colors=((1, 0, 1), (0, 0, 0)), wait=True, n=3)

rgb.off()
sleep(1)

# blink red 1 second, green 0.5 seconds, blue 0.25 seconds
rgb.blink((1, 0.5, 0.25), colors=((1, 0, 0), (0, 1, 0), (0, 0, 1)), wait=True, n=2)

14 changes: 14 additions & 0 deletions docs/examples/rgb_cycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from picozero import RGBLED
from time import sleep

rgb = RGBLED(1, 2, 3)

# Gradually colour cycle through colours between red and green, green and blue then blue and red
rgb.cycle()
sleep(4)
rgb.off()
sleep(1)

# Colour cycle slower in the opposite direction
rgb.cycle(fade_times=3, colors=((0, 0, 1), (0, 1, 0), (1, 0, 0)), wait=True, n=2)
rgb.off()
18 changes: 18 additions & 0 deletions docs/examples/rgb_pulse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from picozero import RGBLED
from time import sleep

rgb = RGBLED(1, 2, 3)

rgb.pulse() # does not wait
sleep(6)
rgb.off()
sleep(1)

# 2 second to fade from purple to off, 0.5 seconds to change from off to purple
rgb.pulse(fade_times=(2, 0.5), colors=((1, 0, 1), (0, 0, 0)), wait=True, n=3)

rgb.off()
sleep(1)

# 4 seconds to change from red to green, 2 to change from green to blue, then 1 to change from blue back to red
rgb.pulse((4, 2, 1), colors=((1, 0, 0), (0, 1, 0), (0, 0, 1)), wait=True, n=2)
26 changes: 11 additions & 15 deletions docs/examples/speaker.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
from time import sleep
from picozero import Speaker
from time import sleep

speaker = Speaker(10)

speaker.play()

sleep(1)
speaker = Speaker(5)

def tada():
c_note = 523
speaker.play(freq=c_note, duration=0.1)
speaker.play(c_note, 0.1)
sleep(0.1)
speaker.play(c_note, 0.4)
for i in range(100, 0, -1):
speaker.play(freq=c_note, duration=0.01, volume=i/100)

tada()
sleep(1)
speaker.play(c_note, 0.9)

def chirp():
global speaker
Expand All @@ -25,6 +16,11 @@ def chirp():
speaker.play(i, 0.01)
sleep(0.2)

chirp()

speaker.off()
try:
tada()
sleep(1)
chirp()

finally: # Turn the speaker off if interrupted
speaker.off()
24 changes: 24 additions & 0 deletions docs/examples/speaker_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from picozero import Speaker
from time import sleep

speaker = Speaker(5)

BEAT = 0.4

liten_mus = [ ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['d6', BEAT], ['a#5', BEAT], ['d5', BEAT],
['f5', BEAT], ['d#5', BEAT], ['d#5', BEAT], ['c5', BEAT / 2],['d5', BEAT / 2], ['d#5', BEAT],
['c6', BEAT], ['a5', BEAT], ['d5', BEAT], ['g5', BEAT], ['f5', BEAT], ['f5', BEAT], ['d5', BEAT / 2],
['d#5', BEAT / 2], ['f5', BEAT], ['g5', BEAT], ['a5', BEAT], ['a#5', BEAT], ['a5', BEAT], ['g5', BEAT],
['g5', BEAT], ['', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT / 2], ['d6', BEAT / 2], ['c6', BEAT / 2],
['a#5', BEAT / 2], ['a5', BEAT / 2], ['g5', BEAT / 2], ['a5', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT],
['f5', BEAT], ['f5', BEAT], ['f5', BEAT / 2], ['d#5', BEAT / 2], ['d5', BEAT], ['f5', BEAT], ['d6', BEAT],
['d6', BEAT / 2], ['c6', BEAT / 2], ['b5', BEAT], ['g5', BEAT], ['g5', BEAT], ['c6', BEAT / 2],
['a#5', BEAT / 2], ['a5', BEAT], ['f5', BEAT], ['d6', BEAT], ['a5', BEAT], ['a#5', BEAT * 1.5]]

try:
for note in liten_mus:
speaker.play(note)
sleep(0.1) # leave a gap between notes

finally: # Turn speaker off if interrupted
speaker.off()
21 changes: 21 additions & 0 deletions docs/examples/speaker_tune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from picozero import Speaker

speaker = Speaker(5)

BEAT = 0.25 # 240 BPM

liten_mus = [ ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['d6', BEAT], ['a#5', BEAT], ['d5', BEAT],
['f5', BEAT], ['d#5', BEAT], ['d#5', BEAT], ['c5', BEAT / 2],['d5', BEAT / 2], ['d#5', BEAT],
['c6', BEAT], ['a5', BEAT], ['d5', BEAT], ['g5', BEAT], ['f5', BEAT], ['f5', BEAT], ['d5', BEAT / 2],
['d#5', BEAT / 2], ['f5', BEAT], ['g5', BEAT], ['a5', BEAT], ['a#5', BEAT], ['a5', BEAT], ['g5', BEAT],
['g5', BEAT], ['', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT / 2], ['d6', BEAT / 2], ['c6', BEAT / 2],
['a#5', BEAT / 2], ['a5', BEAT / 2], ['g5', BEAT / 2], ['a5', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT],
['f5', BEAT], ['f5', BEAT], ['f5', BEAT / 2], ['d#5', BEAT / 2], ['d5', BEAT], ['f5', BEAT], ['d6', BEAT],
['d6', BEAT / 2], ['c6', BEAT / 2], ['b5', BEAT], ['g5', BEAT], ['g5', BEAT], ['c6', BEAT / 2],
['a#5', BEAT / 2], ['a5', BEAT], ['f5', BEAT], ['d6', BEAT], ['a5', BEAT], ['a#5', BEAT * 1.5]]

try:
speaker.play(liten_mus)

finally: # Turn speaker off if interrupted
speaker.off()
44 changes: 40 additions & 4 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Create a pulse effect:

.. literalinclude:: examples/led_pulse.py


Button
------

Expand All @@ -99,7 +98,7 @@ Run a function every time a :class:`Button` is pressed:

.. note::

Note that the line ``button.when_pressed = led_on_off`` does not run the
The line ``button.when_pressed = led_on_off`` does not run the
function ``led_on_off``, rather it creates a reference to the function to be
called when the button is pressed. Accidental use of ``button.when_pressed
= led_on_off()`` would set the ``when_pressed`` action to :data:`None` (the
Expand All @@ -113,14 +112,37 @@ Turn the :obj:`pico_led` on when a :class:`Button` is pressed and off when it is
RGBLED
------

Setting colours with an :class:`RGBLED`:
Set colours with an :class:`RGBLED`:

.. literalinclude:: examples/rgb_led.py

Using :meth:`~picozero.RGBLED.toggle` and :meth:`~picozero.RGBLED.invert`:
Use :meth:`~picozero.RGBLED.toggle` and :meth:`~picozero.RGBLED.invert`:

.. literalinclude:: examples/rgb_toggle_invert.py

Blink
~~~~~

Use :meth:`~picozero.RGBLED.blink` blink to change between colours. You can control which colours are used and how long the LED is set to each colour. The colour `(0, 0, 0)` represents off.

You can control whether :meth:`~picozero.RGBLED.blink` runs a fixed number of times and whether it waits until it has finished or returns immediately so other code can run.

.. literalinclude:: examples/rgb_blink.py

Pulse
~~~~~

Use :meth:`~picozero.RGBLED.pulse` to gradually change the LED between colours. The default will pulse between red and off, then green and off, then blue and off.

.. literalinclude:: examples/rgb_pulse.py

Cycle
~~~~~

The default for :meth:`~picozero.RGBLED.cycle` is to cycle from red to green, then green to blue, then blue to red.

.. literalinclude:: examples/rgb_cycle.py

Potentiometer
---------------

Expand All @@ -147,6 +169,20 @@ Control a passive buzzer or speaker that can play different tones or frequencies

.. literalinclude:: examples/speaker.py

Play a tune
~~~~~~~~~~~

Play a tune of note names and durations in beats:

.. literalinclude:: examples/speaker_tune.py

Play individual notes
~~~~~~~~~~~~~~~~~~~~~

Play individual notes and control the timing or perform another action:

.. literalinclude:: examples/speaker_notes.py

Internal Temperature Sensor
---------------------------

Expand Down
17 changes: 13 additions & 4 deletions picozero/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
from .picozero import (
PWMChannelAlreadyInUse,

DigitalOutputDevice,
DigitalLED,
Buzzer,
PWMOutputDevice,
PWMLED,
LED,
pico_led,

PWMBuzzer,
Speaker,

RGBLED,

DigitalInputDevice,
Button,
Switch,
RGBLED,
Button,

AnalogInputDevice,
Potentiometer,
Pot,

TemperatureSensor,
pico_temp_sensor,
TempSensor,
Thermistor,
PWMBuzzer,
Speaker,
)
Loading