Repository navigation
Expand file tree
/
Copy pathflash.py
More file actions
322 lines (260 loc) · 9.56 KB
/
Copy pathflash.py
File metadata and controls
322 lines (260 loc) · 9.56 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3
"""
ESP32 Firmware Flasher - Main Menu
Optimized for flashing multiple devices in sequence
"""
import os
import platform
import subprocess
import sys
from pathlib import Path
# =============================================================================
# Cross-platform colors
# =============================================================================
class Colors:
def __init__(self):
self.enabled = self._supports_color()
if self.enabled and platform.system() == "Windows":
self._enable_windows_ansi()
def _supports_color(self):
if os.getenv("NO_COLOR"):
return False
return hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
def _enable_windows_ansi(self):
try:
import ctypes
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
except:
self.enabled = False
def _w(self, code, text):
return f"\033[{code}m{text}\033[0m" if self.enabled else text
def red(self, t): return self._w("91", t)
def green(self, t): return self._w("92", t)
def yellow(self, t): return self._w("93", t)
def blue(self, t): return self._w("94", t)
def magenta(self, t): return self._w("95", t)
def cyan(self, t): return self._w("96", t)
def white(self, t): return self._w("97", t)
def dim(self, t): return self._w("90", t)
def bold(self, t): return self._w("1", t)
c = Colors()
SCRIPT_DIR = Path(__file__).parent.resolve()
# =============================================================================
# Utilities
# =============================================================================
def clear():
os.system('cls' if platform.system() == 'Windows' else 'clear')
def pause(msg="Press ENTER to continue..."):
try:
input(f"\n {c.dim(msg)}")
except:
pass
def list_ports():
"""List available serial ports."""
print(f"\n {c.cyan('Scanning for serial ports...')}\n")
ports = []
if platform.system() == "Windows":
try:
import serial.tools.list_ports
for p in serial.tools.list_ports.comports():
ports.append((p.device, p.description))
except ImportError:
print(c.yellow(" pyserial not installed. Run option [I] first."))
return
else:
dev = Path("/dev")
for pattern in ["cu.usbmodem*", "cu.usbserial-*", "ttyUSB*", "ttyACM*"]:
for p in dev.glob(pattern):
ports.append((str(p), ""))
if ports:
print(f" {c.green('Found:')}\n")
for device, desc in sorted(ports):
if desc:
print(f" {c.cyan(device):20} {c.dim(desc)}")
else:
print(f" {c.cyan(device)}")
else:
print(c.yellow(" No serial ports found. Connect a device and try again."))
def install_deps():
"""Install required packages."""
print(f"\n {c.cyan('Installing dependencies...')}\n")
pkgs = ["esptool>=4.7.0", "pyserial>=3.5", "meshtastic>=2.3.0"]
for pkg in pkgs:
print(f" Installing {pkg}...", end=" ", flush=True)
try:
subprocess.run([sys.executable, "-m", "pip", "install", pkg, "-q"],
check=True, timeout=120, capture_output=True)
print(c.green("OK"))
except:
print(c.red("FAILED"))
print(f"\n {c.green('Done!')}")
# =============================================================================
# Flasher Functions
# =============================================================================
def run_flasher(script_path, args=None):
"""Run a flasher script."""
if args is None:
args = []
if not script_path.exists():
print(c.red(f"\n Script not found: {script_path}"))
return False
cmd = [sys.executable, str(script_path)] + args
try:
result = subprocess.run(cmd)
return result.returncode == 0
except KeyboardInterrupt:
print(f"\n {c.yellow('Cancelled')}")
return False
except Exception as e:
print(c.red(f"\n Error: {e}"))
return False
def flash_loop(name, script_path, args=None):
"""Flash loop - keeps flashing until user exits."""
count = 0
while True:
clear()
print(f"""
{c.blue('=' * 58)}
{c.bold(c.white(f' {name.upper()} FLASH MODE'))}
{c.blue('=' * 58)}
{c.dim(f'Devices flashed this session: {c.cyan(str(count))}')}
{c.yellow('[ENTER]')} Flash next device
{c.yellow('[B]')} Back to main menu
{c.blue('-' * 58)}
""")
try:
choice = input(f" {c.green('>')} ").strip().upper()
except (EOFError, KeyboardInterrupt):
break
if choice == 'B':
break
# Any other input (including empty) = flash
print(f"\n {c.cyan('=' * 58)}")
print(f" {c.bold('FLASHING...')}")
print(f" {c.cyan('=' * 58)}\n")
success = run_flasher(script_path, args)
if success:
count += 1
print(f"\n {c.green('=' * 58)}")
print(f" {c.bold(c.green(f' SUCCESS! Total flashed: {count}'))}")
print(f" {c.green('=' * 58)}")
else:
print(f"\n {c.red('=' * 58)}")
print(f" {c.bold(c.red(' FLASH FAILED - Check connection and try again'))}")
print(f" {c.red('=' * 58)}")
print(f"\n {c.dim('Connect next device and press ENTER, or B to go back')}")
try:
choice = input(f" {c.green('>')} ").strip().upper()
if choice == 'B':
break
except:
break
return count
# =============================================================================
# Main Menu
# =============================================================================
def main_menu():
clear()
print(f"""
{c.blue('+' + '=' * 56 + '+')}
{c.blue('|')}{c.bold(c.white(' ESP32 FIRMWARE FLASHER'))}{' ' * 22}{c.blue('|')}
{c.blue('+' + '=' * 56 + '+')}
{c.dim(f'Platform: {platform.system()} | Python: {sys.version.split()[0]}')}
{c.cyan('-' * 58)}
{c.bold(' SELECT FIRMWARE:')}
{c.cyan('-' * 58)}
{c.yellow('[1]')} {c.bold('Meshtastic-Heltec-V3')}
{c.dim('Downloads latest stable, flashes & configures')}
{c.yellow('[2]')} {c.bold('OUI Spy Detector')}
{c.dim('Flashes local firmware')}
{c.yellow('[3]')} {c.bold('Mesh-Detect-RemoteID')}
{c.dim('Flashes local firmware')}
{c.cyan('-' * 58)}
{c.bold(' UTILITIES:')}
{c.cyan('-' * 58)}
{c.magenta('[P]')} List Serial Ports
{c.magenta('[I]')} Install Dependencies
{c.red('[Q]')} Quit
{c.cyan('-' * 58)}
""")
def check_and_install_deps():
"""Check for required packages and install if missing."""
missing = []
try:
import serial
except ImportError:
missing.append("pyserial")
try:
import esptool
except ImportError:
missing.append("esptool")
if missing:
print(f"\n {c.yellow('Missing dependencies:')} {', '.join(missing)}")
print(f" {c.cyan('Installing automatically...')}\n")
pkgs = ["esptool>=4.7.0", "pyserial>=3.5", "meshtastic>=2.3.0"]
for pkg in pkgs:
print(f" Installing {pkg}...", end=" ", flush=True)
try:
subprocess.run(
[sys.executable, "-m", "pip", "install", pkg, "-q"],
check=True, timeout=120, capture_output=True
)
print(c.green("OK"))
except Exception as e:
print(c.red("FAILED"))
print(f"\n {c.green('Dependencies installed!')}")
pause()
def main():
# Auto-install missing dependencies
check_and_install_deps()
while True:
main_menu()
try:
choice = input(f" {c.green('Enter choice:')} ").strip().upper()
except (EOFError, KeyboardInterrupt):
break
if choice == 'Q':
break
elif choice == '1':
total = flash_loop(
"Meshtastic-Heltec-V3",
SCRIPT_DIR / "flashers" / "flash_meshtastic.py",
["--device", "heltec-v3"]
)
if total > 0:
print(f"\n {c.green(f'Session complete. Flashed {total} device(s).')}")
pause()
elif choice == '2':
total = flash_loop(
"OUI Spy Detector",
SCRIPT_DIR / "flashers" / "flash_oui_detector.py"
)
if total > 0:
print(f"\n {c.green(f'Session complete. Flashed {total} device(s).')}")
pause()
elif choice == '3':
total = flash_loop(
"Mesh-Detect-RemoteID",
SCRIPT_DIR / "flashers" / "flash_remoteid.py"
)
if total > 0:
print(f"\n {c.green(f'Session complete. Flashed {total} device(s).')}")
pause()
elif choice == 'P':
list_ports()
pause()
elif choice == 'I':
install_deps()
pause()
else:
print(f"\n {c.red('Invalid choice')}")
pause()
clear()
print(f"\n {c.green('Goodbye!')}\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(f"\n\n {c.yellow('Interrupted')}")
sys.exit(0)