Last active
October 1, 2015 04:29
-
-
Save greatislander/c43ac2901d475a9ab2ae to your computer and use it in GitHub Desktop.
Morse Code for Raspberry Pi + Sense Hat
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
#!/usr/bin/python | |
import time | |
from sense_hat import SenseHat | |
sense = SenseHat() | |
duration = 0.3 # length of the dot duration in seconds | |
on = (255, 255, 255) # on state colour | |
off = (0, 0, 0) # off state colour | |
def dot(): | |
sense.clear(on) | |
time.sleep(duration) | |
sense.clear(off) | |
time.sleep(duration) | |
def dash(): | |
sense.clear(on) | |
time.sleep(duration * 3) | |
sense.clear(off) | |
time.sleep(duration) | |
def space(): | |
time.sleep(duration * (1 + 1 / 3)) | |
code = { | |
'A': '.-', 'B': '-...', 'C': '-.-.', | |
'D': '-..', 'E': '.', 'F': '..-.', | |
'G': '--.', 'H': '....', 'I': '..', | |
'J': '.---', 'K': '-.-', 'L': '.-..', | |
'M': '--', 'N': '-.', 'O': '---', | |
'P': '.--.', 'Q': '--.-', 'R': '.-.', | |
'S': '...', 'T': '-', 'U': '..-', | |
'V': '...-', 'W': '.--', 'X': '-..-', | |
'Y': '-.--', 'Z': '--..', | |
'0': '-----', '1': '.----', '2': '..---', | |
'3': '...--', '4': '....-', '5': '.....', | |
'6': '-....', '7': '--...', '8': '---..', | |
'9': '----.' | |
} | |
msg = raw_input('Message: ') | |
for char in msg: | |
if char.upper() in code: | |
output = code[char.upper()] | |
for char in output: | |
if char == '.': | |
dot() | |
elif char == '-': | |
dash() | |
elif char == ' ': | |
space() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment