-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduino.py
More file actions
72 lines (60 loc) · 2.47 KB
/
Arduino.py
File metadata and controls
72 lines (60 loc) · 2.47 KB
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
import serial, pyautogui
import serial.tools.list_ports
from tkinter import messagebox
from pyautogui import press, write
class Arduino:
def __init__(self):
self.serialPort = None
self.serialPorts = self.getSerialPorts()
self.serialPortInput = ''
self.assentosKeys = ['´', '`', '^', '~']
self.programModes = ['Normal Keyboard', 'encrypt', 'decrypt']
self.keysAccepts = ["[ESC]", "[Enter]", "[PgDn]", "[PgUp]", "[Left]", "[Right]", "[Up]", "[Down]", "[Backspace]","[Tab]"]
# get list of serial ports
def getSerialPorts(self):
return serial.tools.list_ports.comports()
# connect with the serial port
def connectSerialPort(self, port):
try:
self.serialPort = serial.Serial(port.split('-')[0].strip(), 115200, timeout=1)
except FileNotFoundError as error:
messagebox.showerror("Error", error)
# listening the serial port
def listenSerialPort(self):
while True:
try:
self.serialPortInput += self.serialPort.readline().decode('ascii')
if len(self.serialPortInput):
self.serialPortInput = self.verifyString()
except KeyboardInterrupt:
break
except serial.serialutil.SerialException:
pyautogui.alert('Arduino desconectado')
# send text to decode in arduino
def sendTextToDecryptSerial(self, message):
self.sendToSerial('[DECODE]' + message)
# send text to arduino
def sendToSerial(self, message):
self.serialPort.write(message.encode())
# send mode to arduino
def sendModeToSerial(self, message):
self.sendToSerial('[MODE]' + message)
# works with the string that arrives from the serial
def verifyString(self):
while self.serialPortInput:
for key in self.keysAccepts:
if self.serialPortInput.find(key) == 0:
keyFormated = key.replace("[", "").replace("]", "").lower()
pyautogui.press(keyFormated)
self.serialPortInput = self.serialPortInput[len(key):]
break
else:
break
if self.serialPortInput:
for char in self.serialPortInput:
if char not in self.assentosKeys:
write(char)
else:
press(char, presses=2)
self.serialPortInput = ''
return self.serialPortInput