-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_firmware.ps1
More file actions
668 lines (576 loc) · 23.1 KB
/
upload_firmware.ps1
File metadata and controls
668 lines (576 loc) · 23.1 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#
# SledLink Firmware Upload Script for Windows
# This script guides non-technical users through uploading firmware
# to the SledLink Judge or Sled Controller.
#
# Ensure we stop on errors
$ErrorActionPreference = "Stop"
# Get the directory where this script is located
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Determine if we're in a release package or local repo
# Release: C:\SledLink-v1.0.0\tools\upload_firmware.ps1 -> look for C:\SledLink-v1.0.0\source\arduino
# Repo: C:\GitRepos\SledLink\upload_firmware.ps1 -> look for C:\GitRepos\SledLink\arduino
# Check if we're at the repo root with source/arduino structure
$SourceArduinoPath = Join-Path (Join-Path $ScriptDir "source") "arduino"
if (Test-Path $SourceArduinoPath) {
# Release package structure with source/ subfolder
$ArduinoDir = $SourceArduinoPath
} elseif (Test-Path (Join-Path $ScriptDir "arduino")) {
# Local repo structure with arduino/ at root
$ArduinoDir = Join-Path $ScriptDir "arduino"
} else {
# Must be in tools/ subdirectory (release package)
$ReleaseRoot = Split-Path -Parent $ScriptDir
$ArduinoDir = Join-Path (Join-Path $ReleaseRoot "source") "arduino"
}
# Global variables
$script:SelectedPort = ""
$script:SelectedController = ""
$script:ArduinoCli = ""
#############################################################################
# Helper Functions
#############################################################################
function Write-Header {
Clear-Host
Write-Host ""
Write-Host "================================================================" -ForegroundColor Cyan
Write-Host " SledLink Firmware Upload Tool for Windows" -ForegroundColor White
Write-Host " For Tractor Pull Distance Measurement" -ForegroundColor Gray
Write-Host "================================================================" -ForegroundColor Cyan
Write-Host ""
}
function Write-Step {
param([string]$Text)
Write-Host ""
Write-Host "----------------------------------------------------------------" -ForegroundColor Blue
Write-Host $Text -ForegroundColor White
Write-Host "----------------------------------------------------------------" -ForegroundColor Blue
Write-Host ""
}
function Write-Success {
param([string]$Text)
Write-Host "[OK] $Text" -ForegroundColor Green
}
function Write-Error-Custom {
param([string]$Text)
Write-Host "[X] $Text" -ForegroundColor Red
}
function Write-Warning-Custom {
param([string]$Text)
Write-Host "[!] $Text" -ForegroundColor Yellow
}
function Write-Info {
param([string]$Text)
Write-Host ">>> $Text" -ForegroundColor Cyan
}
function Wait-ForEnter {
Write-Host ""
Write-Host "Press ENTER to continue..." -ForegroundColor Yellow
Read-Host
}
function Ask-YesNo {
param([string]$Prompt)
while ($true) {
$response = Read-Host "$Prompt (yes/no)"
switch -Regex ($response) {
"^[Yy](es)?$" { return $true }
"^[Nn]o?$" { return $false }
default { Write-Host "Please type 'yes' or 'no'" }
}
}
}
#############################################################################
# Arduino CLI Installation
#############################################################################
function Test-ArduinoCli {
Write-Step "Step 1: Checking for Arduino CLI"
# Check if arduino-cli is in PATH
$cli = Get-Command "arduino-cli" -ErrorAction SilentlyContinue
if ($cli) {
$script:ArduinoCli = $cli.Source
Write-Success "Arduino CLI found: $($script:ArduinoCli)"
return $true
}
# Check common installation locations
$commonPaths = @(
"$env:LOCALAPPDATA\Arduino15\arduino-cli.exe",
"$env:USERPROFILE\bin\arduino-cli.exe",
"$env:USERPROFILE\arduino-cli\arduino-cli.exe",
"C:\Program Files\Arduino CLI\arduino-cli.exe",
"C:\arduino-cli\arduino-cli.exe"
)
foreach ($path in $commonPaths) {
if (Test-Path $path) {
$script:ArduinoCli = $path
Write-Success "Arduino CLI found: $path"
return $true
}
}
Write-Warning-Custom "Arduino CLI is not installed."
Write-Host ""
return $false
}
function Install-ArduinoCli {
Write-Step "Installing Arduino CLI"
Write-Host "Arduino CLI is the tool that uploads firmware to the controller."
Write-Host "We need to install it on your computer."
Write-Host ""
if (-not (Ask-YesNo "Would you like to download and install Arduino CLI?")) {
return $false
}
Write-Host ""
Write-Info "Downloading Arduino CLI..."
# Create installation directory
$installDir = "$env:USERPROFILE\arduino-cli"
if (-not (Test-Path $installDir)) {
New-Item -ItemType Directory -Path $installDir | Out-Null
}
$zipPath = "$env:TEMP\arduino-cli.zip"
$downloadUrl = "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_64bit.zip"
try {
# Download the CLI
Write-Info "Downloading from Arduino..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing
# Extract the ZIP
Write-Info "Extracting files..."
Expand-Archive -Path $zipPath -DestinationPath $installDir -Force
# Clean up
Remove-Item $zipPath -ErrorAction SilentlyContinue
$script:ArduinoCli = Join-Path $installDir "arduino-cli.exe"
if (Test-Path $script:ArduinoCli) {
Write-Success "Arduino CLI installed to: $installDir"
Write-Host ""
Write-Warning-Custom "Note: You may want to add $installDir to your PATH"
Write-Warning-Custom " for easier use in the future."
return $true
} else {
Write-Error-Custom "Installation completed but arduino-cli.exe not found"
return $false
}
}
catch {
Write-Error-Custom "Download failed: $_"
Write-Host ""
Write-Host "You can install Arduino CLI manually from:" -ForegroundColor Yellow
Write-Host "https://arduino.github.io/arduino-cli/installation/" -ForegroundColor Cyan
return $false
}
}
function Setup-ArduinoCli {
Write-Step "Step 2: Setting up Arduino CLI for ESP32"
Write-Host "Checking if ESP32 board support is installed..."
Write-Host ""
try {
# Initialize config if needed
$configPath = "$env:LOCALAPPDATA\Arduino15\arduino-cli.yaml"
if (-not (Test-Path $configPath)) {
Write-Info "Initializing Arduino CLI configuration..."
& $script:ArduinoCli config init 2>$null
}
# Check if ESP32 URL is already added
$configOutput = & $script:ArduinoCli config dump 2>&1 | Out-String
if ($configOutput -notmatch "espressif") {
Write-Info "Adding ESP32 board support URL..."
& $script:ArduinoCli config add board_manager.additional_urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
}
# Check if ESP32 core is installed
$coreList = & $script:ArduinoCli core list 2>&1 | Out-String
if ($coreList -notmatch "esp32:esp32") {
Write-Info "Installing ESP32 board support (this may take several minutes)..."
Write-Host ""
Write-Host "Please wait - downloading ESP32 tools and libraries..." -ForegroundColor Yellow
& $script:ArduinoCli core update-index
& $script:ArduinoCli core install esp32:esp32
Write-Success "ESP32 board support installed!"
} else {
Write-Success "ESP32 board support is already installed."
}
# Check if LiquidCrystal library is installed
$libList = & $script:ArduinoCli lib list 2>&1 | Out-String
if ($libList -notmatch "LiquidCrystal") {
Write-Info "Installing LCD library..."
& $script:ArduinoCli lib install "LiquidCrystal"
Write-Success "LCD library installed!"
} else {
Write-Success "LCD library is already installed."
}
Write-Host ""
Write-Success "Arduino CLI is fully configured!"
return $true
}
catch {
Write-Error-Custom "Setup failed: $_"
return $false
}
}
#############################################################################
# Controller Selection
#############################################################################
function Select-Controller {
Write-Step "Step 3: Select Controller Type"
Write-Host "Which controller do you want to upload firmware to?"
Write-Host ""
Write-Host " 1) SLED Controller" -ForegroundColor White
Write-Host " - This goes ON THE SLED" -ForegroundColor Gray
Write-Host " - Has the measuring wheel encoder attached" -ForegroundColor Gray
Write-Host " - Sends distance to the judge" -ForegroundColor Gray
Write-Host ""
Write-Host " 2) JUDGE Controller" -ForegroundColor White
Write-Host " - This stays at the JUDGE'S TABLE" -ForegroundColor Gray
Write-Host " - Receives and displays distance" -ForegroundColor Gray
Write-Host " - No encoder attached" -ForegroundColor Gray
Write-Host ""
while ($true) {
$choice = Read-Host "Enter 1 for SLED or 2 for JUDGE"
switch ($choice) {
"1" {
$script:SelectedController = "SledController"
Write-Success "Selected: SLED Controller"
return
}
"2" {
$script:SelectedController = "JudgeController"
Write-Success "Selected: JUDGE Controller"
return
}
default {
Write-Error-Custom "Please enter 1 or 2"
}
}
}
}
#############################################################################
# Port Detection
#############################################################################
function Get-SerialPorts {
# Get COM ports using .NET which is most reliable
$ports = @()
try {
# Get all available COM ports
$comPorts = [System.IO.Ports.SerialPort]::GetPortNames()
if ($comPorts -and $comPorts.Count -gt 0) {
# Try to get friendly names from WMI
try {
$devices = Get-WmiObject Win32_PnPEntity | Where-Object { $_.Name -match "COM\d+" }
$deviceMap = @{}
foreach ($device in $devices) {
if ($device.Name -match "(COM\d+)") {
$deviceMap[$Matches[1]] = $device.Name
}
}
} catch {
$deviceMap = @{}
}
# Create port objects with both Port and Name
foreach ($comPort in $comPorts) {
$displayName = if ($deviceMap.ContainsKey($comPort)) {
$deviceMap[$comPort]
} else {
$comPort
}
$ports += @{
Port = $comPort
Name = $displayName
}
}
}
} catch {}
# If no ports found, try WMI as fallback
if ($ports.Count -eq 0) {
try {
$devices = Get-WmiObject Win32_PnPEntity | Where-Object { $_.Name -match "COM\d+" }
foreach ($device in $devices) {
if ($device.Name -match "(COM\d+)") {
$ports += @{
Port = $Matches[1]
Name = $device.Name
}
}
}
} catch {}
}
return , $ports
}
function Select-Port {
Write-Step "Step 4: Connect the Controller"
Write-Host "Now we need to connect to the controller."
Write-Host ""
Write-Host "Instructions:" -ForegroundColor White
Write-Host " 1. Get a USB cable (micro-USB for most ESP32 boards)"
Write-Host " 2. Plug one end into the controller board"
Write-Host " 3. Plug the other end into this computer"
Write-Host ""
Write-Host "Windows Users:" -ForegroundColor Yellow
Write-Host " If this is your first time connecting an ESP32, Windows" -ForegroundColor Gray
Write-Host " may need to install a driver. This usually happens" -ForegroundColor Gray
Write-Host " automatically. Wait a few seconds after plugging in." -ForegroundColor Gray
Write-Host ""
Write-Host " If no port appears, you may need to install a driver from:" -ForegroundColor Gray
Write-Host " https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers" -ForegroundColor Cyan
Write-Host ""
while ($true) {
if (Ask-YesNo "Is the controller connected via USB?") {
Write-Host ""
Write-Info "Scanning for connected devices..."
Write-Host ""
$ports = Get-SerialPorts
if ($ports.Count -eq 0) {
Write-Error-Custom "No serial devices found!"
Write-Host ""
Write-Host "Possible issues:" -ForegroundColor Yellow
Write-Host " - USB cable might be charge-only (try a different cable)"
Write-Host " - USB driver might not be installed"
Write-Host " - Try a different USB port on your computer"
Write-Host " - Wait a few seconds for Windows to detect the device"
Write-Host ""
if (-not (Ask-YesNo "Would you like to try again?")) {
return $false
}
continue
}
if ($ports.Count -eq 1) {
# Access properties safely
if ($ports[0] -is [hashtable]) {
$port = $ports[0]['Port']
$name = $ports[0]['Name']
} else {
$port = $ports[0].Port
$name = $ports[0].Name
}
# Ensure we have at least the port number
if (-not $port) {
$port = "COM?"
}
$script:SelectedPort = $port
# Show port and name for clarity
if ($name -and $name -ne $port) {
Write-Success "Found device: $port - $name"
} else {
Write-Success "Found device: $port"
}
Write-Host ""
if (Ask-YesNo "Use this device?") {
return $true
}
} else {
Write-Host "Multiple devices found:" -ForegroundColor White
Write-Host ""
for ($i = 0; $i -lt $ports.Count; $i++) {
# Access properties safely
if ($ports[$i] -is [hashtable]) {
$port = $ports[$i]['Port']
$name = $ports[$i]['Name']
} else {
$port = $ports[$i].Port
$name = $ports[$i].Name
}
# Ensure we have at least the port number
if (-not $port) {
$port = "COM?"
}
# Show port and name separately for clarity
if ($name -and $name -ne $port) {
Write-Host " $($i + 1)) $port - $name"
} else {
Write-Host " $($i + 1)) $port"
}
}
Write-Host ""
while ($true) {
$choice = Read-Host "Enter the number of the device to use"
$choiceNum = 0
if ([int]::TryParse($choice, [ref]$choiceNum) -and $choiceNum -ge 1 -and $choiceNum -le $ports.Count) {
$script:SelectedPort = $ports[$choiceNum - 1].Port
Write-Success "Selected: $($ports[$choiceNum - 1].Name)"
return $true
} else {
Write-Error-Custom "Please enter a number between 1 and $($ports.Count)"
}
}
}
} else {
Write-Host ""
Write-Host "Please connect the controller and try again."
Write-Host ""
}
}
}
#############################################################################
# Firmware Upload
#############################################################################
function Upload-Firmware {
Write-Step "Step 5: Upload Firmware"
$firmwarePath = Join-Path $ArduinoDir $script:SelectedController
if (-not (Test-Path $firmwarePath)) {
Write-Error-Custom "Firmware folder not found: $firmwarePath"
return $false
}
Write-Host "Ready to upload firmware:"
Write-Host ""
Write-Host " Controller: $($script:SelectedController)" -ForegroundColor White
Write-Host " Port: $($script:SelectedPort)" -ForegroundColor White
Write-Host " Firmware: $firmwarePath" -ForegroundColor Gray
Write-Host ""
Write-Host "IMPORTANT:" -ForegroundColor Yellow
Write-Host " - Do NOT disconnect the USB cable during upload"
Write-Host " - The upload takes about 30-60 seconds"
Write-Host " - The controller will restart automatically when done"
Write-Host ""
if (-not (Ask-YesNo "Start the upload?")) {
Write-Warning-Custom "Upload cancelled."
return $false
}
Write-Host ""
Write-Info "Compiling firmware (this may take a minute)..."
Write-Host ""
try {
# Compile the firmware
$compileOutput = & $script:ArduinoCli compile --fqbn esp32:esp32:esp32 $firmwarePath 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error-Custom "Compilation failed!"
Write-Host $compileOutput -ForegroundColor Red
Write-Host ""
Write-Host "This might mean there's a problem with the firmware files."
Write-Host "Please contact support for help."
return $false
}
Write-Host ""
Write-Success "Compilation successful!"
Write-Host ""
Write-Info "Uploading to controller..."
Write-Host ""
# Upload the firmware
$uploadOutput = & $script:ArduinoCli upload -p $script:SelectedPort --fqbn esp32:esp32:esp32 $firmwarePath 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error-Custom "Upload failed!"
Write-Host $uploadOutput -ForegroundColor Red
Write-Host ""
Write-Host "Possible issues:" -ForegroundColor Yellow
Write-Host " - Try unplugging and replugging the USB cable"
Write-Host " - Try holding the BOOT button on the ESP32 during upload"
Write-Host " - Try a different USB cable"
Write-Host " - Make sure no other program is using the serial port"
return $false
}
Write-Host ""
Write-Host "========================================================" -ForegroundColor Green
Write-Host " FIRMWARE UPLOAD COMPLETE!" -ForegroundColor Green
Write-Host "========================================================" -ForegroundColor Green
Write-Host ""
return $true
}
catch {
Write-Error-Custom "Error during upload: $_"
return $false
}
}
#############################################################################
# Post-Upload Options
#############################################################################
function Show-PostUploadMenu {
Write-Step "What would you like to do next?"
Write-Host " 1) Upload firmware to another controller"
Write-Host " 2) View serial output (for troubleshooting)"
Write-Host " 3) Exit"
Write-Host ""
while ($true) {
$choice = Read-Host "Enter your choice (1-3)"
switch ($choice) {
"1" { return "continue" }
"2" {
Show-SerialOutput
return "continue"
}
"3" { return "exit" }
default {
Write-Error-Custom "Please enter 1, 2, or 3"
}
}
}
}
function Show-SerialOutput {
Write-Step "Serial Monitor"
Write-Host "This will show you the output from the controller."
Write-Host "This is useful for:"
Write-Host " - Checking the MAC address (for pairing)"
Write-Host " - Verifying the controller is working"
Write-Host " - Troubleshooting problems"
Write-Host ""
Write-Host "Press Ctrl+C to stop the serial monitor" -ForegroundColor Yellow
Write-Host ""
if (Ask-YesNo "Start serial monitor?") {
Write-Host ""
Write-Info "Starting serial monitor at 115200 baud..."
Write-Host "-----------------------------------------------------" -ForegroundColor Cyan
try {
& $script:ArduinoCli monitor -p $script:SelectedPort -c baudrate=115200
} catch {}
Write-Host "-----------------------------------------------------" -ForegroundColor Cyan
Write-Host ""
Write-Info "Serial monitor closed."
}
}
#############################################################################
# Main Program
#############################################################################
function Main {
Write-Header
Write-Host "This tool will help you upload firmware to your SledLink controller."
Write-Host "Just follow the prompts - no technical knowledge required!"
Write-Host ""
Wait-ForEnter
# Check for and install Arduino CLI if needed
if (-not (Test-ArduinoCli)) {
if (-not (Install-ArduinoCli)) {
Write-Error-Custom "Cannot continue without Arduino CLI."
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
# Setup Arduino CLI (ESP32 support, libraries)
if (-not (Setup-ArduinoCli)) {
Write-Error-Custom "Failed to configure Arduino CLI."
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
Wait-ForEnter
# Main loop - allows uploading to multiple controllers
while ($true) {
# Select which controller to upload
Select-Controller
Wait-ForEnter
# Select serial port
if (-not (Select-Port)) {
Write-Error-Custom "Could not find a serial port."
if (-not (Ask-YesNo "Would you like to try again?")) {
break
}
continue
}
Wait-ForEnter
# Upload the firmware
if (Upload-Firmware) {
$result = Show-PostUploadMenu
if ($result -eq "exit") {
break
}
} else {
Write-Host ""
if (-not (Ask-YesNo "Would you like to try again?")) {
break
}
}
}
Write-Host ""
Write-Success "Thank you for using SledLink Firmware Upload Tool!"
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
# Run main program
Main