def __init__(self): GPIO.setmode(GPIO.BCM) GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) self.counter = 0 self.clkLastState = GPIO.input(clk) i2c_bus = busio.I2C(SCL, SDA) trelli = [ [ NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F), NeoTrellis(i2c_bus, False, addr=0x30) ], ] trellis = MultiTrellis(trelli)
def __init__(self, i2c_bus): super().__init__() self.led_matrix = [[BLANK for x in range(W)] for y in range(H)] self.old_led_matrix = [[BLANK for x in range(W)] for y in range(H)] self.trelli = [[] for i in range(int(H/4))] # [[],[]] print("Creating Trelli") for x, slice in enumerate(trellis_addresses): for y, addr in enumerate(slice): t = NeoTrellis(i2c_bus, False, addr=addr) t.pixels.auto_write = False self.trelli[x].append(t) sleep(0.1) pass print("Linking Trelli") self.trellis = MultiTrellis(self.trelli) print("Trelli linked") button_cb = self.make_button_cb() print("Initializing Trelli inputs") for y in range(H): for x in range(W): pass sleep(0.01) self.trellis.activate_key(x, y, NeoTrellis.EDGE_RISING) sleep(0.01) self.trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING) self.trellis.set_callback(x, y, button_cb) debug("Inputs initialized") self.blank_screen()
def init(self): self.states.enterMenu(MENUSTATE_INIT) self.config.load() self.states.sleepEnabled = self.config.isSleepEnabled() self.config.registerFilechangedListener(self.onConfigChanged) self.btctl = BluetoothCtl(self.config.getBluetoothMacs()) # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis self.trellis = NeoTrellis(i2c_bus) for i in range(16): # activate rising edge events on all keys self.trellis.activate_key(i, NeoTrellis.EDGE_RISING) # activate falling edge events on all keys self.trellis.activate_key(i, NeoTrellis.EDGE_FALLING) # set all keys to trigger the blink callback self.trellis.callbacks[i] = self.onKeyPressed
def main(trellis: NeoTrellis): for i in range(BUTTON_COUNT): trellis.activate_key(i, NeoTrellis.EDGE_RISING) trellis.activate_key(i, NeoTrellis.EDGE_FALLING) trellis.callbacks[i] = button_callback(trellis, i) while True: for i in range(BUTTON_COUNT): trellis.pixels[i] = button_target_color(i) trellis.sync() time.sleep(0.02)
def __init__(self, startup_color='random', debug=True): self.debug = debug self.nbuttons = 16 self.colors = { 'off': (0, 0, 0), 'purple': (180, 0, 255), 'red': (255, 0, 0), 'orange': (255, 164, 0), 'green': (0, 255, 0), 'yellow': (158, 152, 17), 'gray': (100, 100, 100), 'blue': (0, 0, 255), 'lightblue': (7, 34, 81), 'blueish': (33, 211, 237), 'darkgray': (10, 10, 10), 'seagreen': (30, 255, 30), 'lightseagreen': (39, 239, 120), 'salmon': (206, 28, 41), 'lightorange': (176, 76, 9), 'lightpurple': (87, 20, 174), 'lighterpurple': (70, 27, 87), 'pink': (100, 0, 100) } # create the i2c object for the trellis self.i2c_bus = busio.I2C(SCL, SDA) # create the trellis self.trellis = NeoTrellis( self.i2c_bus) # can set interrupt=True here... # for handling colors of groups of buttons self.startup_color = startup_color self.color_map = {} # to ensure callback set self.button_handler = None
def __init__(self, button_bus, led_bus, i2c_bus): Thread.__init__(self, name='Display') # super(Display, self).__init__() self.button_bus = button_bus self.led_bus = led_bus debug("Creating i2c bus") lcd.flash("Creating i2c bus") # i2c_bus = busio.I2C(SCL, SDA) lcd.setup_hw(i2c_bus) debug("i2c bus created") lcd.flash("i2c bus created") debug("Creating Trelli") lcd.flash("Creating Trelli") trelli = [[], [], [], []] # addrs = trellis_addresses # Create trelli sequentially with a slight pause between each for x, slice in enumerate(trellis_addresses): for y, addr in enumerate(slice): t = NeoTrellis(i2c_bus, False, addr=addr) t.pixels.auto_write = False trelli[x].append(t) sleep(0.1) debug("Linking Trelli") lcd.flash("Linking Trelli") self.trellis = MultiTrellis(trelli) debug("Trelli linked") lcd.flash("Trelli linked") self.grid_h = H self.grid_w = W self.state = 'play' self.led_matrix = [[(0, 0, 0) for x in range(w)] for y in range(h)] self.old_led_matrix = [[(0, 0, 0) for x in range(w)] for y in range(h)] button_cb = self.make_cb() debug("Initializing Trelli inputs") lcd.flash("Initializing Trelli inputs") for y in range(self.grid_h): for x in range(self.grid_w): sleep(0.01) self.trellis.activate_key(x, y, NeoTrellis.EDGE_RISING) sleep(0.01) self.trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING) self.trellis.set_callback(x, y, button_cb) self.ins_button = digitalio.DigitalInOut(D13) self.gbl_button = digitalio.DigitalInOut(D6) debug("Inputs initialized") lcd.flash("Inputs initialized") self.col_scheme = select_scheme('default') return
def __init__(self): # setup trellis i2c_bus = busio.I2C(SCL, SDA) self.trellis = MultiTrellis( [ [ NeoTrellis(i2c_bus, True, addr=0x31), NeoTrellis(i2c_bus, True, addr=0x30), NeoTrellis(i2c_bus, True, addr=0x2F), NeoTrellis(i2c_bus, True, addr=0x2E), ], [ NeoTrellis(i2c_bus, True, addr=0x35), NeoTrellis(i2c_bus, True, addr=0x34), NeoTrellis(i2c_bus, True, addr=0x33), NeoTrellis(i2c_bus, True, addr=0x32), ], ] ) # interrupt pin - sync only when needed self.interrupt = digitalio.DigitalInOut(D5) self.interrupt.direction = digitalio.Direction.INPUT
import time import random import board from board import SCL, SDA import digitalio import busio from adafruit_neotrellis.neotrellis import NeoTrellis # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis trellis = NeoTrellis(i2c_bus) button_pin = board.D6 button = digitalio.DigitalInOut(button_pin) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP # some color definitions OFF = (0, 0, 0) RED = (25, 0, 0) YELLOW = (25, 15, 0) GREEN = (0, 25, 0) CYAN = (0, 25, 25) BLUE = (0, 0, 25) PURPLE = (18, 0, 25) WHITE = (127, 127, 127) PUSH_COLOR = GREEN
sample_dir = 'samples' try: sample_name = sys.argv[1] except: sample_name = 'wurly' soundfiles = glob.glob(os.path.join(sample_dir, sample_name, '*.wav')) samples = [pygame.mixer.Sound(soundfile) for soundfile in soundfiles] print('Loaded {} samples from {}'.format(len(samples), sample_name)) #create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) print (i2c_bus) #create the trellis trellis = NeoTrellis(i2c_bus) # can set interrupt=True here... print(trellis) #some color definitions OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) #this will be called when button events are received def blink(event): #turn the LED on when a rising edge is detected if event.edge == NeoTrellis.EDGE_RISING:
def gradient_1d(start, stop, steps): gradient = list(map(col_to_rgb, start.range_to(stop, steps))) return gradient # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis trelli = [[], [], [], []] addrs = [[0x31, 0x30, 0x2f, 0x2e], [0x35, 0x34, 0x33, 0x32], [0x36, 0x37, 0x38, 0x39], [0x3a, 0x3c, 0x3b, 0x3d]] # Create trelli sequentially with a slight pause between each for x, slice in enumerate(addrs): for y, addr in enumerate(slice): t = NeoTrellis(i2c_bus, False, addr=addr) t.pixels.auto_write = False trelli[x].append(t) sleep(0.2) sizeY = len(trelli) * 4 sizeX = len(trelli[0]) * 4 trellis = MultiTrellis(trelli) for ts in trelli: for t in ts: print(t) print(type(t)) t.pixels.auto_write = False
from board import SDA, SCL from adafruit_neotrellis.neotrellis import NeoTrellis import busio import adafruit_ble from adafruit_ble.advertising import Advertisement from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.standard.hid import HIDService from adafruit_ble.services.standard.device_info import DeviceInfoService from adafruit_hid.keyboard import Keyboard from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS from adafruit_hid.keycode import Keycode i2c_bus = busio.I2C(SCL, SDA) trellis = NeoTrellis(i2c_bus) hid = HIDService() KEYCOUNT = 16 OFF = (0, 0, 0) ON = (125, 125, 125) RED = (125, 0, 0) GREEN = (0, 125, 0) BLUE = (0, 0, 125) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) # define the buttons and positions
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import busio from adafruit_neotrellis.neotrellis import NeoTrellis # create the i2c object for the trellis SCL = board.GP27 SDA = board.GP26 i2c_bus = busio.I2C(SCL, SDA) # create the trellis trellis = NeoTrellis(i2c_bus) # some color definitions OFF = (0, 0, 0) ON = (255, 255, 255) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) # default color-layout defpix = [ YELLOW, YELLOW, YELLOW, ON, YELLOW, BLUE, YELLOW, ON, YELLOW, YELLOW, YELLOW, ON, GREEN, BLUE, RED, ON
#Author: Becky Button #This is a file to test the 4x4 trellis module, and #test that the colors are being cycled correctly! import time from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis #create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) #create the trellis trellis = NeoTrellis(i2c_bus) #colors OFF = (0, 0, 0) RED = (255, 0, 0) ORANGE = (255, 127, 0) YELLOW = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 255) INDIGO = (46, 43, 95) VIOLET = (139, 0, 255) #this stores all of the possible color options COLORS = [OFF, RED, ORANGE, YELLOW, GREEN, BLUE,\ INDIGO, VIOLET] #the current color index the given button is at
button_LED = DigitalInOut(board.D13) button_LED.direction = Direction.OUTPUT button_LED.value = True pixel_pin = board.D5 num_pixels = 34 pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False) unpixel = pixels[1] print(unpixel) # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis object trellis = NeoTrellis(i2c_bus) boton = 17 count = 0 # color definitions OFF = (0, 0, 0) RED = (255, 0, 0) ROUGE = (210, 0, 50) DM_RED = (20, 0, 0) YELLOW = (235, 150, 0) GREEN = (0, 210, 20) CYAN = (0, 100, 240) DM_CYAN = (0, 50, 120) BLUE = (0, 10, 230) PURPLE = (80, 0, 240)
class Nightstand: def __init__(self): self.config = NightStandConfig(CONFIGFILE) self.audioPlayer = None self.states = NightstandStates() self.btctl = None self.states.registerMenuStateChangeListener(self.onMenuChanged) self.states.registerResetListener(self.reset) self.states.registerSleepListener(self.onGoToSleep) self.timer = RepeatedTimer(TIMER_INTERVAL, self.onTimerTick) def init(self): self.states.enterMenu(MENUSTATE_INIT) self.config.load() self.states.sleepEnabled = self.config.isSleepEnabled() self.config.registerFilechangedListener(self.onConfigChanged) self.btctl = BluetoothCtl(self.config.getBluetoothMacs()) # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis self.trellis = NeoTrellis(i2c_bus) for i in range(16): # activate rising edge events on all keys self.trellis.activate_key(i, NeoTrellis.EDGE_RISING) # activate falling edge events on all keys self.trellis.activate_key(i, NeoTrellis.EDGE_FALLING) # set all keys to trigger the blink callback self.trellis.callbacks[i] = self.onKeyPressed def doConnect(self): self.btctl.doConnectAsync(BTCONNECT_RETRIES, self.onConnectFinished, self.onConnectFailed) def doDisconnect(self): self.btctl.doDisconnect() def onConnectFinished(self): print("bt connections established") self.states.enterMenu(MENUSTATE_IDLE) def onConnectFailed(self): print("failed to connect to bt devices") self.states.enterMenu(MENUSTATE_ERROR) def onMenuChanged(self, oldMenuState, newMenuState): print("menu changed from " + str(oldMenuState) + " to " + str(newMenuState)) if oldMenuState == MENUSTATE_SLEEPCONFIG: self.states.setSleepConfig(self.config.isSleepEnabled(), self.config.getSleepTime()) self.states.enterMenu( MENUSTATE_PLAYING if self.audioPlayer is not None else MENUSTATE_IDLE, True) return self.initColors() def reset(self, startupSequence=True, stopAudio=False): self.states.enterMenu(MENUSTATE_INIT) self.states.resetCounter = RESET_COUNTER_START self.btctl = BluetoothCtl(self.config.getBluetoothMacs()) if stopAudio: self.playAudio(None) if startupSequence: for i in range(16): self.trellis.pixels[i] = RED time.sleep(INIT_DELAY) for i in range(16): self.trellis.pixels[i] = YELLOW time.sleep(INIT_DELAY) for i in range(16): self.trellis.pixels[i] = GREEN time.sleep(INIT_DELAY) for i in range(16): self.trellis.pixels[i] = OFF time.sleep(INIT_DELAY) self.initColors() if self.config.doBluetoothAutoConnect(): self.states.enterMenu(MENUSTATE_CONNECTING) self.doConnect() else: self.states.enterMenu(MENUSTATE_IDLE) def onGoToSleep(self): self.enterMenu(MENUSTATE_SLEEPING) self.playAudio(None) self.doDisconnect() def getKeyColor(self, index): if self.states.menuState == MENUSTATE_SLEEPCONFIG: time = self.config.getSleepTime() if index < 12: return YELLOW if time > index * 300 else OFF elif index == 12: return WHITE elif index == 13: return GREEN if self.config.isSleepEnabled() else RED elif index == 14: return GREEN if self.config.isSleepEnabled() else RED elif index == 15: return WHITE elif self.states.menuState == MENUSTATE_DIMMING or self.states.menuState == MENUSTATE_STOPPED or self.states.menuState == MENUSTATE_SLEEPING: return OFF else: kc = self.config.getKeyConfig(index) if not kc.isPlayerButton( ) or self.states.menuState == MENUSTATE_PLAYING: return kc.color else: return OFF def initColors(self): for i in range(16): self.trellis.pixels[i] = self.getKeyColor(i) def onTimerTick(self): self.states.onResetTimerTick() self.states.onSleepTimerTick() self.states.onDimmingTimerTick() def onConfigChanged(self): print("config changed") self.reset(False) def onKeyPressed(self, event): self.states.resetDimmingTimer() # turn the LED on when a rising edge is detected if event.edge == NeoTrellis.EDGE_RISING: self.trellis.pixels[event.number] = self.config.getKeyConfig( event.number).keyPressedColor self.states.keyStates[event.number] = KEYSTATE_PRESSED # turn the LED off when a rising edge is detected elif event.edge == NeoTrellis.EDGE_FALLING: if self.states.menuState == MENUSTATE_DIMMING: self.states.menuBack() return kc = self.config.getKeyConfig(event.number) if not kc.isPlayerButton( ) or self.states.menuState == MENUSTATE_PLAYING: self.trellis.pixels[event.number] = kc.color else: self.trellis.pixels[event.number] = OFF if self.states.menuState == MENUSTATE_SLEEPCONFIG: self.onSleepConfigKeyPressed(event.number) else: self.onAudioKeyPressed(event.number) self.states.keyStates[event.number] = KEYSTATE_NONE def onSleepConfigKeyPressed(self, index): curSleep = self.config.getSleepTime() incdec = 300 if index == 12: self.config.setSleepTime(curSleep - incdec if curSleep > incdec else 0) if self.config.getSleepTime() == 0: self.config.enableSleep(False) if index == 15: self.config.setSleepTime(curSleep + incdec if curSleep < 11 * incdec else 12 * incdec) if curSleep == 0: self.config.enableSleep(True) if index == 13 or index == 14: self.config.enableSleep(not self.config.isSleepEnabled()) self.states.stayinSleepConfig() self.initColors() def onAudioKeyPressed(self, index): if self.states.menuState == MENUSTATE_IDLE or ( self.states.menuState == MENUSTATE_PLAYING and not self.config.getKeyConfig(index).isPlayerButton()): print("start audio") self.playAudio(self.config.getMedia(index)) elif self.states.menuState == MENUSTATE_PLAYING: keyConfig = self.config.getKeyConfig(index) if keyConfig.isPlayerButton(): self.audioPlayerButtonClick(keyConfig.playerButton) def audioPlayerButtonClick(self, button): print("exec audio button ", button) if not self.audioPlayer is None: if button == KeyConfig.AUDIOBUTTON_PLAY_PAUSE: if self.audioPlayer.is_playing(): self.audioPlayer.pause() else: self.audioPlayer.play() elif button == KeyConfig.AUDIOBUTTON_STOP: self.playAudio(None) elif button == KeyConfig.AUDIOBUTTON_VOLUMEUP: vol = self.audioPlayer.audio_get_volume() + VOL_INCDEC self.audioPlayer.audio_set_volume(vol) elif button == KeyConfig.AUDIOBUTTON_VOLUMEDOWN: vol = self.audioPlayer.audio_get_volume() - VOL_INCDEC self.audioPlayer.audio_set_volume(vol) elif button == KeyConfig.AUDIOBUTTON_NEXT: self.audioPlayer.next_chapter() elif button == KeyConfig.AUDIOBUTTON_PREV: self.audioPlayer.previous_chapter() def playAudio(self, audio): if not self.audioPlayer is None: print("stopping current audio") self.audioPlayer.stop() self.states.enterMenu(MENUSTATE_IDLE) if audio is None: return self.states.enterMenu(MENUSTATE_PLAYING) print("start playing ", audio) self.audioPlayer = vlc.MediaPlayer(audio) self.audioPlayer.play() def startServer(self): print('Hello from the Nightstand Service') try: while True: # call the sync function call any triggered callbacks self.trellis.sync() # the trellis can only be read every 17 millisecons or so time.sleep(0.02) except: self.config.stop() self.stop() def startCLI(self): print('Hello from the Nightstand Server') self.reset() self.timer.start() try: while True: # call the sync function call any triggered callbacks self.trellis.sync() # the trellis can only be read every 17 millisecons or so time.sleep(0.02) except KeyboardInterrupt: self.config.stop() self.stop() def stop(self): self.config.join() self.timer.stop() self.states.enterMenu(MENUSTATE_STOPPED) self.doDisconnect() self.states.unregisterMenuStateChangeListener(self.onMenuChanged)
### The number of rows is also the number of NEOPIXEL ###rows = 10 ### CPB with build in 10 RGB ###rows = 12 ### CLUE with SnowPi RGB rows = 12 ### PyPortal with SnowPi RGB ###rows = 16 ### CLUE with NeoTrellis ###strip = neopixel.NeoPixel(board.NEOPIXEL, rows, brightness=BRIGHTNESS) ### CPB ###strip = neopixel.NeoPixel(board.P2, rows, brightness=BRIGHTNESS) ### CLUE with SnowPi RGB strip = neopixel.NeoPixel(board.D4, rows, brightness=BRIGHTNESS) ### PyPortal with SnowPi RGB # create the i2c object for the trellis i2c_bus = busio.I2C(board.SCL, board.SDA) # create the trellis trellis = NeoTrellis(i2c_bus) for i in range(16): trellis.pixels[i] = (0, 0, 31) time.sleep(0.05) for i in range(16): trellis.pixels[i] = (0, 0, 0) time.sleep(0.05) ### Neopixel version for CPB and SnowPi strip.fill((0, 0, 31)) time.sleep(0.5) strip.fill((0, 0, 0))
import time from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis from adafruit_neotrellis.multitrellis import MultiTrellis # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) """ None should work here but code will throw if we try to call actions on the missing quadrant. May wanna eventually write a wrapper class to safeguard """ trelli = [ [None, NeoTrellis(i2c_bus, False, addr=0x2F)], [ NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31) ], ] trellis = MultiTrellis(trelli) # some color definitions OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255)
CYAN = (0, 25, 25) BLUE = (0, 0, 25) PURPLE = (18, 0, 25) WHITE = (127, 127, 127) # Instrument instru = "" # Path path = "" # Create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # Create the trellis trellis = NeoTrellis(i2c_bus, False, addr=0x2F) print("NeoTrellis created") audio_file = None PUSH_COLOR = GREEN ANIM_COLOR = WHITE COLORS = ["RED", "YELLOW", "GREEN", "CYAN", "BLUE", "PURPLE", "WHITE"] COLOR_TUPLES = [RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE] buttons = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] button_colors = [ OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF
import time import sys import select from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis i2c_bus = busio.I2C(SCL, SDA) trellis = NeoTrellis(i2c_bus) def clicked(event): sys.stdout.write('Clicked {0}\n'.format(event.number)) sys.stdout.flush() for i in range(16): trellis.activate_key(i, NeoTrellis.EDGE_RISING) trellis.pixels[i] = (0, 0, 0) trellis.callbacks[i] = clicked while True: trellis.sync() if select.select([ sys.stdin, ], [], [], 0.0)[0]: while True: line = sys.stdin.readline() if not line: break
class Trellis: """ relays button presses by adding them to a queue buttons can be referred to by name, index, or color group name """ def __init__(self, startup_color='random', debug=True): self.debug = debug self.nbuttons = 16 self.colors = { 'off': (0, 0, 0), 'purple': (180, 0, 255), 'red': (255, 0, 0), 'orange': (255, 164, 0), 'green': (0, 255, 0), 'yellow': (158, 152, 17), 'gray': (100, 100, 100), 'blue': (0, 0, 255), 'lightblue': (7, 34, 81), 'blueish': (33, 211, 237), 'darkgray': (10, 10, 10), 'seagreen': (30, 255, 30), 'lightseagreen': (39, 239, 120), 'salmon': (206, 28, 41), 'lightorange': (176, 76, 9), 'lightpurple': (87, 20, 174), 'lighterpurple': (70, 27, 87), 'pink': (100, 0, 100) } # create the i2c object for the trellis self.i2c_bus = busio.I2C(SCL, SDA) # create the trellis self.trellis = NeoTrellis( self.i2c_bus) # can set interrupt=True here... # for handling colors of groups of buttons self.startup_color = startup_color self.color_map = {} # to ensure callback set self.button_handler = None def set_color_map(self, color_map): self.color_map = color_map def set_callback(self, fcn): # callback for when buttons are pressed self.button_handler = fcn # set handlers for button press self.activate(self.startup_color, lightshow=True) def activate(self, startup_color=None, lightshow=False): if self.button_handler is None: print("Error: callback must be set using 'set_callback'") for i in range(self.nbuttons): # activate rising edge events on all keys self.trellis.activate_key(i, BUTTON_PRESSED) # activate falling edge events on all keys self.trellis.activate_key(i, BUTTON_RELEASED) # set all keys to trigger the blink callback self.trellis.callbacks[i] = self.button_handler if not lightshow: continue #cycle the LEDs on startup if startup_color is not None: if startup_color == 'random': color = random_color() else: color = self.colors[startup_color] self.trellis.pixels[i] = color time.sleep(.03) for i in range(self.nbuttons): self.trellis.pixels[i] = self.colors['off'] if lightshow: time.sleep(.03) def end_lightshow(self, event=None): # reset callbacks and turn lights off if event is None or event.edge == BUTTON_PRESSED: self.activate() self.lightshow_on = False def lightshow(self): self.lightshow_on = True # first, set callback to interrupt the show for i in range(self.nbuttons): self.trellis.callbacks[i] = self.end_lightshow # now pick buttons and flash lights on/off in random order while True: button_indices = list(range(self.nbuttons)) random.shuffle(button_indices) for i in button_indices: self.trellis.pixels[i] = random_color() time.sleep(.07) if not self.lightshow_on: return for i in button_indices: self.trellis.pixels[i] = self.colors['off'] time.sleep(.07) if not self.lightshow_on: return self.sync() time.sleep(.02) def set_color_all_buttons(self, color): for i in range(self.nbuttons): self.set_color(i, color) def set_color(self, index, color): if color in self.color_map: color = self.color_map[color] self.trellis.pixels[index] = self.colors[color] def sync(self): self.trellis.sync() def terminate(self): for i in range(self.nbuttons): self.trellis.pixels[i] = self.colors['off'] self.sync()
import busio from adafruit_neotrellis.neotrellis import NeoTrellis from adafruit_neotrellis.multitrellis import MultiTrellis #create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) """create the trellis. This is for a 2x2 array of NeoTrellis boards for a 2x1 array (2 boards connected left to right) you would use: trelli = [ [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)] ] """ trelli = [[ NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F) ], [ NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31) ]] trellis = MultiTrellis(trelli) #some color definitions OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255)
from neotrellism4 import NeoTrellisM4 #create the i2c object for the trellis I2C = busio.I2C(SCL, SDA) """create the trellis. This is for a 2x2 array of TrellisM4 (first row) with 2 Neotrellis (second row). [ NeoM4_left | NeoM4_right ] neotrellis0 | neotrellis1 """ trellim4_left = NeoTrellisM4() trellim4_right = NeoTrellisM4(left_part=trellim4_left) trelli = [[trellim4_left, trellim4_right], [ NeoTrellis(I2C, False, addr=0x2F), NeoTrellis(I2C, False, addr=0x2E) ]] trellis = MultiTrellis(trelli) #some color definitions OFF = (0, 0, 0) RED = (127, 0, 0) YELLOW = (127, 75, 0) GREEN = (0, 127, 0) CYAN = (0, 127, 127) BLUE = (0, 0, 127) PURPLE = (90, 0, 127)
if i in OUTER_RING: float_color = hsv_to_rgb(sin_time(0.25), 0.8, 0.8) if i in INNER_RING: float_color = hsv_to_rgb(cos_time(0.25), 0.8, 0.8) return float_to_byte_color(float_color) def main(trellis: NeoTrellis): for i in range(BUTTON_COUNT): trellis.activate_key(i, NeoTrellis.EDGE_RISING) trellis.activate_key(i, NeoTrellis.EDGE_FALLING) trellis.callbacks[i] = button_callback(trellis, i) while True: for i in range(BUTTON_COUNT): trellis.pixels[i] = button_target_color(i) trellis.sync() time.sleep(0.02) if __name__ == "__main__": i2c = busio.I2C(SCL, SDA) trellis = NeoTrellis(i2c) try: main(trellis) except KeyboardInterrupt: for i in range(BUTTON_COUNT): trellis.pixels[i] = (0, 0, 0)
# some color definitions OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) WHITE = (255, 255, 255) # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) trelli = [ [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F), NeoTrellis(i2c_bus, False, addr=0x30)], ] trellis = MultiTrellis(trelli) # this will be called when button events are received def blink(xcoord, ycoord, edge): # turn the LED on when a rising edge is detected if edge == NeoTrellis.EDGE_RISING: trellis.color(xcoord, ycoord, GREEN) # turn the LED off when a rising edge is detected elif edge == NeoTrellis.EDGE_FALLING: trellis.color(xcoord, ycoord, OFF) def get_next(x, y): if x == 3 and y == 3: return 4, 0
import boardStateDriver, tests import time from random import randrange from adafruit_seesaw.neopixel import NeoPixel import board from board import SCL, SDA import digitalio import busio from adafruit_neotrellis.neotrellis import NeoTrellis # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis trellis = NeoTrellis(i2c_bus) button_pin = board.D6 button = digitalio.DigitalInOut(button_pin) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP # ALWAYS PASS x.theBoard TO ITS SELF WHEN CHANGING STATE!!!!!!!!!!!! x = boardStateDriver.boardState([0] * 16) for i in range(16): # activate rising edge events on all keys trellis.activate_key(i, NeoTrellis.EDGE_RISING) # activate falling edge events on all keys #trellis.activate_key(i, NeoTrellis.EDGE_FALLING) # set all keys to trigger the blink callback trellis.callbacks[i] = x.redrawBoard
import time from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis #create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) #create the trellis trellis = NeoTrellis(i2c_bus) #some color definitions OFF = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) #this will be called when button events are received def blink(event): #turn the LED on when a rising edge is detected if event.edge == NeoTrellis.EDGE_RISING: trellis.pixels[event.number] = CYAN #turn the LED off when a rising edge is detected elif event.edge == NeoTrellis.EDGE_FALLING: trellis.pixels[event.number] = OFF for i in range(16):
# running CircuitPython 5 or newer import time from board import SCL, SDA import busio from adafruit_neotrellis.neotrellis import NeoTrellis import usb_midi import adafruit_midi from adafruit_midi.note_on import NoteOn from adafruit_midi.note_off import NoteOff # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis trellis = NeoTrellis(i2c_bus) # color definitions OFF = (0, 0, 0) CYAN = (0, 16, 16) ORANGE = (30, 20, 0) MAGENTA = (15, 0, 30) WHITE = (5, 5, 6) STARTUP_COLOR = WHITE COLOR_A = MAGENTA COLOR_B = CYAN midi_channel = 1 # change this if you want to send on a different MIDI channel midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=midi_channel-1)
#Authors Hunter Hannula, Cameron Kerley import time from random import randrange # possibly remove, if we use random we should write a seprate module for those functions from adafruit_seesaw.neopixel import NeoPixel import board from board import SCL, SDA import digitalio import busio from adafruit_neotrellis.neotrellis import NeoTrellis # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis trellis = NeoTrellis(i2c_bus) button_pin = board.D6 button = digitalio.DigitalInOut(button_pin) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP # some color definitions OFF = (0, 0, 0) RED = (25, 0, 0) YELLOW = (25, 15, 0) GREEN = (0, 25, 0) CYAN = (0, 25, 25) BLUE = (0, 0, 25) PURPLE = (18, 0, 25)
from digitalio import DigitalInOut, Direction button_LED = DigitalInOut(board.D13) button_LED.direction = Direction.OUTPUT button_LED.value = True pixel_pin = board.D5 num_pixels = 136 pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False) # create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) # create the trellis object trellis = NeoTrellis(i2c_bus) # color definitions OFF = (0, 0, 0) RED = (255, 0, 0) ROUGE = (210, 0, 50) DM_RED = (100, 0, 0) YELLOW = (235, 150, 0) GREEN = (0, 255, 20) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (80, 0, 240) ORANGE = (255, 50, 0) PINK = (255, 0, 100) WHITE = (255, 255, 255)
from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService from toy import Toy ble = BLERadio() uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) i2c = board.I2C() trelli = [ [ NeoTrellis(i2c, False, addr=0x2F), NeoTrellis(i2c, False, addr=0x2E), ], [ NeoTrellis(i2c, False, addr=0x31), NeoTrellis(i2c, False, addr=0x30), ], ] trellis = MultiTrellis(trelli) class TrellisWrapper(object): """Wrapper that implements the basic color iface, for use with Toy""" def __init__(self, trellis, brightness=1): self.brightness = brightness self.trellis = trellis
help='port for the websocket server') parser.add_argument('-c','--config',required=False, default='/etc/keypad.conf.json', help='keypad config file to load') parser.add_argument('-d', required=False, help='run as daemon.no cli') args = vars(parser.parse_args()) port=args["port"] server = SimpleWebSocketServer('', port, NeoTrellisSocket) #create the i2c object for the trellis i2c_bus = busio.I2C(SCL, SDA) #create the trellis trellis = NeoTrellis(i2c_bus) for i in range(16): colors.append(KeyLedConfig()) #activate rising edge events on all keys trellis.activate_key(i, NeoTrellis.EDGE_RISING) #activate falling edge events on all keys trellis.activate_key(i, NeoTrellis.EDGE_FALLING) #set all keys to trigger the blink callback trellis.callbacks[i] = keyEvent loadConfig(args["config"]) if runAsDaemon: while True: server.serveonce()