From 112721fecd9838ba40e29cc8c5cb14d3531d55b6 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 13 Jun 2025 09:11:03 -0700 Subject: [PATCH] Metro RP2350 CP 10.x /sd Demo code for CircuitPython 10.x SD card list and write. The 9.x examples are not compatible. --- .../code-cp10x.py | 37 +++++++++++++++++++ .../CircuitPython_SDCard_ListFiles/code.py | 2 +- .../CircuitPython_SDCard_Write/code-cp10x.py | 23 ++++++++++++ .../CircuitPython_SDCard_Write/code.py | 2 + 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py create mode 100644 Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py diff --git a/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py b/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py new file mode 100644 index 000000000..43e5bf4bb --- /dev/null +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py @@ -0,0 +1,37 @@ +# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +CircuitPython 10.x SD Card Directory Listing Demo +Assumes the SD card is auto-mounted at /sd by the runtime. +""" + +import os + +def print_directory(path, tabs=0): + for name in os.listdir(path): + full_path = f"{path}/{name}" + stats = os.stat(full_path) + filesize = stats[6] + isdir = bool(stats[0] & 0x4000) + + # human-readable size + if filesize < 1_000: + sizestr = f"{filesize} bytes" + elif filesize < 1_000_000: + sizestr = f"{filesize/1_000:.1f} KB" + else: + sizestr = f"{filesize/1_000_000:.1f} MB" + + indent = " " * tabs + display_name = indent + name + ("/" if isdir else "") + + # <40 pads or truncates to 40 chars, then we append the size + print(f"{display_name:<40} Size: {sizestr}") + + if isdir: + print_directory(full_path, tabs + 1) + +print("Files on /sd:") +print("====================") +print_directory("/sd") diff --git a/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py b/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py index be5386756..0003e1971 100644 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py @@ -3,7 +3,7 @@ """ CircuitPython Essentials SD Card Read Demo - +* Not compatible with CircuitPython 10.x """ import os diff --git a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py new file mode 100644 index 000000000..f663393d6 --- /dev/null +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +CircuitPython 10.x SD Write Demo + • Assumes /sd is auto-mounted by the runtime + • Just open and append — no mount or SD driver code needed + • Not compatible with CircuitPython 9.x + • Uses SDIO +""" + +import time +import microcontroller + +print("Logging temperature to /sd/temperature.txt") + +while True: + t = microcontroller.cpu.temperature + print("T = %0.1f °C" % t) + with open("/sd/temperature.txt", "a") as f: + f.write("%0.1f\n" % t) + time.sleep(1) diff --git a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py index 596c2b75f..5fa050f73 100644 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py @@ -8,6 +8,8 @@ REMOVE THIS LINE AND ALL BELOW IT BEFORE SUBMITTING TO LEARN Update CHIP_SELECT_PIN to match the CS pin on your board. For example, for the Metro ESP32-S3, you would use: board.SD_CS. + +This code stops working with CircuitPython 10.x which automounts /sd """ import time