For those running Mac OS/X, Linux or using a Raspberry Pi, shortcuts like those for Windows are less useful. 

Using serial interface programs like Minicom or Screen are great and work wonderfully, however, a simple script that sends commands to the HandRaiser are certainly helpful.

Like on Windows, we'll use a scripting language that is on every major Linux release and in Mac OS/X: the Bash shell.

in the Scripts folder, you'll find a script named colorChange.sh.  Let's take a look at it.

#!/bin/bash
# The line above has to be bash (not sh) because we're using the printf builtin

#before we start, make sure we know whether we're under Linux or OS/X (Darwin)
uname -s | grep -i Linux > /dev/null
ISLINUX=$?

#$ISLINUX will be 0 (true) if we're on linux and 1 (false) if we're on mac

#First, get the name of the most recent Adafruit serial device connected
#Conveniently linux helps us by grouping them by manufacturer id
#Mac doesn't so we just take the most recently added serial device overall

if [ "$ISLINUX" -eq 0 ]; then

    DEVNAME=`ls -1t /dev/serial/by-id/ | grep Adafruit | head -1`

    if [[ -z "$DEVNAME" ]]; then
        echo "No Adafruit Device Found"
        exit
    fi

    FULLDEVPATH=/dev/serial/by-id/$DEVNAME
else
    DEVNAME=`ls -1t /dev/cu.usb* | head -1`
    if [[ -z "$DEVNAME" ]]; then
        echo "No Serial Device Found"
        exit
    fi
    FULLDEVPATH=$DEVNAME
fi

#Comment this to remove debug text
echo "Found recent Adafruit device at $FULLDEVPATH"

text=$@

if [[ -z "$text" ]]; then
    echo "Usage: colorChange.sh <Text To Send>"
    exit
fi

#Comment this to remove debug text
echo "Sending text '$text' to serial device"

printf "%b\r" $text > $FULLDEVPATH
#for some reason input() needs to have the serial port READ to return on linux
if [ "$ISLINUX" -eq 0 ]; then
	head -1 $FULLDEVPATH > /dev/null
fi

First, we need to find the most recently added Adafruit device with a serial port (just like we did on Windows).  How we do this differs depending on whether we're on Linux or Mac OS/X.  We determine that by running the command:

uname -s | grep -i Linux

and saving the return code in the $ISLINUX variable.

Detecting Boards on Linux

Thankfully, Linux makes this easy by already grouping all of the serial port in a folder named /dev/serial/by-id/.  Here's a listing:

As you can see with the HandRaiser attached, there is an entry for a serial device that has an id that includes Adafruit_Industries_LLC_Trinket_M0.  We'll use this to find the correct device.

DEVNAME=`ls -1t /dev/serial/by-id/ | grep Adafruit | head -1`

This command calls the ls command with the -1t option asking for a simple name output in order of modification, sends the output to grep to look for the word "Adafruit" and then takes just the first line by using "head -1".  results are stored in the DEVNAME variable using backticks (`)

If we don't find one, we exit with an error message.

We build the full pathname to the serial device using this command:

FULLDEVPATH=/dev/serial/by-id/$DEVNAME

Detecting Boards under OS/X

If we're on Mac OS/X, we don't have the list by device id, so we just use the list of all serial devices which can be found under /dev/cu.usb* and follow a similar path to determine the FULLPATH

DEVNAME=`ls -1t /dev/cu.usb* | head -1`

In this case, the FULLDEVPATH is actually returned directly:

FULLDEVPATH=$DEVNAME

Sending the Text using Printf

Next, we read the text from the command line using this line:

text=$@

That stores the command line arguments (which bash puts in $@) into a variable named text.  If we don't get any we return with an error.

Finally we need to send the command with a carriage return and read from the serial port to make the Trinket process the command.  All of that can be done using these two lines:

printf "%q\r" "$text" > $FULLDEVPATH
head -1 $FULLDEVPATH > /dev/null

The printf call is a bash built-in function that lets you specify formatting characters like the carriage return (\r) easily and to pass through special characters like "%" using the %q option.  We send the output directly to the serial port using the redirection operator (>)

The head command reads a single line from the serial port and sends it to /dev/null (basically this just ignores the result).

Using the colorChange.sh Script

To use the colorChange.sh script, copy it to your Linux machine and make sure you have permissions to access the serial port.  Kattni's guide on using the Serial port can really help here.

Once installed you can use the script like this:

./colorChange.sh "red"

Set the color to red

./colorChange.sh "%50"

Set the brightness to 50% (note the quotes are required here!)

./colorChange.sh "@wheel"

Set the device to rotate through the colors

./colorChange.sh "#AA00AA"

Set the color to dark purple.

This guide was first published on Jun 25, 2019. It was last updated on Mar 19, 2024.

This page (Linux/Mac Scripts) was last updated on Mar 08, 2024.

Text editor powered by tinymce.