Skip to content

Instantly share code, notes, and snippets.

@WangYihang
Last active August 12, 2020 12:40
Show Gist options
  • Save WangYihang/bb40e8054acbfbe16346967e0578d680 to your computer and use it in GitHub Desktop.
Save WangYihang/bb40e8054acbfbe16346967e0578d680 to your computer and use it in GitHub Desktop.
# encoding:utf-8
import gpiozero
import time
import sys
class Broadcast:
def __init__(self, gpio):
self.buzzer = gpiozero.Buzzer(gpio)
self.unit = 0.04
self.breath = self.unit * 0.5
self.gap_table = {
".": self.unit * 1,
"-": self.unit * 3,
"/": self.unit * 7,
}
def beep(self, gap):
self.buzzer.on()
time.sleep(gap)
self.buzzer.off()
time.sleep(self.breath)
def emit(self, data):
for i in data:
sys.stdout.write(i)
sys.stdout.flush()
self.beep(self.gap_table[i])
def encode(data):
table = {
'A': '01' ,
'B': '1000' ,
'C': '1010' ,
'D': '100' ,
'E': '0' ,
'F': '0010' ,
'G': '110' ,
'H': '0000' ,
'I': '00' ,
'J': '0111' ,
'K': '101' ,
'L': '0100' ,
'M': '11' ,
'N': '10' ,
'O': '111' ,
'P': '0110' ,
'Q': '1101' ,
'R': '010' ,
'S': '000' ,
'T': '1' ,
'U': '001' ,
'V': '0001' ,
'W': '011' ,
'X': '1001' ,
'Y': '1011' ,
'Z': '1100' ,
'0': '11111' ,
'1': '01111' ,
'2': '00111' ,
'3': '00011' ,
'4': '00001' ,
'5': '00000' ,
'6': '10000' ,
'7': '11000' ,
'8': '11100' ,
'9': '11110' ,
'.': '010101' ,
',': '110011' ,
'?': '001100' ,
"'": '011110' ,
'!': '101011' ,
'/': '10010' ,
'(': '10110' ,
')': '101101' ,
'&': '01000' ,
':': '111000' ,
';': '101010' ,
'=': '10001' ,
'+': '01010' ,
'-': '100001' ,
'_': '001101' ,
'"': '010010' ,
'$': '0001001' ,
'@': '011010' ,
}
unicodes = data.decode("utf-8")
result = []
for i in unicodes:
if i.upper() in table.keys():
result.append(table[i.upper()])
continue
u = ""
for j in i.encode("utf-16-be"):
k = bin(ord(j))[2:].rjust(8, "0")
u += k
result.append(u.lstrip("0"))
return "/".join(result).replace("0", ".").replace("1", "-")
def main():
bc = Broadcast(gpio=17)
data = encode(sys.argv[1])
bc.emit(data)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment