-
Notifications
You must be signed in to change notification settings - Fork 4
/
Engine.cpp
1006 lines (820 loc) · 29.2 KB
/
Engine.cpp
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Engine.cpp
Contains all of the sequencer logic, input handline, etc. This is the
brains of the operation. All of the non-volitile variables used by this
script are stored in Snapshot.php. It was originally programmed such that
multiple 'snapshots' could be supported, but because of the very limited memory
in the Arduino Nano, this feature was never added.
*/
#include "Arduino.h"
#include "Engine.h"
Engine::Engine()
{
rnd = new Rand();
rnd->seed(millis());
// Memory, and display objects (I2C devices)
memory = new Memory(EEPROM_I2C_ADDRESS);
snapshot = new Snapshot(memory);
dual_display_driver = new DualDisplayDriver(DISPLAY_I2C_ADDRESS, snapshot);
sequencer = new Sequencer();
transposer = new Transposer(snapshot);
transposer2 = new Transposer(snapshot);
transposer2->clock_division = 2;
// Rotary encoder objects
step_encoder = new RotaryEncoder(A0, A1, A2); // top encoder
value_encoder = new RotaryEncoder(8, 9, 10); // bottom encoder
// Trigger input objects
clock_button = new TriggerInput(PIN_CLOCK_BUTTON);
clock_input = new TriggerInput(PIN_CLOCK_INPUT);
reset_button = new TriggerInput(PIN_RESET_BUTTON);
reset_input = new TriggerInput(PIN_RESET_INPUT);
// Change the reset and clock input's debounce time to a
// slim 1 millilsecond. (The default is 50 milliseconds,
// which is fine for pushbuttons, but too slow for other uses.)
reset_input->setDebounce(1);
clock_input->setDebounce(1);
// Mode switch
mode_switch = new SwitchInput(PIN_SWITCH_A, PIN_SWITCH_B);
// Output object
output = new Output(DAC_I2C_ADDRESS, snapshot, transposer, transposer2);
}
void Engine::init()
{
// I2C setup
Wire.begin();
snapshot->init();
step_encoder->init();
value_encoder->init();
sequencer->init();
// Initialize display driver and set displays
dual_display_driver->init();
// Test to see if the reset button is being held or
// if the initialized flag is not set to "22" in memory.
// If either is true, initialize the unit by setting all values to 0,
// the length to 8, and the clock division to 1.
uint8_t self_initialization_flag = memory->read(MEM_ADDR_SELF_INITIALIZATION);
if(reset_button->read() || self_initialization_flag != 22)
{
this->factoryReset();
memory->write(MEM_ADDR_SELF_INITIALIZATION, 22);
}
// Initialize rotary encoder sensitivity values
z_sequence_length = snapshot->sequence_length << 1;
z_clock_division = snapshot->clock_division << 1;
z_scale = snapshot->scale << 1;
z_intensity = snapshot->display_intensity << 1;
z_hold = snapshot->hold << 1;
z_song = snapshot->song << 1;
z_song2 = snapshot->song2 << 1;
z_hold_offset = snapshot->hold_offset << 1;
hold_threshold = snapshot->hold_threshold; // Full sensitivity!
// Show a welcome message very briefly
dual_display_driver->writeByteCode(0, __H__); // H
dual_display_driver->writeByteCode(1, __E__); // E
dual_display_driver->writeByteCode(2, __L__); // L
dual_display_driver->writeByteCode(3, __O__); // O
// Firmware version
dual_display_driver->writeDigit(4, 1); // 1
dual_display_driver->writeByteCode(5, __underscore__); // _
dual_display_driver->writeDigit(6, 0); // 0
dual_display_driver->writeDigit(7, 0); // 1
delay(500);
value = snapshot->sequence[step];
output->write(value);
}
void Engine::loop()
{
mode_switch->poll();
// Reading the value encoder is done once, here, for efficiency.
value_encoder_button_pressed = value_encoder->readButton();
switch(mode_switch->position)
{
case 1:
mode = SETTINGS_MODE;
break;
case 2:
if(mode != SEQUENCE_EDIT_MODE)
{
edit_step = step;
edit_value = snapshot->sequence[step];
mode = SEQUENCE_EDIT_MODE;
}
break;
case 3:
if(mode != SEQUENCE_PLAYBACK_MODE)
{
value = snapshot->sequence[step];
mode = SEQUENCE_PLAYBACK_MODE;
}
break;
}
switch(mode)
{
case SEQUENCE_PLAYBACK_MODE:
this->sequencePlaybackMode();
break;
case SEQUENCE_EDIT_MODE:
this->sequenceEditMode();
break;
case SETTINGS_MODE:
this->settingsMode();
break;
}
// Constantly playback when clocked, independent of the mode
this->playback();
}
//
// Engine::playback()
//
// This method is called every loop cycle. It's in charge of keeping the
// sequence playing. Not to be confused with playback_mode, which is the mode
// where users can watch the sequence playback.
//
void Engine::playback()
{
clock_input->poll();
reset_input->poll();
if(reset_input->triggered)
{
if(snapshot->rst_input_assignment == RST_ASSIGNMENT_RESET)
{
step = 0;
sequencer->resetDrift();
transposer->reset();
transposer2->reset();
value = snapshot->sequence[step];
output->write(value);
}
else // if(rst_input_assignment == RST_ASSIGNMENT_SAMPLE_AND_HOLD)
{
sample = true;
}
}
// Step the sequencer using either the clock button or clock input
if(clock_input->triggered)
{
// Step the internal clock variable
clock_counter = clock_counter + 1;
// Once the clock division has been reached, increment the step variable
if(clock_counter >= snapshot->clock_division)
{
if(snapshot->slip == 0 || (rnd->random(100) > snapshot->slip)) // if no slip
{
// Step sequencer
step = step + 1;
if(step >= snapshot->sequence_length)
{
step = 0;
// Step transposers
transposer->clock();
transposer2->clock();
}
// Apply drift
if(snapshot->drift_percentage && (rnd->random(100) < snapshot->drift_percentage))
{
sequencer->drift[step] = sequencer->drift[step] + ((int) rnd->random(snapshot->drift_amount << 1)) - snapshot->drift_amount;
}
}
//
// This large if-statement is saying, in English:
//
// "If the sequence value at the (current step + the hold offset) is greater than the hold_cutoff,
// then go ahead an play the next note. Otherwise hold the current note."
//
if((snapshot->rst_input_assignment != RST_ASSIGNMENT_SAMPLE_AND_HOLD) && snapshot->sequence[((step + snapshot->hold_offset) % snapshot->sequence_length)] > snapshot->hold_threshold)
{
sample = true;
}
if(sample == true)
{
// Get the value from the sequencer
value = snapshot->sequence[step];
drift = sequencer->drift[step];
// int32_t total = value + drift;
// total = constrain(total, 0, 4095);
if((mode == SEQUENCE_PLAYBACK_MODE) && value_encoder_button_pressed)
{
// do not play output while realtime recording in playback mode
}
else
{
output->write(constrain(value + drift, 0, 4095));
}
sample = false;
}
// Reset clock_counter
clock_counter = 0;
}
}
}
//
// Engine::sequencePlaybackMode()
//
// This mode lets the user to watch the sequence playback on the bubble displays.
// The user can manually reset the sequence in this mode using the reset button.
// The user can manually step the sequence in this mode using the step button.
// Neither of the rotary encoders do anything in this mode.
//
void Engine::sequencePlaybackMode()
{
// Poll buttons
clock_button->poll();
reset_button->poll();
// Step the sequencer using the clock button
if(clock_button->triggered)
{
step = (step + 1) % snapshot->sequence_length;
transposer->step();
transposer2->step();
clock_counter = 0;
// Get the value from the sequencer
value = snapshot->sequence[step];
drift = sequencer->drift[step];
int32_t total = value + drift;
total = constrain(total, 0, 4095);
output->write(total);
}
if(reset_button->triggered)
{
step = 0;
clock_counter = 0;
sequencer->resetDrift();
transposer->reset();
transposer2->reset();
value = snapshot->sequence[step];
output->write(value);
}
// Handle realtime recording
if(value_encoder_button_pressed)
{
// Just been pressed?
if(value_encoder->pressed())
{
realtime_recording_value = 0;
}
realtime_recording_value = (realtime_recording_value + (value_encoder->read() * 100));
realtime_recording_value = constrain(realtime_recording_value, 0, 4095);
value = realtime_recording_value;
// Update the sequencer with the new value
snapshot->setValue(step, realtime_recording_value);
output->write(realtime_recording_value);
}
dual_display_driver->write(TOP_DISPLAY, step + 1);
dual_display_driver->write(BOTTOM_DISPLAY, value);
}
//
// Engine::sequencePlaybackMode()
//
// This mode lets the user to edit the sequencer values.
// The top knob selects the step.
// The bottom knob selects the value.
//
void Engine::sequenceEditMode()
{
// Poll buttons
clock_button->poll();
reset_button->poll();
int16_t step_acceleration = 1;
int16_t value_acceleration = 1;
if(step_encoder->readButton() && (snapshot->sequence_length > 16)) step_acceleration = 10;
if(snapshot->press_functionality == PRESS_ASSIGNMENT_FINE) value_acceleration = 100;
if(value_encoder_button_pressed)
{
if(snapshot->press_functionality == PRESS_ASSIGNMENT_FINE)
{
value_acceleration = 1;
}
else
{
value_acceleration = 100;
}
}
z_edit_step = (z_edit_step + (step_encoder->read() * step_acceleration));
// Step the sequencer using the clock button
if(clock_button->triggered) z_edit_step += 2;
if(reset_button->triggered) z_edit_step -= 2;
z_edit_step = constrain(z_edit_step, 0, (snapshot->sequence_length << 1) - 1);
uint16_t edit_step = z_edit_step >> 1;
// Get the value from the sequencer
edit_value = snapshot->sequence[edit_step];
// Read value encoder and adjust value (bottom knob/display)
edit_value = (edit_value + (value_encoder->read() * value_acceleration));
edit_value = constrain(edit_value, 0, 4095);
// Update the sequencer with the new value
snapshot->setValue(edit_step, edit_value);
dual_display_driver->write(TOP_DISPLAY, edit_step + 1);
dual_display_driver->write(BOTTOM_DISPLAY, edit_value);
}
//
// Engine::settingsMode()
//
// This mode lets the user to edit the module's settings.
// Settings are organized into pages, which are selected via the
// top rotary encoder or the step button.
//
void Engine::settingsMode()
{
// Sequence length
if(settings_page == 0)
{
int sequence_length_acceleration = 1;
// Rotary encoder buttons are used for input accelleration
if(value_encoder_button_pressed) sequence_length_acceleration = 20;
// Set sequence length
//
// The variable 'z_sequence_length. is 2X the sequence length, and controls
// the sensitivity of the rotary encoder, which can be too sensitive at times.
z_sequence_length = z_sequence_length + (value_encoder->read() * sequence_length_acceleration);
z_sequence_length = constrain(z_sequence_length, 2, MAX_SEQUENCE_LENGTH << 1);
uint16_t sequence_length = z_sequence_length >> 1;
snapshot->setSequenceLength(sequence_length);
// Update displays
dual_display_driver->writeByteCode(0, __L__); // L
dual_display_driver->writeByteCode(1, __E__); // E
dual_display_driver->writeByteCode(2, __n__); // n
dual_display_driver->writeByteCode(3, __g__); // g
dual_display_driver->write(BOTTOM_DISPLAY, sequence_length);
}
// Clock division settings
if(settings_page == 1)
{
int clock_division_acceleration = 1;
// Rotary encoder buttons are used for input accelleration
if(value_encoder_button_pressed) clock_division_acceleration = 20;
// Set clock division
z_clock_division = z_clock_division + (value_encoder->read() * clock_division_acceleration);
z_clock_division = constrain(z_clock_division, 2, 512 << 1);
uint16_t clock_division = z_clock_division >> 1;
snapshot->setClockDivision(clock_division);
// Update displays
dual_display_driver->writeByteCode(0, __C__); // c
dual_display_driver->writeByteCode(1, __d__); // d
dual_display_driver->writeByteCode(2, __i__); // i
dual_display_driver->writeByteCode(3, __v__); // v
dual_display_driver->write(BOTTOM_DISPLAY, clock_division);
}
// Scale settings
if(settings_page == 2)
{
// Set scale
z_scale = z_scale + (value_encoder->read());
z_scale = constrain(z_scale, 0, NUMBER_OF_SCALES << 1);
uint16_t scale = z_scale >> 1;
snapshot->setScale(scale);
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __C__); // C
dual_display_driver->writeByteCode(2, __A__); // A
dual_display_driver->writeByteCode(3, __L__); // L
switch(scale) {
case 0: // None
dual_display_driver->writeByteCode(4, __n__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __n__);
dual_display_driver->writeByteCode(7, __E__);
break;
case 1: // MAJOR
dual_display_driver->writeByteCode(4, __M__);
dual_display_driver->writeByteCode(5, __A__);
dual_display_driver->writeByteCode(6, __j__);
dual_display_driver->writeByteCode(7, __o__);
break;
case 2: // MINOR
dual_display_driver->writeByteCode(4, __M__);
dual_display_driver->writeByteCode(5, __i__);
dual_display_driver->writeByteCode(6, __n__);
dual_display_driver->writeByteCode(7, __o__);
break;
case 3: // IONIAN
dual_display_driver->writeByteCode(4, __i__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __n__);
dual_display_driver->writeByteCode(7, __i__);
break;
case 4: // DORIAN
dual_display_driver->writeByteCode(4, __d__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __r__);
dual_display_driver->writeByteCode(7, __i__);
break;
case 5: // LYDIAN
dual_display_driver->writeByteCode(4, __L__);
dual_display_driver->writeByteCode(5, __Y__);
dual_display_driver->writeByteCode(6, __d__);
dual_display_driver->writeByteCode(7, __I__);
break;
case 6: // PHRYGIAN
dual_display_driver->writeByteCode(4, __P__);
dual_display_driver->writeByteCode(5, __H__);
dual_display_driver->writeByteCode(6, __r__);
dual_display_driver->writeByteCode(7, __Y__);
break;
case 7: // MIXOLYDIAN
dual_display_driver->writeByteCode(4, __M__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __L__);
dual_display_driver->writeByteCode(7, __Y__);
break;
case 8: // AEOLIAN
dual_display_driver->writeByteCode(4, __A__);
dual_display_driver->writeByteCode(5, __E__);
dual_display_driver->writeByteCode(6, __O__);
dual_display_driver->writeByteCode(7, __L__);
break;
case 9: // LOCRIAN
dual_display_driver->writeByteCode(4, __L__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __c__);
dual_display_driver->writeByteCode(7, __r__);
break;
case 10: // MAJOR_PENTATONIC
dual_display_driver->writeByteCode(4, __M__);
dual_display_driver->writeByteCode(5, __a__);
dual_display_driver->writeByteCode(6, __j__);
dual_display_driver->writeByteCode(7, __P__);
break;
case 11: // MINOR_PENTATONIC
dual_display_driver->writeByteCode(4, __M__);
dual_display_driver->writeByteCode(5, __i__);
dual_display_driver->writeByteCode(6, __n__);
dual_display_driver->writeByteCode(7, __P__);
break;
case 12: // DIMINISHED
dual_display_driver->writeByteCode(4, __d__);
dual_display_driver->writeByteCode(5, __i__);
dual_display_driver->writeByteCode(6, __m__);
dual_display_driver->writeByteCode(7, __i__);
break;
case 13: // CHROMATIC
dual_display_driver->writeByteCode(4, __c__);
dual_display_driver->writeByteCode(5, __h__);
dual_display_driver->writeByteCode(6, __r__);
dual_display_driver->writeByteCode(7, __o__);
break;
case 14: // GATE
dual_display_driver->writeByteCode(4, __g__);
dual_display_driver->writeByteCode(5, __A__);
dual_display_driver->writeByteCode(6, __t__);
dual_display_driver->writeByteCode(7, __E__);
break;
}
// dual_display_driver->write(BOTTOM_DISPLAY, scale);
}
// Randomize
if(settings_page == 3)
{
dual_display_driver->writeByteCode(0, __r__); // r
dual_display_driver->writeByteCode(1, __A__); // A
dual_display_driver->writeByteCode(2, __n__); // n
dual_display_driver->writeByteCode(3, __d__); // d
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
if(value_encoder->pressed())
{
dual_display_driver->writeByteCode(4, 0b01001110); // [
dual_display_driver->writeByteCode(5, 0b01001000); // =
dual_display_driver->writeByteCode(6, 0b01001000); // =
dual_display_driver->writeByteCode(7, 0b01111000); // ]
for(uint8_t i=0; i<MAX_SEQUENCE_LENGTH; i++)
{
// uint16_t value = random(4096);
uint16_t value = rnd->random(4096);
snapshot->setValue(i, value);
}
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
}
}
// Clear
if(settings_page == 4)
{
dual_display_driver->writeByteCode(0, __C__); // C
dual_display_driver->writeByteCode(1, __L__); // L
dual_display_driver->writeByteCode(2, __E__); // E
dual_display_driver->writeByteCode(3, __r__); // r
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
if(value_encoder->pressed())
{
dual_display_driver->writeByteCode(4, 0b01001110); // [
dual_display_driver->writeByteCode(5, 0b01001000); // =
dual_display_driver->writeByteCode(6, 0b01001000); // =
dual_display_driver->writeByteCode(7, 0b01111000); // ]
// Load sequence from non-volitile ram
for(int i=0; i<MAX_SEQUENCE_LENGTH; i++)
{
snapshot->setValue(i, 0);
}
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
}
}
// Shift Left
if(settings_page == 5)
{
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __H__); // H
dual_display_driver->writeByteCode(2, 0b01001110); // [
dual_display_driver->writeByteCode(3, 0b01001110); // [
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
if(value_encoder->pressed())
{
dual_display_driver->writeByteCode(4, 0b01001110); // [
dual_display_driver->writeByteCode(5, 0b01001000); // =
dual_display_driver->writeByteCode(6, 0b01001000); // =
dual_display_driver->writeByteCode(7, 0b01111000); // ]
int tmp = snapshot->sequence[0];
for(int i=1; i < snapshot->sequence_length; i++)
{
snapshot->setValue(i-1, snapshot->sequence[i]);
}
snapshot->setValue(snapshot->sequence_length - 1, tmp);
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
}
}
// Shift right
if(settings_page == 6)
{
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __H__); // H
dual_display_driver->writeByteCode(2, 0b01111000); // ]
dual_display_driver->writeByteCode(3, 0b01111000); // ]
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
if(value_encoder->pressed())
{
dual_display_driver->writeByteCode(4, 0b01001110); // [
dual_display_driver->writeByteCode(5, 0b01001000); // =
dual_display_driver->writeByteCode(6, 0b01001000); // =
dual_display_driver->writeByteCode(7, 0b01111000); // ]
int tmp = snapshot->sequence[snapshot->sequence_length - 1];
for(int i=snapshot->sequence_length - 1; i > 0; i--)
{
snapshot->setValue(i, snapshot->sequence[i-1]);
}
snapshot->setValue(0, tmp);
dual_display_driver->writeByteCode(4, __dash__); // -
dual_display_driver->writeByteCode(5, __dash__); // -
dual_display_driver->writeByteCode(6, __dash__); // -
dual_display_driver->writeByteCode(7, __dash__); // -
}
}
// Slip
if(settings_page == 7)
{
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __L__); // L
dual_display_driver->writeByteCode(2, __i__); // i
dual_display_driver->writeByteCode(3, __P__); // P
// Set slip
int8_t slip = snapshot->slip + (value_encoder->read());
slip = constrain(slip, 0, 99);
snapshot->setSlip(slip);
dual_display_driver->write(BOTTOM_DISPLAY, slip);
}
// Drift Percentage
if(settings_page == 8)
{
int16_t drift_percentage_acceleration = 1;
if(value_encoder_button_pressed) drift_percentage_acceleration = 10;
dual_display_driver->writeByteCode(0, __d__); // d
dual_display_driver->writeByteCode(1, __r__); // r
dual_display_driver->writeByteCode(2, __dash__); // -
dual_display_driver->writeByteCode(3, __P__); // P
int8_t drift_percentage = snapshot->drift_percentage + (value_encoder->read() * drift_percentage_acceleration);
drift_percentage = constrain(drift_percentage, 0, 100);
snapshot->setDriftPercentage(drift_percentage);
dual_display_driver->write(BOTTOM_DISPLAY, drift_percentage);
}
// Drift Amount
if(settings_page == 9)
{
int16_t drift_acceleration = 1;
if(value_encoder_button_pressed) drift_acceleration = 100;
dual_display_driver->writeByteCode(0, __d__); // d
dual_display_driver->writeByteCode(1, __r__); // r
dual_display_driver->writeByteCode(2, __underscore__); // _
dual_display_driver->writeByteCode(3, __A__); // A
int16_t drift_amount = snapshot->drift_amount;
drift_amount = drift_amount + (value_encoder->read() * drift_acceleration);
drift_amount = constrain(drift_amount, 0, 300);
snapshot->setDriftAmount(drift_amount);
dual_display_driver->write(BOTTOM_DISPLAY, drift_amount);
}
// Hold offset
if(settings_page == 10)
{
int16_t acceleration = 1;
if(value_encoder_button_pressed) acceleration = 10;
dual_display_driver->writeByteCode(0, __h__); // h
dual_display_driver->writeByteCode(1, __d__); // d
dual_display_driver->writeByteCode(2, __dash__); // -
dual_display_driver->writeByteCode(3, __o__); // o
z_hold_offset = z_hold_offset + (value_encoder->read() * acceleration);
z_hold_offset = constrain(z_hold_offset, 0, 64 << 1);
uint16_t hold_offset = z_hold_offset >> 1;
snapshot->setHoldOffset(hold_offset);
dual_display_driver->write(BOTTOM_DISPLAY, hold_offset);
}
// Hold threshold
// Notice: Encoder set to full throttle
if(settings_page == 11)
{
int16_t acceleration = 1;
if(value_encoder_button_pressed) acceleration = 100;
dual_display_driver->writeByteCode(0, __h__); // h
dual_display_driver->writeByteCode(1, __d__); // d
dual_display_driver->writeByteCode(2, __dash__); // -
dual_display_driver->writeByteCode(3, __t__); // t
hold_threshold = hold_threshold + (value_encoder->read() * acceleration);
hold_threshold = constrain(hold_threshold, 0, 4095);
snapshot->setHoldThreshold(hold_threshold);
dual_display_driver->write(BOTTOM_DISPLAY, hold_threshold);
}
// Song pattern
if(settings_page == 12)
{
int encoder_value = value_encoder->read();
if(encoder_value != 0)
{
z_song = z_song + encoder_value;
z_song = constrain(z_song, 0, (NUMBER_OF_SONGS-1) << 1); // 0 == off
snapshot->setSong(z_song >> 1);
}
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __o__); // o
dual_display_driver->writeByteCode(2, __n__); // n
dual_display_driver->writeDigit(3, 1); // 1
dual_display_driver->write(BOTTOM_DISPLAY, z_song >> 1);
}
// Song2 pattern
if(settings_page == 13)
{
int encoder_value = value_encoder->read();
if(encoder_value != 0)
{
z_song2 = z_song2 + encoder_value;
z_song2 = constrain(z_song2, 0, (NUMBER_OF_SONGS-1) << 1); // 0 == off
snapshot->setSong2(z_song2 >> 1);
}
dual_display_driver->writeByteCode(0, __S__); // S
dual_display_driver->writeByteCode(1, __o__); // o
dual_display_driver->writeByteCode(2, __n__); // n
dual_display_driver->writeDigit(3, 2); // 2
dual_display_driver->write(BOTTOM_DISPLAY, z_song2 >> 1);
}
// LED Intensity
if(settings_page == 14)
{
int encoder_value = value_encoder->read();
if(encoder_value != 0)
{
z_intensity = z_intensity + encoder_value;
z_intensity = constrain(z_intensity, 0, 15 << 1);
dual_display_driver->setIntensity(z_intensity >> 1);
}
dual_display_driver->writeByteCode(0, __L__); // L
dual_display_driver->writeByteCode(1, __E__); // E
dual_display_driver->writeByteCode(2, __d__); // d
dual_display_driver->writeByteCode(3, __S__); // S
dual_display_driver->write(BOTTOM_DISPLAY, z_intensity >> 1);
}
// Reset input assignment
if(settings_page == 15)
{
// int16_t hold_acceleration = 1;
// if(value_encoder->readButton()) hold_acceleration = 20;
dual_display_driver->writeByteCode(0, __r__); // r
dual_display_driver->writeByteCode(1, __S__); // S
dual_display_driver->writeByteCode(2, __t__); // t
dual_display_driver->writeByteCode(3, 0b00000000); //
if(value_encoder->pressed())
{
if(snapshot->rst_input_assignment == RST_ASSIGNMENT_SAMPLE_AND_HOLD)
{
snapshot->setRstInputAssignment(RST_ASSIGNMENT_RESET);
}
else
{
snapshot->setRstInputAssignment(RST_ASSIGNMENT_SAMPLE_AND_HOLD);
}
}
if(snapshot->rst_input_assignment == RST_ASSIGNMENT_SAMPLE_AND_HOLD)
{
dual_display_driver->writeByteCode(4, __S__); // S
dual_display_driver->writeByteCode(5, __h__); // h
dual_display_driver->writeByteCode(6, 0b00000000); //
dual_display_driver->writeByteCode(7, 0b00000000); //
}
if(snapshot->rst_input_assignment == RST_ASSIGNMENT_RESET)
{
dual_display_driver->writeByteCode(4, __r__); // r
dual_display_driver->writeByteCode(5, __S__); // S
dual_display_driver->writeByteCode(6, __t__); // t
dual_display_driver->writeByteCode(7, 0b00000000); //
}
}
if(settings_page == 16)
{
dual_display_driver->writeByteCode(0, __P__);
dual_display_driver->writeByteCode(1, __r__);
dual_display_driver->writeByteCode(2, __E__);
dual_display_driver->writeByteCode(3, __S__);
if(value_encoder->pressed())
{
if(snapshot->press_functionality == PRESS_ASSIGNMENT_FINE)
{
snapshot->setPressFunctionality(PRESS_ASSIGNMENT_COARSE);
}
else
{
snapshot->setPressFunctionality(PRESS_ASSIGNMENT_FINE);
}
}
if(snapshot->press_functionality == PRESS_ASSIGNMENT_FINE)
{
dual_display_driver->writeByteCode(4, __F__);
dual_display_driver->writeByteCode(5, __i__);
dual_display_driver->writeByteCode(6, __n__);
dual_display_driver->writeByteCode(7, __E__);
}
if(snapshot->press_functionality == PRESS_ASSIGNMENT_COARSE)
{
dual_display_driver->writeByteCode(4, __c__);
dual_display_driver->writeByteCode(5, __o__);
dual_display_driver->writeByteCode(6, __r__);
dual_display_driver->writeByteCode(7, __S__);
}
}
// Select settings page using the clock button
// Go back a page using the reset button
clock_button->poll();
reset_button->poll();
if(clock_button->triggered) z_settings_page += 4;
if(reset_button->triggered) z_settings_page -= 4;
// You can also use the rotary top encoder to select the page
z_settings_page = z_settings_page + step_encoder->read();
z_settings_page = constrain(z_settings_page, 0, (NUMBER_OF_SETTINGS_PAGES << 2) - 1);
// z_settings_page is used to slow down the reaction to the rotary encoder, which
// normally could be a bit sensitive.
settings_page = z_settings_page >> 2;
}
void Engine::factoryReset()
{
snapshot->setClockDivision(1);
snapshot->setSequenceLength(8);
snapshot->setDriftPercentage(0);
snapshot->setDriftAmount(0);
snapshot->setSlip(0);
snapshot->setScale(0);
snapshot->setHoldOffset(0);
snapshot->setHoldThreshold(0);
snapshot->setSong(0);
snapshot->setSong2(0);
snapshot->setDisplayIntensity(15);
snapshot->setRstInputAssignment(RST_ASSIGNMENT_RESET);
snapshot->setPressFunctionality(PRESS_ASSIGNMENT_FINE);
for(uint8_t i=0; i<MAX_SEQUENCE_LENGTH; i++)
{
uint16_t value = random(4096);
snapshot->setValue(i, value);
}
dual_display_driver->setIntensity(15);
// Animate reset message
dual_display_driver->writeByteCode(0, 0b00000000); //
dual_display_driver->writeByteCode(1, 0b00000000); //
dual_display_driver->writeByteCode(2, 0b00000000); //
dual_display_driver->writeByteCode(3, 0b00000000); //
dual_display_driver->writeByteCode(4, 0b00000000); //
dual_display_driver->writeByteCode(5, 0b00000000); //
dual_display_driver->writeByteCode(6, 0b00000000); //
dual_display_driver->writeByteCode(7, 0b00000000); //
delay(400);
dual_display_driver->writeByteCode(4, 0b01000110); // r
delay(400);
dual_display_driver->writeByteCode(5, 0b01011011); // S
delay(400);
dual_display_driver->writeByteCode(6, 0b01001111); // E
delay(400);
dual_display_driver->writeByteCode(7, 0b00001111); // t
delay(400);