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