From 3db2fc63fa25f07e25141c23ee91085f27d5ecd7 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 12:21:49 -0500 Subject: [PATCH 1/7] Changes to the tone function: - uses a fixed length square wave as default (can be variable) like in the mega demo - frequency is now a float instead of an int --- simpleio.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/simpleio.py b/simpleio.py index a79daf4..25056d3 100644 --- a/simpleio.py +++ b/simpleio.py @@ -36,27 +36,27 @@ def tone(pin, frequency, duration=1): """ - Generates a square wave of the specified frequency (50% duty cycle) - on a pin + Generates a square wave of the specified frequency on a pin :param ~microcontroller.Pin Pin: Pin on which to output the tone - :param int frequency: Frequency of tone in Hz + :param float frequency: Frequency of tone in Hz + :param int length: Variable size buffer (optional) :param int duration: Duration of tone in seconds (optional) """ +def tone(pin, frequency, length = 100, duration = 1): try: - length = 4000 // frequency - square_wave = array.array("H", [0] * length) - for i in range(length): - if i < length / 2: - square_wave.append(0xFFFF) - else: - square_wave.append(0x00) - with audioio.AudioOut(pin, square_wave) as waveform: - waveform.play(loop=True) + sample_length = length + s = array.array("H", [0] * sample_length) + for i in range(sample_length / 2): + s[i] = 0xFFFF + sample_tone = audioio.AudioOut(pin, s) + sample_tone.frequency = int(len(s) * frequency) + if not sample_tone.playing: + sample_tone.play(loop = True) time.sleep(duration) - waveform.stop() - except (NameError, ValueError): - with pulseio.PWMOut(pin, frequency=frequency, variable_frequency=False) as pwm: + sample_tone.stop() + except(NameError, ValueError): + with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm: pwm.duty_cycle = 0x8000 time.sleep(duration) From 6667bb66dfba0960c56dfd8c01b59a33331a6d1b Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 13:29:59 -0500 Subject: [PATCH 2/7] linted and added example --- examples/tone_demo.py | 15 +++++++++++++++ simpleio.py | 13 ++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 examples/tone_demo.py diff --git a/examples/tone_demo.py b/examples/tone_demo.py new file mode 100644 index 0000000..f361095 --- /dev/null +++ b/examples/tone_demo.py @@ -0,0 +1,15 @@ +""" +'tone_demo.py'. + +================================================= +a short piezo song using tone() +""" +import time +import board +import simpleio + + +while True: + for f in (262, 294, 330, 349, 392, 440, 494, 523): + simpleio.tone(board.A0, f, 100, 0.25) + time.sleep(1) diff --git a/simpleio.py b/simpleio.py index 25056d3..d451abe 100644 --- a/simpleio.py +++ b/simpleio.py @@ -34,7 +34,7 @@ import digitalio import pulseio -def tone(pin, frequency, duration=1): +def tone(pin, frequency, duration=1, length=100): """ Generates a square wave of the specified frequency on a pin @@ -43,16 +43,15 @@ def tone(pin, frequency, duration=1): :param int length: Variable size buffer (optional) :param int duration: Duration of tone in seconds (optional) """ -def tone(pin, frequency, length = 100, duration = 1): try: sample_length = length - s = array.array("H", [0] * sample_length) + square_wave = array.array("H", [0] * sample_length) for i in range(sample_length / 2): - s[i] = 0xFFFF - sample_tone = audioio.AudioOut(pin, s) - sample_tone.frequency = int(len(s) * frequency) + square_wave[i] = 0xFFFF + sample_tone = audioio.AudioOut(pin, square_wave) + sample_tone.frequency = int(len(square_wave) * frequency) if not sample_tone.playing: - sample_tone.play(loop = True) + sample_tone.play(loop=True) time.sleep(duration) sample_tone.stop() except(NameError, ValueError): From 36086a9583e09cc15f22d8d8abb3f28b5d6842a3 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 12:21:49 -0500 Subject: [PATCH 3/7] Changes to the tone function: - uses a fixed length square wave as default (can be variable) like in the mega demo - frequency is now a float instead of an int linted and added example --- examples/tone_demo.py | 15 +++++++++++++++ simpleio.py | 31 +++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 examples/tone_demo.py diff --git a/examples/tone_demo.py b/examples/tone_demo.py new file mode 100644 index 0000000..f361095 --- /dev/null +++ b/examples/tone_demo.py @@ -0,0 +1,15 @@ +""" +'tone_demo.py'. + +================================================= +a short piezo song using tone() +""" +import time +import board +import simpleio + + +while True: + for f in (262, 294, 330, 349, 392, 440, 494, 523): + simpleio.tone(board.A0, f, 100, 0.25) + time.sleep(1) diff --git a/simpleio.py b/simpleio.py index a79daf4..d451abe 100644 --- a/simpleio.py +++ b/simpleio.py @@ -34,29 +34,28 @@ import digitalio import pulseio -def tone(pin, frequency, duration=1): +def tone(pin, frequency, duration=1, length=100): """ - Generates a square wave of the specified frequency (50% duty cycle) - on a pin + Generates a square wave of the specified frequency on a pin :param ~microcontroller.Pin Pin: Pin on which to output the tone - :param int frequency: Frequency of tone in Hz + :param float frequency: Frequency of tone in Hz + :param int length: Variable size buffer (optional) :param int duration: Duration of tone in seconds (optional) """ try: - length = 4000 // frequency - square_wave = array.array("H", [0] * length) - for i in range(length): - if i < length / 2: - square_wave.append(0xFFFF) - else: - square_wave.append(0x00) - with audioio.AudioOut(pin, square_wave) as waveform: - waveform.play(loop=True) + sample_length = length + square_wave = array.array("H", [0] * sample_length) + for i in range(sample_length / 2): + square_wave[i] = 0xFFFF + sample_tone = audioio.AudioOut(pin, square_wave) + sample_tone.frequency = int(len(square_wave) * frequency) + if not sample_tone.playing: + sample_tone.play(loop=True) time.sleep(duration) - waveform.stop() - except (NameError, ValueError): - with pulseio.PWMOut(pin, frequency=frequency, variable_frequency=False) as pwm: + sample_tone.stop() + except(NameError, ValueError): + with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm: pwm.duty_cycle = 0x8000 time.sleep(duration) From 3ee8aa167a75c1196f72458b2c4e949d73b8bc43 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 14:03:34 -0500 Subject: [PATCH 4/7] fixed tone demo --- tone_demo.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tone_demo.py diff --git a/tone_demo.py b/tone_demo.py new file mode 100644 index 0000000..4a947a5 --- /dev/null +++ b/tone_demo.py @@ -0,0 +1,15 @@ +""" +'tone_demo.py'. + +================================================= +a short piezo song using tone() +""" +import time +import board +import simpleio + + +while True: + for f in (262, 294, 330, 349, 392, 440, 494, 523): + simpleio.tone(board.A0, f) + time.sleep(1) From b2569e61091beb8f0ca143a261433dd9be4cc8c6 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 14:19:45 -0500 Subject: [PATCH 5/7] demo moved --- examples/tone_demo.py | 2 +- tone_demo.py | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) delete mode 100644 tone_demo.py diff --git a/examples/tone_demo.py b/examples/tone_demo.py index f361095..60f80be 100644 --- a/examples/tone_demo.py +++ b/examples/tone_demo.py @@ -11,5 +11,5 @@ while True: for f in (262, 294, 330, 349, 392, 440, 494, 523): - simpleio.tone(board.A0, f, 100, 0.25) + simpleio.tone(board.A0, f, 0.25) time.sleep(1) diff --git a/tone_demo.py b/tone_demo.py deleted file mode 100644 index 4a947a5..0000000 --- a/tone_demo.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -'tone_demo.py'. - -================================================= -a short piezo song using tone() -""" -import time -import board -import simpleio - - -while True: - for f in (262, 294, 330, 349, 392, 440, 494, 523): - simpleio.tone(board.A0, f) - time.sleep(1) From edd00babf7009a52b30a6164caf78625a7bbf7ea Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 12:21:49 -0500 Subject: [PATCH 6/7] Changes to the tone function: - uses a fixed length square wave as default (can be variable) like in the mega demo - frequency is now a float instead of an int linted and added example Changes to the tone function: - uses a fixed length square wave as default (can be variable) like in the mega demo - frequency is now a float instead of an int linted and added example fixed tone demo demo moved --- examples/tone_demo.py | 15 +++++++++++++++ simpleio.py | 31 +++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 examples/tone_demo.py diff --git a/examples/tone_demo.py b/examples/tone_demo.py new file mode 100644 index 0000000..60f80be --- /dev/null +++ b/examples/tone_demo.py @@ -0,0 +1,15 @@ +""" +'tone_demo.py'. + +================================================= +a short piezo song using tone() +""" +import time +import board +import simpleio + + +while True: + for f in (262, 294, 330, 349, 392, 440, 494, 523): + simpleio.tone(board.A0, f, 0.25) + time.sleep(1) diff --git a/simpleio.py b/simpleio.py index a79daf4..d451abe 100644 --- a/simpleio.py +++ b/simpleio.py @@ -34,29 +34,28 @@ import digitalio import pulseio -def tone(pin, frequency, duration=1): +def tone(pin, frequency, duration=1, length=100): """ - Generates a square wave of the specified frequency (50% duty cycle) - on a pin + Generates a square wave of the specified frequency on a pin :param ~microcontroller.Pin Pin: Pin on which to output the tone - :param int frequency: Frequency of tone in Hz + :param float frequency: Frequency of tone in Hz + :param int length: Variable size buffer (optional) :param int duration: Duration of tone in seconds (optional) """ try: - length = 4000 // frequency - square_wave = array.array("H", [0] * length) - for i in range(length): - if i < length / 2: - square_wave.append(0xFFFF) - else: - square_wave.append(0x00) - with audioio.AudioOut(pin, square_wave) as waveform: - waveform.play(loop=True) + sample_length = length + square_wave = array.array("H", [0] * sample_length) + for i in range(sample_length / 2): + square_wave[i] = 0xFFFF + sample_tone = audioio.AudioOut(pin, square_wave) + sample_tone.frequency = int(len(square_wave) * frequency) + if not sample_tone.playing: + sample_tone.play(loop=True) time.sleep(duration) - waveform.stop() - except (NameError, ValueError): - with pulseio.PWMOut(pin, frequency=frequency, variable_frequency=False) as pwm: + sample_tone.stop() + except(NameError, ValueError): + with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm: pwm.duty_cycle = 0x8000 time.sleep(duration) From 767e7c552ed367860a2098993a962f58b16cd145 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 5 Jan 2018 14:38:52 -0500 Subject: [PATCH 7/7] saving some memory --- simpleio.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/simpleio.py b/simpleio.py index d451abe..cd9e032 100644 --- a/simpleio.py +++ b/simpleio.py @@ -44,6 +44,10 @@ def tone(pin, frequency, duration=1, length=100): :param int duration: Duration of tone in seconds (optional) """ try: + with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm: + pwm.duty_cycle = 0x8000 + time.sleep(duration) + except ValueError: sample_length = length square_wave = array.array("H", [0] * sample_length) for i in range(sample_length / 2): @@ -54,10 +58,8 @@ def tone(pin, frequency, duration=1, length=100): sample_tone.play(loop=True) time.sleep(duration) sample_tone.stop() - except(NameError, ValueError): - with pulseio.PWMOut(pin, frequency=int(frequency), variable_frequency=False) as pwm: - pwm.duty_cycle = 0x8000 - time.sleep(duration) + + def bitWrite(x, n, b): #pylint: disable-msg=invalid-name """