Skip to content

Commit 022c549

Browse files
Add self-extracting data files for frozen qwiic modules
1 parent a10cef3 commit 022c549

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

sparkfun_build.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,74 @@ function download_qwiic_release {
1616
cp -r qwiic-py-py-${LATEST_RELEASE}/lib/* $1
1717
}
1818

19+
# Creates a frozen data filesystem for micropython using the freezefs python package
20+
# This will search the qwiic directory for any modules that have data files that need to be frozen via firmware and extracted with boot.py or main.py
21+
# Options:
22+
# $1: Qwiic directory
23+
# $2: Output py file of frozen data
24+
# $3: Ignored modules (optional, default: none)
25+
function create_frozen_data_fs {
26+
local IGNORED_MODULES=${3:-""}
27+
28+
# Add the freezefs python package for creating self-extracting/self-mounting archives for micropython
29+
pip install freezefs
30+
31+
# create our "_frozen_data" directory
32+
local FROZEN_DATA_DIR="_frozen_data"
33+
mkdir ${FROZEN_DATA_DIR}
34+
35+
# Iterate over all of the folders in the qwiic directory and check if they have another directory inside them
36+
# This represents that they have data files that we need to freeze with freezefs
37+
# Ignore the modules passed in the IGNORED_MODULES option
38+
for module in $(find $1 -mindepth 1 -maxdepth 1 -type d | grep -vE "${IGNORED_MODULES}"); do
39+
# Check if the module has a top-level directory inside it
40+
for data_dir in $(find $module -mindepth 1 -maxdepth 1 -type d); do
41+
# If it does, we will freeze the data directory
42+
echo "Freezing data for module: $data_dir"
43+
44+
# Copy the data directory to the _frozen_data directory that we will freeze with freezefs
45+
# If the data directory name is already used in the _frozen_data directory, we'll prepend the module name to the directory when we copy it
46+
if [ -d "${FROZEN_DATA_DIR}/$(basename $data_dir)" ]; then
47+
cp -r $data_dir ${FROZEN_DATA_DIR}/$(basename $module)_$(basename $data_dir)
48+
else
49+
cp -r $data_dir ${FROZEN_DATA_DIR}/$(basename $data_dir)
50+
fi
51+
done
52+
done
53+
54+
# Now we will use freezefs to create a self-extracting archive from the _frozen_data directory
55+
echo "Creating self-extracting archive from ${FROZEN_DATA_DIR}"
56+
python -m freezefs ${FROZEN_DATA_DIR} $2
57+
if [ $? -ne 0 ]; then
58+
echo "Error creating frozen data filesystem. Please check the freezefs documentation for more information."
59+
exit 1
60+
fi
61+
}
62+
63+
# Adds the frozen data filesystem to the boot.py file for the given port
64+
# Options:
65+
# $1: Port name
66+
# $2: Frozen data file path
67+
function add_frozen_data_to_boot_for_port {
68+
local TARGET_PORT_NAME=$1
69+
local FROZEN_DATA_FILE=$2
70+
71+
# Remove the ".py" extension from the frozen data file
72+
local FROZEN_DATA_BASENAME=$(basename $FROZEN_DATA_FILE .py)
73+
74+
# Check if the _boot.py file exists in the port's modules directory and error out if it does not
75+
if [ ! -f ports/${TARGET_PORT_NAME}/modules/_boot.py ]; then
76+
echo "Error: _boot.py file not found in ports/${TARGET_PORT_NAME}/modules/"
77+
exit 1
78+
fi
79+
80+
# Add the frozen data filesystem to the _boot.py file
81+
echo "Adding frozen data filesystem to _boot.py for port ${TARGET_PORT_NAME}"
82+
echo "import ${FROZEN_DATA_BASENAME}" >> ports/${TARGET_PORT_NAME}/modules/_boot.py
83+
echo "Content of _boot.py after adding frozen data filesystem:"
84+
cat ports/${TARGET_PORT_NAME}/modules/_boot.py
85+
}
86+
1987
# Builds all SparkFun boards for the given port
2088
# Options:
2189
# $1: Port name
@@ -150,6 +218,8 @@ function add_qwiic_manifest {
150218
local BOARD_PREFIX=$3 # The prefix of the SparkFun board directories (e.g. SPARKFUN_)
151219
local MPCONFIG_FILE="${4:-mpconfigboard.cmake}" # The file to add the frozen manifest line to (e.g. mpconfigboard.cmake or mpconfigboard.mk. )
152220

221+
echo "Called add_qwiic_manifest with $QWIIC_DIRECTORY, $BOARD_DIRECTORY, $BOARD_PREFIX"
222+
153223
for board in $(find ${BOARD_DIRECTORY} -type d -name "${BOARD_PREFIX}*"); do
154224
echo "Adding Qwiic drivers to manifest.py for $board"
155225
if [ ! -f ${board}/manifest.py ]; then
@@ -174,6 +244,8 @@ function add_qwiic_manifest {
174244
echo "Adding freeze line to manifest.py for $board"
175245
printf "\nfreeze(\"${QWIIC_DIRECTORY}\")" >> ${board}/manifest.py
176246

247+
248+
177249
echo "Manifest.py for $board:"
178250
cat ${board}/manifest.py
179251
done
@@ -229,6 +301,16 @@ function build_sparkfun {
229301
# Perform Qwiic download
230302
download_qwiic_release ${QWIIC_DIRECTORY}
231303

304+
# Create the frozen (data) filesystem for micropython (for non .py files)
305+
# Ignore modules we don't care about the data of (or that have unnecessary files that seem like data files)
306+
# For now, we are doing this for only the qwiic_vl53l5cx module to cut down the size for testing
307+
create_frozen_data_fs ${QWIIC_DIRECTORY} "${QWIIC_DIRECTORY}/_frozen_qwiic_data.py" "qwiic_vl53l5cx"
308+
309+
# Add the frozen (data) filesystem to the boot.py file for each port
310+
add_frozen_data_to_boot_for_port "esp32" "${QWIIC_DIRECTORY}/_frozen_qwiic_data.py"
311+
add_frozen_data_to_boot_for_port "rp2" "${QWIIC_DIRECTORY}/_frozen_qwiic_data.py"
312+
add_frozen_data_to_boot_for_port "mimxrt" "${QWIIC_DIRECTORY}/_frozen_qwiic_data.py"
313+
232314
# This is an ugly way to pass the qwiic path. Should make it cleaner than a relative path...
233315
# Add the downloaded Qwiic drivers to the manifest.py for each esp32 board
234316
add_qwiic_manifest "../../../../${QWIIC_DIRECTORY}" "ports/esp32/boards" "SPARKFUN_"

0 commit comments

Comments
 (0)