#!/usr/bin/python3
# ====================================================================
# convert a string to Morse code
# ====================================================================
import time
import sys
mcode = { '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':'--..',
'1':'.----', '2':'..---', '3':'...--', '4':'....-',
'5':'.....', '6':'-....', '7':'--...', '8':'---..',
'9':'----.', '0':'-----',
'.':'.-.-.-', ',':'--..--', '?':'..--..', '!':'-.-.--',
':':'---...', ';':'-.-.-.', '/':'-..-.', '=':'-...-',
'+':'.-.-.', '-':'-....-' }
time_unit = 0.001 # milliseconds
dit_time = time_unit
dah_time = 3.0 * time_unit
dit_dah_gap = time_unit
between_letters = 3.0 * time_unit
between_words = 7.0 * time_unit
# --------------------------------------------------------------------
# ---- dit
# --------------------------------------------------------------------
def dit():
# LED on
time.sleep(dit_time)
# LED 0ff
# --------------------------------------------------------------------
# ---- dah
# --------------------------------------------------------------------
def dah():
# LED on
time.sleep(dah_time)
# LED 0ff
# --------------------------------------------------------------------
# ---- convert a string to Morse code
# --------------------------------------------------------------------
def convert_string_to_morse_code(s):
for c in s:
c = c.lower()
# ---- space/blank character?
if c == ' ':
print('word break')
time.sleep(between_words)
continue
# ---- output dit/dah
if c not in mcode:
print()
print(f'unknown character ({c}) in ({s})')
return False
print(f'{c} = ',end='')
for dd in mcode[c]:
print(dd,end='')
if dd == '.':
dit()
time.sleep(dit_dah_gap)
continue
if dd == '-':
dah()
time.sleep(dit_dah_gap)
continue
print()
time.sleep(between_letters)
return True
# --------------------------------------------------------------------
# ---- validate dit/dah string
# ---- (this way you only need to do this one time at program start)
# --------------------------------------------------------------------
def validate_dit_dah():
for k,v in mcode.items():
for dd in v:
if dd != '.' and dd != '-':
print(f'Invalid char ({k}) in dit/dah string ({v})')
return False
return True
# --------------------------------------------------------------------
# ---- main
# --------------------------------------------------------------------
if __name__ == '__main__':
if not validate_dit_dah():
print()
sys.exit()
# ---- test 1
print('---- test 1 ----')
s = 'abc def'
print(f'convert string ({s}) to Morse code')
if not convert_string_to_morse_code(s):
print()
print(f'Morse code conversion error - ({s})')
# ---- test 2
print('---- test 2 ----')
s = 'Hello World! 4 + 5 = 9'
print(f'convert string ({s}) to Morse code')
if not convert_string_to_morse_code(s):
print()
print(f'Morse code conversion error - ({s})')