-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveload.h
819 lines (784 loc) · 24.7 KB
/
saveload.h
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* saveload.h -- Flash storage engine for Multitrack Sequencer (for Feather M4 Express)
* Part of https://github.com/PatchworkBoy/Neotrellis-Gate-Sequencer
* 04 Nov 2023 - @apatchworkboy / Marci
* Based on https://github.com/todbot/picostepseq/
* 28 Apr 2023 - @todbot / Tod Kurt
* 15 Aug 2022 - @todbot / Tod Kurt
*/
#ifndef MULTI_SEQUENCER_FLASH
#define MULTI_SEQUENCER_FLASH
//
// --- sequence load / save functions
//
uint32_t last_sequence_write_millis = 0;
// write all notes to "disk"
// write all velocities to "disk"
void notes_write() {
if (marci_debug) Serial.println(F("notes_write"));
last_sequence_write_millis = millis();
for (uint8_t p = 0; p < numpresets; ++p) {
if (marci_debug) Serial.println(p);
DynamicJsonDocument doc(8192); // assistant said 6144
for (int j = 0; j < numtracks; j++) {
JsonArray notes_array = doc.createNestedArray();
for (int i = 0; i < num_steps; i++) {
int s = seqr.notes[p][j][i];
notes_array.add(s);
}
}
toggle_write();
fatfs.remove(nfiles[p]);
File32 nfile = fatfs.open(nfiles[p], FILE_WRITE);
if (!nfile) {
if (marci_debug) Serial.println(F("notes_write: Failed to create file"));
if (marci_debug) Serial.println(p);
return;
}
if (serializeJson(doc, nfile) == 0) {
if (marci_debug) Serial.println(F("notes_write: Failed to write to file"));
if (marci_debug) Serial.println(p);
}
nfile.close();
doc.clear();
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("notes saved"));
sure = 0;
presetmode = 0;
divedit = 0 ;
init_interface();
show_sequence(sel_track);
}
// write all gates to "disk"
void gates_write() {
if (marci_debug) Serial.println(F("gates_write"));
last_sequence_write_millis = millis();
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
for (int j = 0; j < numtracks; j++) {
JsonArray prob_array = doc.createNestedArray();
for (int i = 0; i < num_steps; i++) {
int s = seqr.gates[p][j][i];
prob_array.add(s);
}
}
toggle_write();
fatfs.remove(gfiles[p]);
File32 file = fatfs.open(gfiles[p], FILE_WRITE);
if (!file) {
if (marci_debug) Serial.println(F("gates_write: Failed to create file"));
if (marci_debug) Serial.println(p);
return;
}
if (serializeJson(doc, file) == 0) {
if (marci_debug) Serial.println(F("gates_write: Failed to write to file"));
if (marci_debug) Serial.println(p);
}
file.close();
doc.clear();
if (marci_debug) Serial.print(F("Gate bank saved"));
if (marci_debug) Serial.println(p);
//serializeJson(doc, Serial);
}
if (marci_debug) Serial.println(F("gates saved"));
notes_write();
}
// write all probabilities to "disk"
void probabilities_write() {
if (marci_debug) Serial.println(F("probabilities_write"));
last_sequence_write_millis = millis();
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
for (int j = 0; j < numtracks; j++) {
JsonArray prob_array = doc.createNestedArray();
for (int i = 0; i < num_steps; i++) {
int s = seqr.probs[p][j][i];
prob_array.add(s);
}
}
toggle_write();
fatfs.remove(prbfiles[p]);
File32 file = fatfs.open(prbfiles[p], FILE_WRITE);
if (!file) {
if (marci_debug) Serial.println(F("probabilities_write: Failed to create file"));
if (marci_debug) Serial.println(p);
return;
}
if (serializeJson(doc, file) == 0) {
if (marci_debug) Serial.println(F("probabilities_write: Failed to write to file"));
if (marci_debug) Serial.println(p);
}
file.close();
doc.clear();
if (marci_debug) Serial.print(F("Probability Bank Saved"));
if (marci_debug) Serial.println(p);
//serializeJson(doc, Serial);
}
if (marci_debug) Serial.println(F("probabilities saved"));
gates_write();
}
// write all settings to "disk"
void settings_write() {
if (marci_debug) Serial.println(F("settings_write"));
last_sequence_write_millis = millis();
DynamicJsonDocument doc(8192); // assistant said 6144
JsonArray set_array = doc.createNestedArray();
set_array.add(tempo);
set_array.add(cfg.step_size);
set_array.add(transpose);
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.track_notes[i]);
}
for (uint8_t i = 0; i < 3; ++i) {
set_array.add(seqr.ctrl_notes[i]);
}
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.track_chan[i]);
}
set_array.add(seqr.ctrl_chan);
set_array.add(seqr.swing);
set_array.add(brightness);
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.modes[i]);
}
for (uint8_t i = 0; i < 2; ++i) {
set_array.add(hzv[i]);
}
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.divs[i]);
}
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.offsets[i]);
}
for (uint8_t i = 0; i < 8; ++i) {
set_array.add(seqr.lengths[i]);
}
toggle_write();
fatfs.remove(settings_file);
File32 file = fatfs.open(settings_file, FILE_WRITE);
if (!file) {
if (marci_debug) Serial.println(F("settings_write: Failed to create file"));
return;
}
if (serializeJson(doc, file) == 0) {
if (marci_debug) Serial.println(F("settings_write: Failed to write to file"));
}
file.close();
doc.clear();
//if (marci_debug) Serial.print(F("saved_settings_json = \""));
//serializeJson(doc, Serial);
if (marci_debug) Serial.println(F("settings saved"));
probabilities_write();
}
// write all velocities to "disk"
void velocities_write() {
if (marci_debug) Serial.println(F("velocities_write"));
last_sequence_write_millis = millis();
for (uint8_t p = 0; p < numpresets; ++p) {
if (marci_debug) Serial.println(p);
DynamicJsonDocument doc(8192); // assistant said 6144
for (int j = 0; j < numtracks; j++) {
JsonArray vels_array = doc.createNestedArray();
for (int i = 0; i < num_steps; i++) {
int s = seqr.vels[p][j][i];
vels_array.add(s);
}
}
toggle_write();
fatfs.remove(vfiles[p]);
File32 vfile = fatfs.open(vfiles[p], FILE_WRITE);
if (!vfile) {
if (marci_debug) Serial.println(F("velocities_write: Failed to create file"));
if (marci_debug) Serial.println(p);
return;
}
if (serializeJson(doc, vfile) == 0) {
if (marci_debug) Serial.println(F("velocities_write: Failed to write to file"));
if (marci_debug) Serial.println(p);
}
vfile.close();
doc.clear();
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("velocities saved"));
settings_write();
}
// write all sequences to "disk"
void sequences_write() {
// save wear & tear on flash, only allow writes every 10 seconds
if ((millis() - last_sequence_write_millis) < (10 * 1000)) { // only allow writes every 10 secs
if (marci_debug) Serial.println(F("sequences_write: too soon, wait a bit more"));
trellis.setPixelColor(59, C127);
trellis.show();
return;
}
if (marci_debug) Serial.println(F("millis ok"));
last_sequence_write_millis = millis();
if (marci_debug) Serial.println(F("key color"));
trellis.setPixelColor(59, R127);
if (marci_debug) Serial.println(F("show done"));
trellis.show();
if (marci_debug) Serial.println(F("sequences_write"));
for (uint8_t p = 0; p < numpresets; ++p) {
if (marci_debug) Serial.println(p);
DynamicJsonDocument doc(8192); // assistant said 6144
for (uint8_t j = 0; j < numtracks; j++) {
if (marci_debug) Serial.println(j);
JsonArray seq_array = doc.createNestedArray();
for (uint8_t i = 0; i < num_steps; i++) {
if (marci_debug) {
Serial.print(i);
Serial.print(" = ");
Serial.println(seqr.seqs[p][j][i]);
}
auto s = seqr.seqs[p][j][i];
seq_array.add(s);
}
}
if (marci_debug) Serial.println(F("sequences buffered to doc"));
if (marci_debug) Serial.println(F("led flash"));
toggle_write();
if (marci_debug) Serial.println(F("remove old file"));
fatfs.remove(pfiles[p]);
if (marci_debug) Serial.println(F("create new file"));
File32 pfile = fatfs.open(pfiles[p], FILE_WRITE);
if (!pfile) {
if (marci_debug) Serial.println(F("sequences_write: Failed to create file"));
if (marci_debug) Serial.println(p);
return;
}
if (serializeJson(doc, pfile) == 0) {
if (marci_debug) Serial.println(F("sequences_write: Failed to write to file"));
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("stored!"));
pfile.close();
if (marci_debug) Serial.println(F("clear buffer"));
doc.clear();
//if (marci_debug) Serial.print(F("saved_sequences_json = \""));
//serializeJson(doc, Serial);
if (marci_debug) Serial.println(F("sequence saved"));
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("sequences saved"));
velocities_write();
}
void pattern_reset() {
trellis.setPixelColor(59, R127);
if (marci_debug) Serial.println(F("pat_bank_resets"));
for (uint8_t p = 0; p < numpresets; ++p) {
if (marci_debug) Serial.println(p);
DynamicJsonDocument doc(8192); // assistant said 6144
DeserializationError error = deserializeJson(doc, patterns[p]);
if (error) {
if (marci_debug) Serial.print(F("pat_bank_reset: deserialize failed: "));
if (marci_debug) Serial.println(p);
if (marci_debug) Serial.println(error.c_str());
return;
}
for (int j = 0; j < numtracks; j++) {
JsonArray seq_array = doc[j];
for (int i = 0; i < num_steps; i++) {
seqr.seqs[p][j][i] = seq_array[i];
}
doc.clear();
}
}
if (marci_debug) Serial.println(F("vel_bank_resets"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc2(8192); // assistant said 6144
DeserializationError error2 = deserializeJson(doc2, velocities[p]);
if (error2) {
if (marci_debug) Serial.print(F("vel_bank_reset: deserialize failed: "));
if (marci_debug) Serial.println(p);
if (marci_debug) Serial.println(error2.c_str());
return;
}
for (int j = 0; j < numtracks; j++) {
JsonArray vel_array = doc2[j];
for (int i = 0; i < num_steps; i++) {
seqr.vels[p][j][i] = vel_array[i];
}
doc2.clear();
}
}
if (marci_debug) Serial.println(F("note_bank_resets"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument docn(8192); // assistant said 6144
DeserializationError errorn = deserializeJson(docn, notebanks[p]);
if (errorn) {
if (marci_debug) Serial.print(F("note_bank_reset: deserialize failed: "));
if (marci_debug) Serial.println(p);
if (marci_debug) Serial.println(errorn.c_str());
return;
}
for (int j = 0; j < numtracks; j++) {
JsonArray note_array = docn[j];
for (int i = 0; i < num_steps; i++) {
seqr.notes[p][j][i] = note_array[i];
}
docn.clear();
}
}
if (marci_debug) Serial.println(F("settings_reset"));
DynamicJsonDocument doc3(8192); // assistant said 6144
DeserializationError error3 = deserializeJson(doc3, settings);
if (error3) {
if (marci_debug) Serial.print(F("settings_reset: deserialize failed: "));
if (marci_debug) Serial.println(error3.c_str());
return;
}
JsonArray set_array = doc3[0];
tempo = set_array[0];
cfg.step_size = set_array[1];
transpose = set_array[2];
uint8_t z = 3;
for (uint8_t i = 0; i < 8; ++i) {
seqr.track_notes[i] = set_array[z];
z++;
}
for (uint8_t i = 0; i < 3; ++i) {
seqr.ctrl_notes[i] = set_array[z];
z++;
}
for (uint8_t i = 0; i < 8; ++i) {
seqr.track_chan[i] = set_array[z];
z++;
}
seqr.ctrl_chan = set_array[z];
seqr.swing = set_array[z + 1];
brightness = set_array[z + 2];
z = z + 3;
for (uint8_t i = 0; i < 8; ++i) {
seqr.modes[i] = set_array[z] > 0 ? set_array[z] : TRIGATE;
z++;
}
for (uint8_t i = 0; i < 2; ++i) {
hzv[i] = set_array[z];
z++;
}
for (uint8_t i = 0; i < 8; ++i) {
seqr.divs[i] = set_array[z];
z++;
}
for (uint8_t i = 0; i < 8; ++i) {
seqr.offsets[i] = set_array[z];
z++;
}
for (uint8_t i = 0; i < 8; ++i) {
seqr.lengths[i] = set_array[z];
z++;
}
doc3.clear();
if (marci_debug) Serial.println(F("prob_bank_resets"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc4(8192); // assistant said 6144
DeserializationError error4 = deserializeJson(doc4, probabilities[p]);
if (error4) {
if (marci_debug) Serial.print(F("prob_bank_reset: deserialize failed: "));
if (marci_debug) Serial.println(p);
if (marci_debug) Serial.println(error4.c_str());
return;
}
for (int j = 0; j < numtracks; j++) {
JsonArray prob_array = doc4[j];
for (int i = 0; i < num_steps; i++) {
seqr.probs[p][j][i] = prob_array[i];
}
doc4.clear();
}
}
if (marci_debug) Serial.println(F("gate_banks_reset"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc5(8192); // assistant said 6144
DeserializationError error5 = deserializeJson(doc5, gatebanks[p]);
if (error5) {
if (marci_debug) {
Serial.print(F("gate_bank_reset: deserialize failed: "));
Serial.println(p);
Serial.println(error5.c_str());
}
return;
}
for (int j = 0; j < numtracks; j++) {
JsonArray gate_array = doc5[j];
for (int i = 0; i < num_steps; i++) {
seqr.gates[p][j][i] = gate_array[i];
}
doc5.clear();
}
}
sequences_write();
trellis.show();
}
// read all sequences from "disk"
void sequences_read() {
if (marci_debug) Serial.println(F("sequences_read"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
File32 pfile = fatfs.open(pfiles[p], FILE_READ);
if (!pfile) {
if (marci_debug) Serial.println(F("sequences_read: no sequences file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, patterns[p]);
if (error) {
if (marci_debug) {
Serial.print(F("sequences_read: deserialize default failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, pfile); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("sequences_read: deserialize failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
}
for (int j = 0; j < numtracks; j++) {
JsonArray seq_array = doc[j];
if (marci_debug) {
Serial.print("Preset ");
Serial.print(p);
Serial.print(", Track ");
Serial.println(j);
}
for (int i = 0; i < num_steps; i++) {
seqr.seqs[p][j][i] = seq_array[i];
}
}
pfile.close();
doc.clear();
if (marci_debug) {
Serial.println(F("Pattern Bank OK"));
}
}
if (marci_debug) Serial.println(F("All patterns loaded"));
trellis.show();
}
// read all velocities from "disk"
void velocities_read() {
if (marci_debug) Serial.println(F("velocities_read"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
File32 file = fatfs.open(vfiles[p], FILE_READ);
if (!file) {
if (marci_debug) Serial.println(F("velocities_read: no sequences file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, velocities[p]);
if (error) {
if (marci_debug) {
Serial.print(F("velocities_read: deserialize default failed: "));
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, file); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("velocities_read: deserialize failed: "));
Serial.println(error.c_str());
}
return;
}
}
for (int j = 0; j < numtracks; j++) {
if (marci_debug) {
Serial.print("Preset ");
Serial.print(p);
Serial.print(", Track ");
Serial.println(j);
}
JsonArray vel_array = doc[j];
for (int i = 0; i < num_steps; i++) {
seqr.vels[p][j][i] = vel_array[i];
}
}
file.close();
doc.clear();
}
if (marci_debug) Serial.println(F("All velocities loaded"));
trellis.show();
}
// read all velocities from "disk"
void notes_read() {
if (marci_debug) Serial.println(F("notes_read"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
File32 file = fatfs.open(nfiles[p], FILE_READ);
if (!file) {
if (marci_debug) Serial.println(F("notes_read: no sequences file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, notebanks[p]);
if (error) {
if (marci_debug) {
Serial.print(F("notes_read: deserialize default failed: "));
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, file); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("notes_read: deserialize failed: "));
Serial.println(error.c_str());
}
return;
}
}
for (int j = 0; j < numtracks; j++) {
JsonArray note_array = doc[j];
if (marci_debug) {
Serial.print("Preset ");
Serial.print(p);
Serial.print(", Track ");
Serial.println(j);
}
for (int i = 0; i < num_steps; i++) {
seqr.notes[p][j][i] = note_array[i];
}
}
file.close();
doc.clear();
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("All notes loaded"));
trellis.show();
}
// read all probabilities from "disk"
void probabilities_read() {
if (marci_debug) Serial.println(F("probabilities_read"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
File32 file = fatfs.open(prbfiles[p], FILE_READ);
if (!file) {
if (marci_debug) Serial.println(F("probabilities_read: no probabilities file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, probabilities[p]);
if (error) {
if (marci_debug) {
Serial.print(F("probabilities_read: deserialize default failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, file); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("probabilities_read: deserialize failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
}
for (int j = 0; j < numtracks; j++) {
if (marci_debug) {
Serial.print("Preset ");
Serial.print(p);
Serial.print(", Track ");
Serial.println(j);
}
JsonArray prob_array = doc[j];
for (int i = 0; i < num_steps; i++) {
seqr.probs[p][j][i] = prob_array[i];
}
}
file.close();
doc.clear();
if (marci_debug) Serial.println(p);
}
if (marci_debug) Serial.println(F("All Probabilities loaded"));
trellis.show();
}
// read all gates from "disk"
void gates_read() {
if (marci_debug) Serial.println(F("gates_read"));
for (uint8_t p = 0; p < numpresets; ++p) {
DynamicJsonDocument doc(8192); // assistant said 6144
File32 file = fatfs.open(gfiles[p], FILE_READ);
if (!file) {
if (marci_debug) Serial.println(F("gates_read: no sequences file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, gatebanks[p]);
if (error) {
if (marci_debug) {
Serial.print(F("gates_read: deserialize default failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, file); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("gates_read: deserialize failed: "));
Serial.println(p);
Serial.println(error.c_str());
}
return;
}
}
for (int j = 0; j < numtracks; j++) {
JsonArray gate_array = doc[j];
if (marci_debug) {
Serial.print("Preset ");
Serial.print(p);
Serial.print(", Track ");
Serial.println(j);
}
for (int i = 0; i < num_steps; i++) {
seqr.gates[p][j][i] = gate_array[i];
}
}
file.close();
doc.clear();
}
if (marci_debug) Serial.println(F("All gates loaded"));
trellis.show();
}
void settings_read() {
if (marci_debug) Serial.println(F("settings_read"));
DynamicJsonDocument doc(8192); // assistant said 6144
File32 file = fatfs.open(settings_file, FILE_READ);
if (!file) {
if (marci_debug) Serial.println(F("settings_read: no settings file. Using ROM default..."));
DeserializationError error = deserializeJson(doc, settings);
if (error) {
if (marci_debug) {
Serial.print(F("settings_read: deserialize default failed: "));
Serial.println(error.c_str());
}
return;
}
} else {
DeserializationError error = deserializeJson(doc, file); // inputLength);
if (error) {
if (marci_debug) {
Serial.print(F("settings_read: deserialize failed: "));
Serial.println(error.c_str());
}
return;
}
}
JsonArray set_array = doc[0];
tempo = set_array[0];
if (marci_debug) Serial.println("Loading Stepsize");
cfg.step_size = set_array[1];
if (marci_debug) Serial.println("Loading Transpose");
transpose = set_array[2];
uint8_t z = 3;
if (marci_debug) Serial.println("Loading Track_notes");
for (uint8_t i = 0; i < 8; ++i) {
seqr.track_notes[i] = set_array[z];
z++;
}
if (marci_debug) Serial.println("Loading Ctrl_Notes");
for (uint8_t i = 0; i < 3; ++i) {
seqr.ctrl_notes[i] = set_array[z];
z++;
}
if (marci_debug) Serial.println("Loading Track Channels");
for (uint8_t i = 0; i < 8; ++i) {
seqr.track_chan[i] = set_array[z];
z++;
}
if (marci_debug) Serial.println("Loading Ctrl Channel");
seqr.ctrl_chan = set_array[z] > 0 ? set_array[z] : seqr.ctrl_chan;
if (marci_debug) Serial.println("Loading Swing");
seqr.swing = set_array[z + 1] > 0 ? set_array[z + 1] : seqr.swing;
if (marci_debug) Serial.println("Loading Brightness");
brightness = set_array[z + 2] > 0 ? set_array[z + 2] : brightness;
z = z + 3;
if (marci_debug) Serial.println("Loading Modes");
for (uint8_t i = 0; i < 8; ++i) {
if (marci_debug) {
Serial.print(i);
Serial.print( " = " );
}
seqr.modes[i] = set_array[z] > 0 ? set_array[z] : TRIGATE;
z++;
if (marci_debug) {
Serial.println(seqr.modes[i]);
}
}
if (marci_debug) Serial.println("Loading vOct / HzV");
for (uint8_t i = 0; i < 2; ++i) {
if (marci_debug) {
Serial.print(i);
Serial.print( " = " );
}
hzv[i] = set_array[z];
z++;
if (marci_debug) {
Serial.println(hzv[i]);
}
}
if (marci_debug) Serial.println("Loading Divisions");
for (uint8_t i = 0; i < 8; ++i) {
if (marci_debug) {
Serial.print(i);
Serial.print( " = " );
}
seqr.divs[i] = set_array[z];
z++;
if (marci_debug) {
Serial.println(seqr.divs[i]);
}
}
if (marci_debug) Serial.println("Loading Offsets");
for (uint8_t i = 0; i < 8; ++i) {
if (marci_debug) {
Serial.print(i);
Serial.print( " = " );
}
seqr.offsets[i] = set_array[z];
z++;
if (marci_debug) {
Serial.println(seqr.offsets[i]);
}
}
if (marci_debug) Serial.println("Loading Lengths");
for (uint8_t i = 0; i < 8; ++i) {
if (marci_debug) {
Serial.print(i);
Serial.print( " = " );
}
seqr.lengths[i] = set_array[z];
z++;
if (marci_debug) {
Serial.println(seqr.lengths[i]);
}
}
file.close();
doc.clear();
if (marci_debug) Serial.println("All settings loaded");
trellis.show();
}
// General Storage bits...
// List flash content
void flash_store() {
if (!fatfs.exists(D_FLASH)) {
if (marci_debug) Serial.println(F("" D_FLASH " directory not found, creating..."));
// Use mkdir to create directory (note you should _not_ have a trailing
// slash).
fatfs.mkdir(D_FLASH);
if (!fatfs.exists(D_FLASH)) {
if (marci_debug) Serial.println(F("Error, failed to create " D_FLASH " directory!"));
while (1) {
yield();
}
} else {
if (marci_debug) Serial.println(F("Created " D_FLASH " directory!"));
}
} else {
if (marci_debug) Serial.println(F("Mounted " D_FLASH " directory!"));
}
}
#endif