-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhourglass_new.ino
160 lines (132 loc) · 3.87 KB
/
hourglass_new.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "bitmaps.h"
#include "timer_draw.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050_modified.h>
#define swap(a, b) (a ^= b ^= a ^= b)
#define OPENING_POS_X 32
#define OPENING_POS_Y 63
Adafruit_MPU6050 gyro;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
Timer timer;
int random_update_pos[64];
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setRotation(1);
draw_hourglass(&display);
display.setTextColor(true);
start_timer(&display, &timer, 60 * 60);
for (int i = 0; i < 64; i++) {
random_update_pos[i] = i;
}
}
void loop() {
randomize_updates(random_update_pos);
update_sim(&display, &timer, 0, random_update_pos);
draw_remaining_time(&display, &timer);
sensors_event_t acc;
gyro.getEvent(&acc);
display.setCursor(0, 0);
display.display();
update_timer(&timer);
}
bool is_valid(Adafruit_SSD1306 *display, int x, int y) {
if (x < 0 || x >= display->width() || y < 0 || y >= display->height()) {
return false;
}
int index = y * display->width() + x;
return !((pgm_read_byte(&(bitmap_valid_pixels[index / 8])) << (index % 8)) &
0x80);
}
void draw_hourglass(Adafruit_SSD1306 *display) {
display->clearDisplay();
display->drawBitmap(0, 0, bitmap_hourglass_texture, 64, 128, true);
}
void fill_hourglass(Adafruit_SSD1306 *display, int sand_count) {
for (int y = display->height() / 2 - 1; y >= 0; y--) {
for (int x = display->width(); x >= 0; x--) {
if (sand_count == 0) {
return;
}
if (is_valid(display, x, y)) {
display->drawPixel(x, y, true);
sand_count--;
}
}
}
}
void update_sand(Adafruit_SSD1306 *display, int x, int y, int rotation) {
static char neighs[4][3][2] = {{{0, 1}, {-1, 1}, {1, 1}},
{{1, 0}, {1, 1}, {1, -1}},
{{0, -1}, {1, -1}, {-1, -1}},
{{-1, 0}, {-1, -1}, {-1, 1}}};
if (!is_valid(display, x, y) || !display->getPixel(x, y)) {
return;
}
for (int i = 0; i < 3; i++) {
int nx = x + neighs[rotation][i][0], ny = y + neighs[rotation][i][1];
if (!is_valid(display, nx, ny)) {
continue;
}
if (!display->getPixel(nx, ny)) {
display->drawPixel(nx, ny, true);
display->drawPixel(x, y, false);
return;
}
}
}
void update_sim(Adafruit_SSD1306 *display, Timer *timer, int rotation,
int *random_pos) {
for (int y = 0; y < display->height(); y++) {
for (int *x = random_pos; x < random_pos + 64; x++) {
int update_x, update_y;
switch (rotation) {
case 0:
update_y = display->height() - y - 1;
update_x = *x;
break;
case 1:
update_y = y / 64 ? *x + 64 : *x;
update_x = y % 64;
update_x = 64 - update_x - 1;
break;
case 2:
update_x = *x;
update_y = y;
break;
case 3:
update_y = y / 64 ? *x + 64 : *x;
update_x = y % 64;
break;
}
if (!(update_x == OPENING_POS_X && update_y == OPENING_POS_Y) ||
is_opening_open(display, timer)) {
update_sand(display, update_x, update_y, rotation);
}
}
}
}
void randomize_updates(int *positions) {
for (int i = 0; i < 64; i++) {
int a = random(0, 64);
int b = random(0, 64);
int temp = positions[a];
positions[a] = positions[b];
positions[b] = temp;
}
}
void start_timer(Adafruit_SSD1306 *display, Timer *timer, int seconds) {
draw_hourglass(display);
create_timer(timer, seconds);
fill_hourglass(display, seconds / (SAND_TIME / 1000) + 1);
}
bool is_opening_open(Adafruit_SSD1306 *display, Timer *timer) {
bool opening_pixel = display->getPixel(OPENING_POS_X, OPENING_POS_Y);
if (!opening_pixel) {
return true;
}
if (timer->sand_let_through) {
timer->sand_let_through--;
return true;
}
return false;
}