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