Last active
December 29, 2017 04:02
-
-
Save cversek/a8a13527d1c635d7c081b587ef973784 to your computer and use it in GitHub Desktop.
Start of a Morse Code lab... I basically smashed two Adafruit guide demos together. Transition state machine approach now calls sample.play and sample.stop at the right times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from digitalio import DigitalInOut, Direction, Pull | |
import audioio | |
import board | |
import array | |
import time | |
import math | |
FREQUENCY = 440 # 440 Hz middle 'A' | |
SAMPLERATE = 8000 # 8000 samples/second, recommended! | |
# Generate one period of sine wav. | |
length = SAMPLERATE // FREQUENCY | |
sine_wave = array.array("H", [0] * length) | |
for i in range(length): | |
sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) | |
# enable the speaker | |
spkrenable = DigitalInOut(board.SPEAKER_ENABLE) | |
spkrenable.direction = Direction.OUTPUT | |
spkrenable.value = True | |
sample = audioio.AudioOut(board.SPEAKER, sine_wave) | |
sample.frequency = SAMPLERATE | |
led = DigitalInOut(board.D13) | |
led.direction = Direction.OUTPUT | |
button = DigitalInOut(board.BUTTON_A) | |
button.direction = Direction.INPUT | |
button.pull = Pull.DOWN | |
output_state = False | |
while True: | |
if output_state == True: | |
if button.value == False: # button is pushed | |
led.value = False | |
sample.stop() # we tell the board to stop | |
output_state = False | |
else: | |
if button.value == True: | |
led.value = True | |
sample.play(loop=True) # keep playing the sample over and over | |
output_state = True | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment