Add functionality for automatically identifying available COM ports.

This commit is contained in:
Maff 2018-07-13 20:35:30 +01:00
parent 0011948163
commit d90504c205
1 changed files with 81 additions and 0 deletions

View File

@ -2,9 +2,90 @@ param(
[String]$COMPort,
[switch]$Extras,
[switch]$ResetAfterFlash,
[switch]$WriteMicropython,
[ValidateSet("ESP32","ESP8266","ESP8285","PYBv1.0","PYBv1.1","PYBLite","PYBv3")]
[string]$BoardType,
[switch]$Verbose,
[String[]]$Libs
)
<#
Supported board types (at time of writing) for MicroPython are:
- esp32, esp8266 (and esp8285 by extension)
- stm32, teensy
- CC3200
Boards themselves:
STM32-
- pyBoard (v1.0, v1.1, Lite, v3) and chinese clones of pyBoard (uses v3 layout)
- Espruino Pico
- Olimex STM32 405STK, E407, H405, H407, P407
- ST Micro Discovery STM32F401, F407, F429, F746
- Teensy 3.1, 3.5, 3.6
Not going to list supported Espressif ESP8266, ESP8285 and ESP32 boards because the list is endless.
#>
#TODO Get-MicroPythonFirmware {}
######## Download latest firmware from website or whatever
#TODO Write-MicroPythonFirmware {}
######## esptool(.py)? erase_flash, then write_flash - erase_flash only needed if it's a fresh install
#TODO Update-uPyConfig {} - to dynamically generate a board config from PS values
######## uPyConfig.py will contain template values and will become uPyConfig.tpl
Function Find-SerialPort {
param (
[switch]$DeviceNameOnly,
[switch]$First
)
#On OSX, all serial devices are created with both a cu. and tty. file
# "cu" is "Call-Up", where we initiate the connection to the device
# "tty" is "Totally Terminal, Yeah", where the device initiates the connection
#On Linux, all serial devices have one device file. While simpler, this means
# more work is needed in order to determine what serial devices are present.
# Possible way of determining is to see if the owning group is 'dialout'?
#On Windows, all serial devices are registered with COM ports.
#On Windows, if running Windows PowerShell rather than pwsh, $IsWindows will be undefined
# but we can safely assume that $IsWindows being null categorically means we're on windows
# because Microsoft have never released Windows PowerShell on anything other than windows.
#Remember the distinction between Windows PowerShell and the open-source PowerShell (pwsh)
If ($IsWindows -or $IsWindows -eq $Null) {
$Devices=change.exe port /query | ? { $_ -notlike '*DosDevices*' }
$Devices=($Devices|%{$device=$_ -split ' = ';[PSCustomObject]@{
Name = $device[0]
File = $device[1]
}})
} ElseIf ($IsMacOS) {
$Devices=Get-ChildItem -Path /dev -Filter 'cu.*' | ? { $_.Name -notlike '*Bluetooth*' }
$Devices=@($Devices|%{[PSCustomObject]@{
Name = $_.Name
File = ($_|Convert-Path)
}})
} ElseIf ($IsLinux) {
$Devices=Get-ChildItem -Path /dev -File | ? { (stat -c '%G' ($_|Convert-Path)) -ieq 'dialout' }
$Devices=@($Devices|%{[PSCustomObject]@{
Name = $_.Name
File = ($_|Convert-Path)
}})
} Else {
#Sorry FreeBSD
Write-Error -Message "OS currently unsupported for this function" -ErrorAction Stop
}
If ($Devices.Count -eq 0) {
Write-Error -Message "No serial ports found on system" -ErrorAction Stop
}
If ($First) {
If ($DeviceNameOnly) {
$Devices[0].Name
} Else {
$Devices[0]
}
} Else {
If ($DeviceNameOnly) {
$Devices | % { $_.Name }
} Else {
$Devices
}
}
}
Function Find-LibraryFile {
[cmdletbinding()]