0% found this document useful (0 votes)
2 views

Encryption Code

This document contains a Python script for generating Morse code signals using an LED. It defines the timing for dots and dashes, a mapping of characters to Morse code, and functions to control the LED based on the input text. The script is set to continuously play the Morse code for the phrase 'Hello World'.

Uploaded by

Anjali Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Encryption Code

This document contains a Python script for generating Morse code signals using an LED. It defines the timing for dots and dashes, a mapping of characters to Morse code, and functions to control the LED based on the input text. The script is set to continuously play the Morse code for the phrase 'Hello World'.

Uploaded by

Anjali Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import time

DEBUG = True

UNIT_LENGTH = 100
DIT_LENGTH = UNIT_LENGTH * 1
DAH_LENGTH = UNIT_LENGTH * 3
ELEMENT_GAP = UNIT_LENGTH * 1
SHORT_GAP = UNIT_LENGTH * 2
MEDIUM_GAP = UNIT_LENGTH * 4

codeset = {
'!': '-.-.--',
'"': '.-..-.',
'#': None,
'$': '...-..-',
'%': None,
'&': '.-...',
"'": '.----.',
'(': '-.--.',
')': '-.--.-',
'*': None,
'+': '.-.-.',
',': '--..--',
'-': '-....-',
'.': '.-.-.-',
'/': '-..-.',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
':': '---...',
';': '-.-.-.',
'<': None,
'=': '-...-',
'>': None,
'?': '..--..',
'@': '.--.-.',
'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': '--..',
'[': None,
'\\': None,
']': None,
'^': None,
'_': '..--.-'
}

def get_code(c):
# To uppercase if needed
if 'a' <= c <= 'z':
c = chr(ord(c) - (ord('a') - ord('A')))

if '!' <= c <= '_':


return codeset[c]

return None

def setup():
import board
import digitalio
import time

board.LED_BUILTIN.direction = digitalio.Direction.OUTPUT
import serial
serial.Serial(baudrate=19200)

def dit():
if DEBUG:
print(".", end="")
board.LED_BUILTIN.value = True
time.sleep(DIT_LENGTH / 1000)
board.LED_BUILTIN.value = False
time.sleep(ELEMENT_GAP / 1000)

def dah():
if DEBUG:
print("-", end="")
board.LED_BUILTIN.value = True
time.sleep(DAH_LENGTH / 1000)
board.LED_BUILTIN.value = False
time.sleep(ELEMENT_GAP / 1000)

def letter():
if DEBUG:
print(" ", end="")
time.sleep(SHORT_GAP / 1000)

def space():
if DEBUG:
print()
time.sleep(MEDIUM_GAP / 1000)

def play(input_str):
for c in input_str:
if c == ' ':
space()
else:
code = get_code(c)
if code is not None:
for symbol in code:
if symbol == '.':
dit()
elif symbol == '-':
dah()
letter()

text = "Hello World "

def loop():
play(text)

setup()
while True:
loop()

You might also like