-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbouncing_ball_mode.h
More file actions
376 lines (326 loc) · 10.2 KB
/
Copy pathbouncing_ball_mode.h
File metadata and controls
376 lines (326 loc) · 10.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef BOUNCING_BALL_MODE_H
#define BOUNCING_BALL_MODE_H
#include "common_definitions.h"
#include "ui_elements.h"
#include "midi_utils.h"
// Pong-style Ambient MIDI mode variables
struct Ball {
float x, y;
float vx, vy;
uint16_t color;
int size;
bool active;
};
#define MAX_BALLS 4
Ball balls[MAX_BALLS];
int numActiveBalls = 1;
// Simple wall system - notes triggered by wall hits
struct Wall {
int x, y, w, h;
int note;
String noteName;
uint16_t color;
bool active;
unsigned long activeTime;
int side; // 0=top, 1=right, 2=bottom, 3=left
};
#define NUM_WALLS 24 // 8 top + 8 bottom + 4 left + 4 right
Wall walls[NUM_WALLS];
int ballScale = 0; // Scale selection
int ballKey = 0; // Key selection
int ballOctave = 4;
// Function declarations
void initializeBouncingBallMode();
void drawBouncingBallMode();
void handleBouncingBallMode();
void initializeBalls();
void initializeWalls();
void updateBouncingBall();
void updateBalls();
void drawBalls();
void drawWalls();
void checkWallCollisions();
// Implementations
void initializeBouncingBallMode() {
ballScale = 0;
ballKey = 0;
ballOctave = 4;
numActiveBalls = 1;
initializeBalls();
initializeWalls();
}
void drawBouncingBallMode() {
tft.fillScreen(THEME_BG);
drawHeader("ZEN", "Ambient Bouncing");
// Controls
drawRoundButton(10, 200, 40, 25, "ADD", THEME_SUCCESS);
drawRoundButton(60, 200, 40, 25, "RESET", THEME_WARNING);
drawRoundButton(110, 200, 50, 25, "SCALE", THEME_ACCENT);
drawRoundButton(170, 200, 40, 25, "KEY-", THEME_SECONDARY);
drawRoundButton(220, 200, 40, 25, "KEY+", THEME_SECONDARY);
drawRoundButton(270, 200, 40, 25, "OCT", THEME_PRIMARY);
// Status display
tft.setTextColor(THEME_TEXT_DIM, THEME_BG);
String keyName = getNoteNameFromMIDI(ballKey);
tft.drawString(keyName + " " + scales[ballScale].name, 10, 180, 1);
tft.drawString("Oct:" + String(ballOctave), 200, 180, 1);
tft.drawString("Balls:" + String(numActiveBalls), 270, 180, 1);
drawWalls();
drawBalls();
}
void initializeBalls() {
for (int i = 0; i < MAX_BALLS; i++) {
balls[i].x = random(80, 240);
balls[i].y = random(80, 150);
// Slower, more zen-like movement
balls[i].vx = random(-15, 15) / 10.0; // -1.5 to 1.5
balls[i].vy = random(-15, 15) / 10.0;
if (abs(balls[i].vx) < 0.5) balls[i].vx = (balls[i].vx >= 0) ? 0.8 : -0.8;
if (abs(balls[i].vy) < 0.5) balls[i].vy = (balls[i].vy >= 0) ? 0.8 : -0.8;
// Softer, more zen colors
balls[i].color = random(0x2000, 0x8FFF);
balls[i].size = random(4, 7);
balls[i].active = (i < numActiveBalls);
}
}
void initializeWalls() {
int wallIndex = 0;
// Top wall - 8 segments
for (int i = 0; i < 8; i++) {
walls[wallIndex].x = 50 + i * 28;
walls[wallIndex].y = 60;
walls[wallIndex].w = 28;
walls[wallIndex].h = 3;
walls[wallIndex].note = getNoteInScale(ballScale, i, ballOctave) + ballKey;
walls[wallIndex].noteName = getNoteNameFromMIDI(walls[wallIndex].note);
walls[wallIndex].color = THEME_PRIMARY;
walls[wallIndex].active = false;
walls[wallIndex].side = 0;
wallIndex++;
}
// Right wall - 4 segments
for (int i = 0; i < 4; i++) {
walls[wallIndex].x = 272;
walls[wallIndex].y = 63 + i * 28;
walls[wallIndex].w = 3;
walls[wallIndex].h = 28;
walls[wallIndex].note = getNoteInScale(ballScale, i, ballOctave + 1) + ballKey;
walls[wallIndex].noteName = getNoteNameFromMIDI(walls[wallIndex].note);
walls[wallIndex].color = THEME_SECONDARY;
walls[wallIndex].active = false;
walls[wallIndex].side = 1;
wallIndex++;
}
// Bottom wall - 8 segments
for (int i = 0; i < 8; i++) {
walls[wallIndex].x = 50 + i * 28;
walls[wallIndex].y = 177;
walls[wallIndex].w = 28;
walls[wallIndex].h = 3;
walls[wallIndex].note = getNoteInScale(ballScale, 7 - i, ballOctave) + ballKey;
walls[wallIndex].noteName = getNoteNameFromMIDI(walls[wallIndex].note);
walls[wallIndex].color = THEME_ACCENT;
walls[wallIndex].active = false;
walls[wallIndex].side = 2;
wallIndex++;
}
// Left wall - 4 segments
for (int i = 0; i < 4; i++) {
walls[wallIndex].x = 50;
walls[wallIndex].y = 63 + i * 28;
walls[wallIndex].w = 3;
walls[wallIndex].h = 28;
walls[wallIndex].note = getNoteInScale(ballScale, 3 - i, ballOctave + 1) + ballKey;
walls[wallIndex].noteName = getNoteNameFromMIDI(walls[wallIndex].note);
walls[wallIndex].color = THEME_WARNING;
walls[wallIndex].active = false;
walls[wallIndex].side = 3;
wallIndex++;
}
}
void handleBouncingBallMode() {
// Back button
if (touch.justPressed && isButtonPressed(10, 10, 50, 25)) {
exitToMenu();
return;
}
if (touch.justPressed) {
// Add ball button
if (isButtonPressed(10, 200, 40, 25)) {
if (numActiveBalls < MAX_BALLS) {
numActiveBalls++;
initializeBalls();
drawBouncingBallMode();
}
return;
}
// Reset button
if (isButtonPressed(60, 200, 40, 25)) {
numActiveBalls = 1;
initializeBalls();
drawBouncingBallMode();
return;
}
// Scale button
if (isButtonPressed(110, 200, 50, 25)) {
ballScale = (ballScale + 1) % NUM_SCALES;
initializeWalls();
drawBouncingBallMode();
return;
}
// Key controls
if (isButtonPressed(170, 200, 40, 25)) {
ballKey = (ballKey - 1 + 12) % 12;
initializeWalls();
drawBouncingBallMode();
return;
}
if (isButtonPressed(220, 200, 40, 25)) {
ballKey = (ballKey + 1) % 12;
initializeWalls();
drawBouncingBallMode();
return;
}
// Octave button
if (isButtonPressed(270, 200, 40, 25)) {
ballOctave = (ballOctave == 7) ? 2 : ballOctave + 1;
initializeWalls();
drawBouncingBallMode();
return;
}
}
// Update physics and display
updateBouncingBall();
}
void updateBouncingBall() {
// Smooth 60 FPS animation
static unsigned long lastUpdate = 0;
if (millis() - lastUpdate > 16) {
// Clear entire play area to prevent flickering
tft.fillRect(53, 63, 219, 114, THEME_BG);
updateBalls();
checkWallCollisions();
// Draw walls
drawWalls();
// Draw balls
drawBalls();
lastUpdate = millis();
}
}
void updateBalls() {
for (int i = 0; i < numActiveBalls; i++) {
if (!balls[i].active) continue;
// Update position
balls[i].x += balls[i].vx;
balls[i].y += balls[i].vy;
// Bounce off walls with proper collision detection
if (balls[i].x - balls[i].size <= 53) {
balls[i].vx = abs(balls[i].vx);
balls[i].x = 53 + balls[i].size;
}
if (balls[i].x + balls[i].size >= 272) {
balls[i].vx = -abs(balls[i].vx);
balls[i].x = 272 - balls[i].size;
}
if (balls[i].y - balls[i].size <= 63) {
balls[i].vy = abs(balls[i].vy);
balls[i].y = 63 + balls[i].size;
}
if (balls[i].y + balls[i].size >= 177) {
balls[i].vy = -abs(balls[i].vy);
balls[i].y = 177 - balls[i].size;
}
}
}
void drawBalls() {
for (int i = 0; i < numActiveBalls; i++) {
if (!balls[i].active) continue;
tft.fillCircle(balls[i].x, balls[i].y, balls[i].size, balls[i].color);
tft.drawCircle(balls[i].x, balls[i].y, balls[i].size, THEME_TEXT);
}
}
void drawWalls() {
for (int i = 0; i < NUM_WALLS; i++) {
uint16_t color = walls[i].color;
// Bright flash when active
if (walls[i].active) {
unsigned long elapsed = millis() - walls[i].activeTime;
if (elapsed < 200) {
color = THEME_TEXT; // Bright white flash
} else {
walls[i].active = false;
}
}
// Draw wall
tft.fillRect(walls[i].x, walls[i].y, walls[i].w, walls[i].h, color);
// Add note name for longer walls
if (walls[i].w > walls[i].h && walls[i].w > 50) {
tft.setTextColor(THEME_BG, color);
tft.drawCentreString(walls[i].noteName,
walls[i].x + walls[i].w/2,
walls[i].y - 2, 1);
}
}
}
void checkWallCollisions() {
for (int b = 0; b < numActiveBalls; b++) {
if (!balls[b].active) continue;
static float lastX[MAX_BALLS], lastY[MAX_BALLS];
static bool initialized = false;
if (!initialized) {
for (int i = 0; i < MAX_BALLS; i++) {
lastX[i] = balls[i].x;
lastY[i] = balls[i].y;
}
initialized = true;
}
// Check collision with each wall segment
for (int w = 0; w < NUM_WALLS; w++) {
if (walls[w].active) continue; // Skip if wall is already active
bool collision = false;
// Check collision based on ball position and wall bounds
if (walls[w].side == 0) { // Top walls
if (balls[b].y - balls[b].size <= walls[w].y + walls[w].h &&
balls[b].x >= walls[w].x && balls[b].x <= walls[w].x + walls[w].w &&
lastY[b] > balls[b].y) {
collision = true;
}
}
else if (walls[w].side == 1) { // Right walls
if (balls[b].x + balls[b].size >= walls[w].x &&
balls[b].y >= walls[w].y && balls[b].y <= walls[w].y + walls[w].h &&
lastX[b] < balls[b].x) {
collision = true;
}
}
else if (walls[w].side == 2) { // Bottom walls
if (balls[b].y + balls[b].size >= walls[w].y &&
balls[b].x >= walls[w].x && balls[b].x <= walls[w].x + walls[w].w &&
lastY[b] < balls[b].y) {
collision = true;
}
}
else if (walls[w].side == 3) { // Left walls
if (balls[b].x - balls[b].size <= walls[w].x + walls[w].w &&
balls[b].y >= walls[w].y && balls[b].y <= walls[w].y + walls[w].h &&
lastX[b] > balls[b].x) {
collision = true;
}
}
if (collision) {
if (deviceConnected) {
sendMIDI(0x90, walls[w].note, random(70, 110));
sendMIDI(0x80, walls[w].note, 0);
}
walls[w].active = true;
walls[w].activeTime = millis();
Serial.printf("Wall segment hit: %s\n", walls[w].noteName.c_str());
break; // Only trigger one wall per ball per frame
}
}
// Update last positions
lastX[b] = balls[b].x;
lastY[b] = balls[b].y;
}
}
#endif