From 07bbba41e46155ed459fadeca9587b4d5e95e62c Mon Sep 17 00:00:00 2001 From: Atharv Nagavarapu <30nagava@elmbrookstudents.org> Date: Fri, 30 Jan 2026 13:49:03 +0000 Subject: [PATCH] Update diagnostics/MicroPythonDiagnostics.py --- diagnostics/MicroPythonDiagnostics.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/diagnostics/MicroPythonDiagnostics.py b/diagnostics/MicroPythonDiagnostics.py index 63e4e78..6a1d36b 100644 --- a/diagnostics/MicroPythonDiagnostics.py +++ b/diagnostics/MicroPythonDiagnostics.py @@ -10,7 +10,24 @@ class MicroPythonDiagnostics: print("[Hub Diagnostics - MicroPython - Version] Pybricks version information:", usys.version_info) print("[Hub Diagnostics - MicroPython - Version] MicroPython information:", usys.implementation) def performMemoryDiagnostics(): + print("[Hub Diagnostics - MicroPython - Memory] Memory information (retrieved from the MicroPython environment):") micropython.mem_info(1) + print("[Hub Diagnostics - MicroPython - Memory] Testing heap lock and unlock.") + print("[Hub Diagnostics - MicroPython - Memory] Allocating memory while heap is unlocked:") + try: + x = 5000 + print("[Hub Diagnostics - MicroPython - Memory] There was no MemoryError raised. The value of the variable x is", x) + except MemoryError: + print("[Hub Diagnostics - MicroPython - Memory] Allocation failed. Your memory may be faulty.") + print("[Hub Diagnostics - MicroPython - Memory] Locking the heap:") + micropython.heap_lock() + print("[Hub Diagnostics - MicroPython - Memory] Heap was locked. Attempting to allocate memory (this should fail):") + try: + x = 5000 + print("[Hub Diagnostics - MicroPython - Memory] There was no MemoryError raised. Heap lock failed.") + except MemoryError: + print("[Hub Diagnostics - MicroPython - Memory] Allocation failed. Test successful. The heap was successfully locked.") + # Attempt to add gc to this for memory diagnostics, in addition test machine.soft_reset() and add that first to reset the heap. def printAll(): printVersionDiagnostics() performMemoryDiagnostics()