-
Notifications
You must be signed in to change notification settings - Fork 3
/
tinyQuan.ino
564 lines (418 loc) · 15.7 KB
/
tinyQuan.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <avr/pgmspace.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MCP4725.h>
#include <ADS1X15.h>
#include "definitions.h"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
MCP4725 MCP(0x60);
ADS1115 ADS(0x48);
bool tuning_dac = false;
bool tuning_adc = false;
bool volatile refresh_little_indicator = false;
uint8_t volatile layout_index = 0;
bool volatile refresh_layout = false;
uint8_t pixel_loc = 0;
char key_buffer[10];
int16_t key0 = 0;
char short_scale_name_buffer[15];
uint8_t current_scale = 1; // Force refresh at start
uint16_t current_scale_mask = MAJOR;
uint8_t nb_notes_in_scale = 12;
uint8_t current_root_semitone = 1; // Force refresh at start
// in_scale_cv_mode = 0 -> 1 semitone / 83 mV (non-constant CV difference to change note in scale)
// = 1 -> 1 note in_scale / 83 mV (constant CV difference to change note in scale
bool volatile in_scale_cv_mode = true;
uint8_t last_semitone_index = 0;
long int trigger_out_width = 10;
bool reset_trigger_out = false;
long int last_trigger_out_millis = millis();
/////////// ROTARY ENCODERS //////////
uint8_t volatile D10D11_state = 0b1111;
uint8_t volatile D6D7_state = 0b1111;
int8_t volatile new_root = 0;
int8_t volatile new_scale = 0;
bool volatile rotary_change = false;
//////////////////////////////////////
void setup() {
//////// ROTARY ENCODER INTERRUPTS AND PINS ////////
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
cli();
PCICR |= 0b00000101;
PCMSK0 |= 0b00001100;
PCMSK2 |= 0b11000000;
sei();
////////////////////////////////////////////////////
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(TRIGGER_OUT_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(CHANGE_CV_MODE_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CHANGE_CV_MODE_PIN),
change_cv_mode_ISR, FALLING);
pinMode(CHANGE_LAYOUT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CHANGE_LAYOUT_PIN),
change_layout_ISR, FALLING);
digitalWrite(TRIGGER_OUT_PIN, LOW);
Wire.begin();
Wire.setClock(400000);
MCP.begin();
ADS.begin();
ADS.setGain(0);
display.clearDisplay();
display.display();
keyboard_layout = keyboard_layouts[layout_index];
if (tuning_dac) {
input_and_play_semitone();
}
if (tuning_adc) {
print_adc();
}
}
void loop() {
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
// Here is where the magic happen //
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
// Serial.println();
// Serial.print("cv_to_quantize:");
// Serial.print(cv_to_quantize);
// Serial.print(" ");
uint8_t semitones_above_root_in_scale;
uint16_t temp_scale_mask = current_scale_mask;
uint8_t note_in_scale;
uint8_t root_semitone;
int16_t cv_to_quantize = ADS.readADC(0);
Serial.println(cv_to_quantize);
if (reset_trigger_out && millis() - last_trigger_out_millis >= trigger_out_width) {
digitalWrite(TRIGGER_OUT_PIN, LOW);
reset_trigger_out = false;
}
if (in_scale_cv_mode) {
// This doesn't work as well as I wanted,
// sometimes the first note of an octave is
// trigger_outed above a round volt probably due
// to the tiny non-linearity of the ADC.
note_in_scale = map(cv_to_quantize,
CV_0V_BOUNDARY_INCLUSIVE, CV_ABOVE_5V_BOUNDARY_EXCLUSIVE,
0, 5 * nb_notes_in_scale + 1);
// 5V spans 5 octave
semitones_above_root_in_scale = 0;
// The upper boundary is a bit janky if root_semitone != 0, we'll constrain it later.
for (uint8_t semitones_above_root = 0, note = 0; semitones_above_root < MAX_DAC_SEMITONE; semitones_above_root++) {
if (bitRead(temp_scale_mask, 0)) {
semitones_above_root_in_scale = semitones_above_root;
note += 1;
if (note >= note_in_scale) {
break;
}
}
temp_scale_mask = rotate12Right(temp_scale_mask, 1);
}
} else {
semitones_above_root_in_scale = 0;
// The upper boundary is a bit janky if root_semitone != 0, we'll constrain it later.
for (uint8_t semitones_above_root = 0; semitones_above_root < MAX_DAC_SEMITONE; semitones_above_root++) {
int16_t semitone_cv = (int16_t)pgm_read_word_near(inter_semitonecv_to_ADC16read + semitones_above_root);
if (semitone_cv >= cv_to_quantize) {
break;
}
if (bitRead(temp_scale_mask, 0)) {
semitones_above_root_in_scale = semitones_above_root;
}
temp_scale_mask = rotate12Right(temp_scale_mask, 1);
}
}
uint8_t semitone_index = current_root_semitone + semitones_above_root_in_scale;
semitone_index = min(semitone_index, MAX_DAC_SEMITONE - 1);
MCP.setValue((int16_t)pgm_read_word_near(semitone_cvs_dac + semitone_index));
if (last_semitone_index != semitone_index) {
digitalWrite(TRIGGER_OUT_PIN, HIGH);
reset_trigger_out = true;
last_semitone_index = semitone_index;
last_trigger_out_millis = millis();
}
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
if (refresh_layout) {
refresh_layout = false;
keyboard_layout = keyboard_layouts[layout_index];
drawKeyboard(current_scale, current_root_semitone, 0);
display.display();
}
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////
if ((new_root != current_root_semitone) || (new_scale != current_scale)) {
// if (new_root != current_root_semitone) {
// Serial.print("change key ");
// Serial.print(current_root_semitone);
// Serial.print(" -> ");
// Serial.println(new_root);
// }
// if (new_scale != current_scale) {
// Serial.print("change scale ");
// Serial.print(current_scale);
// Serial.print(" -> ");
// Serial.println(new_scale);
// }
current_root_semitone = new_root;
current_scale = new_scale;
current_scale_mask = pgm_read_word_near(scales + current_scale);
nb_notes_in_scale = 0;
for (uint8_t i = 0; i < 12; i++) {
nb_notes_in_scale += bitRead(current_scale_mask, i);
}
display.clearDisplay();
printScale(current_scale, 2);
drawKeyboard(current_scale, current_root_semitone, 0);
drawLittleIndicator(current_scale);
display.display();
}
if (refresh_little_indicator) {
digitalWrite(LED_BUILTIN, in_scale_cv_mode);
drawLittleIndicator(current_scale);
display.display();
refresh_little_indicator = false;
}
}
///////////////////////////////////////////
///////////////// Graphics ////////////////
///////////////////////////////////////////
void drawLittleIndicator(uint8_t scale) {
pixel_loc = map(scale, 0, NUM_SCALES - 1, 0, SCREEN_WIDTH - 8);
// display.fillRect(pixel_loc, 15, 8, 1, SSD1306_WHITE);
if (in_scale_cv_mode) {
display.fillRoundRect(pixel_loc, 16, 8, 6, 1, SSD1306_WHITE);
} else {
display.fillRoundRect(pixel_loc, 16, 8, 6, 1, SSD1306_BLACK);
display.drawRoundRect(pixel_loc, 16, 8, 6, 1, SSD1306_WHITE);
}
}
void printKey(uint8_t key, uint8_t text_size) {
display.fillRect(0, 0, SCREEN_WIDTH / 2, 8 * text_size, SSD1306_BLACK);
display.setTextSize(text_size);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
strcpy_P(key_buffer, (char *)pgm_read_word(&(notes[key])));
display.println(key_buffer);
}
void printScale(uint16_t scale, uint8_t text_size) {
display.fillRect(0, 16, SCREEN_WIDTH, 8 * text_size, SSD1306_BLACK);
display.setTextSize(text_size);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);//16);
strcpy_P(short_scale_name_buffer, (char *)pgm_read_word(&(short_scale_names[scale])));
display.println(short_scale_name_buffer);
}
void drawKeyboard(int16_t scale, int8_t root, int8_t origin_key) {
uint16_t on_keys = 0;
static int16_t x0 = 0;
// static int16_t y0 = SCREEN_HEIGHT - PROP * KEYBOARD_LAYOUT_HEIGHT;
static int16_t y0 = SCREEN_HEIGHT - PROP * KEYBOARD_LAYOUT_HEIGHT - 8;
uint8_t root_dot_y_offset;
display.fillRect(x0, y0, SCREEN_WIDTH, PROP * KEYBOARD_LAYOUT_HEIGHT, SSD1306_BLACK);
static uint8_t keys_to_display = ceil(12.0 * float(SCREEN_WIDTH) / float(PROP * KEYBOARD_LAYOUT_WIDTH));
// uint16_t on_keys = scales[scale];
on_keys = pgm_read_word_near(scales + scale);
// Align with the key at origin of display
if (root > origin_key) {
on_keys = rotate12Left(on_keys, root - origin_key);
}
else if (root < origin_key) {
on_keys = rotate12Right(on_keys, origin_key - root);
}
// Last nudge to print _k = -1
on_keys = rotate12Left(on_keys, 1);
//////////////////////////////////////////////
////////////// Draw white notes //////////////
//////////////////////////////////////////////
int8_t x_offset = -keyboard_layout[mod(origin_key - 1, 12)][1] / 2;
for (int16_t _k = -1, k; _k < keys_to_display + 1; _k++) {
k = mod(_k + origin_key, 12);
if (keyboard_layout[k][0] == 0) {
if (on_keys & 0b1) {
display.fillRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_WHITE);
}
else
{
display.drawRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_WHITE);
}
// Draw root note marker
if (k == current_root_semitone) {
root_dot_y_offset = (layout_index == 0) ? (2 * keyboard_layout[k][4]) / 3 : (keyboard_layout[k][4] - ROOT_DOT_SIZE) / 2;
display.fillRoundRect(x0 + PROP * (x_offset + keyboard_layout[k][3] / 2 - 1),
y0 + PROP * (keyboard_layout[k][2] + root_dot_y_offset),
PROP * ROOT_DOT_SIZE, PROP * ROOT_DOT_SIZE, 1,
SSD1306_BLACK);
}
}
x_offset += keyboard_layout[k][1];
on_keys = rotate12Right(on_keys, 1);
}
//////////////////
on_keys = pgm_read_word_near(scales + scale);
if (root > origin_key) {
on_keys = rotate12Left(on_keys, root - origin_key);
}
else if (root < origin_key) {
on_keys = rotate12Right(on_keys, origin_key - root);
}
on_keys = rotate12Left(on_keys, 1);
//////////////////
//////////////////////////////////////////////
////////////// Draw black notes //////////////
//////////////////////////////////////////////
x_offset = -keyboard_layout[mod(origin_key - 1, 12)][1] / 2;
for (int8_t _k = -1, k; _k < keys_to_display + 1; _k++) {
k = mod(_k + origin_key, 12);
if (keyboard_layout[k][0] == 1) {
if (on_keys & 0b1) {
display.fillRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_WHITE);
// Draw a black line around black notes that are ON
// only when the layout is keyboard because black notes
// in the BSP layout do not overlap white notes
if (layout_index == 0) {
display.drawRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_BLACK);
}
} else {
display.fillRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_BLACK);
display.drawRoundRect(x0 + PROP * x_offset,
y0 + PROP * keyboard_layout[k][2],
PROP * keyboard_layout[k][3], PROP * keyboard_layout[k][4],
keyboard_layout[k][5], SSD1306_WHITE);
}
// Draw root note marker
if (k == current_root_semitone) {
root_dot_y_offset = (layout_index == 0) ? (2 * keyboard_layout[k][4]) / 3 : (keyboard_layout[k][4] - ROOT_DOT_SIZE) / 2;
display.fillRoundRect(x0 + PROP * (x_offset + keyboard_layout[k][3] / 2 - 1),
y0 + PROP * (keyboard_layout[k][2] + root_dot_y_offset),
PROP * ROOT_DOT_SIZE, PROP * ROOT_DOT_SIZE, 1,
SSD1306_BLACK);
}
}
x_offset += keyboard_layout[k][1];
on_keys = rotate12Right(on_keys, 1);
}
}
///////////////////////////////////////////
////////////// End graphics ///////////////
///////////////////////////////////////////
///////////// KEY OF ISR
ISR(PCINT0_vect) {
D10D11_state = (D10D11_state << 1) | digitalRead(10);
D10D11_state = (D10D11_state << 1) | digitalRead(11);
if ((D10D11_state & 0b1111) == 0b0111) {
new_root += 1;
} else if ((D10D11_state & 0b1111) == 0b1011) {
new_root -= 1;
}
if (new_root == NUM_NOTES) {
new_root = 0;
}
if (new_root == -1) {
new_root = NUM_NOTES - 1;
}
rotary_change = true;
}
//////////// NEW SCALE ENCODER ISR
ISR(PCINT2_vect) {
D6D7_state = (D6D7_state << 1) | digitalRead(6);
D6D7_state = (D6D7_state << 1) | digitalRead(7);
if ((D6D7_state & 0b1111) == 0b0111) {
new_scale += 1;
} else if ((D6D7_state & 0b1111) == 0b1011) {
new_scale -= 1;
}
if (new_scale == NUM_SCALES) {
new_scale = 0;
}
if (new_scale == -1) {
new_scale = NUM_SCALES - 1;
}
}
void change_layout_ISR() {
layout_index = (layout_index + 1) % 2;
refresh_layout = true;
}
void change_cv_mode_ISR() {
in_scale_cv_mode = !in_scale_cv_mode;
refresh_little_indicator = true;
}
///////////// Utilities //////////////
int mod(int x, int m) {
return (x % m + m) % m;
}
uint16_t rotate12Left(uint16_t n, uint16_t d) {
return 0xfff & ((n << (d % 12)) | (n >> (12 - (d % 12))));
}
uint16_t rotate12Right(uint16_t n, uint16_t d) {
return 0xfff & ((n >> (d % 12)) | (n << (12 - (d % 12))));
}
void print_adc() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(" Tuning");
display.println(" ADC");
display.println("dval/d83mV");
display.display();
for (;;) {
Serial.println(ADS.readADC(0));
}
}
void input_and_play_semitone() {
static String inData;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("");
display.println(" Tuning");
display.println(" DAC");
display.println("d83mV/semi");
display.display();
Serial.println("");
for (;;) {
char received = ' '; // Each character received
inData = ""; // Clear recieved buffer
Serial.print("DAC value (0-4095): ");
while (received != '\n') { // When new line character is received (\n = LF, \r = CR)
if (Serial.available() > 0) // When character in serial buffer read it
{
received = Serial.read();
Serial.print(received); // Echo each received character, terminal dont need local echo
inData += received; // Add received character to inData buffer
}
}
inData.trim(); // Eliminate \n, \r, blank and other not “printable”
Serial.println();
MCP.setValue(inData.toInt());
}
}