-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht36_ssd1327__lissajous.ino
351 lines (300 loc) · 9.81 KB
/
t36_ssd1327__lissajous.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
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
/* -------- MAIN DISPLAY ---------------------------------------------------- */
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1327.h>
#include "Font5x7FixedMono.h"
#define OLED_CLK 13
#define OLED_MOSI 11
#define OLED_CS 10
#define OLED_DC 8
#define OLED_RESET -1
Adafruit_SSD1327 display(128, 128, &SPI, OLED_DC, OLED_RESET, OLED_CS);
void display_setup(void) {
Serial.println("-> connecting display");
if ( ! display.begin(0x3D) ) {
Serial.println("Unable to initialize OLED");
while (1) yield();
}
display.setTextSize(1);
display.setTextWrap(false);
display.setTextColor(SSD1327_WHITE);
display.setFont(&Font5x7FixedMono);
display.setRotation(1);
display.clearDisplay();
display.display();
}
// -------- INPUT ----------------------------------------------------------- */
#include <Bounce.h>
#define BOUNCE_MS 10
#define NUM_BUTTONS 6
#define B0 0
#define B1 1
#define B2 2
#define B3 3
#define B4 4
#define B5 5
// TODO rewrite this as a class
namespace buttons {
void setup(void) {
pinMode(B0, INPUT_PULLUP);
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(B3, INPUT_PULLUP);
pinMode(B4, INPUT_PULLUP);
pinMode(B5, INPUT_PULLUP);
};
Bounce debounced[NUM_BUTTONS] = {
Bounce(B0, BOUNCE_MS),
Bounce(B1, BOUNCE_MS),
Bounce(B2, BOUNCE_MS),
Bounce(B3, BOUNCE_MS),
Bounce(B4, BOUNCE_MS),
Bounce(B5, BOUNCE_MS)
};
bool held[NUM_BUTTONS];
bool down[NUM_BUTTONS];
bool up[NUM_BUTTONS];
void update(void) {
/* REMINDER:
- myButton.fallingEdge() == ON
- myButton.risingEdge() == OFF
...because our buttons are wired active LOW
*/
for (int i = 0; i < NUM_BUTTONS; i++) {
down[i] = false;
up[i] = false;
buttons::debounced[i].update();
if (buttons::debounced[i].fallingEdge()) {
buttons::down[i] = true;
buttons::held[i] = true;
} else if (buttons::debounced[i].risingEdge()) {
buttons::up[i] = true;
buttons::held[i] = false;
}
}
}
};
/* ------- GRID COORDS ------------------------------------------------------ */
#define GRID_LENGTH 26
#define GRID_START_X 22
#define GRID_START_Y 20
#define cols 4
#define rows cols
// array of center coordinates
int grid[4][4][2] {
{{22, 20}, {48, 20}, {74, 20}, {100, 20}},
{{22, 46}, {48, 46}, {74, 46}, {100, 46}},
{{22, 72}, {48, 72}, {74, 72}, {100, 72}},
{{22, 98}, {48, 98}, {74, 98}, {100, 98}}
};
/* ------- TRIG ------------------------------------------------------------- */
#define RADIUS_MAX 21
#define RADIUS_MIN 2
int radius = 10;
void dec_radius(void) {
if (radius > RADIUS_MIN) radius--;
};
void inc_radius(void) {
if (radius < RADIUS_MAX) radius++;
};
double theta = 0;
double polar_xs[rows] = { 0, 0, 0 };
double polar_ys[cols] = { 1, 1, 1 };
#define DELTA_MAX 0.2
#define DELTA_MIN 0.01
#define DELTA_STEP 0.01
float delta = 0.08;
void inc_delta(void) {
if (delta + DELTA_STEP < DELTA_MAX) delta += DELTA_STEP;
}
void dec_delta(void) {
if (delta - DELTA_STEP > DELTA_MIN) delta -= DELTA_STEP;
}
/* ------- PIXELS ----------------------------------------------------------- */
int pixels[cols][rows][3];
void clear_pixels(void) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < rows; k++) {
pixels[j][k][0] = 0;
pixels[j][k][1] = 0;
pixels[j][k][2] = 0;
}
}
}
void add_pixel(int col, int row, uint8_t x, uint8_t y, uint8_t z) {
pixels[col][row][0] = x;
pixels[col][row][1] = y;
pixels[col][row][2] = z;
}
#define pixel_A pixels[0][0][0], pixels[0][0][1]
#define pixel_B pixels[0][1][0], pixels[0][1][1]
#define pixel_C pixels[0][2][0], pixels[0][2][1]
#define pixel_D pixels[0][3][0], pixels[0][3][1]
#define pixel_E pixels[1][0][0], pixels[1][0][1]
#define pixel_F pixels[1][1][0], pixels[1][1][1]
#define pixel_G pixels[1][2][0], pixels[1][2][1]
#define pixel_H pixels[1][3][0], pixels[1][3][1]
#define pixel_I pixels[2][0][0], pixels[2][0][1]
#define pixel_J pixels[2][1][0], pixels[2][1][1]
#define pixel_K pixels[2][2][0], pixels[2][2][1]
#define pixel_L pixels[2][3][0], pixels[2][3][1]
#define pixel_M pixels[3][0][0], pixels[3][0][1]
#define pixel_N pixels[3][1][0], pixels[3][1][1]
#define pixel_O pixels[3][2][0], pixels[3][2][1]
#define pixel_P pixels[3][3][0], pixels[3][3][1]
/* ------- TRIANGLE OCTOGON ------------------------------------------------- */
#define OCTOGON_TRIANGLES 14
int tri_oct_brightness_min = 2;
int tri_oct_brightness_max = 14;
int tri_oct_brightness[OCTOGON_TRIANGLES] = {
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max)),
int(random(tri_oct_brightness_min, tri_oct_brightness_max))
};
int t_o_b(int index) {
return tri_oct_brightness[index];
}
/* ------- SCALES ----------------------------------------------------------- */
enum SCALES { MULT, PYTHAG, JUST, NUM_SCALES };
double scales[NUM_SCALES][cols][2] = {
// MULT
{ {1, 1}, {2, 2}, {3, 3}, {4, 4} },
// PYTHAG
{ {1, 1}, {9, 8}, {4, 3}, {3, 2} },
// JUST
{ {1, 1}, {9, 8}, {6, 5}, {5, 4} },
};
int current_scale = JUST;
void next_scale(void) {
current_scale++;
if (current_scale == NUM_SCALES) current_scale = MULT;
}
void prev_scale(void) {
current_scale--;
if (current_scale < MULT) current_scale = JUST;
}
/* -------------------------------------------------------------------------- */
uint8_t tick = 0;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Starting...");
randomSeed(analogRead(0));
// CONNECT BUTTONS
buttons::setup();
// CONNECT MAIN DISPLAY
display_setup();
}
void loop() {
tick++;
bool every_other = tick % 2 == 0;
bool every_third = tick % 3 == 0;
bool every_sixteenth = tick % 16 == 0;
bool every_eighth = tick % 8 == 0;
buttons::update();
/* BUTTON LAYOUT
4
0 1 2 5
3
*/
#ifdef DEBUG
for (int i = 0; i < NUM_BUTTONS; i++) {
if (buttons::held[i]) Serial.printf("%i is held\n", i);
if (buttons::down[i]) Serial.printf("%i down\n", i);
if (buttons::up[i]) Serial.printf("%i up\n", i);
}
#endif
if (buttons::down[0]) dec_radius();
if (buttons::down[1]) inc_radius();
if (buttons::down[2]) prev_scale();
if (buttons::down[5]) next_scale();
if (buttons::down[3] || (every_eighth && buttons::held[3])) dec_delta();
if (buttons::down[4] || (every_eighth && buttons::held[4])) inc_delta();
theta += ( delta / 2 );
// UPDATE POLAR OFFSETS
for (int i = 0; i < cols; i++) {
if (current_scale != JUST) {
polar_xs[i] = sin(theta * (scales[current_scale][i][0])) * radius;
} else {
polar_xs[i] = cos(theta * (scales[current_scale][i][0])) * radius;
}
polar_ys[i] = sin(theta * (scales[current_scale][i][1])) * radius;
}
// UPDATE PIXELS
for (int j = 0; j < cols; j++) {
for (int k = 0; k < rows; k++) {
add_pixel(
j, k,
grid[j][k][0] + int(polar_xs[j]),
grid[j][k][1] + int(polar_ys[k]),
60
);
}
}
// REDRAW
display.clearDisplay();
/* ___CELL MACROS___
A B C D
E F G H
I J K L
M N O P
*/
#ifdef DRAW_MESH
// draw triangle mesh
// top left box
display.drawTriangle(pixel_A, pixel_B, pixel_E, 8);
display.drawTriangle(pixel_B, pixel_F, pixel_E, 8);
// top middle box
display.drawTriangle(pixel_B, pixel_C, pixel_F, 8);
display.drawTriangle(pixel_C, pixel_G, pixel_F, 8);
// top right box **FLIPPED**
display.drawTriangle(pixel_C, pixel_H, pixel_G, 8);
display.drawTriangle(pixel_C, pixel_D, pixel_H, 8);
// mid left box
display.drawTriangle(pixel_E, pixel_F, pixel_I, 8);
display.drawTriangle(pixel_F, pixel_J, pixel_I, 8);
// mid middle box
display.drawTriangle(pixel_F, pixel_G, pixel_J, 8);
display.drawTriangle(pixel_G, pixel_K, pixel_J, 8);
// mid right box
display.drawTriangle(pixel_G, pixel_H, pixel_K, 8);
display.drawTriangle(pixel_H, pixel_L, pixel_K, 8);
// bot left box **FLIPPED**
display.drawTriangle(pixel_I, pixel_N, pixel_M, 8);
display.drawTriangle(pixel_I, pixel_J, pixel_N, 8);
// bot middle box
display.drawTriangle(pixel_J, pixel_K, pixel_N, 8);
display.drawTriangle(pixel_K, pixel_O, pixel_N, 8);
// bot right box
display.drawTriangle(pixel_K, pixel_L, pixel_O, 8);
display.drawTriangle(pixel_L, pixel_P, pixel_O, 8);
#endif
// fill the octogon (14 triangles)
display.fillTriangle(pixel_B, pixel_F, pixel_E, t_o_b(0));
display.fillTriangle(pixel_B, pixel_C, pixel_F, t_o_b(1));
display.fillTriangle(pixel_C, pixel_G, pixel_F, t_o_b(2));
display.fillTriangle(pixel_C, pixel_H, pixel_G, t_o_b(3));
display.fillTriangle(pixel_E, pixel_F, pixel_I, t_o_b(4));
display.fillTriangle(pixel_F, pixel_J, pixel_I, t_o_b(5));
display.fillTriangle(pixel_F, pixel_G, pixel_J, t_o_b(6));
display.fillTriangle(pixel_G, pixel_K, pixel_J, t_o_b(7));
display.fillTriangle(pixel_G, pixel_H, pixel_K, t_o_b(8));
display.fillTriangle(pixel_H, pixel_L, pixel_K, t_o_b(9));
display.fillTriangle(pixel_I, pixel_J, pixel_N, t_o_b(10));
display.fillTriangle(pixel_J, pixel_K, pixel_N, t_o_b(11));
display.fillTriangle(pixel_K, pixel_O, pixel_N, t_o_b(12));
display.fillTriangle(pixel_K, pixel_L, pixel_O, t_o_b(13));
// TODO better timing model
delay(10);
display.display();
}