-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupload.sh
More file actions
executable file
·181 lines (155 loc) · 5.46 KB
/
upload.sh
File metadata and controls
executable file
·181 lines (155 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
# upload.sh - Upload script for virtual pet project
# Note, this installs boot.py on the device which is required for standalone operation
# However, when boot.py is installed, mpremote will struggl to get a REPL shell, which makes development difficult
# There are instructions in the README for how to remove boot.py so you can develop again
set -e # Exit on any error
echo "=== Virtual Pet Upload Script ==="
echo ""
# Configuration
DEVICE_PORT="${1:-}" # Optional: pass port as first argument
SRC_DIR="src"
BUILD_DIR="build"
# Color codes for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Check if source directory exists
if [ ! -d "$SRC_DIR" ]; then
echo -e "${RED}Error: $SRC_DIR directory not found!${NC}"
exit 1
fi
# Check if mpremote is available
if ! command -v mpremote &> /dev/null; then
echo -e "${RED}Error: mpremote not found. Install with: pip install mpremote${NC}"
exit 1
fi
# Check for mpy-cross
if ! command -v mpy-cross &> /dev/null; then
echo -e "${RED}Error: mpy-cross not found. Install with: pip install mpy-cross${NC}"
exit 1
fi
# Function to run mpremote with optional port
mp() {
if [ -n "$DEVICE_PORT" ]; then
mpremote connect "$DEVICE_PORT" "$@"
else
mpremote "$@"
fi
}
echo -e "${YELLOW}Step 1: Compiling .py to .mpy...${NC}"
echo " (skipping src/assets/ — frozen into firmware, not needed on filesystem)"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
while read -r pyfile; do
REL_PATH="${pyfile#$SRC_DIR/}"
MPY_PATH="$BUILD_DIR/${REL_PATH%.py}.mpy"
mkdir -p "$(dirname "$MPY_PATH")"
echo -n " Compiling $REL_PATH..."
if mpy-cross -march=xtensawin "$pyfile" -o "$MPY_PATH" 2>/tmp/mpy_cross_err; then
echo -e " ${GREEN}✓${NC}"
else
echo -e " ${RED}✗${NC}"
cat /tmp/mpy_cross_err
echo -e "${RED}Compilation failed. Fix the errors above and try again.${NC}"
exit 1
fi
done < <(find "$SRC_DIR" -name "*.py" -not -path "$SRC_DIR/assets/*")
echo -e "${GREEN}✓ Compilation complete${NC}"
echo ""
echo -e "${YELLOW}Converting level files...${NC}"
mkdir -p "$BUILD_DIR/platformer_levels"
for txt in levels/level_*.txt; do
name=$(basename "${txt%.txt}")
python3 tools/convert_level.py "$txt" "$name" "$BUILD_DIR/platformer_levels" --quiet
done
echo ""
echo "Step 2: Checking connection..."
if mp fs ls / > /dev/null 2>&1; then
echo -e "${GREEN}✓ Connected to device${NC}"
else
echo -e "${RED}✗ Failed to connect to device${NC}"
echo ""
echo " If boot.py is installed, hold A+B while running this script"
echo " to stay in REPL mode during the 1s startup window."
exit 1
fi
# Remove boot.py immediately so subsequent mpremote reconnects don't trigger
# a game start (no boot.py = device falls to REPL on any soft reset).
# It will be re-uploaded at the end of this script.
mp fs rm /boot.py 2>/dev/null || true
echo " (boot.py removed - will be restored at end)"
echo ""
echo "Step 3: Installing dependencies..."
mp mip install ssd1306
echo -e "${GREEN}✓ SSD1306 library installed${NC}"
echo ""
echo -e "${YELLOW}Step 4: Cleaning ALL files from device...${NC}"
echo " (keeping boot.py and lib/ directory for safety)"
# Function to recursively delete files and directories
clean_device() {
# mpremote fs ls / format: " NNN name" for files, " 0 name/" for directories
# The header line "ls :/" does not match the numeric prefix pattern and is skipped.
mp fs ls / | while read -r line; do
if [[ $line =~ ^[0-9]+[[:space:]]+(.+)$ ]]; then
name="${BASH_REMATCH[1]}"
if [[ $name == */ ]]; then
# It's a directory (trailing slash)
dirname="${name%/}"
if [[ "$dirname" != "lib" ]]; then
echo " Removing directory: /$dirname/"
mp fs rm -r "/$dirname" 2>/dev/null || true
fi
else
# It's a file
if [[ "$name" != "boot.py" ]] && [[ "$name" != "webrepl_cfg.py" ]] && [[ "$name" != "save.json" ]]; then
echo " Removing file: /$name"
mp fs rm "/$name" 2>/dev/null || true
fi
fi
fi
done
}
# Clean the device
clean_device
echo -e "${GREEN}✓ Device cleaned${NC}"
echo ""
echo "Step 5: Uploading compiled files..."
# Count files for progress
TOTAL_FILES=$(find $BUILD_DIR -type f \( -name "*.mpy" -o -name "*.bin" \) | wc -l)
CURRENT=0
# Upload all compiled .mpy files and binary data files
find $BUILD_DIR -type f \( -name "*.mpy" -o -name "*.bin" \) | while read -r file; do
CURRENT=$((CURRENT + 1))
# Get relative path and remove build/ prefix
REL_PATH="${file#$BUILD_DIR/}"
echo -n " [$CURRENT/$TOTAL_FILES] Uploading $REL_PATH..."
# Create directory if needed
DIR_PATH=$(dirname "/$REL_PATH")
if [ "$DIR_PATH" != "/" ]; then
mp fs mkdir "$DIR_PATH" 2>/dev/null || true
fi
# Upload file
mp fs cp "$file" ":/$REL_PATH"
echo -e " ${GREEN}✓${NC}"
done
echo ""
echo "Step 6: Verifying upload..."
echo "Root files:"
mp fs ls /
echo ""
if mp fs ls /scenes > /dev/null 2>&1; then
echo "Scenes directory:"
mp fs ls /scenes
fi
echo ""
echo "Step 7: Uploading boot.py..."
mp fs cp boot.py :boot.py
echo -e "${GREEN}✓ boot.py uploaded${NC}"
echo ""
echo "Step 8: Restarting device..."
mp reset
echo -e "${GREEN}✓ Device restarted${NC}"
echo ""
echo -e "${GREEN}=== Upload Complete! ===${NC}"