Simple test¶
Ensure your device works with this simple test.
Single Adafruit Trellis Board
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Basic example of turning on LEDs and handling Keypad
5# button activity.
6
7# This example uses only one Trellis board, so all loops assume
8# a maximum of 16 LEDs (0-15). For use with multiple Trellis boards,
9# see the documentation.
10
11import time
12import busio
13from board import SCL, SDA
14from adafruit_trellis import Trellis
15
16# Create the I2C interface
17i2c = busio.I2C(SCL, SDA)
18
19# Create a Trellis object
20trellis = Trellis(i2c) # 0x70 when no I2C address is supplied
21
22# 'auto_show' defaults to 'True', so anytime LED states change,
23# the changes are automatically sent to the Trellis board. If you
24# set 'auto_show' to 'False', you will have to call the 'show()'
25# method afterwards to send updates to the Trellis board.
26
27# Turn on every LED
28print("Turning all LEDs on...")
29trellis.led.fill(True)
30time.sleep(2)
31
32# Turn off every LED
33print("Turning all LEDs off...")
34trellis.led.fill(False)
35time.sleep(2)
36
37# Turn on every LED, one at a time
38print("Turning on each LED, one at a time...")
39for i in range(16):
40 trellis.led[i] = True
41 time.sleep(0.1)
42
43# Turn off every LED, one at a time
44print("Turning off each LED, one at a time...")
45for i in range(15, 0, -1):
46 trellis.led[i] = False
47 time.sleep(0.1)
48
49# Now start reading button activity
50# - When a button is depressed (just_pressed),
51# the LED for that button will turn on.
52# - When the button is relased (released),
53# the LED will turn off.
54# - Any button that is still depressed (pressed_buttons),
55# the LED will remain on.
56print("Starting button sensory loop...")
57pressed_buttons = set()
58while True:
59 # Make sure to take a break during each trellis.read_buttons
60 # cycle.
61 time.sleep(0.1)
62
63 just_pressed, released = trellis.read_buttons()
64 for b in just_pressed:
65 print("pressed:", b)
66 trellis.led[b] = True
67 pressed_buttons.update(just_pressed)
68 for b in released:
69 print("released:", b)
70 trellis.led[b] = False
71 pressed_buttons.difference_update(released)
72 for b in pressed_buttons:
73 print("still pressed:", b)
74 trellis.led[b] = True