-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandle_sample.py
81 lines (67 loc) · 2.29 KB
/
candle_sample.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
import mpu6050, machine, neopixel, time
class Candle_sample:
def __init__(self, acllimitval, shakeslimitcount, color):
self.acl = None
while 1:
try:
self.acl = mpu6050.accel()
print("accel init");
break
except:
print("accelinit iicerror")
self.np = neopixel.NeoPixel(machine.Pin(14), 1)
self.np[0] = (10, 0, 0)
self.np.write()
self.acllimitval = acllimitval
self.shakeslimitcount = shakeslimitcount
self.shakescount = 0
self.val = None
self.color = color
self.vibrator = machine.Pin(12,machine.Pin.OUT)
self.vibrator.value(0)
def getval(self):
while 1:
try:
self.val = self.acl.get_values()
break
except:
print("getval0 iicerror")
def valbeyonlimits(self):
self.getval()
x = self.val["GyX"]
y = self.val["GyY"]
z = self.val["GyZ"]
velocity = (x**2 + y**2 + z**2)**0.5
print("veloc:",velocity)
if velocity > self.acllimitval:
return True
# if abs() > self.acllimitval or abs(self.val["GyY"]) > self.acllimitval or abs(
# self.val["GyZ"]) > self.acllimitval:
# print("1:" + str(self.val["GyX"]) + " " + str(self.val["GyY"]) + " " + str(self.val["GyZ"]))
# return True
return False
def gameover(self):
self.np[0] = (5, 0, 0)
self.np.write()
self.vibrator.value(1)
time.sleep(2)
self.vibrator.value(0)
def remapcol(self):
retcol = []
for col in self.color:
retcol.append(int((col / self.shakeslimitcount) * (self.shakeslimitcount - self.shakescount)))
return retcol
def start(self):
while 1:
print(self.shakescount)
if self.valbeyonlimits():
self.shakescount += 1
else:
if self.shakescount > 0:
self.shakescount -= 1
if self.shakescount > self.shakeslimitcount:
self.gameover()
self.shakescount = 0
self.np[0] = self.remapcol()
self.np.write()
candle = Candle_sample(5000, 10, [255, 50, 0])