from microbit import * # Morse Code Text Sender. G3RSP December 2016 # Version 1 # Key out on pin 1 # Speed volts on pin 2 # textcounter = message string index # char = ASCII character to be sent # mc = morse "code" integer string # elcounter = mc index lookup = { "a": [1, 0, 1, 1, 1, 0], "b": [1, 1, 1, 0, 1, 0, 1, 0, 1, 0], "c": [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0], "d": [1, 1, 1, 0, 1, 0, 1, 0], "e": [1, 0], "f": [1, 0, 1, 0, 1, 1, 1, 0, 1, 0], "g": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0], "h": [1, 0, 1, 0, 1, 0, 1, 0], "i": [1, 0, 1, 0], "j": [1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "k": [1, 1, 1, 0, 1, 0, 1, 1, 1, 0], "l": [1, 0, 1, 1, 1, 0, 1, 0, 1, 0], "m": [1, 1, 1, 0, 1, 1, 1, 0], "n": [1, 1, 1, 0, 1, 0], "o": [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "p": [1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0], "q": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0], "r": [1, 0, 1, 1, 1, 0, 1, 0], "s": [1, 0, 1, 0, 1, 0], "t": [1, 1, 1, 0], "u": [1, 0, 1, 0, 1, 1, 1, 0], "v": [1, 0, 1, 0, 1, 0, 1, 1, 1, 0], "w": [1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "x": [1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0], "y": [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "z": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0], "1": [1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "2": [1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "3": [1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "4": [1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0], "5": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], "6": [1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], "7": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0], "8": [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0], "9": [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0], "0": [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "/": [1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0], ".": [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0], ",": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], "?": [1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0], " ": [0, 0], "(": [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0], ")": [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0], "=": [1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0] } def decode(buffer): return lookup.get(buffer) def send_text(): textcounter = 0 while textcounter < len(text): char = text[textcounter] buffer = char mc = decode(buffer) elcounter = 0 speed = pin2.read_analog() speed = speed/4 textcounter = textcounter + 1 sleep(speed * 2) while elcounter < len(mc): pin1.write_digital(mc[elcounter]) sleep(speed) elcounter = elcounter + 1 msg1 = "abcdefghijklmnopqrstuvwxyz1234567890/.,?(=)" msg2 = "cq the quick brown fox jumped over the lazy dogs" while True: if button_a.is_pressed(): text = msg1 send_text() elif button_b.is_pressed(): text = msg2 send_text()