Skip to content

Commit 6667bb6

Browse files
author
brentru
committed
linted and added example
1 parent 3db2fc6 commit 6667bb6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

examples/tone_demo.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
'tone_demo.py'.
3+
4+
=================================================
5+
a short piezo song using tone()
6+
"""
7+
import time
8+
import board
9+
import simpleio
10+
11+
12+
while True:
13+
for f in (262, 294, 330, 349, 392, 440, 494, 523):
14+
simpleio.tone(board.A0, f, 100, 0.25)
15+
time.sleep(1)

simpleio.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import digitalio
3535
import pulseio
3636

37-
def tone(pin, frequency, duration=1):
37+
def tone(pin, frequency, duration=1, length=100):
3838
"""
3939
Generates a square wave of the specified frequency on a pin
4040
@@ -43,16 +43,15 @@ def tone(pin, frequency, duration=1):
4343
:param int length: Variable size buffer (optional)
4444
:param int duration: Duration of tone in seconds (optional)
4545
"""
46-
def tone(pin, frequency, length = 100, duration = 1):
4746
try:
4847
sample_length = length
49-
s = array.array("H", [0] * sample_length)
48+
square_wave = array.array("H", [0] * sample_length)
5049
for i in range(sample_length / 2):
51-
s[i] = 0xFFFF
52-
sample_tone = audioio.AudioOut(pin, s)
53-
sample_tone.frequency = int(len(s) * frequency)
50+
square_wave[i] = 0xFFFF
51+
sample_tone = audioio.AudioOut(pin, square_wave)
52+
sample_tone.frequency = int(len(square_wave) * frequency)
5453
if not sample_tone.playing:
55-
sample_tone.play(loop = True)
54+
sample_tone.play(loop=True)
5655
time.sleep(duration)
5756
sample_tone.stop()
5857
except(NameError, ValueError):

0 commit comments

Comments
 (0)