From 511aa221beda847a9c320fd467ebd1ad4f7cb1a2 Mon Sep 17 00:00:00 2001 From: RaffaelW <146560011+RaffaelW@users.noreply.github.com> Date: Mon, 21 Jul 2025 16:12:31 +0200 Subject: [PATCH] Add memory debugging plotter and update requirements - Implement a Python script to visualize free heap memory over time. - Add serial port selection for connecting to the device. - Update requirements.txt to include necessary libraries. - Log free heap memory in the main application loop for monitoring. --- plot_heap.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ src/main.cpp | 2 ++ 3 files changed, 81 insertions(+) create mode 100644 plot_heap.py create mode 100644 requirements.txt diff --git a/plot_heap.py b/plot_heap.py new file mode 100644 index 0000000..118fcdd --- /dev/null +++ b/plot_heap.py @@ -0,0 +1,77 @@ +import glob +import re +import sys + +import matplotlib.pyplot as plt +import serial + + +def list_serial_ports(): + # Linux: /dev/ttyUSB*, /dev/ttyACM* + ports = glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*') + return ports + +def select_port(): + ports = list_serial_ports() + if not ports: + print("Keine seriellen Ports gefunden.") + sys.exit(1) + print("Verfügbare serielle Ports:") + for i, port in enumerate(ports): + print(f" [{i}] {port}") + while True: + try: + idx = int(input(f"Port wählen [0-{len(ports)-1}]: ")) + if 0 <= idx < len(ports): + return ports[idx] + except Exception: + pass + print("Ungültige Eingabe.") + +def main(): + port = select_port() + try: + with serial.Serial(port, 9600, timeout=1) as ser: + heap_values = [] + times = [] + + plt.ion() + fig, ax = plt.subplots() + line, = ax.plot(times, heap_values) + ax.set_xlabel('Messung') + ax.set_ylabel('Free Heap (Bytes)') + ax.set_title(f'Serial: {port}') + + print("Drücke Strg+C oder schließe das Plot-Fenster zum Beenden.") + try: + while True: + # Check if the plot window is still open + if not plt.fignum_exists(fig.number): + print("Plot-Fenster geschlossen. Beende.") + break + line_raw = ser.readline().decode(errors='ignore') + m = re.search(r'\[MEM_DEBUG\] Free heap: (\d+)', line_raw) + if m: + heap = int(m.group(1)) + heap_values.append(heap) + times.append(len(times)) + line.set_xdata(times) + line.set_ydata(heap_values) + ax.relim() + ax.autoscale_view() + plt.draw() + plt.pause(0.01) + else: + plt.pause(0.01) + except KeyboardInterrupt: + print("\nBeendet.") + plt.close(fig) + finally: + plt.ioff() + plt.show() + except Exception as e: + print(f"Fehler beim Öffnen von {port}: {e}") + sys.exit(1) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..acfe2f8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +matplotlib +pyserial diff --git a/src/main.cpp b/src/main.cpp index 5c18ba1..0fbeddc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -493,6 +493,8 @@ void transmitDmxToArtnet(dmx_port_t dmxPort, byte *dmx_data, uint8_t artnetUnive void loop() { + Serial.printf("[MEM_DEBUG] Free heap: %u bytes\n", xPortGetFreeHeapSize()); + // only check for artnet packets if we expect to receive data if (direction1 == Output || direction2 == Output) {