-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarbluino.ino
305 lines (271 loc) · 6.89 KB
/
marbluino.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <WiredDevice.h>
#include <RegisterBasedWiredDevice.h>
#include <Accelerometer.h>
#include <AccelerometerMMA8451.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#define BUZZER_PIN D8
#define DISPLAY_CS_PIN D3
#define DISPLAY_DC_PIN D0
#define DISPLAY_RS_PIN D4
#endif
#ifdef __AVR__
#include "LowPower.h"
#define BUZZER_PIN 6
#define DISPLAY_CS_PIN 10
#define DISPLAY_DC_PIN 9
#define DISPLAY_RS_PIN 8
#endif
/**
* Depending on how the sensor is oriented in relation to the display, we need to adjust sensor readings.
* Uncomment only one of ORN_X_ and ORN_Y_ so that the X and Y are read from correct sensor direction:
*/
//#define ORN_X_FROM_X
#define ORN_X_FROM_Y
//#define ORN_X_FROM_Z
#define ORN_Y_FROM_X
//#define ORN_Y_FROM_Y
//#define ORN_Y_FROM_Z
/**
* Depending on how the sensor is oriented, the reading needs to be inverted or not. Uncomment if any of
* X or Y reading needs to be inverted:
*/
#define ORN_X_INV
//#define ORN_Y_INV
#define BALLSIZE 4
#define ACC_FACTOR 0.5
#define BOUNCE_FACTOR -0.5
#define DELAY 50
#define MAX_TIMER 10*1000/DELAY
#define MIN_DISTANCE 30 // avoid spawning flags too close to the ball
#define BADDIE_RATE 5 // spawn new baddie on every nth gathered flag
U8G2_PCD8544_84X48_F_4W_HW_SPI u8g2(U8G2_R0, DISPLAY_CS_PIN, DISPLAY_DC_PIN, DISPLAY_RS_PIN);
AccelerometerMMA8451 acc(0);
struct fpoint {
float x;
float y;
};
struct upoint {
uint8_t x;
uint8_t y;
};
uint8_t max_x, max_y, points, timer = MAX_TIMER;
struct fpoint ball, speed = {0.0, 0.0};
struct upoint flag, baddies[5];
uint16_t tonesFlag[][2] = {{698, 1}, {880, 1}, {1047, 1}, {0, 0}};
uint16_t tonesLevel[][2] = {{1047, 1}, {988, 1}, {1047, 1}, {988, 1}, {1047, 1}, {0, 0}};
uint16_t tonesSad[][2] = {{262, 1}, {247, 1}, {233, 1}, {220, 3}, {0, 0}};
uint8_t melodyIndex;
uint16_t (*currentMelody)[2];
void startMotionDetection() {
acc.standby();
acc.setMotionDetection(false, true, 0x03);
acc.setMotionDetectionThreshold(false, 0x1a);
acc.setMotionDetectionCount(0x10);
acc.enableInterrupt(AccelerometerMMA8451::INT_FF_MT);
acc.routeInterruptToInt1(AccelerometerMMA8451::INT_FF_MT);
acc.activate();
}
void setupMMA()
{
acc.standby();
acc.disableInterrupt(AccelerometerMMA8451::INT_ALL);
acc.setDynamicRange(AccelerometerMMA8451::DR_2G);
acc.setOutputDataRate(AccelerometerMMA8451::ODR_50HZ_20_MS);
acc.activate();
}
void drawBoard(void) {
static char buf[12];
u8g2.clearBuffer();
// draw marble
u8g2.drawDisc(ball.x, ball.y, BALLSIZE/2);
// draw flag
u8g2.drawTriangle(flag.x, flag.y-3, flag.x-3, flag.y+2, flag.x+3, flag.y+2);
// draw baddies
for(int i=0; i <= baddiesCount(); i++) {
u8g2.drawFrame(baddies[i].x-2, baddies[i].y-2, 4, 4);
}
// write points and time
itoa(points, buf, 10);
u8g2.drawStr(0, 5, buf);
itoa(timer/10, buf, 10);
uint8_t width = u8g2.getStrWidth(buf);
u8g2.drawStr(max_x-width, 5, buf);
u8g2.sendBuffer();
}
void showPopup(char *line_1, char *line_2) {
u8g2.clearBuffer();
u8g2.drawRFrame(0, 0, max_x, max_y, 7);
uint8_t width = u8g2.getStrWidth(line_1);
u8g2.drawStr((max_x-width)/2, max_y/2 - 2, line_1);
width = u8g2.getStrWidth(line_2);
u8g2.drawStr((max_x-width)/2, max_y/2 + 8, line_2);
u8g2.sendBuffer();
}
void placeRandomly(struct upoint *point) {
do {
(*point).x = random(max_x - 2*BALLSIZE) + BALLSIZE;
(*point).y = random(max_y - 2*BALLSIZE) + BALLSIZE;
// ensure not spawning too close to the ball
} while (abs((*point).x-ball.x) + abs((*point).y-ball.y) < MIN_DISTANCE);
}
// used to play the melody asynchronously while the user is playing
void playSound(void) {
if (currentMelody) {
uint8_t totalCount = 0;
for (uint8_t i = 0; 1; i++) {
uint16_t freq = currentMelody[i][0];
uint16_t dur = currentMelody[i][1];
if (melodyIndex == totalCount) {
if (dur == 0) {
noTone(BUZZER_PIN);
currentMelody = NULL;
melodyIndex = 0;
} else {
tone(BUZZER_PIN, freq);
}
}
totalCount += dur;
if (totalCount > melodyIndex)
break;
}
melodyIndex++;
}
}
int baddiesCount(void) {
return points/BADDIE_RATE;
}
bool isCollided(struct upoint point) {
return abs(ball.x-point.x) < 3 && abs(ball.y-point.y) < 3;
}
void melodySad(void) {
// this is played synchronously
for (uint8_t i = 0; tonesSad[i][1] > 0; i++) {
tone(BUZZER_PIN, tonesSad[i][0], tonesSad[i][1]*300);
delay(tonesSad[i][1] * 300 + 50);
}
}
void melodyFlag(void) {
currentMelody = tonesFlag;
melodyIndex = 0;
}
void melodyLevel(void) {
currentMelody = tonesLevel;
melodyIndex = 0;
}
void gameOver(void) {
char msg[50];
sprintf(msg, "score: %d", points);
showPopup("GAME OVER", msg);
melodySad();
points = 0;
timer = MAX_TIMER;
ball.x = max_x / 2;
ball.y = max_y / 2;
placeRandomly(&flag);
}
void checkCollision(void) {
for(int i=0; i <= baddiesCount(); i++) {
if (isCollided(baddies[i])) {
return gameOver();
}
}
if (isCollided(flag)) {
placeRandomly(&flag);
points++;
timer = MAX_TIMER;
if (points % BADDIE_RATE == 0) {
// spawn new baddie
placeRandomly(&(baddies[points / BADDIE_RATE]));
melodyLevel();
} else {
melodyFlag();
}
}
}
void updateMovement(void) {
ball.x += speed.x;
ball.y += speed.y;
#ifdef ORN_X_FROM_X
float xg = acc.readXg();
#endif
#ifdef ORN_X_FROM_Y
float xg = acc.readYg();
#endif
#ifdef ORN_X_FROM_Z
float xg = acc.readZg();
#endif
#ifdef ORN_Y_FROM_X
float yg = acc.readXg();
#endif
#ifdef ORN_Y_FROM_Y
float yg = acc.readYg();
#endif
#ifdef ORN_Y_FROM_Z
float yg = acc.readZg();
#endif
#ifdef ORN_X_INV
xg = -xg;
#endif
#ifdef ORN_Y_INV
yg = -yg;
#endif
speed.x += ACC_FACTOR * xg;
speed.y += ACC_FACTOR * yg;
}
// bounce off walls with a diminishing factor
void bounce(void) {
if ((speed.x > 0 && ball.x >= max_x-BALLSIZE) || (speed.x < 0 && ball.x <= BALLSIZE)) {
speed.x = BOUNCE_FACTOR * speed.x;
}
if ((speed.y > 0 && ball.y >= max_y-BALLSIZE) || (speed.y < 0 && ball.y <= BALLSIZE)) {
speed.y = BOUNCE_FACTOR * speed.y;
}
}
void goToSleep() {
showPopup("SLEEPING...", "shake to wake");
startMotionDetection();
#ifdef ESP8266
ESP.deepSleep(0);
#endif
#ifdef __AVR__
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
#endif
}
void setup(void) {
randomSeed(analogRead(0));
#ifdef DEBUG
Serial.begin(115200);
#endif
#ifdef ESP8266
WiFi.mode(WIFI_OFF);
#endif
setupMMA();
u8g2.begin();
u8g2.setFont(u8g2_font_baby_tf);
max_x = u8g2.getDisplayWidth();
max_y = u8g2.getDisplayHeight();
ball.x = max_x / 2;
ball.y = max_y / 2;
placeRandomly(&flag);
}
void loop(void) {
bounce();
checkCollision();
drawBoard();
updateMovement();
playSound();
delay(DELAY);
if (timer > 0) {
timer--;
} else {
if (points == 0) {
goToSleep();
} else {
gameOver();
}
}
}