Windows Shortcuts

The HandRaiser includes a set of Windows Shortcuts that can be used to change the LED just by double clicking them.  You'll find them in the WindowsCommands folder on the CIRCUITPY drive that's created when you plug in the HandRaiser.

These scripts use a language that many of us aren't very familiar with, but that is installed on every modern Windows machine: PowerShell!

The folder has a PowerShell script named "ColorChange.ps1" that connects to the Serial Port on the HandRaiser and sends a message. It also has .BAT files that simply call that file with different messages.

For example, the "red.bat" file has the following text:

powershell -executionpolicy bypass -File "ColorChange.ps1" "red"

When executed, it calls ColorChange.ps1 and request that it send the code "red" to the Trinket. (the "-executionpolicy bypass" is necessary to run unsigned scripts from the command line)

Let's take a look at the ColorChange.ps1 file and how it works.

Download: file
function sendColor($colorName)
{
    $portInfo = ( Get-WmiObject Win32_SerialPort | Where { $_.PNPDeviceID -like '*VID_239A*' } | select -last 1 )
    $port = new-Object System.IO.Ports.SerialPort $portInfo.DeviceID,9600,None,8,one
    $port.open()
    $text = $colorName + "`r"
    $port.Write($text)
    start-sleep -m 50
    $port.ReadExisting()
    $port.Close()
}
if ($args.Length -eq 0)
{
    echo "Usage: ColorChange <color>"
}
else
{
    sendColor($args[0])
}

This PowerShell script has a single function sendColor() that takes a string and sends it cleanly to the Trinket over the Serial connection.  The critical section begins on line 3:

Download: file
    $portInfo = ( Get-WmiObject Win32_SerialPort | Where { $_.PNPDeviceID -like '*VID_239A*' } | select -last 1 )
    $port = new-Object System.IO.Ports.SerialPort $portInfo.DeviceID,9600,None,8,one
    $port.open()

Here, PowerShell requests information about the Serial Ports and filters that down to only return those with a Plug and Play DeviceID that includes "VID_239A" which is Adafruit's identifier.  It then selects only the last device (the most recently added device).

This means that if you're connecting multiple Adafruit devices (perhaps a Trinket, Circuit Playground and a Feather) it will always connect to the most recently added device with a Serial Port.

The next line creates a new SerialPort object connecting at 9600 baud and the standard "N-8-1" parity settings.

Then the port is opened so that we can read and write to it.

Download: file
    $text = $colorName + "`r"
    $port.Write($text)

Next we add a carriage return character to the end of any text passed in.  This is because the CircuitPython code running on the Trinket uses that to recognize the end of a a command.

And we use the Write() method to send the command and carriage return to the serial port.

Download: file
    start-sleep -m 50
    $port.ReadExisting()
    $port.Close()

These lines are necessary to get the Trinket to stop listening for the command to perform the action.  A short delay (50ms) is added and then the port is read before closing it.

This is a limitation of the input() method in CircuitPython and you may have to increase or decrease your delay to match your device.  However, we've found the 50ms delay is adequate for the Trinket M0 we're using.

This guide was first published on Jun 25, 2019. It was last updated on Jun 25, 2019. This page (Windows Shortcuts) was last updated on Oct 16, 2019.