Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_neotrellis.neotrellis import NeoTrellis
7
8# create the i2c object for the trellis
9i2c_bus = board.I2C() # uses board.SCL and board.SDA
10# i2c_bus = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11
12# create the trellis
13trellis = NeoTrellis(i2c_bus)
14
15# Set the brightness value (0 to 1.0)
16trellis.brightness = 0.5
17
18# some color definitions
19OFF = (0, 0, 0)
20RED = (255, 0, 0)
21YELLOW = (255, 150, 0)
22GREEN = (0, 255, 0)
23CYAN = (0, 255, 255)
24BLUE = (0, 0, 255)
25PURPLE = (180, 0, 255)
26
27
28# this will be called when button events are received
29def blink(event):
30 # turn the LED on when a rising edge is detected
31 if event.edge == NeoTrellis.EDGE_RISING:
32 trellis.pixels[event.number] = CYAN
33 # turn the LED off when a falling edge is detected
34 elif event.edge == NeoTrellis.EDGE_FALLING:
35 trellis.pixels[event.number] = OFF
36
37
38for i in range(16):
39 # activate rising edge events on all keys
40 trellis.activate_key(i, NeoTrellis.EDGE_RISING)
41 # activate falling edge events on all keys
42 trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
43 # set all keys to trigger the blink callback
44 trellis.callbacks[i] = blink
45
46 # cycle the LEDs on startup
47 trellis.pixels[i] = PURPLE
48 time.sleep(0.05)
49
50for i in range(16):
51 trellis.pixels[i] = OFF
52 time.sleep(0.05)
53
54while True:
55 # call the sync function call any triggered callbacks
56 trellis.sync()
57 # the trellis can only be read every 17 millisecons or so
58 time.sleep(0.02)
MultiTrellis test¶
The multitrellis submodule helps manage multiple trellis as one.
1# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_neotrellis.neotrellis import NeoTrellis
7from adafruit_neotrellis.multitrellis import MultiTrellis
8
9# Create the I2C object for the NeoTrellis
10i2c_bus = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# Create the NeoTrellis object
14"""
15# This is for a 2x1 array (2 boards connected left to right):
16
17trelli = [
18 [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
19 ]
20
21"""
22# This is for a 2x2 array of NeoTrellis boards:
23trelli = [
24 [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
25 [NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31)],
26]
27
28trellis = MultiTrellis(trelli)
29
30# Set the brightness value (0 to 1.0)
31trellis.brightness = 0.5
32
33# some color definitions
34OFF = (0, 0, 0)
35RED = (255, 0, 0)
36YELLOW = (255, 150, 0)
37GREEN = (0, 255, 0)
38CYAN = (0, 255, 255)
39BLUE = (0, 0, 255)
40PURPLE = (180, 0, 255)
41
42
43# This will be called when button events are received
44def blink(xcoord, ycoord, edge):
45 # Turn the LED on when a rising edge is detected
46 if edge == NeoTrellis.EDGE_RISING:
47 trellis.color(xcoord, ycoord, BLUE)
48 # Turn the LED off when a falling edge is detected
49 elif edge == NeoTrellis.EDGE_FALLING:
50 trellis.color(xcoord, ycoord, OFF)
51
52
53for y in range(8):
54 for x in range(8):
55 # Activate rising edge events on all keys
56 trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
57 # Activate falling edge events on all keys
58 trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING)
59 trellis.set_callback(x, y, blink)
60 trellis.color(x, y, PURPLE)
61 time.sleep(0.05)
62
63for y in range(8):
64 for x in range(8):
65 trellis.color(x, y, OFF)
66 time.sleep(0.05)
67
68while True:
69 # The NeoTrellis can only be read every 17 milliseconds or so
70 trellis.sync()
71 time.sleep(0.02)