Simple test

Ensure your device works with this simple test.

Single Adafruit Trellis Board

examples/trellis_simpletest.py
 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
12
13import busio
14from board import SCL, SDA
15
16from adafruit_trellis import Trellis
17
18# Create the I2C interface
19i2c = busio.I2C(SCL, SDA)
20
21# Create a Trellis object
22trellis = Trellis(i2c)  # 0x70 when no I2C address is supplied
23
24# 'auto_show' defaults to 'True', so anytime LED states change,
25# the changes are automatically sent to the Trellis board. If you
26# set 'auto_show' to 'False', you will have to call the 'show()'
27# method afterwards to send updates to the Trellis board.
28
29# Turn on every LED
30print("Turning all LEDs on...")
31trellis.led.fill(True)
32time.sleep(2)
33
34# Turn off every LED
35print("Turning all LEDs off...")
36trellis.led.fill(False)
37time.sleep(2)
38
39# Turn on every LED, one at a time
40print("Turning on each LED, one at a time...")
41for i in range(16):
42    trellis.led[i] = True
43    time.sleep(0.1)
44
45# Turn off every LED, one at a time
46print("Turning off each LED, one at a time...")
47for i in range(15, 0, -1):
48    trellis.led[i] = False
49    time.sleep(0.1)
50
51# Now start reading button activity
52# - When a button is depressed (just_pressed),
53#   the LED for that button will turn on.
54# - When the button is relased (released),
55#   the LED will turn off.
56# - Any button that is still depressed (pressed_buttons),
57#   the LED will remain on.
58print("Starting button sensory loop...")
59pressed_buttons = set()
60while True:
61    # Make sure to take a break during each trellis.read_buttons
62    # cycle.
63    time.sleep(0.1)
64
65    just_pressed, released = trellis.read_buttons()
66    for b in just_pressed:
67        print("pressed:", b)
68        trellis.led[b] = True
69    pressed_buttons.update(just_pressed)
70    for b in released:
71        print("released:", b)
72        trellis.led[b] = False
73    pressed_buttons.difference_update(released)
74    for b in pressed_buttons:
75        print("still pressed:", b)
76        trellis.led[b] = True