code.py
  1  # SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # Ambient Color Control Pad
  6  # NeoTrellis to select colors of NeoPixel strip
  7  # NeoTrellis connected to Feather M4 (need the extra memory vs. M0) SCL, SDA
  8  # NeoPixel 120 strip connected to pin D5
  9  # NeoPixel strip powered over 5V 2A DC wall power supply
 10  # On/off button RGB connects En to GND, LED to D13
 11  
 12  import time
 13  import board
 14  from board import SCL, SDA
 15  import busio
 16  import neopixel
 17  from adafruit_neotrellis.neotrellis import NeoTrellis
 18  from digitalio import DigitalInOut, Direction
 19  
 20  button_LED = DigitalInOut(board.D13)
 21  button_LED.direction = Direction.OUTPUT
 22  button_LED.value = True
 23  
 24  pixel_pin = board.D5
 25  num_pixels = 120
 26  
 27  pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False)
 28  
 29  # create the i2c object for the trellis
 30  i2c_bus = busio.I2C(SCL, SDA)
 31  
 32  # create the trellis object
 33  trellis = NeoTrellis(i2c_bus)
 34  
 35  # color definitions
 36  OFF = (0, 0, 0)
 37  RED = (255, 0, 0)
 38  YELLOW = (255, 150, 0)
 39  GREEN = (0, 255, 0)
 40  YELLOW_GREEN = (127, 255, 0)
 41  CYAN = (0, 255, 255)
 42  LIGHT_BLUE = (0, 127, 255)
 43  BLUE = (0, 0, 255)
 44  PURPLE = (127, 0, 255)
 45  ORANGE = (255, 80, 0)
 46  PINK = (255, 0, 255)
 47  ROUGE = (255, 0, 127)
 48  WHITE = (100, 100, 100)
 49  WHITE_WARM = (120, 100, 80)
 50  WHITE_COOL = (80, 100, 120)
 51  WHITE_GREEN = (80, 120, 100)
 52  
 53  
 54  COLORS = [  # pixel colors
 55      RED, ORANGE, YELLOW, YELLOW_GREEN,
 56      GREEN, CYAN, LIGHT_BLUE, BLUE,
 57      PURPLE, PINK, ROUGE, WHITE,
 58      WHITE_WARM, WHITE_COOL, WHITE_GREEN, OFF
 59  ]
 60  
 61  pixels.fill(COLORS[1])  # turn on the strip
 62  pixels.show()
 63  
 64  
 65  def dimmed_colors(color_values):
 66      (red_value, green_value, blue_value) = color_values
 67      return (red_value // 10, green_value // 10, blue_value // 10)
 68  
 69  
 70  # this will be called when button events are received
 71  def blink(event):
 72      # turn the trellis LED on when a rising edge is detected
 73      # do the chase for the NeoPixel strip
 74      if event.edge == NeoTrellis.EDGE_RISING:
 75          trellis.pixels[event.number] = dimmed_colors(COLORS[event.number])
 76          for chase_off in range(num_pixels):  # chase LEDs off
 77              pixels[chase_off] = (OFF)
 78              pixels.show()
 79              time.sleep(0.005)
 80  
 81          for chase_on in range(num_pixels - 1, -1, -1):  # chase LEDs on
 82              pixels[chase_on] = (COLORS[event.number])
 83              pixels.show()
 84              time.sleep(0.03)
 85  
 86      # turn the trellis LED back to full color when a rising edge is detected
 87      elif event.edge == NeoTrellis.EDGE_FALLING:
 88          trellis.pixels[event.number] = COLORS[event.number]
 89  
 90  
 91  # boot up animation on trellis
 92  trellis.pixels.brightness = 0.2
 93  for i in range(16):
 94      # activate rising edge events on all keys
 95      trellis.activate_key(i, NeoTrellis.EDGE_RISING)
 96      # activate falling edge events on all keys
 97      trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
 98      # set all keys to trigger the blink callback
 99      trellis.callbacks[i] = blink
100  
101      # light the trellis LEDs on startup
102      trellis.pixels[i] = COLORS[i]
103      time.sleep(.05)
104  
105  print("  Ambient Color Control Pad")
106  print("    ---press a button to change the ambient color---")
107  
108  while True:
109  
110      # call the sync function call any triggered callbacks
111      trellis.sync()
112      # the trellis can only be read every 17 milliseconds or so
113      time.sleep(.02)