-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
183 lines (160 loc) · 5.81 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import machine
import utime
import re
import urequests
short_dur = 500
long_dur = 3000
key_f = False
ctrl_f = False
key_s = 0
ctrl_s = 0
morse = {
'.-': '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'
}
inv_morse = {v: k for k, v in morse.items()}
morse_in = []
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
}
def key_interrupt(pin):
global key_f, key_s
if not pin.value(): # key pressed
key_f = True
key_s = utime.ticks_ms()
else: # key released
if key_f:
key_f = False
key_dur = utime.ticks_diff(utime.ticks_ms(), key_s)
if key_dur < short_dur:
morse_in.append('.')
haptic(200, 1023, 1000) # duration, duty (0-1023), frequency
print('.')
elif key_dur >= short_dur:
morse_in.append('-')
haptic(500, 1023, 1000)
print('-')
def ctrl_interrupt(pin):
global ctrl_f, ctrl_s, morse_in
if not pin.value(): # ctrl pressed
ctrl_f = True
ctrl_s = utime.ticks_ms()
else: # ctrl released
if ctrl_f:
ctrl_f = False
ctrl_dur = utime.ticks_diff(utime.ticks_ms(), ctrl_s)
if ctrl_dur <= short_dur: # ' ' = '/'
print(' ')
haptic(200, 1023, 1000)
morse_in.append(' ')
elif ctrl_dur > short_dur and ctrl_dur < long_dur:
print('SUBMIT')
haptic(500, 1023, 1000)
source = transcode(''.join(morse_in), True)
if source is not None:
print('translate')
target = translate('en', 'fr', source)
print(f'{source} --> {target}')
res = transcode(target, False)
print(f'{target} --> {res}')
vibrate(res, 1023, 1000)
morse_in = []
else:
print('raise error')
#raise_vib(404)
elif ctrl_dur >= long_dur:
print('CLEAR')
vibrate(1000, 1023, 1000)
morse_in = []
def haptic(dur, duty, freq): # int, int, int
led.value(0)
vib_pwm.freq(freq)
vib_pwm.duty(duty)
start = utime.ticks_ms()
while utime.ticks_diff(utime.ticks_ms(), start) < dur:
pass
led.value(1)
vib_pwm.duty(0)
def vibrate(job, duty, freq): # str, int, int
print(job)
for dd in job:
if dd == '.':
led.value(0)
vib_pwm.duty(duty)
utime.sleep_ms(200)
elif dd == '-':
led.value(0)
vib_pwm.duty(duty)
utime.sleep_ms(500)
elif dd == ' ':
led.value(0)
vib_pwm.duty(0)
utime.sleep_ms(500)
led.value(1)
vib_pwm.duty(0)
utime.sleep_ms(100)
def transcode(job, codec): # str, bool
# '.... .. -.. .- ...- .'
if job:
if codec:
print('decode')
fr = []
for word in job.split(' '): # ['.... ..', '-.. .- ...- .']
for char in word.split(' '): # ['....', '..']
if char:
res = morse.get(char)
if res:
fr.append(res)
else:
print(f'KeyError: {char}')
return None
else:
print('EMPTY')
return None
fr.append(' ')
#print(''.join(fr))
#return ''.join(fr)
elif not codec:
print('encode')
fr = []
for word in job.split(' '):
#print(word)
for char in word:
#print(char)
res = inv_morse.get(char.upper())
if res:
fr.append(res)
else:
print(f'KeyError: {char}')
continue
fr.append(' ')
fr.append(' ')
else:
print('EMPTY')
return None
return ''.join(fr)
def translate(sl, tl, fuck): # str, str, str
#print(fuck)
url = f'https://translate.google.com/m?sl={sl}&tl={tl}&q={fuck}'
#print(url)
response = urequests.get(url, headers=headers).text
#print(response)
res = re.compile(r'<div class="result-container">(.*?)</div>').search(str(response))
return res.group(1)
# set up pins and handlers
key_p = machine.Pin(19, machine.Pin.IN, machine.Pin.PULL_UP)
key_p.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=key_interrupt)
ctrl_p = machine.Pin(23, machine.Pin.IN, machine.Pin.PULL_UP)
ctrl_p.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=ctrl_interrupt)
led = machine.Pin(5, machine.Pin.OUT)
led.value(1)
vib_pwm = machine.PWM(machine.Pin(12))
vib_pwm.freq(1000)
vib_pwm.duty(0)