Skip to content

Instantly share code, notes, and snippets.

@kusti8
Created May 21, 2019 01:32
Show Gist options
  • Save kusti8/ea468bc4bebd4550be4a3a18e0dcfcb1 to your computer and use it in GitHub Desktop.
Save kusti8/ea468bc4bebd4550be4a3a18e0dcfcb1 to your computer and use it in GitHub Desktop.
import time
characters = ""
started = False
morseAlphabet = {
"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": "--..",
" ": "....-",
"/": "---...",
"_": ".---."
}
morseAlphabet = dict((v, k) for (k, v) in morseAlphabet.items())
threshold = 200 # Theshold for sensor, greater than means it is active
# less than means it is not active
short_press = 90
long_press = 350
new_char = 1200
reset = 6000
queue = ""
started = False
def s():
global queue
queue += "."
def l():
global queue
queue += "-"
def get_value():
v = ser.readline().strip()
if v.isdigit():
return int(v)
else:
return 0
while True:
value = get_value()
millis = int(round(time.time() * 1000))
while value < threshold: # Not active
value = get_value()
change = int(round(time.time() * 1000)) - millis
if new_char < change:
characters += morseAlphabet[queue]
if characters[-1] == '_':
characters = characters[:-2]
queue = ""
break
if value > threshold: # Active
millis = int(round(time.time() * 1000))
while value > threshold:
value = get_value()
change = int(round(time.time() * 1000)) - millis
if short_press < change < long_press:
s()
elif long_press < change < reset:
l()
elif change > reset:
characters = ""
queue = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment