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