Skip to content

Commit

Permalink
Merge pull request #37 from RaspberryPiFoundation/dev
Browse files Browse the repository at this point in the history
push to master to v0.0.1
  • Loading branch information
Martin O'Hanlon authored Mar 21, 2022
2 parents 80281ec + fc965a3 commit b6fd7f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ A beginner-friendly library for using common electronics components with the Ras
else:
led.off()
Full documentation is available at `picozero.readthedocs.io <https://picozero.readthedocs.io>`_ :
Status
------

Pre-alpha - not yet for general release. Documentation is not yet available. There will be many bugs and issues.

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

- `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 structure), but is by design lighter weight and aligned with the Raspberry Pi Pico.
Notes
-----

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. Thank you to everyone who has contributed to the gpiozero project.

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

speaker = Speaker(5)

BEAT = 0.25 # 240 BPM

dance = [[55, BEAT], ['', BEAT / 2], [55, BEAT], ['', BEAT / 2], [55, BEAT * 1.25], [55, BEAT], ['', BEAT / 2], [55, BEAT],
[57, BEAT], ['', BEAT / 2], [57, BEAT], ['', BEAT / 2], [54, BEAT * 1.25], [54, BEAT], ['', BEAT / 2], [55, BEAT],
[55, BEAT], [79, BEAT / 2], [55, BEAT], [79, BEAT / 2], [55, BEAT * 1.25], [55, BEAT], ['', BEAT / 2], [55, BEAT],
[57, BEAT], [75, BEAT / 2], [57, BEAT], [75, BEAT / 2], [72, BEAT * 1.25], [56, BEAT], ['', BEAT / 2], [56, BEAT]]

try:
speaker.play(dance)

finally: # Turn speaker off if interrupted
speaker.off()
24 changes: 15 additions & 9 deletions picozero/picozero.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def volume(self):
@volume.setter
def volume(self, value):
self._volume = value
self.value = (self.freq, self._volume)
self.value = (self.freq, self.volume)

@property
def freq(self):
Expand All @@ -622,14 +622,16 @@ def freq(self):

@freq.setter
def freq(self, freq):
self._pwm_buzzer.freq(freq)

self.value = (freq, self.volume)
def _write(self, value):
# set the frequency
self._pwm_buzzer.freq = value[0]
if value[0] is not None:
self._pwm_buzzer.freq = value[0]

# write the volume value
self._pwm_buzzer.volume = value[1]
if value[1] is not None:
self._pwm_buzzer.volume = value[1]

def _to_freq(self, freq):
if freq is not None and freq != '' and freq != 0:
Expand Down Expand Up @@ -688,7 +690,7 @@ def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
+ a frequency in Hz e.g. `440`
+ a midi note e.g. `60`
+ a note name as a string e.g `"E4"`
+ a list of single notes e.g. `[440, 60, "E4"]`
+ a list of note and duration e.g. `[440, 1]` or `["E4", 2]`
+ a list of 2 value tuples of (note, duration) e.g. `[(440,1), (60, 2), ("e4", 3)]`
Defaults to `440`.
Expand All @@ -712,10 +714,13 @@ def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
# tune isnt a list, so it must be a single frequency or note
if not isinstance(tune, (list, tuple)):
tune = [(tune, duration)]
# if the first element isnt a list, then it must be list of a single note and duration
elif not isinstance(tune[0], (list, tuple)):
tune = [tune]

def tune_generator():
for note in tune:

# note isnt a list or tuple, it must be a single frequency or note
if not isinstance(note, (list, tuple)):
# make it into a tuple
Expand All @@ -724,12 +729,13 @@ def tune_generator():
# turn the notes in frequencies
freq = self._to_freq(note[0])
freq_duration = note[1]
freq_volume = volume if freq is not None else 0

# if this is a tune of greater than 1 note, add gaps between notes
if len(tune) == 1:
yield ((freq, volume), freq_duration)
yield ((freq, freq_volume), freq_duration)
else:
yield ((freq, volume), freq_duration * 0.9)
yield ((freq, freq_volume), freq_duration * 0.9)
yield ((freq, 0), freq_duration * 0.1)

self._start_change(tune_generator, n, wait)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

__project__ = 'picozero'
__packages__ = ['picozero']
__desc__ = 'A beginner-friendly library for using common electronics components with the Raspberry Pi Pico.'
__desc__ = 'A beginner-friendly library for using common electronics components with the Raspberry Pi Pico. Not yet for general release.'
__version__ = '0.0.1'
__author__ = "Raspberry Pi Foundation"
__author_email__ = 'learning@raspberrypi.org'
__license__ = 'MIT'
__url__ = 'https://github.com/raspberrypilearning/picozero'
__url__ = 'https://github.com/RaspberryPiFoundation/picozero'
__keywords__ = [
'raspberry',
'pi',
Expand Down

0 comments on commit b6fd7f5

Please sign in to comment.