/ examples / neotrellis_simpletest.py
neotrellis_simpletest.py
 1  import time
 2  
 3  from board import SCL, SDA
 4  import busio
 5  from adafruit_neotrellis.neotrellis import NeoTrellis
 6  
 7  # create the i2c object for the trellis
 8  i2c_bus = busio.I2C(SCL, SDA)
 9  
10  # create the trellis
11  trellis = NeoTrellis(i2c_bus)
12  
13  # some color definitions
14  OFF = (0, 0, 0)
15  RED = (255, 0, 0)
16  YELLOW = (255, 150, 0)
17  GREEN = (0, 255, 0)
18  CYAN = (0, 255, 255)
19  BLUE = (0, 0, 255)
20  PURPLE = (180, 0, 255)
21  
22  # this will be called when button events are received
23  def blink(event):
24      # turn the LED on when a rising edge is detected
25      if event.edge == NeoTrellis.EDGE_RISING:
26          trellis.pixels[event.number] = CYAN
27      # turn the LED off when a rising edge is detected
28      elif event.edge == NeoTrellis.EDGE_FALLING:
29          trellis.pixels[event.number] = OFF
30  
31  
32  for i in range(16):
33      # activate rising edge events on all keys
34      trellis.activate_key(i, NeoTrellis.EDGE_RISING)
35      # activate falling edge events on all keys
36      trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
37      # set all keys to trigger the blink callback
38      trellis.callbacks[i] = blink
39  
40      # cycle the LEDs on startup
41      trellis.pixels[i] = PURPLE
42      time.sleep(0.05)
43  
44  for i in range(16):
45      trellis.pixels[i] = OFF
46      time.sleep(0.05)
47  
48  while True:
49      # call the sync function call any triggered callbacks
50      trellis.sync()
51      # the trellis can only be read every 17 millisecons or so
52      time.sleep(0.02)