Skip to content

Conversation

asiemsen
Copy link
Contributor

@asiemsen asiemsen commented Aug 6, 2025

Summary

Created SDCardManager class to initialize and mount sd card, as well as adding functionality to the logger in order to set a log path for the sd card. The log path is set using a setter within the logger and checks that the directory exists before writing.

This PR also creates a new boot.py function that creates set mount points on the root of the file system.

How was this tested

  • Added new unit tests
  • Ran code on hardware (screenshots are helpful)
  • Other (Please describe)

boot_out.txt first run:

Adafruit CircuitPython 9.2.8 on 2025-05-28; PROVES Kit v4 with rp2040
Board ID:proveskit_rp2040_v4
UID:E4639C3563112E2D
boot.py output:
Disabled USB drive
Remounted root filesystem
Mount point /poke created.
Enabled USB drive

boot_out.txt subsequent runs:

Adafruit CircuitPython 9.2.8 on 2025-05-28; PROVES Kit v4 with rp2040
Board ID:proveskit_rp2040_v4
UID:E4639C3563112E2D
boot.py output:
Disabled USB drive
Remounted root filesystem
Mount point /poke already exists.
Enabled USB drive

Demonstrating logging to file:
Captura de pantalla 2025-09-01 a la(s) 19 03 31

Copy link
Member

@nateinaction nateinaction left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start! Congrats getting this PR up, I know it's been in the making for a while!

I've made some suggestions that I hope will improve separation of concerns between the SD Card Manager and the Logger classes. Let me know what you think!

chip_select: Pin,
baudrate: int = 400000,
mount_path: str = "/sd",
mounted: bool = False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A caller probably shouldn't be able to set the mounted state when creating an SD()

mount_path: str = "/sd",
mounted: bool = False,
log_size: int = 50000, # 50 kb
log_rotate: int = 10,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not including log rotation in this PR, let's remove all mention of it.

log_rotate: int = 10,
) -> None:
self.mounted = mounted
self.log_size = log_size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with log size

Comment on lines 40 to 41
else:
self.mounted = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this appears to be valid it's also unusual to see a try/else. How about just putting this at the end of try?

Comment on lines 43 to 45
if "logs" not in os.listdir("/sd"):
print("/sd/logs does not exist, creating...")
os.mkdir("/sd/logs")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SD is our generic manager for handling SD card mounting, the specific usage of the SD card by the logger should likely live in the logger.

Comment on lines 39 to 41
print("error mounting sd card", e)
else:
self.mounted = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the concept of mounted=True let's raise a hardware initialization error like other managers.

def __init__(
self,
error_counter: Counter,
sd_card: SDCardManager = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was wrong about this. Instead of accepting an SDCard here we should accept a path. If a path is set, we should verify it exists during the init if it doesn't raise an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Nate, first I just wanted to say thank you for all of your help so far with this PR! I'm finally about done getting it updated, but was having some trouble understanding the intentions of this recommendation. Because the sd card cannot be initialized before the logger, if the path is ever set on initialization (ex: sd_path = sd/logs) it can never exist, or if it does, the sd card will not be mounted to it which I'm not sure what the resulting behavior would be if we tried writing to that path. Maybe there could be a function within the logger to set the sd_path, verifying it exists when set? Another thing that I wasn't sure of was when verifying if it exists, would we then want to create the new directory and then raise an error, sort of similar to what is implemented now?:

if "logs" not in os.listdir("/sd"):
            print("/sd/logs does not exist, creating...")
            os.mkdir("/sd/logs")

Not sure if I am just misunderstanding how the path works or just the implementation you are imagining with the comment.

Thanks again for all the help!

@nateinaction nateinaction mentioned this pull request Sep 1, 2025
3 tasks
@nateinaction nateinaction changed the title Sdcard logging Add option to log to file mount SD Card Sep 2, 2025
Copy link

sonarqubecloud bot commented Sep 3, 2025

@asiemsen asiemsen merged commit 07c921d into main Sep 3, 2025
4 checks passed
@asiemsen asiemsen deleted the sdcard_logging branch September 3, 2025 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants