Skip to content

Commit d17c636

Browse files
Class SDCardManager instead of procedural way
1 parent 0d46d9e commit d17c636

File tree

2 files changed

+62
-58
lines changed

2 files changed

+62
-58
lines changed

internal_filesystem/boot_fri3d-2024.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ def keypad_read_cb(indev, data):
263263
import mpos.battery_voltage
264264
mpos.battery_voltage.init_adc(13, 2 / 1000)
265265

266-
# SD card doesn't need to be plugged in at boot, can be plugged in later
267266
import mpos.sdcard
268-
mpos.sdcard.inform_sdcard(machine.SDCard(spi_bus=spi_bus,cs=14))
267+
mpos.sdcard.init(spi_bus, cs_pin=14)
269268

270269
print("boot.py finished")
Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,77 @@
11
import os
22
import machine
3+
import vfs
34

4-
sdcard = None
5+
class SDCardManager:
6+
def __init__(self, spi_bus, cs_pin):
7+
self._sdcard = None
8+
try:
9+
self._sdcard = machine.SDCard(spi_bus=spi_bus, cs=cs_pin)
10+
self._sdcard.info()
11+
print("SD card initialized")
12+
except Exception as e:
13+
print(f"ERROR: Failed to initialize SD card: {e}")
514

6-
# This gets called by (the device-specific) boot*.py
7-
def inform_sdcard(so):
8-
global sdcard
9-
sdcard = so
15+
def mount(self, mount_point):
16+
if not self._sdcard:
17+
return False
18+
try:
19+
os.mount(self._sdcard, mount_point)
20+
print(f"Mounted at {mount_point}")
21+
return True
22+
except OSError as e:
23+
print(f"Mount failed: {e}")
24+
return False
1025

11-
def format_sdcard(sdcard):
12-
"""Format the SD card with FAT filesystem"""
13-
try:
26+
def format_and_mount(self, mount_point):
27+
if not self._sdcard:
28+
return False
1429
print("Formatting SD card...")
15-
# Unmount if already mounted
1630
try:
17-
os.umount('/sdcard')
31+
os.umount(mount_point)
1832
except:
1933
pass
20-
21-
# Format the SD card (this erases all data!)
22-
sdcard.format()
23-
print("SD card formatted successfully")
24-
return True
25-
except Exception as e:
26-
print(f"WARNING: Failed to format SD card: {e}")
27-
return False
34+
try:
35+
vfs.VfsFat.mkfs(self._sdcard)
36+
print("Formatted")
37+
return self.mount(mount_point)
38+
except OSError as e:
39+
print(f"Format failed: {e}")
40+
return False
2841

29-
# Tries to mount and returns True if it worked
30-
def try_mount(sdcard):
31-
try:
32-
os.mount(sdcard, '/sdcard')
33-
print("SD card mounted successfully at /sdcard")
34-
return True
35-
except Exception as e:
36-
print(f"WARNING: failed to mount SD card: {e}")
37-
return False
42+
def is_mounted(self, mount_point):
43+
try:
44+
return mount_point in os.listdir('/') and os.path.ismount(mount_point)
45+
except:
46+
return False
3847

39-
# Returns True if everything successful, otherwise False if it failed to mount
40-
def mount_sdcard_optional_format():
41-
global sdcard # should have been initialized by inform_sdcard()
48+
def list(self, mount_point):
49+
try:
50+
return os.listdir(mount_point)
51+
except:
52+
return []
4253

43-
if not sdcard:
44-
print("WARNING: no machine.SDCard object has been initialized at boot, aborting...")
45-
return False
54+
# --- Global instance (singleton) ---
55+
_manager = None
4656

47-
# Try to detect SD card
48-
try:
49-
sdcard.info()
50-
print("SD card detected")
51-
except Exception as e:
52-
print(f"WARNING: sdcard.info() got exception: {e}")
53-
print("SD card not detected or hardware issue")
54-
return False
57+
def init(spi_bus, cs_pin):
58+
"""Initialize the global SD card manager."""
59+
global _manager
60+
if _manager is None:
61+
_manager = SDCardManager(spi_bus, cs_pin)
62+
return _manager
5563

56-
if not try_mount(sdcard):
57-
if format_sdcard(sdcard):
58-
if not try_mount(sdcard):
59-
print(f"WARNING: failed to mount SD card even after formatting, aborting...")
60-
return False
61-
else:
62-
print("WARNING: Could not format SD card - check hardware connections or reformat on a PC. Aborting...")
63-
return False
64+
def get():
65+
"""Get the global SD card manager instance."""
66+
return _manager
6467

65-
try:
66-
print("SD card contents:", os.listdir('/sdcard'))
67-
except Exception as e:
68-
print(f"WARNING: SD card did not fail to mount but could not list SD card contents: {e}")
69-
return False
68+
# Optional: convenience functions
69+
def mount(mount_point):
70+
mgr = get()
71+
return mgr and mgr.mount(mount_point)
7072

71-
print("SD card mounted successfully!")
72-
return True
73+
def mount_with_format(mount_point):
74+
mgr = get()
75+
if mgr and not mgr.mount(mount_point):
76+
return mgr.format_and_mount(mount_point)
77+
return mgr and mgr.is_mounted(mount_point)

0 commit comments

Comments
 (0)