From 55cfdf3de594b99e7f8f519962ca85be4786dd87 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Fri, 19 May 2023 21:37:34 -0400 Subject: [PATCH 1/4] Update examples with umount Help avoid data corruption by unmounting SD card after use --- examples/bitmapsaver_screenshot_simpletest.py | 11 ++++++++--- examples/bitmapsaver_screenshot_tft_featherwing.py | 12 +++++++++--- examples/bitmapsaver_simpletest.py | 11 +++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/examples/bitmapsaver_screenshot_simpletest.py b/examples/bitmapsaver_screenshot_simpletest.py index 047028f..2ca8dd7 100644 --- a/examples/bitmapsaver_screenshot_simpletest.py +++ b/examples/bitmapsaver_screenshot_simpletest.py @@ -12,6 +12,7 @@ import storage from adafruit_bitmapsaver import save_pixels +TAKE_SCREENSHOT = False # Set to True to take a screenshot spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) cs = digitalio.DigitalInOut(board.SD_CS) @@ -19,6 +20,10 @@ vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") -print("Taking Screenshot...") -save_pixels("/sd/screenshot.bmp") -print("Screenshot taken") +if TAKE_SCREENSHOT: + print("Taking Screenshot... ") + save_pixels("/sd/screenshot.bmp") + print("Screenshot Saved") + storage.umount(vfs) + print("SD Card Unmounted") # Do not remove SD card until unmounted + \ No newline at end of file diff --git a/examples/bitmapsaver_screenshot_tft_featherwing.py b/examples/bitmapsaver_screenshot_tft_featherwing.py index c7b7f0e..051c765 100644 --- a/examples/bitmapsaver_screenshot_tft_featherwing.py +++ b/examples/bitmapsaver_screenshot_tft_featherwing.py @@ -16,6 +16,8 @@ DISPLAY_WIDTH = 480 DISPLAY_HEIGHT = 320 +TAKE_SCREENSHOT = False # Set to True to take a screenshot + # Initialize Protocol Busses and SD Card spi = board.SPI() cs = digitalio.DigitalInOut(board.D5) @@ -33,6 +35,10 @@ virtual_root = "/sd" storage.mount(vfs, virtual_root) -print("Taking Screenshot... ") -save_pixels("/sd/screenshot.bmp", display) -print("Screenshot taken") +if TAKE_SCREENSHOT: + print("Taking Screenshot... ") + save_pixels("/sd/screenshot.bmp", display) + print("Screenshot Saved") + storage.umount(vfs) + print("SD Card Unmounted") # Do not remove SD card until unmounted + diff --git a/examples/bitmapsaver_simpletest.py b/examples/bitmapsaver_simpletest.py index 8b2258b..b6a2751 100644 --- a/examples/bitmapsaver_simpletest.py +++ b/examples/bitmapsaver_simpletest.py @@ -14,6 +14,8 @@ # pylint:disable=invalid-name +TAKE_SCREENSHOT = False # Set to True to take a screenshot + print("Setting up SD card") spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) cs = digitalio.DigitalInOut(board.SD_CS) @@ -50,5 +52,10 @@ else: bitmap[x, y] = 0 -print("Saving bitmap") -save_pixels("/sd/test.bmp", bitmap, palette) +if TAKE_SCREENSHOT: + print("Taking Screenshot... ") + save_pixels("/sd/screenshot.bmp", bitmap, palette) + print("Screenshot Saved") + storage.umount(vfs) + print("SD Card Unmounted") # Do not remove SD card until unmounted + From 877f507c4cff1c0bf9d7fa16b3d3670632aff85f Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Fri, 19 May 2023 21:50:25 -0400 Subject: [PATCH 2/4] black gets me every time ran black on directory --- examples/bitmapsaver_screenshot_simpletest.py | 1 - examples/bitmapsaver_screenshot_tft_featherwing.py | 1 - examples/bitmapsaver_simpletest.py | 1 - 3 files changed, 3 deletions(-) diff --git a/examples/bitmapsaver_screenshot_simpletest.py b/examples/bitmapsaver_screenshot_simpletest.py index 2ca8dd7..dbb47a3 100644 --- a/examples/bitmapsaver_screenshot_simpletest.py +++ b/examples/bitmapsaver_screenshot_simpletest.py @@ -26,4 +26,3 @@ print("Screenshot Saved") storage.umount(vfs) print("SD Card Unmounted") # Do not remove SD card until unmounted - \ No newline at end of file diff --git a/examples/bitmapsaver_screenshot_tft_featherwing.py b/examples/bitmapsaver_screenshot_tft_featherwing.py index 051c765..ff6f36f 100644 --- a/examples/bitmapsaver_screenshot_tft_featherwing.py +++ b/examples/bitmapsaver_screenshot_tft_featherwing.py @@ -41,4 +41,3 @@ print("Screenshot Saved") storage.umount(vfs) print("SD Card Unmounted") # Do not remove SD card until unmounted - diff --git a/examples/bitmapsaver_simpletest.py b/examples/bitmapsaver_simpletest.py index b6a2751..5273420 100644 --- a/examples/bitmapsaver_simpletest.py +++ b/examples/bitmapsaver_simpletest.py @@ -58,4 +58,3 @@ print("Screenshot Saved") storage.umount(vfs) print("SD Card Unmounted") # Do not remove SD card until unmounted - From 96803dc1e9811925a10a6e91a06a31b986d33ffa Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Tue, 13 Jun 2023 12:40:31 -0400 Subject: [PATCH 3/4] Move SD card initializations into the if statement As requested by review. Unable to test the "bitmapsaver_simpletest" bitmap palette example on my featherwing since SCK is in use by other things. --- examples/bitmapsaver_screenshot_simpletest.py | 14 +++++------ .../bitmapsaver_screenshot_tft_featherwing.py | 24 +++++++++---------- examples/bitmapsaver_simpletest.py | 22 ++++++++--------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/examples/bitmapsaver_screenshot_simpletest.py b/examples/bitmapsaver_screenshot_simpletest.py index dbb47a3..49c426f 100644 --- a/examples/bitmapsaver_screenshot_simpletest.py +++ b/examples/bitmapsaver_screenshot_simpletest.py @@ -1,7 +1,6 @@ # SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries # SPDX-License-Identifier: MIT - """Example of taking a screenshot.""" # pylint:disable=invalid-name @@ -14,13 +13,14 @@ TAKE_SCREENSHOT = False # Set to True to take a screenshot -spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) -cs = digitalio.DigitalInOut(board.SD_CS) -sdcard = adafruit_sdcard.SDCard(spi, cs) -vfs = storage.VfsFat(sdcard) -storage.mount(vfs, "/sd") - if TAKE_SCREENSHOT: + # Initialize SD Card & Mount Virtual File System + spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) + cs = digitalio.DigitalInOut(board.SD_CS) + sdcard = adafruit_sdcard.SDCard(spi, cs) + vfs = storage.VfsFat(sdcard) + storage.mount(vfs, "/sd") # /sd is root dir of SD Card + print("Taking Screenshot... ") save_pixels("/sd/screenshot.bmp") print("Screenshot Saved") diff --git a/examples/bitmapsaver_screenshot_tft_featherwing.py b/examples/bitmapsaver_screenshot_tft_featherwing.py index ff6f36f..a715f89 100644 --- a/examples/bitmapsaver_screenshot_tft_featherwing.py +++ b/examples/bitmapsaver_screenshot_tft_featherwing.py @@ -1,7 +1,8 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries +# SPDX-FileCopyrightText: 2023 DJDevon3 # SPDX-License-Identifier: MIT -"""Screenshot on a 3.5" TFT Featherwing (integrated SD Card)""" +""" Screenshot on a 3.5" TFT Featherwing (integrated SD Card) """ # pylint:disable=invalid-name + import board import digitalio import displayio @@ -18,24 +19,23 @@ TAKE_SCREENSHOT = False # Set to True to take a screenshot -# Initialize Protocol Busses and SD Card +# Initialize SPI Bus spi = board.SPI() -cs = digitalio.DigitalInOut(board.D5) -sdcard = adafruit_sdcard.SDCard(spi, cs) -vfs = storage.VfsFat(sdcard) -displayio.release_displays() -# Setup Pinouts according to your feather board +# Initialize TFT Featherwing Display tft_cs = board.D9 tft_dc = board.D10 display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) display = HX8357(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT) -# Mount Virtual File System -virtual_root = "/sd" -storage.mount(vfs, virtual_root) - if TAKE_SCREENSHOT: + # Initialize SD Card & Mount Virtual File System + cs = digitalio.DigitalInOut(board.D5) + sdcard = adafruit_sdcard.SDCard(spi, cs) + vfs = storage.VfsFat(sdcard) + virtual_root = "/sd" # /sd is root dir of SD Card + storage.mount(vfs, virtual_root) + print("Taking Screenshot... ") save_pixels("/sd/screenshot.bmp", display) print("Screenshot Saved") diff --git a/examples/bitmapsaver_simpletest.py b/examples/bitmapsaver_simpletest.py index 5273420..df624be 100644 --- a/examples/bitmapsaver_simpletest.py +++ b/examples/bitmapsaver_simpletest.py @@ -1,8 +1,7 @@ # SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries # SPDX-License-Identifier: MIT - - """Example of using save_bitmap""" +# pylint:disable=invalid-name import board import busio @@ -12,16 +11,7 @@ import storage from adafruit_bitmapsaver import save_pixels -# pylint:disable=invalid-name - -TAKE_SCREENSHOT = False # Set to True to take a screenshot - -print("Setting up SD card") -spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) -cs = digitalio.DigitalInOut(board.SD_CS) -sdcard = adafruit_sdcard.SDCard(spi, cs) -vfs = storage.VfsFat(sdcard) -storage.mount(vfs, "/sd") +TAKE_SCREENSHOT = False # Set True to take a screenshot WHITE = 0xFFFFFF BLACK = 0x000000 @@ -53,6 +43,14 @@ bitmap[x, y] = 0 if TAKE_SCREENSHOT: + # Initialize SD Card & Mount Virtual File System + print("Setting up SD card") + spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) + cs = digitalio.DigitalInOut(board.SD_CS) + sdcard = adafruit_sdcard.SDCard(spi, cs) + vfs = storage.VfsFat(sdcard) + storage.mount(vfs, "/sd") # /sd is root dir of SD Card + print("Taking Screenshot... ") save_pixels("/sd/screenshot.bmp", bitmap, palette) print("Screenshot Saved") From b05534e2d959203dd1ece1bedfd296816359e35f Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:23:37 -0400 Subject: [PATCH 4/4] required to run black again --- examples/bitmapsaver_screenshot_tft_featherwing.py | 2 +- examples/bitmapsaver_simpletest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/bitmapsaver_screenshot_tft_featherwing.py b/examples/bitmapsaver_screenshot_tft_featherwing.py index a715f89..d279fb4 100644 --- a/examples/bitmapsaver_screenshot_tft_featherwing.py +++ b/examples/bitmapsaver_screenshot_tft_featherwing.py @@ -35,7 +35,7 @@ vfs = storage.VfsFat(sdcard) virtual_root = "/sd" # /sd is root dir of SD Card storage.mount(vfs, virtual_root) - + print("Taking Screenshot... ") save_pixels("/sd/screenshot.bmp", display) print("Screenshot Saved") diff --git a/examples/bitmapsaver_simpletest.py b/examples/bitmapsaver_simpletest.py index df624be..d4ecb75 100644 --- a/examples/bitmapsaver_simpletest.py +++ b/examples/bitmapsaver_simpletest.py @@ -50,7 +50,7 @@ sdcard = adafruit_sdcard.SDCard(spi, cs) vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") # /sd is root dir of SD Card - + print("Taking Screenshot... ") save_pixels("/sd/screenshot.bmp", bitmap, palette) print("Screenshot Saved")