-
Notifications
You must be signed in to change notification settings - Fork 22
Add option to log to file mount SD Card #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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!
pysquared/sd_card.py
Outdated
chip_select: Pin, | ||
baudrate: int = 400000, | ||
mount_path: str = "/sd", | ||
mounted: bool = False, |
There was a problem hiding this comment.
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()
pysquared/sd_card.py
Outdated
mount_path: str = "/sd", | ||
mounted: bool = False, | ||
log_size: int = 50000, # 50 kb | ||
log_rotate: int = 10, |
There was a problem hiding this comment.
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.
pysquared/sd_card.py
Outdated
log_rotate: int = 10, | ||
) -> None: | ||
self.mounted = mounted | ||
self.log_size = log_size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with log size
pysquared/sd_card.py
Outdated
else: | ||
self.mounted = True |
There was a problem hiding this comment.
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?
pysquared/sd_card.py
Outdated
if "logs" not in os.listdir("/sd"): | ||
print("/sd/logs does not exist, creating...") | ||
os.mkdir("/sd/logs") |
There was a problem hiding this comment.
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.
pysquared/sd_card.py
Outdated
print("error mounting sd card", e) | ||
else: | ||
self.mounted = True |
There was a problem hiding this comment.
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.
pysquared/logger.py
Outdated
def __init__( | ||
self, | ||
error_counter: Counter, | ||
sd_card: SDCardManager = None, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
…le inside pysquared
|
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
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:
