Code and Play

Plug your NeoTrellis into a USB port on your computer. In your file explorer/finder, you should see a new flash drive named CIRCUITPY pop up.

If you see a new drive named TRELM4BOOT, you will need to load CircuitPython onto your NeoTrellis first. See this page to do so. Your board should reboot when you put the code on the board and a CIRCUITPY drive should now be available.

Library

You will need one library file in the /lib directory on your NeoTrellis. Check for the file adafruit_trellism4.mpy. If you do not have this file, you can download the CircuitPython 4.x library bundle available at this link. Go into the zip file, get the adafruit_trellism4.mpy file and put it on your CIRCUITPY drive as /lib/adafruit_trellism4.mpy (create the /lib directory if it isn't there).

Code

Copy the code below over to the CIRCUITPY drive as code.py. The program will start as soon as the code is in the drive. The NeoTrellis will clear all the buttons on startup.

# Simple paint program for Trellis M4 Express
# Press any button it will cycle through a palette of colors!
#
# Mike Barela for Adafruit Industries   November, 2018
#
import time
import adafruit_trellism4

trellis = adafruit_trellism4.TrellisM4Express()

# See https://www.w3schools.com/colors/colors_picker.asp
RED = 0xFF0000
ORANGE = 0xB34700
YELLOW = 0xFFFF00
OLIVE = 0x66DD00
GREEN = 0x008000
AQUA = 0x00FF66
TEAL = 0x00BFFF
BLUE = 0x0080FF
NAVY = 0x000080
MAROON = 0x800000
PURPLE = 0x800080
PINK = 0xFF66B3
WHITE = 0xFFFFFF
BLACK = 0x000000

color_cycle = [BLACK, RED, ORANGE, YELLOW, OLIVE, GREEN, AQUA,
               TEAL, BLUE, NAVY, MAROON, PURPLE, PINK, WHITE]

colors = 13  # Number of colors in color_cycle

key_state = [0 for _ in range(32)]  # All keys are color 0 (BLACK)

trellis.pixels.fill(BLACK)  # Turn off all pixels

current_press = set()
while True:
    pressed = set(trellis.pressed_keys)
    for press in pressed - current_press:
        if press:
            print("Pressed:", press)
            x, y = press
            pixel = (press[1] * 8) + press[0]
            if key_state[pixel] == colors:  # If we're at white
                key_state[pixel] = 0        #  Set back to black
            else:
                key_state[pixel] += 1       # Use next color
            # Change the pushed pixel to the next color
            trellis.pixels[x, y] = color_cycle[key_state[pixel]]

    time.sleep(0.08)
    current_press = pressed

Play!

Press the buttons to get colors. Use multiple presses to advance through the rainbow available. You can change the color of a button any time just by pushing it and cycling through the color palette.

This guide was first published on Dec 19, 2018. It was last updated on Dec 19, 2018. This page (Code and Play) was last updated on Dec 05, 2019.