-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhourglass.ino
131 lines (108 loc) · 3.02 KB
/
hourglass.ino
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
#include <Adafruit_LIS3DH.h>
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
#define FLAT_UP 0
#define FLAT_DOWN 1
#define PORTRAIT_UP 2
#define PORTRAIT_DOWN 3
#define LANDSCAPE_LEFT 4
#define LANDSCAPE_RIGHT 5
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_IS31FL3731.h>
Adafruit_IS31FL3731 ledmatrix = Adafruit_IS31FL3731();
#include <math.h>
long startmillis;
long duration = 1000 * 10;
void setup() {
Serial.begin(9600);
delay(100);
_initAccel();
_initLed();
startmillis = millis();
}
void loop() {
_getOrientation();
ledmatrix.setRotation(1);
ledmatrix.clear();
_drawHourglassShape();
float progress = (float) (millis() - startmillis) / duration;
if (progress > 1.0) {
progress = 1;
}
_drawSand(progress);
delay(100);
}
void _initAccel() {
Serial.println("LIS3DH test!");
while (!lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
yield();
}
Serial.println("LIS3DH found!");
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
}
int _getOrientation() {
sensors_event_t event;
lis.getEvent(&event);
float x, y, z;
x = event.acceleration.x;
y = event.acceleration.y;
z = event.acceleration.z;
if (z > 9) {
return FLAT_UP;
} else if (z < -9) {
return FLAT_DOWN;
} else if (x < -9) {
return LANDSCAPE_LEFT;
} else if (x > 9) {
return LANDSCAPE_RIGHT;
} else if (y < -9) {
return PORTRAIT_UP;
} else if (y > 9) {
return PORTRAIT_DOWN;
}
return -1;
}
void _initLed() {
Serial.println("IS31 test");
while (! ledmatrix.begin()) {
Serial.println("IS31 not found");
yield();
}
Serial.println("IS31 found!");
}
void _drawHourglassShape() {
#define shapeBrightness 72
ledmatrix.drawFastHLine(0, 0, 9, shapeBrightness);
ledmatrix.drawFastVLine(0, 0, 4, shapeBrightness);
ledmatrix.drawFastVLine(8, 0, 4, shapeBrightness);
ledmatrix.drawLine(0, 4, 3, 7, shapeBrightness);
ledmatrix.drawLine(8, 4, 5, 7, shapeBrightness);
ledmatrix.drawLine(0, 11, 3, 8, shapeBrightness);
ledmatrix.drawLine(8, 11, 5, 8, shapeBrightness);
ledmatrix.drawFastVLine(0, 12, 4, shapeBrightness);
ledmatrix.drawFastVLine(8, 12, 4, shapeBrightness);
ledmatrix.drawFastHLine(0, 15, 9, shapeBrightness);
}
void _drawSand(float progress) {
#define sandBrightness 32
if (progress < 1) {
// upper
// triangle flows through neck
int upperTop = 3 + 4 * pow(progress, 1.5);
int upperLeft = upperTop / 2;
int upperRight = 8 - upperTop / 2;
ledmatrix.fillTriangle(upperLeft, upperTop, upperRight, upperTop, 4, 7, sandBrightness);
// lower
// triangle grows
int lowerTop = 15 - 4 * pow(progress, 1.0/1.5);
int lowerBottom = lowerTop + 4;
ledmatrix.fillTriangle(0, lowerBottom, 8, lowerBottom, 4, lowerTop, sandBrightness);
// stream
ledmatrix.drawLine(4, 8, 4, 15, sandBrightness);
} else {
ledmatrix.fillTriangle(0, 15, 8, 15, 4, 11, sandBrightness);
}
}