From 38cb803bc46a0232e7fcb143416d79dc99748ad3 Mon Sep 17 00:00:00 2001 From: BlitzCityDIY Date: Tue, 14 Feb 2023 09:44:26 -0500 Subject: [PATCH] adding examples for microsd bff Adding a read and write example for the microsd bff --- microSD_BFF_Examples/read_sd/code.py | 53 ++++++++++++++++++++++++ microSD_BFF_Examples/write_to_sd/code.py | 34 +++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 microSD_BFF_Examples/read_sd/code.py create mode 100644 microSD_BFF_Examples/write_to_sd/code.py diff --git a/microSD_BFF_Examples/read_sd/code.py b/microSD_BFF_Examples/read_sd/code.py new file mode 100644 index 000000000..b1cf0b75e --- /dev/null +++ b/microSD_BFF_Examples/read_sd/code.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import os +import busio +import digitalio +import board +import storage +import adafruit_sdcard + +# The SD_CS pin is the chip select line. + +SD_CS = board.TX # setup for microSD BFF + +# Connect to the card and mount the filesystem. +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(spi, cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +# This helper function will print the contents of the SD +def print_directory(path, tabs=0): + for file in os.listdir(path): + stats = os.stat(path + "/" + file) + filesize = stats[6] + isdir = stats[0] & 0x4000 + + if filesize < 1000: + sizestr = str(filesize) + " bytes" + elif filesize < 1000000: + sizestr = "%0.1f KB" % (filesize / 1000) + else: + sizestr = "%0.1f MB" % (filesize / 1000000) + + prettyprintname = "" + for _ in range(tabs): + prettyprintname += " " + prettyprintname += file + if isdir: + prettyprintname += "/" + print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr)) + + # recursively print directory contents + if isdir: + print_directory(path + "/" + file, tabs + 1) + + +print("Files on filesystem:") +print("====================") +print_directory("/sd") diff --git a/microSD_BFF_Examples/write_to_sd/code.py b/microSD_BFF_Examples/write_to_sd/code.py new file mode 100644 index 000000000..3f0e4f7df --- /dev/null +++ b/microSD_BFF_Examples/write_to_sd/code.py @@ -0,0 +1,34 @@ +# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import time +import adafruit_sdcard +import board +import busio +import digitalio +import microcontroller +import storage + +# default CS pin for the microSD bff is TX +SD_CS = board.TX + +# Connect to the card and mount the filesystem. +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(spi, cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +print("Logging temperature to filesystem") +# append to the file! +while True: + # open file for append + with open("/sd/temperature.txt", "a") as f: + t = microcontroller.cpu.temperature + print("Temperature = %0.1f" % t) + f.write("%0.1f\n" % t) + # file is saved + time.sleep(1)