-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.py
More file actions
74 lines (61 loc) · 2.3 KB
/
Copy pathmove.py
File metadata and controls
74 lines (61 loc) · 2.3 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
73
74
import pygame
import serial
import threading
import pygame_chart as pyc
# Configurar conexión al monitor serial
ser = serial.Serial('/dev/ttyUSB0', 115200) # Cambia '/dev/ttyUSB0' por tu puerto serial
# Inicializar Pygame
pygame.init()
# Configurar la pantalla
screen = pygame.display.set_mode((640, 640),)
# Figure instance on screen with position and size
figure = pyc.Figure(screen, 50, 50, 700, 500)
pygame.display.set_caption('Detección de Teclas y Lectura Serial')
# Variable para controlar el bucle principal
running = True
# Función para leer datos del monitor serial
def read_serial():
while running:
if ser.in_waiting > 0: # Comprobar si hay datos disponibles
data = ser.readline().decode('utf-8') # Leer y decodificar los datos
print(data)
# Iniciar el hilo para lectura serial
serial_thread = threading.Thread(target=read_serial, daemon=True)
serial_thread.start()
# Bucle principal
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
#print(f'Tecla {pygame.key.name(event.key)} presionada')
if pygame.key.name(event.key) == 'w':
#print('Adelante')
ser.write(b'f')
elif pygame.key.name(event.key) == 'a':
#print('Izquierda')
ser.write(b'l')
elif pygame.key.name(event.key) == 's':
#print('Atrás')
ser.write(b'b')
elif pygame.key.name(event.key) == 'd':
#print('Derecha')
ser.write(b'r')
elif pygame.key.name(event.key) == 'up':
#print('Adelante')
ser.write(b'z')
elif pygame.key.name(event.key) == 'down':
#print('Atrás')
ser.write(b'x')
elif pygame.key.name(event.key) == 'left':
#print('Izquierda')
ser.write(b'v')
elif pygame.key.name(event.key) == 'right':
#print('Derecha')
ser.write(b'c')
elif event.type == pygame.KEYUP:
#print(f'Tecla {pygame.key.name(event.key)} liberada')
ser.write(b's')
# Finalizar Pygame y cerrar conexión serial
pygame.quit()
ser.close()