adding examples for microsd bff
Adding a read and write example for the microsd bff
This commit is contained in:
parent
6fc13980e9
commit
38cb803bc4
2 changed files with 87 additions and 0 deletions
53
microSD_BFF_Examples/read_sd/code.py
Normal file
53
microSD_BFF_Examples/read_sd/code.py
Normal file
|
|
@ -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")
|
||||
34
microSD_BFF_Examples/write_to_sd/code.py
Normal file
34
microSD_BFF_Examples/write_to_sd/code.py
Normal file
|
|
@ -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)
|
||||
Loading…
Reference in a new issue