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()
class Trellis(object): 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 def update(self): if self.interrupt.value is False: self.trellis.sync()
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): 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)
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 def _adjustColor(self, color): return (int(c * self.brightness) for c in color) def color(self, x, y, color): self.trellis.color(x, y, self._adjustColor(color)) def width(self):
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) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) #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:
""" 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)], ] tempo = 60 # Starting BPM # You can use the accelerometer to speed/slow down tempo by tilting! ENABLE_TILT_TEMPO = True MIN_TEMPO = 100 MAX_TEMPO = 300 BUTTONS = BUTTONS8x8 trellis = MultiTrellis(trelli) # pressed_keys - A list of tuples of currently pressed button coordinates trellis.pressed_keys = set() # currently pressed sequencer buttons current_press = set() # Our global state current_step = 7 # we actually start on the last step since we increment first # the state of the sequencer beatset = [[False] * 8, [False] * 8, [False] * 8, [False] * 8, [False] * 8,
# 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 print('ready') wait = 0.05 OFF = (0, 0, 0) LOW = (5, 5, 5) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255)
# ------------------------- # SETUP # ------------------------- # 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=0x30)], [NeoTrellis(i2c_bus, False, addr=0x31), NeoTrellis(i2c_bus, False, addr=0x2F)] ] trellis = MultiTrellis(trelli) UPPER_LEFT = 40 # some color definitions OFF = (0, 0, 0) V = 100 # default velocity (color "volume") # Tell the device to act like a keyboard. keyboard = Keyboard(usb_hid.devices) # Send a keypress of ESCAPE keyboard.send(Keycode.ESCAPE) # Send CTRL-A (select all in most text editors) keyboard.send(Keycode.CONTROL, Keycode.A)
class Display(Thread): """docstring for Display.""" 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 run(self): debug("Display thread started") while True: sleep(0.01) m = self.get_cmds() if m.get('cmd') != None: debug(m) self.button_bus.put(m) if not self.led_bus.empty(): status, led_grid = self.led_bus.get() self.draw_all(status, led_grid) return def read_gbl_button(self): return not self.gbl_button.value def read_ins_button(self): return not self.ins_button.value def get_cmds(self): try: self.trellis.sync() # TODO undo? Fails if called too often except Exception as e: debug("HW error: {}".format(str(e))) m = {'cmd': None} if self.read_gbl_button(): if self.state != 'gbl_cfg': m['cmd'] = "CONFIG_A" self.state = 'gbl_cfg' else: self.state = 'play' elif self.read_ins_button(): if self.state != 'ins_cfg': m['cmd'] = "CONFIG_B" self.state = 'ins_cfg' else: self.state = 'play' return m def draw_all(self, status, led_grid): self.draw_note_grid(led_grid) self.redraw_diff() return def blank_screen(self): for x in range(len(self.led_matrix)): for y in range(len(self.led_matrix[x])): self.led_matrix[x][y] = OFF def redraw_diff(self): diffs = [] for x in range(len(self.led_matrix)): for y in range(len(self.led_matrix[x])): if self.led_matrix[x][y] != self.old_led_matrix[x][y]: diffs.append((x, y, self.led_matrix[x][y])) self.old_led_matrix[x][y] = self.led_matrix[x][y] for diff in diffs: self.trellis.color(diff[0], diff[1], diff[2]) sleep(0.001) if len(diffs) > 0: if not AUTO_WRITE: for ts in self.trellis._trelli: for t in ts: t.pixels.show() return def draw_note_grid(self, led_grid): for x in range(len(led_grid)): for y in range(len(led_grid[x])): color = self.col_scheme.get_color(led_grid[x][y], x, y) # col = c.PALLETE[led_grid[x][y]] self.led_matrix[x][self.grid_h - 1 - y] = color return def make_cb(self): def button_cb(xcoord, ycoord, edge): if edge == NeoTrellis.EDGE_RISING: self.button_bus.put({ 'cmd': 'note', 'x': xcoord, 'y': self.grid_h - 1 - ycoord }) return return button_cb def draw_load_screen(self): return
class Trellis(ActorThread): """Physical Trellis hardware""" 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 make_button_cb(self): def button_cb(xcoord, ycoord, edge): if edge == NeoTrellis.EDGE_RISING: post('button_grid', {'event': 'press', 'x': xcoord, 'y': H-1-ycoord}) elif edge == NeoTrellis.EDGE_FALLING: post('button_grid', {'event': 'release', 'x': xcoord, 'y': H-1-ycoord}) return return button_cb def blank_screen(self): self.cb_draw_grid({'led_grid': [[BLANK for x in range(W)] for y in range(H)]}) def cb_draw_grid(self, msg): led_grid = msg['led_grid'] diffs = [] for x in range(len(self.old_led_matrix)): for y in range(len(self.old_led_matrix[x])): if led_grid[x][y] != self.old_led_matrix[x][y]: col = led_grid[x][y] diffs.append((x, y, col)) self.old_led_matrix[x][y] = col self.trellis.color(diff[0], diff[1], diff[2]) sleep(0.001) # for diff in diffs: # self.trellis.color(diff[0], diff[1], diff[2]) # sleep(0.001) if len(diffs) > 0: if not AUTO_WRITE: for x, y in boards_to_update(diffs): pass self.trellis._trelli[x][y].pixels.show() # for ts in self.trellis._trelli: # for t in ts: # t.pixels.show() return def cb_anymsg(self, msg): self.cb_draw_grid(msg) return
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) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) #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, BLUE)