-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
547 lines (457 loc) · 22.2 KB
/
main.py
File metadata and controls
547 lines (457 loc) · 22.2 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
"""
ESP32 CYD Main Application with PC Communication
Displays buttons, system information, and handles PC communication
"""
import time
import gc
import sys
import select
from display import DisplayManager
from button import Button, ButtonManager, ButtonColors
from audio import AudioManager
# Serial communication handled through USB debug channel only
from json_protocol import extract_system_data, extract_ack_info, MessageType
class CYDApplication:
"""Main application class for ESP32 CYD with PC communication"""
def __init__(self):
# Initialize components
self.display_manager = DisplayManager()
self.audio_manager = AudioManager()
self.serial_comm = None
# Display and dimensions
self.display = None
self.width = 320
self.height = 240
# System data from PC
self.system_data = {
"date": "----/--/--",
"time": "--:--:--",
"cpu_percent": 0.0,
"ram_used_gb": 0.0,
"ram_total_gb": 0.0,
"network_sent_mb": 0.0,
"network_recv_mb": 0.0
}
# Connection status
self.pc_connected = False
self.usb_connected = False
self.last_system_update = 0
self.last_heartbeat = 0
self.last_usb_check = 0 # For non-blocking USB communication
self.touch_idle_counter = 0 # For touch-priority architecture
# Non-blocking serial communication
self.serial_buffer = "" # Buffer for building messages character by character
self.message_queue = [] # Queue for complete messages ready to process
# Menu system
self.button_manager = ButtonManager()
self.current_menu = "main"
self.menu_stack = [] # For navigation history
self.show_system_info = False
# Menu definitions
self.menus = {
"main": ["Setup", "System", "Sound", "Control"],
"setup": ["Calibrate", "WIFI", "EXIT"],
"system": ["Control", "EXIT"],
"sound": ["Alarm", "Car", "Bell", "Dog", "-->"],
"sound2": ["<--", "Police", "Tick", "Modem", "Applause"],
"control": ["Browser", "Terminal", "Shutdown", "EXIT"]
}
def create_menu_buttons(self):
"""Create buttons for the current menu"""
self.button_manager.buttons.clear() # Clear existing buttons
menu_items = self.menus.get(self.current_menu, [])
if self.current_menu == "system" and self.show_system_info:
# For system menu, use fixed small height for buttons, leave most space for system info
available_height = 40 # Fixed 40px total for both buttons (20px each)
self.button_manager.create_full_width_buttons(
self.width, available_height, menu_items,
ButtonColors.BUTTON_TEXT, ButtonColors.BUTTON_BG, ButtonColors.BUTTON_BORDER
)
else:
# Use full screen for buttons
self.button_manager.create_full_width_buttons(
self.width, self.height, menu_items,
ButtonColors.BUTTON_TEXT, ButtonColors.BUTTON_BG, ButtonColors.BUTTON_BORDER
)
# Set button callbacks
for button in self.button_manager.buttons:
button.set_callback(self.handle_menu_button_press)
print(f"Created {len(menu_items)} buttons for menu: {self.current_menu}")
def handle_menu_button_press(self, button):
"""Handle menu button press"""
button_text = button.text
print(f"Menu button pressed: {button_text}")
# Audio feedback
self.audio_manager.play_button_click_tone()
# Handle navigation
if button_text == "EXIT":
self.navigate_back()
elif button_text == "Setup":
self.navigate_to_menu("setup")
elif button_text == "System":
self.navigate_to_menu("system")
self.show_system_info = True
elif button_text == "Sound":
self.navigate_to_menu("sound")
elif button_text == "Control":
self.navigate_to_menu("control")
elif button_text == "-->":
self.navigate_to_menu("sound2")
elif button_text == "<--":
# Return to main menu when "<--" is pressed
self.current_menu = "main"
self.menu_stack.clear() # Clear navigation history
self.show_system_info = False
self.create_menu_buttons()
print("Navigated back to main menu")
elif button_text in ["Browser", "Terminal", "Shutdown"]:
self.execute_pc_command(button_text)
elif button_text in ["Alarm", "Car", "Bell", "Dog", "Police", "Tick", "Modem", "Applause"]:
self.execute_sound_command(button_text)
elif button_text in ["Calibrate", "WIFI"]:
print(f"{button_text} functionality will be implemented in the future")
# Redraw interface
self.draw_interface()
def navigate_to_menu(self, menu_name):
"""Navigate to a specific menu"""
if menu_name in self.menus:
self.menu_stack.append(self.current_menu)
self.current_menu = menu_name
if menu_name != "system":
self.show_system_info = False
self.create_menu_buttons()
print(f"Navigated to menu: {menu_name}")
def navigate_back(self):
"""Navigate back to previous menu"""
if self.menu_stack:
self.current_menu = self.menu_stack.pop()
self.show_system_info = False
self.create_menu_buttons()
print(f"Navigated back to menu: {self.current_menu}")
else:
print("Already at main menu")
def execute_pc_command(self, command):
"""Execute PC command via USB debug output"""
command_map = {
"Browser": "TEST", # Maps to Chrome
"Terminal": "INIT", # Maps to Terminal
"Shutdown": "EXIT" # Maps to Shutdown
}
pc_command = command_map.get(command, command.upper())
print(f"PC_COMMAND:{pc_command}")
print(f"Command {command} sent to PC")
def execute_sound_command(self, sound_name):
"""Execute sound command via USB debug output"""
print(f"PC_SOUND:{sound_name}")
print(f"Sound {sound_name} sent to PC")
def init_hardware(self):
"""Initialize all hardware components"""
print("Initializing ESP32 CYD hardware...")
# Initialize display
self.display = self.display_manager.init_display()
self.width, self.height = self.display_manager.get_dimensions()
print(f"Display initialized: {self.width}x{self.height}")
# Initialize touch
self.display_manager.init_touch()
print("Touch initialized")
# Initialize audio
self.audio_manager.init_speaker()
self.audio_manager.play_startup_tone()
print("Audio initialized")
# Serial communication will be handled through USB debug channel
# No separate UART needed - system data comes through debug output
self.serial_comm = None
print("Using USB debug channel for PC communication")
def handle_pc_message(self, message, is_json):
"""Handle messages received from PC"""
# Debug: Echo all received messages
print(f"RECEIVED: {'JSON' if is_json else 'TEXT'}: {message}")
if is_json:
message_type = message.get("type")
print(f"JSON Message Type: {message_type}")
if message_type == MessageType.SYSTEM_DATA:
# Update system data
data = extract_system_data(message)
print(f"Extracted system data: {data}")
if data:
self.system_data.update(data)
self.last_system_update = time.time()
print("System data updated from JSON")
elif message_type == MessageType.ACK:
# Handle command acknowledgment
command, result, details = extract_ack_info(message)
print(f"Command {command} result: {result}")
if details:
print(f"Details: {details}")
elif message_type == MessageType.STATUS:
state = message.get("state")
print(f"PC Status: {state}")
else:
# Non-JSON message (debug output) - check for data messages
if message.startswith("ESP32_DATA:"):
print(f"Found ESP32_DATA message: {message}")
if self.parse_esp32_data(message):
return # Successfully parsed data, don't print as debug
elif message.startswith("PC_SYSTEM_DATA:"):
print(f"Found PC_SYSTEM_DATA message: {message}")
if self.parse_debug_system_data(message):
return # Successfully parsed data, don't print as debug
print(f"PC: {message}")
def parse_esp32_data(self, debug_message):
"""Parse system data from ESP32_DATA debug output"""
try:
if debug_message.startswith("ESP32_DATA:"):
json_str = debug_message.split("ESP32_DATA:")[1].strip()
# Try to parse the JSON using the same method as regular messages
from json_protocol import parse_message
message = parse_message(json_str)
if message and message.get("type") == MessageType.SYSTEM_DATA:
data = extract_system_data(message)
if data:
self.system_data.update(data)
self.last_system_update = time.time()
print("System data updated from ESP32_DATA")
return True
except Exception as e:
print(f"Error parsing ESP32_DATA: {e}")
return False
def parse_debug_system_data(self, debug_message):
"""Parse system data from debug output"""
try:
if debug_message.startswith("PC_SYSTEM_DATA:"):
json_str = debug_message.split("PC_SYSTEM_DATA:")[1].strip()
# Try to parse the JSON using the same method as regular messages
from json_protocol import parse_message
message = parse_message(json_str)
if message and message.get("type") == MessageType.SYSTEM_DATA:
data = extract_system_data(message)
if data:
self.system_data.update(data)
self.last_system_update = time.time()
print("System data updated from PC_SYSTEM_DATA")
return True
except Exception as e:
print(f"Error parsing PC_SYSTEM_DATA: {e}")
return False
def reset_system_data(self):
"""Reset system data to show no data available"""
self.system_data = {
"date": "No Data",
"time": "No Data",
"cpu_percent": 0.0,
"ram_used_gb": 0.0,
"ram_total_gb": 0.0,
"network_sent_mb": 0.0,
"network_recv_mb": 0.0
}
def handle_connection_change(self, connected):
"""Handle serial connection state changes"""
self.pc_connected = connected
if connected:
print("Connected to PC!")
else:
print("Disconnected from PC")
def draw_interface(self):
"""Draw the complete user interface"""
# Clear display
self.display_manager.clear_screen()
# Draw menu buttons
self.button_manager.draw_all(self.display)
# Draw system information if in system menu
if self.current_menu == "system" and self.show_system_info:
self.draw_system_info()
def draw_system_info(self):
"""Draw system information received from PC in bottom half of screen"""
# Start drawing in the bottom half of the screen
y = self.height // 2 + 10
line_height = 16
# Connection status
current_time = time.time()
data_is_fresh = (self.last_system_update > 0 and
current_time - self.last_system_update < 30.0)
if data_is_fresh:
status_text = "Status: Connected"
status_color = 0x07E0 # Green
else:
status_text = "Status: No Data"
status_color = 0xF800 # Red
self.display.draw_text8x8(10, y, status_text, status_color)
y += line_height
# Date and Time
self.display.draw_text8x8(10, y, f"Date: {self.system_data['date']}", 0xFFFF)
y += line_height
self.display.draw_text8x8(10, y, f"Time: {self.system_data['time']}", 0xFFFF)
y += line_height
# CPU Usage
cpu_text = f"CPU: {self.system_data['cpu_percent']:.1f}%"
self.display.draw_text8x8(10, y, cpu_text, 0xFFE0) # Yellow
y += line_height
# RAM Usage
ram_text = f"RAM: {self.system_data['ram_used_gb']:.1f}/{self.system_data['ram_total_gb']:.1f}GB"
self.display.draw_text8x8(10, y, ram_text, 0x07FF) # Cyan
y += line_height
# Network Usage
net_text = f"NET: U:{self.system_data['network_sent_mb']:.1f} D:{self.system_data['network_recv_mb']:.1f}MB"
self.display.draw_text8x8(10, y, net_text, 0xF81F) # Magenta
def check_touch_input(self):
"""Check for touch input and handle menu button presses"""
touch_detected = False
if self.display_manager.is_touched():
touch_detected = True
x, y = self.display_manager.get_touch_coordinates()
print(f"Touch detected at: ({x}, {y})")
# Use button manager to handle touch - need both down and up for proper click
pressed_button = self.button_manager.handle_touch_down(x, y)
if pressed_button:
print(f"Button pressed: {pressed_button.text}")
# Immediately handle touch up for simple click behavior
clicked_button = self.button_manager.handle_touch_up(x, y)
if clicked_button:
print(f"Button clicked: {clicked_button.text}")
# Reset touch idle counter when touch is detected
self.touch_idle_counter = 0
else:
# Increment touch idle counter when no touch
self.touch_idle_counter += 1
return touch_detected
def check_usb_input_nonblocking(self):
"""Non-blocking USB input using brace-based JSON message parsing with smart buffer management"""
try:
# Check if ANY data is available (no blocking)
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
# Read single character (non-blocking)
char = sys.stdin.read(1)
if char:
if char == '{':
# Improved buffer management - don't lose substantial data
if len(self.serial_buffer) > 50:
print(f"WARNING: New message started before completion: {self.serial_buffer[:100]}...")
# Try to salvage partial message if it looks like valid JSON start
if self.serial_buffer.startswith('{"type":'):
print("Attempting to salvage partial message")
# Queue partial message for debugging
self.message_queue.append(f"PARTIAL: {self.serial_buffer}")
# Start new message
self.serial_buffer = '{'
print("JSON_START: New message started")
elif char == '}':
# End of JSON message - complete it
self.serial_buffer += '}'
print(f"JSON_END: Message completed ({len(self.serial_buffer)} chars)")
# Validate and queue complete JSON message
if self.is_valid_json(self.serial_buffer):
self.message_queue.append(self.serial_buffer)
print(f"QUEUED_MESSAGE: {self.serial_buffer}")
else:
print(f"INVALID_JSON: {self.serial_buffer}")
# Clear buffer for next message
self.serial_buffer = ""
else:
# Add character to current message (only if we have started with '{')
if self.serial_buffer.startswith('{'):
self.serial_buffer += char
# Ignore characters outside of JSON messages
# Prevent buffer overflow
if len(self.serial_buffer) > 2000:
print("Serial buffer overflow, clearing")
self.serial_buffer = ""
except Exception as e:
# Ignore errors - stdin might not be available in all environments
pass
def is_valid_json(self, text):
"""Check if text is valid JSON"""
try:
from json_protocol import parse_message
return parse_message(text) is not None
except:
return False
def process_message_queue(self):
"""Process all complete messages from queue"""
ui_needs_update = False
while self.message_queue:
message = self.message_queue.pop(0)
print(f"PROCESSING: {message}")
# Check for direct JSON messages first
if message.startswith('{"type":'):
try:
from json_protocol import parse_message
parsed = parse_message(message)
if parsed and parsed.get("type") == "system_data":
data = extract_system_data(parsed)
if data:
self.system_data.update(data)
self.last_system_update = time.time()
ui_needs_update = True
print("System data updated from direct JSON")
continue
except Exception as e:
print(f"Error parsing direct JSON: {e}")
# Check for prefixed system data messages
if message.startswith("PC_SYSTEM_DATA:"):
print(f"Found PC_SYSTEM_DATA message: {message}")
if self.parse_debug_system_data(message):
ui_needs_update = True
elif message.startswith("ESP32_DATA:"):
print(f"Found ESP32_DATA message: {message}")
if self.parse_esp32_data(message):
ui_needs_update = True
else:
# Handle other messages
print(f"PC_MESSAGE: {message}")
# Update UI only if we received new system data
if ui_needs_update and self.current_menu == "system" and self.show_system_info:
print("UI update triggered by new system data")
self.draw_interface()
return ui_needs_update
def update_communication(self):
"""Update communication status with non-blocking USB checks"""
current_time = time.time()
# Non-blocking character reading (very fast)
self.check_usb_input_nonblocking()
# Process any complete messages in queue
self.process_message_queue()
# Debug: Log communication status every 30 seconds
if current_time - self.last_heartbeat > 30: # Every 30 seconds
print(f"Communication status: USB_Debug=True, Last_data={current_time - self.last_system_update:.1f}s ago")
print(f"Buffer: '{self.serial_buffer}', Queue: {len(self.message_queue)} messages")
self.last_heartbeat = current_time
def run(self):
"""Main application loop"""
print("ESP32 CYD Application Starting...")
# Initialize hardware
self.init_hardware()
# Initialize menu system
self.create_menu_buttons()
# Draw initial interface
self.draw_interface()
print("Application ready - waiting for input")
# Main loop with touch-priority architecture
while True:
try:
current_time = time.time()
# Priority 1: ALWAYS check touch input first (never blocked)
touch_detected = self.check_touch_input()
# Priority 2: Only check serial communication when touch is idle
if not touch_detected and self.touch_idle_counter > 5: # After 5 loops without touch
self.update_communication()
self.touch_idle_counter = 0 # Reset counter after checking serial
# UI updates are now handled only when new data arrives in process_message_queue()
# No periodic UI updates - makes communication more robust
# Reduced delay for faster touch response
time.sleep(0.01) # 10ms for fast touch response
# Periodic garbage collection
if time.ticks_ms() % 10000 < 50: # Every ~10 seconds
gc.collect()
except KeyboardInterrupt:
print("Application interrupted")
break
except Exception as e:
print(f"Error in main loop: {e}")
time.sleep(1) # Wait before retrying
print("Application stopped")
def main():
"""Main entry point"""
app = CYDApplication()
app.run()
if __name__ == "__main__":
main()