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()] param( [String]$Library, [String]$SearchPath = $(Get-Location) ) return (Get-ChildItem -Path $SearchPath -Recurse -Verbose:$Verbose -Name "$Library.py"|Convert-Path -Verbose:$Verbose) } Function Write-AmpyFile { [cmdletbinding()] param( [String]$Filename, [String]$COMPort ) If ((Get-Command -Name ampy -ErrorAction SilentlyContinue) -eq $false) { Write-Error -Message "Couldn't locate ``ampy`` in environment paths - please run ``pip install adafruit-ampy``." -ErrorAction Stop } Write-Verbose "Writing $Filename to port $COMPort" -Verbose:$Verbose ampy -p $COMPort put $Filename } Function Reset-AmpyDevice { [cmdletbinding()] param( [String]$COMPort ) If ((Get-Command -Name ampy -ErrorAction SilentlyContinue) -eq $false) { Write-Error -Message "Couldn't locate ``ampy`` in environment paths - please run ``pip install adafruit-ampy``." -ErrorAction Stop } Write-Verbose "Resetting port $COMPort" -Verbose:$Verbose ampy -p $COMPort reset } $BaseFiles = ( 'boot', 'main', 'uPyConfig' ) $ExtraFiles = ( 'init_sample' ) $CfgFiles = ( 'wifi_cfg', 'webrepl_cfg' ) $BaseFiles | % { $Lib = Find-LibraryFile -Library $_ -Verbose:$Verbose Write-AmpyFile -Filename "$Lib" -COMPort $COMPort -Verbose:$Verbose } $CfgFiles | % { If (Get-ChildItem "$_.py" -ErrorAction SilentlyContinue) { Write-AmpyFile -Filename "$_.py" -COMPort $COMPort -Verbose:$Verbose } } If ($Extras) { $ExtraFiles | % { $Lib = Find-LibraryFile -Library $_ -Verbose:$Verbose Write-AmpyFile -Filename "$Lib" -COMPort $COMPort -Verbose:$Verbose } } If ($Libs.Count -gt 0) { $Libs | % { $Lib = Find-LibraryFile -Library $_ -Verbose:$Verbose Write-AmpyFile -Filename "$Lib" -COMPort $COMPort -Verbose:$Verbose } } If ($ResetAfterFlash) { Reset-AmpyDevice -COMPort $COMPort -Verbose:$Verbose }