Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -5,6 +5,9 @@ import queue
|
||||
import threading
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.animation as animation
|
||||
import io
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
# CONFIGURAZIONE
|
||||
BASYS3_PID = 0x6010
|
||||
@@ -48,37 +51,71 @@ def receive_graph_mode(ser):
|
||||
reader_thread = threading.Thread(target=serial_reader, daemon=True)
|
||||
reader_thread.start()
|
||||
|
||||
latest_point = [64, 64] # Punto iniziale al centro del grafico
|
||||
latest_color = 'blue'
|
||||
latest_size = 100
|
||||
point = [64, 64] # Punto iniziale al centro del grafico
|
||||
color = 'blue'
|
||||
size = 100
|
||||
|
||||
release = True
|
||||
rgb = [0, 0, 0]
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
sc = ax.scatter([latest_point[0]], [latest_point[1]], c=[latest_color], s=[latest_size])
|
||||
|
||||
# Load PNG directly
|
||||
png_path = r'LAB3\test\Color_circle_(RGB).png'
|
||||
img = Image.open(png_path).convert('RGB')
|
||||
img = img.resize((127, 127), Image.LANCZOS) # Ensure image is 127x127
|
||||
img_np = np.array(img)
|
||||
|
||||
# Show the image as background
|
||||
ax.imshow(img, extent=[0, 127, 0, 127], aspect='auto', zorder=0)
|
||||
|
||||
sc = ax.scatter([point[0]], [point[1]], s=[size], zorder=1)
|
||||
ax.set_xlim(0, 127)
|
||||
ax.set_ylim(0, 127)
|
||||
ax.set_xlabel("X")
|
||||
ax.set_ylabel("Y")
|
||||
ax.set_title("Coordinate in tempo reale")
|
||||
|
||||
def send_rgb_over_serial(ser, rgb):
|
||||
"""
|
||||
Send RGB values over serial with a fixed first byte (0xC0).
|
||||
"""
|
||||
bytes_to_send = [0xC0] # Primo byte fisso
|
||||
for part in rgb:
|
||||
val = int(part)
|
||||
bytes_to_send.append(val)
|
||||
ser.write(bytearray(bytes_to_send))
|
||||
print(f"Inviato: {' '.join(f'{b:02X}' for b in bytes_to_send)}")
|
||||
|
||||
def update(frame):
|
||||
nonlocal latest_point, latest_color, latest_size
|
||||
nonlocal point, color, size, release, rgb
|
||||
|
||||
while not q.empty():
|
||||
x, y, flags = q.get()
|
||||
latest_point[0] = x
|
||||
latest_point[1] = y
|
||||
# Bit 0: red if set, else blue
|
||||
point[0] = x
|
||||
point[1] = y
|
||||
|
||||
if flags & 0b00000001:
|
||||
latest_color = 'red'
|
||||
rgb = [0, 0, 0]
|
||||
size = 50
|
||||
if release:
|
||||
send_rgb_over_serial(ser, rgb)
|
||||
release = False
|
||||
|
||||
elif flags & 0b00000010:
|
||||
flipped_y = 127 - y # Flip y to match image coordinates
|
||||
rgb = img_np[flipped_y, x]
|
||||
size = 300
|
||||
if release:
|
||||
send_rgb_over_serial(ser, rgb)
|
||||
release = False
|
||||
|
||||
else:
|
||||
latest_color = 'blue'
|
||||
# Bit 1: bigger if set
|
||||
if flags & 0b00000010:
|
||||
latest_size = 300
|
||||
else:
|
||||
latest_size = 100
|
||||
sc.set_offsets([latest_point])
|
||||
sc.set_color([latest_color])
|
||||
sc.set_sizes([latest_size])
|
||||
size = 100
|
||||
release = True # Reset release when no button is pressed
|
||||
|
||||
sc.set_offsets([point])
|
||||
sc.set_sizes([size])
|
||||
return sc,
|
||||
|
||||
ani = animation.FuncAnimation(fig, update, interval=10, blit=True, cache_frame_data=False)
|
||||
|
||||
Reference in New Issue
Block a user