def record(): """Record and return message.""" print("Start typing your Morse Code message! Wait 5 seconds to exit") pressed_time = 0 delay_time = 0 symbols = "" msg = "" x = 0 while True: if button.value() == 0: # pushed, light LED, play sound, and count how long its pushed led.high() speaker.duty_u16(VOLUME_MAX) delay_time = 0 pressed_time += 1 elif button.value() == 1: # not pushed, accum symbol/letter/message after appropriate pauses speaker.duty_u16(VOLUME_OFF) led.off() # if button was just released, capture symbol if pressed_time > 0: if pressed_time <= 25: symbols += "s" width = 4 else: symbols += "l" width = 8 oled.fill_rect(x, 32, width, 4, 1) oled.show() x += width + 6 pressed_time = 0 delay_time += 1 # break between symbols (find letter) if delay_time == 60 and symbols: letter = lookup_code(symbols) msg += letter print(symbols, ":", letter, "=>", msg) symbols = "" oled.fill_rect(0, 28, 128, 16, 0) x = 0 # break between words (find word) if delay_time == 300: msg += " " if delay_time == 500: print("(Exiting recording mode)") oled.fill_rect(x, 28, 128, 16, 0) led.on() return msg.strip() sleep(0.01)
def play_note(note, length): if note.startswith(">"): length *= 1.5 note = note[1:] speaker.duty_u16(1500) speaker.freq(NOTES[note]) sleep(length) speaker.duty_u16(0) sleep(0.05)
def play_letter(letter, listen=False): """Play single letter, flashing LED and making sound.""" code = LETTER_TO_CODE.get(letter, None) if code is None: # space or some other punctuation sleep(PACE_SEC * 4) return if not listen: print(letter, ":", code) # Draw symbol on display -- first, clear it oled_clear() oled.text(letter.upper(), 0, 29) x = 16 for c in code: width = 4 if c == 's' else 8 oled.fill_rect(x, 32, width, 4, 1) x += width + 6 oled.show() # Play symbol for c in code: pace = PACE_SEC if c == 's' else PACE_SEC * 2 led.high() speaker.duty_u16(VOLUME_MAX) sleep(pace) led.low() speaker.duty_u16(VOLUME_OFF) sleep(PACE_SEC) sleep(PACE_SEC * 3) oled.fill_rect(0, 28, 128, 16, 0)
def start(): mark("music") oled_page("Music!") led.high() for c in TAKE_ON_ME: play_note(c, 0.2) speaker.duty_u16(0) print(""" A-ha! :-) I can hum decently well, can't I? You can learn more at {url} When you'd like to move on: >>> import morse >>> morse.start() """.format(url=url("pwm")))
# In order for this to work, you'll need a light sensor connected, which wasn't # in the kit. from machine import ADC from common import speaker, button, VOLUME_OFF, VOLUME_OFF, oled_page import time import math light_sensor = ADC(1) oled_page("Light theramin", "Expose sensor", "to more/less", "light. Press", "button to stop") speaker.duty_u16(VOLUME_MAX) print(""" Raise and lower your hands over the light sensor or shine a light on it (it's between the Pico and the screen.) To stop, press the black button. """) prev = light_sensor.read_u16() while button.value() == 1: light = light_sensor.read_u16() print(light) if abs(prev - light) > 200: speaker.freq(light // 10) prev = light time.sleep(0.1)