-
Notifications
You must be signed in to change notification settings - Fork 5
/
dizzy.h
108 lines (95 loc) · 1.79 KB
/
dizzy.h
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
float lastgyro = 0;
CRGB pickPass(int v)
{
switch (v)
{
case 0:
return CRGB(0, 0, 0);
case 1:
return CRGB(255, 0, 0);
case 2:
return CRGB(0, 255, 0);
case 3:
return CRGB(0, 0, 255);
default:
return CRGB(64, 64, 64);
}
}
int clamp(int a, int b, int c)
{
if (a < b)
return b;
if (a > c)
return c;
return a;
}
void DizzyGame( uint8_t *state, CRGB *display, int size) {
#ifdef USEACCEL
static char *valid = "0001010100111110";
static int quads[4];
static float lax, laz;
static float laa;
static int level = 5;
float ax = mpu6050.getAccX();
float ay = mpu6050.getAccY();
float az = mpu6050.getAccZ();
/*
Serial.print(ax);
Serial.print(",");
Serial.print(ay);
Serial.print(",");
Serial.print(az);
Serial.println("");
*/
float plane = sqrt((ax * ax) + (az * az));
float total = sqrt((ax * ax) + (ay * ay));
int quad = 0;
if (ax > 0)
quad += 1;
if (az > 0)
quad += 2;
int maxquad = 0;
quads[quad] = 0;
int delta = 1;
int mask = 0;
for (int i = 0; i < 4; i++)
{
quads[i]++;
mask = mask << 1;
if (quads[i] > 50)
{
quads[i] = 50;
mask += 1;
}
}
if (ay > 2.0)
delta = -1;
if (ay < -2.0)
delta = -1;
if (valid[mask] == '0')
delta = -1;
if (total < 1.2)
delta = -1;
if (plane < 0.2)
delta = -1;
bool maxed = (level + delta) > 400;
level = clamp(level + delta, 5, 400);
int scaled = (level * size) / 100;
int base = scaled / size;
int mod = scaled % size;
CRGB low = pickPass(base);
CRGB hi = pickPass(base + 1);
if (maxed)
{
low = CRGB(0, 255, 255);
hi = CRGB(0, 255, 255);
}
for (int i = 0; i < size; i++)
{
if (i > mod)
display[i] = low;
else
display[i] = hi;
}
#endif
}