-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.py
executable file
·150 lines (125 loc) · 4.57 KB
/
topology.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
#!/usr/bin/python3
from launchpad import *
from random import *
from common import *
import numpy as np
import matplotlib.pyplot as plt
import colorsys
import time
lp = LaunchpadMiniMk3(pickPortInteractive())
grid = None
pressedButtons = []
# setup the figure and axes
# fig = plt.figure(figsize=(8, 3))
# ax1 = fig.add_subplot(121, projection='3d')
# ax2 = fig.add_subplot(122, projection='3d')
# fake data
# _x = np.arange(4)
# _y = np.arange(5)
# _xx, _yy = np.meshgrid(_x, _y)
# x, y = _xx.ravel(), _yy.ravel()
# top = x + y
# bottom = np.zeros_like(top)
# width = depth = 1
# ax1.bar3d(x, y, bottom, width, depth, top, shade=True)
# ax1.set_title('Shaded')
# ax2.bar3d(x, y, bottom, width, depth, top, shade=False)
# ax2.set_title('Not Shaded')
# plt.show()
class Plotter:
def __init__(self):
pass
class Grid:
def __init__(self):
self.gridArr = [[0 for j in range(0, GRID_HEIGHT)] for i in range(0, GRID_WIDTH)]
self.decay = 1
def getCellValue(self, x, y):
return self.gridArr[x][y]
def setCellValue(self, x, y, val):
self.gridArr[x][y] = val
def printGridInt(self):
for j in range(0, GRID_HEIGHT):
print(["%04d" % int(i) for i in self.gridArr[j]])
print("\n")
def update(self):
newGridArr = [[0 for j in range(0, GRID_HEIGHT)] for i in range(0, GRID_WIDTH)]
for i in range(0, GRID_WIDTH):
for j in range(0, GRID_HEIGHT):
neighborVals = []
neighborVals.append(self.getCellValue((i+1)%GRID_WIDTH, j))
neighborVals.append(self.getCellValue((i-1)%GRID_WIDTH, j))
neighborVals.append(self.getCellValue(i, (j+1)%GRID_HEIGHT))
neighborVals.append(self.getCellValue(i, (j-1)%GRID_HEIGHT))
neighborVals.append(self.getCellValue((i+1)%GRID_WIDTH, (j+1)%GRID_HEIGHT))
neighborVals.append(self.getCellValue((i-1)%GRID_WIDTH, (j+1)%GRID_HEIGHT))
neighborVals.append(self.getCellValue((i+1)%GRID_HEIGHT, (j-1)%GRID_HEIGHT))
neighborVals.append(self.getCellValue((i-1)%GRID_HEIGHT, (j-1)%GRID_HEIGHT))
avg = sum(neighborVals)/len(neighborVals)
diff = self.getCellValue(i, j) - avg
# print(int(diff))
decay = 0#(avg / 100)
newVal = avg + (-1 * decay if avg >= 0 else decay)
# newVal = avg - self.decay
# newVal = (0 if newVal < 0 else newVal)
# newVal = (500 if newVal > 500 else newVal)
newGridArr[i][j] = newVal
self.gridArr = newGridArr
def draw(self):
baseH = 200
baseS = 0
baseV = 100
for i in range(0, GRID_WIDTH):
for j in range(0, GRID_HEIGHT):
h = baseH + (self.getCellValue(i, j) / 2) / 2
# h = (3*H_MAX if h > 3*H_MAX else h)
s = baseS + self.getCellValue(i,j) / 5
s = (100 if s > 100 else s)
v = baseV
# v = self.getCellValue(i,j) * 100 / 500
# v = (100 if v > 100 else v)
cellColor = colorsys.hsv_to_rgb((h)/H_MAX, s/S_MAX, v/V_MAX)
cellColor = [i * 127 for i in cellColor]
lp.SetPixelRgb(i, j, int(cellColor[0]), int(cellColor[1]), int(cellColor[2]))
def buttonRelease(msg):
x = int(msg.note % 10) - 1
y = int(msg.note / 10) - 1
for i in range(0, len(pressedButtons)):
if (pressedButtons[i][0] == x and pressedButtons[i][1] == y):
del(pressedButtons[i])
break
print("BUTTON RELEASE", msg)
def controlChange(msg):
print("CONTROL CHANGE", msg)
def buttonPress(msg):
x = int(msg.note % 10) - 1
y = int(msg.note / 10) - 1
pressedButtons.append((x, y))
print("BUTTON PRESS x %d, y %d" % (x, y))
def processButtonPresses():
global grid
for b in pressedButtons:
currVal = grid.getCellValue(b[0], b[1])
newVal = currVal + 1000
grid.setCellValue(b[0], b[1], newVal)
def main():
global grid
# Register launchpad callbacks
lp.onButtonPressCb = buttonPress
lp.onButtonReleaseCb = buttonRelease
lp.onControlChangeCb = controlChange
lp.ClearGrid()
lp.SelectLayout(LAYOUT_PROGRAMMER)
grid = Grid()
running = True
while(running):
try:
lp.Poll()
processButtonPresses()
grid.update()
grid.draw()
time.sleep(0.04)
except KeyboardInterrupt:
running = False
lp.SelectLayout(LAYOUT_CUSTOM1)
if __name__ == "__main__":
main()