-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinWave.py
60 lines (53 loc) · 2.09 KB
/
SinWave.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
import time
import math
import random
class SinWave:
def __init__(self, p):
self.panel = p
self.size = self.panel.width
self.waveHeight = 4.0
self.speed = 0.2
self.pixels1 = []
self.pixels2 = []
for i in range(self.size):
c = i / (self.size * 1.0)
rVal = self.lerp(0,255,c)
gVal = self.lerp(0,255,1 - c)
self.pixels1.append((rVal, gVal, 0))
for i in range(self.size):
c = i / (self.size * 1.0)
rVal = self.lerp(0,255,c)
gVal = self.lerp(0,255,1 - c)
self.pixels2.append((gVal, rVal, 0))
self.stamp = time.time()
def lerp(self, a, b, c):
return (c * a) + ((1-c) * b)
def update(self):
self.panel.setBackground((0,0,0))
t = (time.time() - self.stamp) * self.speed
maxY = round(self.panel.height / 3) + self.waveHeight
minY = round(self.panel.height / 3) + (self.waveHeight * -1)
for xVal in range(len(self.pixels1)):
p = self.pixels1[xVal]
yVal = round(self.panel.height / 3) + round(math.sin(xVal * t) * self.waveHeight)
c = (yVal-maxY) / ((maxY - minY) * 1.0)
c = abs(c)
rVal = self.lerp(255,0,c)
gVal = self.lerp(255,255, c)
bVal = self.lerp(0,234,c)
p = (rVal, gVal, bVal)
self.panel.setPixel(xVal,yVal,p,1)
maxY = (1-round(self.panel.height / 3)) + self.waveHeight
minY = (1-round(self.panel.height / 3)) + (self.waveHeight * -1)
for xVal in range(len(self.pixels2)):
p = self.pixels2[xVal]
yVal = (1 - round(self.panel.height / 3)) + round(math.sin(xVal * t) * self.waveHeight)
c = (yVal-maxY) / ((maxY - minY) * 1.0)
c = abs(c)
#226, 8, 255
#15, 255, 51
rVal = self.lerp(0,226,c)
gVal = self.lerp(255,8, c)
bVal = self.lerp(0,255,c)
p = (rVal, gVal, bVal)
self.panel.setPixel(xVal,yVal,p,1)