-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilestone2.py
More file actions
132 lines (112 loc) · 3.87 KB
/
Copy pathMilestone2.py
File metadata and controls
132 lines (112 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This is a test example for the Adafruit Trellis w/HT16K33
#
# Designed specifically to work with the Adafruit Trellis
# ----> https://www.adafruit.com/products/1616
# ----> https://www.adafruit.com/products/1611
#
# These displays use I2C to communicate, 2 pins are required to
# interface
# Adafruit invests time and resources providing this open source code,
# please support Adafruit and open-source hardware by purchasing
# products from Adafruit!
#
# Written by Limor Fried/Ladyada for Adafruit Industries.
# MIT license, all text above must be included in any redistribution
#
# Python port created by Tony DiCola (tony@tonydicola.com).
import thread
import os
import time
import Adafruit_Trellis
# This example shows reading buttons and setting/clearing buttons in a loop
# "momentary" mode has the LED light up only when a button is pressed
# "latching" mode lets you turn the LED on/off when pressed
#
# Up to 8 matrices per I2C bus can be used but this example will show 4 or 1
MOMENTARY = 0
LATCHING = 1
# Set the mode here:
MODE = LATCHING
def playSound(sound):
os.system("aplay %s" % str(sound))
matrix0 = Adafruit_Trellis.Adafruit_Trellis()
# uncomment the below to add 3 more matrices
#
# matrix1 = Adafruit_Trellis()
# matrix2 = Adafruit_Trellis()
# matrix3 = Adafruit_Trellis()
# you can add more matrices (up to 8 per I2C bus)
# Just one
trellis = Adafruit_Trellis.Adafruit_TrellisSet(matrix0)
# or use the below to select 4, up to 8 per I2C bus can be passed in
# trellis = Adafruit_Trellis.Adafruit_TrellisSet(matrix0, matrix1, matrix2, matrix3)
# set to however many you're working with here, up to 8 per I2C bus
NUMTRELLIS = 1
numKeys = NUMTRELLIS * 16
# Connect Trellis Vin to 5V and Ground to ground.
# Connect Trellis INT wire to a digital input (optional)
# Connect Trellis I2C SDA pin to your board's SDA line
# Connect Trellis I2C SCL pin to your board's SCL line
# All Trellises on an I2C bus share the SDA, SCL and INT pin!
# Even 8 tiles use only 3 wires max
# Set this to the number of the I2C bus that the Trellises are attached to:
I2C_BUS = 1
# Setup
print 'Trellis Demo'
# TODO: Setup the INT input
# begin() with the I2C addresses and bus numbers of each panel in order
# I find it easiest if the addresses are in order
trellis.begin((0x70, I2C_BUS)) # only one
# trellis.begin((0x70, I2C_BUS), (0x71, I2C_BUS), (0x72, I2C_BUS), (0x73, I2C_BUS)) # or four!
# light up all the LEDs in order
for i in range(numKeys):
trellis.setLED(i)
trellis.writeDisplay()
time.sleep(0.05)
# then turn them off
for i in range(numKeys):
trellis.clrLED(i)
trellis.writeDisplay()
time.sleep(0.05)
# Loop
print 'Press Ctrl-C to quit.'
while True:
time.sleep(0.03)
if MODE == MOMENTARY:
# If a button was just pressed or released...
if trellis.readSwitches():
# go through every button
for i in range(numKeys):
# if it was pressed, turn it on
if trellis.justPressed(i):
print 'v{0}'.format(i)
if i==0:
os.system('aplay Bass-Drum-1.wav')
if i==1:
os.system('aplay Ensoniq-ESQ-1-Snare.wav')
trellis.setLED(i)
# if it was released, turn it off
if trellis.justReleased(i):
print '^{0}'.format(i)
trellis.clrLED(i)
# tell the trellis to set the LEDs we requested
trellis.writeDisplay()
if MODE == LATCHING:
# If a button was just pressed or released...
if trellis.readSwitches():
# go through every button
for i in range(numKeys):
# if it was pressed...
if trellis.justPressed(i):
print 'v{0}'.format(i)
if i==0:
thread.start_new_thread(playSound, ('Bass-Drum-1.wav',))
if i==1:
thread.start_new_thread(playSound, ('Ensoniq-ESQ-1-Snare.wav',))
# Alternate the LED
if trellis.isLED(i):
trellis.clrLED(i)
else:
trellis.setLED(i)
# tell the trellis to set the LEDs we requested
trellis.writeDisplay()