-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.py
184 lines (140 loc) · 5.51 KB
/
web.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from __future__ import print_function
from flask import Flask, render_template, request
from .the_matrix import TheMatrix, DEFAULT_CURRENT_SOURCE_MA
from .layout import Layout
from .detect import detect
import getopt, re, sys
bus_number = 1
app = Flask(__name__)
def usage():
print("Usage: {} [-b <bus_number>]".format(sys.argv[0]), file=sys.stderr)
try:
opts, args = getopt.getopt(sys.argv[1:], "hb:")
except getopt.GetoptError:
usage()
sys.exit(2)
if len(args+opts) == 0:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt == '-b':
bus_number = int(arg)
addresses = detect(bus_number)
matrices = dict((address, TheMatrix(address, bus_number=bus_number)) for address in addresses)
blinkPWMFrames = dict((address, None) for address in addresses)
onOffFrames = dict((address, None) for address in addresses)
isReversed = dict((address, False) for address in addresses)
ledCurrents = dict((address, DEFAULT_CURRENT_SOURCE_MA) for address in addresses)
resetCurrents = dict((address, DEFAULT_CURRENT_SOURCE_MA) for address in addresses)
cs_pairs = [(anode, cathode) for cathode in range(12) for anode in [a for a in range(12) if a != cathode][:10]]
chip = [{'label': label} for label in 'GND CS6 VDD CS7 CS8 CS9 GND CS11 CS10 VDD IRQ SYNC RSTN GND SCL SDA ADDR VDD VDD CS5 CS4 GND CS2 CS3 CS0 VDD CS1 GND'.split()]
for pin in chip:
match = re.match('^CS(\d+)', pin['label'])
if match:
signal = int(match.group(1))
pin['clickable'] = True
# get anode-connected LEDs
anode_led_indices = [i for i in range(len(cs_pairs)) if cs_pairs[i][0] == signal]
pin['anode_leds'] = [[int(i/5), i%5] for i in anode_led_indices]
# get cathode-connected LEDs
cathode_led_indices = [i for i in range(len(cs_pairs)) if cs_pairs[i][1] == signal]
pin['cathode_leds'] = [[int(i/5), i%5] for i in cathode_led_indices]
def updateFrame(address, frameNumber):
matrices[address].writeOnOffFrame(frameNumber, onOffFrames[address])
return ""
@app.route('/')
def main_route():
pixels = dict((address, [[onOffFrames[address].getPixel(x, y) for x in range(24)] for y in range(5)]) for address in matrices.keys())
return render_template(
'the_matrix.html',
width=24,
height=5,
current=ledCurrents,
resetCurrents=resetCurrents,
pixels=pixels,
chip=chip,
reversed=isReversed,
addresses=sorted(matrices.keys())
)
@app.route('/reset', methods=['POST'])
def reset():
try:
addresses = [int(request.form['address'])]
except:
addresses = matrices.keys()
for address in addresses:
isReversed[address] = False
matrix = matrices[address]
matrix.reset()
matrix.selectMemoryConfig(1)
ledCurrents[address] = DEFAULT_CURRENT_SOURCE_MA
matrix.setCurrentSource(ledCurrents[address])
layout = Layout(reversed=isReversed[address])
blinkPWMFrames[address] = TheMatrix.BlinkPWMFrame(layout=layout)
matrix.writeBlinkPWMFrame(0, blinkPWMFrames[address])
onOffFrames[address] = TheMatrix.OnOffFrame(layout=layout)
matrix.writeOnOffFrame(0, onOffFrames[address])
matrix.setDisplayOptions()
matrix.display(1)
matrix.displayPictureFrame(0)
return ""
@app.route('/setCurrent', methods=['POST'])
def setCurrent():
address = int(request.form['address'])
current = int(request.form['current'])
ledCurrents[address] = current
matrices[address].setCurrentSource(ledCurrents[address])
return ""
@app.route('/allOff', methods=['POST'])
def allOff():
address = int(request.form['address'])
blinkPWMFrames[address] = TheMatrix.BlinkPWMFrame()
onOffFrames[address] = TheMatrix.OnOffFrame(0)
matrix = matrices[address]
matrix.writeBlinkPWMFrame(0, blinkPWMFrames[address])
matrix.writeOnOffFrame(0, onOffFrames[address])
return ""
@app.route('/allOn', methods=['POST'])
def allOn():
address = int(request.form['address'])
blinkPWMFrames[address] = TheMatrix.BlinkPWMFrame()
onOffFrames[address] = TheMatrix.OnOffFrame(1)
matrix = matrices[address]
matrix.writeBlinkPWMFrame(0, blinkPWMFrames[address])
matrix.writeOnOffFrame(0, onOffFrames[address])
return ""
@app.route('/setPixel', methods=['POST'])
def setPixel():
address = int(request.form['address'])
for coords in request.form.getlist('coords[]'):
x, y = [int(i) for i in coords.split(',')]
onOffFrames[address].setPixel(x, y)
return updateFrame(address, 0)
@app.route('/clearPixel', methods=['POST'])
def clearPixel():
address = int(request.form['address'])
for coords in request.form.getlist('coords[]'):
x, y = [int(i) for i in coords.split(',')]
onOffFrames[address].setPixel(x, y, 0)
return updateFrame(address, 0)
@app.route('/setReversed', methods=['POST'])
def setReversed():
address = int(request.form['address'])
reversedFlag = request.form['reversed']
newReversed = reversedFlag == 'true'
if newReversed != isReversed[address]:
isReversed[address] = newReversed
newLayout = Layout(reversed=newReversed)
blinkPWMFrames[address].layout = newLayout
onOffFrames[address].layout = newLayout
matrix = matrices[address]
matrix.writeBlinkPWMFrame(0, blinkPWMFrames[address])
matrix.writeOnOffFrame(0, onOffFrames[address])
return ""
def main():
reset()
app.run(host='0.0.0.0')
if __name__ == "__main__":
main()