forked from tuxintrouble/sigbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
91 lines (77 loc) · 3.06 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
# This file is part of the SigBit project
# https://github.com/tuxintrouble/sigbit
# Author: Sebastian Stetter, DJ5SE
# License: GNU GENERAL PUBLIC LICENSE Version 3
#
# common utility functions for SigBit
def ditlen(wpm):
"""returns the lenght of a dit in seconds for a given words-per-minute"""
#PARIS incl. Abstände == 50 ditlängen -> 1 ditlänge at1wpm = 60s / (50 * wpm)
return 60 / (50 * wpm)
###morse LUT###
morse = {
"0" : "-----", "1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....",
"6" : "-....", "7" : "--...", "8" : "---..", "9" : "----.",
"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" : "--..", "=" : "-...-",
"/" : "-..-.", "+" : ".-.-.", "-" : "-....-", "." : ".-.-.-", "," : "--..--", "?" : "..--..",
":" : "---...", "!" : "-.-.--", "'" : ".----.", ";" : "-.-.-.", "&" : ".-...", "@" : ".--.-.",
"ä" : ".-.-", "ö" : "---.", "ü" : "..--", "ch" : "----", '"' : ".-..-.", "(" : "-.--.", ")" : "-.--.-",
"<sk>" : "...-.-", "<bk>" : "-...-.-"
}
def encode(text):
"""takes a string of characters and returns a wordbuffer"""
wordbuffer = []
for c in text:
if c == " ":
wordbuffer.append("11")
if c in morse:
for el in morse[c]:
if el == "-":
wordbuffer.append("10")
if el == ".":
wordbuffer.append("01")
wordbuffer.append("00")
if len(wordbuffer) > 0:
wordbuffer.pop()
wordbuffer.append("11")
return wordbuffer
def decode(buffer):
"""takes a wordbuffer and returns a string of characters"""
global morse
outcode = ""
outchars = ""
for el in buffer:
if el == "01":
outcode += "."
elif el == "10":
outcode += "-"
elif el == "00":
for letter, code in morse.items():
if code == outcode:
outchars += letter
outcode = ""
elif el =="11":
for letter, code in morse.items():
if code == outcode:
outchars += letter
outcode = ""
outchars = outchars + " "
return outchars
#make own zfill for uPython
def zfill(str,digits):
'''we need to implement our own zfill for uPython)'''
if len(str)>=digits:
return str
else:
return ((digits - len(str)) * '0')+str
#make own ljust for uPython
def ljust(string, width, fillchar=' '):
'''returns the str left justified, remaining space to the right is filed with fillchar, if str is shorter then width, original string is returned '''
while len(string) < width:
string += fillchar
return string