prjbrook

Sd card Circuit Python Thonny 1

Jun 25th, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries PB1 try
  2. # SPDX-License-Identifier: MIT
  3. #Worked !! Sat Jun 25 15:20:38 NZST 2022
  4.  
  5. import os
  6. import busio
  7. import digitalio
  8. import board
  9. import storage
  10. import adafruit_sdcard
  11.  
  12. # The SD_CS pin is the chip select line.
  13. #
  14. # The Adalogger Featherwing with ESP8266 Feather, the SD CS pin is on board.D15
  15. # The Adalogger Featherwing with Atmel M0 Feather, it's on board.D10
  16. # The Adafruit Feather M0 Adalogger use board.SD_CS
  17. # For the breakout boards use any pin that is not taken by SPI
  18.  
  19. #SD_CS = board.SD_CS # setup for M0 Adalogger; change as needed
  20. #block below from DoneBot workshop pb Some capitalisation changes
  21. MOSI = board.GP11
  22. MISO = board.GP12
  23. #clk = board.GP10
  24. SCK = board.GP10
  25. cs = board.GP15
  26.  
  27. # Connect to the card and mount the filesystem.
  28. spi = busio.SPI(board.GP10, board.GP11, board.GP12)
  29. #cs = digitalio.DigitalInOut(SD_CS)
  30.  
  31. #cs = board.GP15
  32. #sdcard = adafruit_sdcard.SDCard(spi, board.GP15)
  33. #cs = digitalio.DigitalInOut(board.SD_CS)
  34. cs = digitalio.DigitalInOut(board.GP15)
  35. sdcard = adafruit_sdcard.SDCard(spi, cs)
  36. vfs = storage.VfsFat(sdcard)
  37. storage.mount(vfs, "/sd")
  38.  
  39. # Use the filesystem as normal! Our files are under /sd
  40.  
  41. # This helper function will print the contents of the SD
  42. def print_directory(path, tabs=0):
  43. for file in os.listdir(path):
  44. stats = os.stat(path + "/" + file)
  45. filesize = stats[6]
  46. isdir = stats[0] & 0x4000
  47.  
  48. if filesize < 1000:
  49. sizestr = str(filesize) + " bytes"
  50. elif filesize < 1000000:
  51. sizestr = "%0.1f KB" % (filesize / 1000)
  52. else:
  53. sizestr = "%0.1f MB" % (filesize / 1000000)
  54.  
  55. prettyprintname = ""
  56. for _ in range(tabs):
  57. prettyprintname += " "
  58. prettyprintname += file
  59. if isdir:
  60. prettyprintname += "/"
  61. print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
  62.  
  63. # recursively print directory contents
  64. if isdir:
  65. print_directory(path + "/" + file, tabs + 1)
  66.  
  67.  
  68. print("Files on filesystemmm:")
  69. print("====================")
  70. print_directory("/sd")
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment