Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added tone() #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2017
Merged
Changes from all commits
Commits
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
29 changes: 28 additions & 1 deletion simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,39 @@

The `simpleio` module contains classes to provide simple access to IO.
"""

import audioio
import array
import digitalio
import pulseio
import math
import time

def tone(pin, frequency, duration = 1):
"""
Generates a square wave of the specified frequency (50% duty cycle)
on a pin

:param ~microcontroller.Pin Pin: Pin on which to output the tone
:param int frequency: Frequency of tone in Hz
: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)
time.sleep(duration)
waveform.stop()
except ValueError:
with pulseio.PWMOut(pin, frequency = frequency, variable_frequency = False) as pwm:
pwm.duty_cycle = 0x8000
time.sleep(duration)

def shift_in(dataPin, clock, msb_first=True):
"""
Shifts in a byte of data one bit at a time. Starts from either the LSB or
Expand Down