From 748f988a520f19d10a26f7154cdd9b9224345ae4 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 13 Jun 2025 11:54:11 -0500 Subject: [PATCH] expand SD comments, remove leftover comment section, remove 10x specific files --- .../code-cp10x.py | 37 ------------------- .../CircuitPython_SDCard_ListFiles/code.py | 12 ++++-- .../CircuitPython_SDCard_Write/code-cp10x.py | 23 ------------ .../CircuitPython_SDCard_Write/code.py | 18 +++++---- 4 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py delete 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 deleted file mode 100644 index 43e5bf4bb..000000000 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code-cp10x.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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 9c03bf1b5..392426831 100644 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_ListFiles/code.py @@ -3,7 +3,6 @@ """ CircuitPython Essentials SD Card Read Demo -* Not compatible with CircuitPython 10.x """ import os @@ -16,16 +15,23 @@ import adafruit_sdcard # The SD_CS pin is the chip select line. SD_CS = board.SD_CS +# For CircuitPython 9.x we must initialize and mount the SD card manually. +# On CircuitPython 10.x the core will automatically initialize and mount the SD. +# This try/except block can be removed with 10.x has a stable release. try: - # Connect to the card and mount the filesystem. + # Initialize the Chip Select pin for the SD card cs = digitalio.DigitalInOut(SD_CS) + # Initialize the SD card sdcard = adafruit_sdcard.SDCard( busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs ) + # Mount the SD card vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") except ValueError: - # SD_CS in use error happens if CircuitPython core initialized the SD automatically + # "ValueError SD_CS in use" error happen on CircuitPython 10.x + # because the core initialized the SD automatically. The error + # can be ignored on 10.x. pass # Use the filesystem as normal! Our files are under /sd diff --git a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py deleted file mode 100644 index f663393d6..000000000 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code-cp10x.py +++ /dev/null @@ -1,23 +0,0 @@ -# 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 2de6b96a1..5100c8aa0 100644 --- a/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py +++ b/Metro_RP2350_Examples/CircuitPython_SDCard_Write/code.py @@ -4,12 +4,6 @@ """ CircuitPython Essentials SD Card Write Demo -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 @@ -23,16 +17,24 @@ import storage # The SD_CS pin is the chip select line. SD_CS = board.SD_CS + +# For CircuitPython 9.x we must initialize and mount the SD card manually. +# On CircuitPython 10.x the core will automatically initialize and mount the SD. +# This try/except block can be removed with 10.x has a stable release. try: - # Connect to the card and mount the filesystem. + # Initialize the Chip Select pin for the SD card cs = digitalio.DigitalInOut(SD_CS) + # Initialize the SD card sdcard = adafruit_sdcard.SDCard( busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs ) + # Mount the SD card vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") except ValueError: - # SD_CS in use error happens if CircuitPython core initialized the SD automatically + # "ValueError SD_CS in use" error happen on CircuitPython 10.x + # because the core initialized the SD automatically. The error + # can be ignored on 10.x. pass # Use the filesystem as normal! Our files are under /sd