-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·284 lines (239 loc) · 7.06 KB
/
dev.sh
File metadata and controls
executable file
·284 lines (239 loc) · 7.06 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# OMI Development Helper Script
# Builds, flashes to both ESP32 boards, and monitors both outputs
set -e # Exit on error
# Configuration
BOARD1_PORT="/dev/ttyACM0"
BOARD2_PORT="/dev/ttyACM1"
BAUD_RATE=115200
# Colors for output
COLOR_RESET="\033[0m"
COLOR_GREEN="\033[0;32m"
COLOR_BLUE="\033[0;34m"
COLOR_YELLOW="\033[0;33m"
COLOR_RED="\033[0;31m"
COLOR_CYAN="\033[0;36m"
# Function to print colored messages
print_msg() {
echo -e "${2}${1}${COLOR_RESET}"
}
# Function to check if devices exist
check_devices() {
print_msg "Checking for ESP32 devices..." "$COLOR_CYAN"
if [ ! -e "$BOARD1_PORT" ]; then
print_msg "ERROR: Board 1 not found at $BOARD1_PORT" "$COLOR_RED"
exit 1
fi
if [ ! -e "$BOARD2_PORT" ]; then
print_msg "ERROR: Board 2 not found at $BOARD2_PORT" "$COLOR_RED"
exit 1
fi
print_msg "✓ Both boards detected" "$COLOR_GREEN"
}
# Function to setup ESP-IDF environment
setup_idf() {
print_msg "Setting up ESP-IDF environment..." "$COLOR_CYAN"
if [ -f "$HOME/esp/esp-idf/export.sh" ]; then
source "$HOME/esp/esp-idf/export.sh" > /dev/null 2>&1
print_msg "✓ ESP-IDF environment loaded" "$COLOR_GREEN"
else
print_msg "ERROR: ESP-IDF not found at $HOME/esp/esp-idf" "$COLOR_RED"
exit 1
fi
}
# Function to build the project
build_project() {
print_msg "Building project..." "$COLOR_CYAN"
idf.py build
print_msg "✓ Build complete" "$COLOR_GREEN"
}
# Function to flash a board
flash_board() {
local port=$1
local board_name=$2
print_msg "Flashing $board_name ($port)..." "$COLOR_YELLOW"
idf.py -p "$port" flash
print_msg "✓ $board_name flashed successfully" "$COLOR_GREEN"
}
# Function to reset a board via RTS/DTR toggle
reset_board() {
local port=$1
local board_name=$2
print_msg "Resetting $board_name ($port)..." "$COLOR_YELLOW"
# Use python to toggle DTR which triggers ESP32 reset
python3 -c "
import serial
import time
try:
s = serial.Serial('$port', $BAUD_RATE)
s.dtr = False
s.rts = True
time.sleep(0.1)
s.dtr = False
s.rts = False
time.sleep(0.1)
s.close()
except Exception as e:
pass # Ignore errors, board may still reset
" 2>/dev/null || true
print_msg "✓ $board_name reset" "$COLOR_GREEN"
}
# Function to reset both boards
reset_both() {
print_msg "Resetting both boards..." "$COLOR_CYAN"
reset_board "$BOARD1_PORT" "Board 1"
reset_board "$BOARD2_PORT" "Board 2"
sleep 1 # Give boards time to reboot
}
# Function to monitor both boards
monitor_both() {
local skip_reset=${1:-false}
print_msg "Starting dual monitor (Ctrl+C to exit)..." "$COLOR_CYAN"
echo ""
# Kill any existing monitor processes on these ports
pkill -f "cat $BOARD1_PORT" 2>/dev/null || true
pkill -f "cat $BOARD2_PORT" 2>/dev/null || true
# Reset both boards before monitoring (unless skipped)
if [ "$skip_reset" != "true" ]; then
reset_both
fi
# Use a named pipe approach for more reliable serial monitoring
# This handles device disconnects/reconnects better
local tmp_dir=$(mktemp -d)
local fifo1="$tmp_dir/board1"
local fifo2="$tmp_dir/board2"
mkfifo "$fifo1" "$fifo2"
# Cleanup function
cleanup() {
kill $cat_pid1 $cat_pid2 $read_pid1 $read_pid2 2>/dev/null || true
rm -rf "$tmp_dir"
exit 0
}
trap cleanup INT TERM EXIT
# Start serial readers that auto-reconnect
(
while true; do
if [ -e "$BOARD1_PORT" ]; then
stty -F "$BOARD1_PORT" "$BAUD_RATE" raw -echo 2>/dev/null || true
cat "$BOARD1_PORT" 2>/dev/null
fi
sleep 0.5
done
) > "$fifo1" &
cat_pid1=$!
(
while true; do
if [ -e "$BOARD2_PORT" ]; then
stty -F "$BOARD2_PORT" "$BAUD_RATE" raw -echo 2>/dev/null || true
cat "$BOARD2_PORT" 2>/dev/null
fi
sleep 0.5
done
) > "$fifo2" &
cat_pid2=$!
# Read from FIFOs and colorize output
(
while IFS= read -r line; do
echo -e "${COLOR_BLUE}[BOARD1]${COLOR_RESET} $line"
done < "$fifo1"
) &
read_pid1=$!
(
while IFS= read -r line; do
echo -e "${COLOR_GREEN}[BOARD2]${COLOR_RESET} $line"
done < "$fifo2"
) &
read_pid2=$!
# Wait for any child to exit (shouldn't happen normally)
wait
}
# Function to show usage
show_usage() {
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " all - Build, flash both boards, and monitor (default)"
echo " build - Build only"
echo " flash - Flash both boards (no build)"
echo " flash1 - Flash board 1 only"
echo " flash2 - Flash board 2 only"
echo " monitor - Reset and monitor both boards"
echo " monitor-no-reset - Monitor without resetting"
echo " reset - Reset both boards"
echo " clean - Clean build directory and sdkconfig"
echo " help - Show this help"
echo ""
echo "Configuration:"
echo " Board 1: $BOARD1_PORT"
echo " Board 2: $BOARD2_PORT"
echo " Baud: $BAUD_RATE"
}
# Main script
main() {
local command="${1:-all}"
print_msg "=== OMI Development Script ===" "$COLOR_CYAN"
echo ""
case "$command" in
all)
check_devices
setup_idf
build_project
echo ""
flash_board "$BOARD1_PORT" "Board 1"
echo ""
flash_board "$BOARD2_PORT" "Board 2"
echo ""
sleep 2 # Give boards time to boot
monitor_both "true" # Skip reset since flash already resets
;;
build)
setup_idf
build_project
;;
flash)
check_devices
setup_idf
flash_board "$BOARD1_PORT" "Board 1"
echo ""
flash_board "$BOARD2_PORT" "Board 2"
;;
flash1)
check_devices
setup_idf
flash_board "$BOARD1_PORT" "Board 1"
;;
flash2)
check_devices
setup_idf
flash_board "$BOARD2_PORT" "Board 2"
;;
monitor)
check_devices
monitor_both
;;
monitor-no-reset)
check_devices
monitor_both "true"
;;
reset)
check_devices
reset_both
;;
clean)
print_msg "Cleaning build..." "$COLOR_YELLOW"
rm -rf build sdkconfig
print_msg "✓ Clean complete" "$COLOR_GREEN"
;;
help|--help|-h)
show_usage
;;
*)
print_msg "ERROR: Unknown command '$command'" "$COLOR_RED"
echo ""
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"