forked from julbouln/musicbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1390 lines (1090 loc) · 28.9 KB
/
main.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
/*
musicbox firmware
*/
#include "mbed.h"
#include "rtos.h"
#include "CcgMusic.h"
#include "Adafruit_SSD1306.h"
#include "string"
#include "USBMIDI.h"
//#include "USBSerial.h"
#include "SDFileSystem.h"
#include "MD_MIDIFile.h"
#include "Rng.h"
#include "Sam2695.h"
#include "mcp23s08.h"
#include "BatteryMonitor.h"
/* Board pins */
#define SCREEN_MOSI PA_7
#define SCREEN_SCK PA_5
#define SCREEN_DC PC_5
#define SCREEN_RST PC_4
#define SCREEN_CS PB_0
#define PAD_MISO PA_6
#define PAD_CS PB_1
#define PAD_INT PA_0
#define MIDI_RST PC_11
#define MIDI_OUT PC_10
#define JACK_SENSE PB_12
#define AMP_SHDN PB_13
#define BAT_SENSE PC_0
#define BAT_STAT1 PB_3
#define BAT_STAT2 PB_4
#define BAT_PG PB_5
#define SD_CS PC_1
#define SD_MISO PC_2
#define SD_MOSI PC_3
#define SD_SCK PB_10
#define SD_CD PC_15
/* PAD */
#define PAD_TOP 0b10111
#define PAD_BOTTOM 0b11110
#define PAD_PREVIOUS 0b11101
#define PAD_NEXT 0b01111
#define PAD_SELECT 0b11011
/* Globals */
const unsigned char usb_icon_bits[] = {
0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0x0c, 0x03, 0x00,
0x0c, 0x06, 0x00, 0x00, 0x1e, 0x03, 0x80, 0x01, 0xff, 0xff, 0xff, 0x07,
0xff, 0xff, 0xff, 0x07, 0x1e, 0x10, 0x80, 0x01, 0x0c, 0x60, 0x00, 0x00,
0x00, 0xc0, 0x1c, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x1c, 0x00
};
mcp23s08 pad(SCREEN_MOSI, PAD_MISO, SCREEN_SCK, PAD_CS, 0x20);
Rng rng;
USBMIDI midiUsb;
InterruptIn jackSense(JACK_SENSE);
DigitalOut shdnAmp(AMP_SHDN);
InterruptIn padInt(PAD_INT);
Sam2695 midiOut(MIDI_OUT, MIDI_RST);
//SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, "sd");
SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, "sd", SD_CD, SDFileSystem::SWITCH_NONE,25000000);
//osThreadId mainThreadID;
// an SPI sub-class that provides a constructed default
class SPIPreInit : public SPI
{
public:
SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi, miso, clk)
{
format(8, 3);
frequency(2000000);
};
};
SPIPreInit screenSpi(SCREEN_MOSI, NC, SCREEN_SCK);
Adafruit_SSD1306_Spi screen(screenSpi, SCREEN_DC, SCREEN_RST, SCREEN_CS, 64, 128);
BatteryMonitor batteryMonitor(BAT_SENSE, BAT_STAT1, BAT_STAT2, BAT_PG);
class MidiRaw : public MidiDriver {
Mutex mutex;
public:
MidiRaw() {
min_queue_size = 100;
max_queue_size = 200;
}
void mutexLock() {
mutex.lock();
}
void mutexUnlock() {
mutex.unlock();
}
void msleep(int ms) {
Thread::wait(ms * 4);
}
void sendMessage(QueueMessage *qm)
{
uint8_t *m = qm->getMessage();
for (int i = 0; i < qm->getSize(); i++)
midiOut.send(m[i]);
// printf("MidiMessage: %x %x %x (%d)\n",m[0],m[1],m[2],qm->getSize());
if (midiUsb.suspended)
midiUsb.write(MIDIMessage(m, qm->getSize()));
}
};
MidiRaw midiWriter;
int low_battery = 0;
/* Callbacks & interrupts */
void jackPlugged() {
shdnAmp = 0;
}
void jackUnplugged() {
shdnAmp = 1;
}
void transmitMessage(MIDIMessage msg) {
for (int i = 1; i < msg.length; i++) {
midiOut.send(msg.data[i]);
}
}
void processMidiDriver(void const *args) {
while (1) {
midiWriter.launch();
// osSignalSet(mainThreadID, 0x1);
Thread::wait(100);
}
}
// external clock on PA_8
// crystal should be 12mhz
void setClockOutput()
{
GPIO_InitTypeDef InitStruct;
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // select PLLCLK as source
//Configure GPIO pin to output clock signal at PA8
// (1 << (PA_8 & 0xF)) == GPIO_PIN_8
InitStruct.Pin = GPIO_PIN_8;
InitStruct.Mode = GPIO_MODE_AF_PP;
InitStruct.Pull = GPIO_NOPULL;
InitStruct.Speed = GPIO_SPEED_HIGH;
InitStruct.Alternate = GPIO_AF0_MCO;
HAL_GPIO_Init(GPIOA, &InitStruct);
}
void renderBattery(int x, int y, int percent)
{
screen.drawRect(x, y, 10, 6, WHITE);
screen.fillRect(x + 11, y + 2, 2, 2, WHITE);
screen.fillRect(x, y, (percent) / 10, 6, WHITE);
}
void checkJackSense() {
if (jackSense) {
shdnAmp = 0;
} else {
shdnAmp = 1;
}
}
void shutdownSound() {
midiOut.shutdown();
shdnAmp = 0;
}
void activateSound() {
midiOut.reset();
checkJackSense();
}
void initJackSense() {
shdnAmp = 0;
// checkJackSense();
jackSense.rise(&jackPlugged);
jackSense.fall(&jackUnplugged);
}
class Program {
public:
bool terminate;
bool isPlaying;
Program() {
terminate = false;
}
virtual void init() {}
virtual void process() {}
virtual void parsePad(int padio) {}
virtual void updateScreen() {}
virtual string name() {
return "";
}
protected:
void play() {
isPlaying = true;
midiWriter.setPause(false);
midiWriter.setStop(false);
}
void pause() {
isPlaying = false;
midiWriter.setPause(true);
}
void stop() {
isPlaying = false;
midiWriter.setStop(true);
}
};
typedef Program *(*ProgramCreator)();
template <typename T>
static Program *makeProgram()
{
return new T {};
};
Program *program;
string nextProgram;
uint8_t pad_gpio = 0;
uint8_t pad_pressed = 0;
Mutex screenMutex;
void padPressed() {
pad_pressed++;
}
void padReleased() {
pad_pressed = 0;
}
void initPad() {
pad.begin();
pad.gpioRegisterWriteByte(pad.IOCON, 0b00100000); //chip configure,serial, HAEN, etc.
pad.gpioRegisterWriteByte(pad.IODIR, 0b00011111); //pin0..4(in) / 5...7(out)
pad.gpioRegisterWriteByte(pad.GPPU, 0b00011111); //pull up resistors
pad.gpioRegisterWriteByte(pad.DEFVAL, 0b00011111); //default comparison value for pin 0..4
pad.gpioRegisterWriteByte(pad.GPINTEN, 0b00011111); //int on change enable on pin 0..4
pad.gpioRegisterWriteByte(pad.INTCON, 0b00011111); //int on change control on pin 0..4
pad.gpioRegisterReadByte(pad.INTCAP);//clear int register
padInt.fall(&padPressed);
padInt.rise(&padReleased);
}
void processInterface(void const *args) {
while (1) {
screenMutex.lock();
/*
if (low_battery) {
midiOut.shutdown();
shdnAmp = 0;
}
*/
if (!program->terminate) {
if (pad_pressed > 0) {
padInt.disable_irq();
pad_gpio = pad.gpioRegisterReadByte(pad.GPIO);
// only call parsePad once per press
if (pad_pressed == 1)
program->parsePad(pad_gpio);
padInt.enable_irq();
}
}
screen.fillScreen(BLACK);
screen.setTextCursor(0, 0);
if (!program->terminate) {
screen.printf("%s\r\n", program->name().c_str());
program->updateScreen();
}
renderBattery(128 - 12, 0, batteryMonitor.percent());
if (midiUsb.suspended) {
screen.drawXBitmap(128 - 27, 14, usb_icon_bits, 27, 12, WHITE);
}
/*
switch (batteryMonitor.state()) {
case BAT_STATE_FAULT:
low_battery = 0;
screen.printf("BAT_STATE_FAULT\r\n");
break;
case BAT_STATE_CHARGE:
low_battery = 0;
screen.printf("BAT_STATE_CHARGE\r\n");
break;
case BAT_STATE_LOW:
low_battery = 1;
screen.printf("BAT_STATE_LOW\r\n");
break;
case BAT_STATE_COMPLETE:
low_battery = 0;
screen.printf("BAT_STATE_COMPLETE\r\n");
break;
case BAT_STATE_NO_BATTERY:
low_battery = 0;
screen.printf("BAT_STATE_NO_BATTERY\r\n");
break;
case BAT_STATE_NO_POWER:
low_battery = 0;
screen.printf("BAT_STATE_NO_POWER\r\n");
break;
}
*/
// screen.printf("VBAT %f\r",batteryMonitor.voltage());
// screen.printf("VBAT %f\r\n", batteryMonitor.voltage());
// screen.printf("VBAT %d\r\n", batteryMonitor.percent());
// screen.printf("PAD %d %d %d\r\n", pad_gpio, pad_pressed,program->terminate);
// screen.printf("MIDI %d\r", midiWriter.getQueueSize());
screen.display();
screenMutex.unlock();
// wait(0.1);
Thread::wait(200);
}
}
//#define PROGRAM_COUNT 5
//Program *programs[PROGRAM_COUNT];
map<string, ProgramCreator> programsMap;
vector<string> programs;
// Ccgmusic program
#define ARRANGEMENTS_COUNT 11
#define STRUCTURES_COUNT 2
#define TEMPO_COUNT 8
class CcgProgram : public Program {
string structureScript;
string arrangementScript;
int tempo;
int seed;
const string arrangements[ARRANGEMENTS_COUNT] {
"Piano Simple Arrangement",
"Piano Advanced Swinging Blues",
"Piano Advanced Boogie Woogie",
"Piano Advanced Disco",
"Piano Advanced Classical",
"Simple Latin Style Arrangement",
"Simple Dance Style Arrangement",
"Simple Instrumental March Arrangement",
"Simple Ballad Style Arrangement",
"Simple Punk Rock Style Arrangement",
"Random Electro Rock"
};
const string structures[STRUCTURES_COUNT] = {
// "Classical Structure Small",
"Classical Structure Big",
"Modern Song Structure"
// "Random Structure"
};
const int tempos[TEMPO_COUNT] = {60, 80, 100, 120, 160, 180, 200, 240};
int currentSel;
public:
CcgProgram() : Program() {
structureScript = "Classical Structure Big";
arrangementScript = "Piano Advanced Classical";
tempo = 120;
setRandomSeed();
isPlaying = true;
currentSel = 0;
}
void process() {
activateSound();
currentSel = 0;
play();
while (!terminate) {
if (isPlaying) {
midiWriter.setStop(false);
SongCreator *songCreator = new SongCreator();
songCreator->createSong(seed, tempo, structureScript, arrangementScript, &midiWriter);
// osSignalWait(0x1, osWaitForever);
while (midiWriter.getQueueSize() > 0) {
wait_ms(100);
}
delete songCreator;
setRandomSeed();
} else {
wait_ms(100);
}
}
}
void parsePad(int pad_gpio) {
if (pad_gpio == PAD_PREVIOUS) {
if (currentSel > 0)
currentSel--;
}
if (pad_gpio == PAD_NEXT) {
if (currentSel < 2)
currentSel++;
}
// select
if (pad_gpio == PAD_SELECT) {
switch (currentSel) {
case 0:
if (isPlaying) {
pause();
}
else {
play();
}
break;
case 1:
nextSeed();
break;
case 2:
stop();
shutdownSound();
program->terminate = true;
//program = programs[0];
nextProgram = "Main menu";
break;
}
}
}
void updateScreen() {
screen.printf("Seed %d\r\n", seed);
screen.printf("%s %s %d\r\n", arrangementScript.c_str(), structureScript.c_str(), tempo);
// screen.printf("%s\r\n", structureScript.c_str());
// screen.printf("Tempo %d\r\n", tempo);
if (currentSel == 0)
screen.printf(">");
else
screen.printf(" ");
if (isPlaying) {
screen.printf("Pause ");
}
else {
screen.printf("Play ");
}
if (currentSel == 1)
screen.printf(">");
else
screen.printf(" ");
screen.printf("Next ");
if (currentSel == 2)
screen.printf(">");
else
screen.printf(" ");
screen.printf("Quit\r\n");
}
string name() {
return "Music generator";
}
private:
void nextSeed()
{
midiWriter.setStop(true);
}
void setRandomSeed() {
seed = rng.randint(0, 2147483647);
srand(seed);
arrangementScript = arrangements[Utils::getRandomInt(0, ARRANGEMENTS_COUNT-1)];
// arrangementScript="Random Electro Rock";
structureScript = structures[Utils::getRandomInt(0, STRUCTURES_COUNT-1)];
tempo = tempos[Utils::getRandomInt(0, TEMPO_COUNT-1)];
/* arrangementScript="Simple Dance Style Arrangement";
structureScript="Modern Song Structure";
tempo=120;
seed=134;
*/
}
};
// MIDI File player program
void smfMidiCallback(midi_event *pev)
{
if ((pev->data[0] >= 0x80) && (pev->data[0] <= 0xe0))
{
MidiDriver::QueueMessage qm;
uint8_t *m = qm.getMessage();
m[0] = pev->data[0] | pev->channel;
for (uint8_t i = 1; i < pev->size; i++)
{
m[i] = pev->data[i];
}
qm.setSize(pev->size);
qm.setTime(pev->timestamp);
midiWriter.queueMessage(qm);
}
else {
MidiDriver::QueueMessage qm;
uint8_t *m = qm.getMessage();
for (uint8_t i = 0; i < pev->size; i++)
{
m[i] = pev->data[i];
}
qm.setSize(pev->size);
qm.setTime(pev->timestamp);
midiWriter.queueMessage(qm);
}
}
void smfSysexCallback(sysex_event *pev)
{
// MidiDriver::QueueMessage qm;
// uint8_t *m = qm.getMessage();
for (uint8_t i = 0; i < pev->size; i++)
{
// m[i] = pev->data[i];
midiOut.send(pev->data[i]);
}
/*
qm.setSize(pev->size);
qm.setTime(0);
midiWriter.queueMessage(qm);
*/
}
class MidiFileProgram : public Program {
MD_MIDIFile SMF;
int file_err, dir_err;
string midiFilename;
int currentSel;
public:
MidiFileProgram() : Program() {
currentSel = 0;
initMidiPlayer();
}
void process() {
activateSound();
currentSel = 0;
play();
while (!terminate) {
wait_ms(100);
processMidiFile(NULL);
}
}
void parsePad(int pad_gpio) {
if (pad_gpio == PAD_PREVIOUS) {
if (currentSel > 0)
currentSel--;
}
if (pad_gpio == PAD_NEXT) {
if (currentSel < 2)
currentSel++;
}
// select
if (pad_gpio == PAD_SELECT) {
switch (currentSel) {
case 0:
if (isPlaying) {
pause();
}
else {
play();
}
break;
case 1:
stop();
break;
case 2:
stop();
shutdownSound();
program->terminate = true;
//program = programs[0];
nextProgram = "Main menu";
break;
}
}
}
void updateScreen() {
if (dir_err < 0 && file_err < 0) {
screen.printf("Playing MIDI file %s\r\n", midiFilename.c_str());
} else {
if (dir_err == 0) {
screen.printf("Could not open SD Card\r\n");
} else {
if (file_err == 0) {
screen.printf("Could not open %s\r\n", midiFilename.c_str());
} else {
screen.printf("Loading\r\n");
}
}
}
if (currentSel == 0)
screen.printf(">");
else
screen.printf(" ");
if (isPlaying) {
screen.printf("Pause ");
}
else {
screen.printf("Play ");
}
if (currentSel == 1)
screen.printf(">");
else
screen.printf(" ");
screen.printf("Next ");
if (currentSel == 2)
screen.printf(">");
else
screen.printf(" ");
screen.printf("Quit\r\n");
}
string name() {
return "MIDI file player";
}
private:
void initMidiPlayer() {
sd.large_frames(true);
SMF.begin("sd");
SMF.setMidiHandler(smfMidiCallback);
SMF.setSysexHandler(smfSysexCallback);
}
void testSD() {
FILE *fp = fopen("/sd/sdtest2.txt", "w");
if (fp == NULL) {
error("Could not open file for write\n");
}
fprintf(fp, "Hello fun SD Card World!");
fclose(fp);
}
void processMidiFile(void const *args) {
dir_err = -1;
file_err = -1;
DIR *d; // Directory pointer
dirent *file;
if ((d = opendir("/sd")) == NULL) { // Attempt to open directory at given location
dir_err = 0;
} else {
// If Successful
while ((file = readdir(d)) != NULL and !terminate) {
play();
string fn(file->d_name);
midiFilename = fn;
SMF.setFilename(("/sd/" + fn).c_str()); //"/sd/test.mid");
file_err = SMF.load();
midiWriter.setTicksPerQuarterNote(SMF.getTicksPerQuarterNote());
midiWriter.sendTempo(0, 0, 60000000 / SMF.getTempo());
if (file_err < 0) {
while (!SMF.isEOF())
{
screenMutex.lock();
midiWriter.wait();
if (midiWriter.getQueueSize() <= 100)
SMF.getNextEvent();
screenMutex.unlock();
if (midiWriter.getStop()) {
break;
}
}
// done with this one
SMF.close();
}
wait_ms(100);
//osSignalWait(0x1, osWaitForever);
while (midiWriter.getQueueSize() > 0) {
wait_ms(100);
}
}
closedir(d);
}
}
};
class EmptyProgram : public Program {
public:
EmptyProgram() : Program() {
}
void process() {
while (!terminate)
{
wait_ms(100);
}
}
void parsePad(int pad_gpio) {
}
void updateScreen() {
}
string name() {
return "Empty";
}
};
class SynthesizerProgram : public Program {
public:
SynthesizerProgram() : Program() {
}
void process() {
activateSound();
while (!terminate)
{
wait_ms(100);
}
}
void parsePad(int pad_gpio) {
if (pad_gpio == PAD_SELECT) {
stop();
shutdownSound();
program->terminate = true;
// program = programs[0];
nextProgram = "Main menu";
}
}
void updateScreen() {
screen.printf(">");
screen.printf("Quit\r\n");
}
string name() {
return "Synthesizer";
}
};
// https://github.com/rbirkby/ArduinoTetris
#define TETRIS_WIDTH 10
#define TETRIS_HEIGHT 14
#define TETRIS_BLOCK 4
class TetrisProgram : public Program {
const int lead_notes[58] = {
// part 1
76, 71, 72, 74, 72, 71, 69, 69, 72, 76, 74, 72, 71, 71, 72, 74, 76, 72, 69, 69, 0,
74, 77, 81, 79, 77, 76, 72, 76, 74, 72, 71, 71, 72, 74, 76, 72, 69, 69, 0,
// part 2
64, 60, 62, 59, 60, 57, 56, 59,
64, 60, 62, 59, 60, 64, 69, 69, 68, 0
};
const float lead_times[58] = {
// part 1
1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.5, 0.5, 1.0, 0.5, 0.5, 1.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
// part 2
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 3.0, 1.0
};
const int bass_notes[128] = {
// part 1
40, 52, 40, 52, 40, 52, 40, 52, 33, 45, 33, 45, 33, 45, 33, 45, 32, 44, 32, 44, 32, 44, 32, 44, 33, 45, 33, 45, 33, 47, 48, 52,
38, 50, 38, 50, 38, 50, 38, 50, 36, 48, 36, 48, 36, 48, 36, 48, 35, 47, 35, 47, 35, 47, 35, 47, 33, 45, 33, 45, 33, 45, 33, 45,
// part 2
33, 40, 33, 40, 33, 40, 33, 40, 32, 40, 32, 40, 32, 40, 32, 40, 33, 40, 33, 40, 33, 40, 33, 40, 32, 40, 32, 40, 32, 40, 32, 40,
33, 40, 33, 40, 33, 40, 33, 40, 32, 40, 32, 40, 32, 40, 32, 40, 33, 40, 33, 40, 33, 40, 33, 40, 32, 40, 32, 40, 32, 40, 32, 40
};
const float bass_times[128] = {
// part 1
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
// part 2
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5
};
int curLeadNote;
int curBassNote;
long int curLeadTime;
long int curBassTime;
struct Piece {
unsigned int *type;
int x;
int y;
int dir;
};
struct Block {
int x;
int y;
};
enum DIR {
UP,
RIGHT,
DOWN,
LEFT,
MIN = UP,
MAX = LEFT
};
int blocks[TETRIS_WIDTH][TETRIS_HEIGHT]; // 2 dimensional array (nx*ny) representing tetris court - either empty block or occupied by a 'piece'
bool playing = true; // true|false - game is in progress
bool lost = false;
Piece current, next; // the current and next piece
unsigned int score = 0; // the current score
int dir = 0;
unsigned int i[4] = {0x0F00, 0x2222, 0x00F0, 0x4444};
unsigned int j[4] = {0x44C0, 0x8E00, 0x6440, 0x0E20};
unsigned int l[4] = {0x4460, 0x0E80, 0xC440, 0x2E00};
unsigned int o[4] = {0xCC00, 0xCC00, 0xCC00, 0xCC00};
unsigned int s[4] = {0x06C0, 0x8C40, 0x6C00, 0x4620};
unsigned int t[4] = {0x0E40, 0x4C40, 0x4E00, 0x4640};
unsigned int z[4] = {0x0C60, 0x4C80, 0xC600, 0x2640};
unsigned int *pieces[7] = {i, j, l, o, s, t, z};
public:
TetrisProgram() : Program() {
int seed = rng.randint(0, 2147483647);
srand(seed);
reset();
curLeadNote = 0;
curBassNote = 0;
curLeadTime = 0;
curBassTime = 0;
}
void process() {
activateSound();
play();
curLeadNote = 0;
curBassNote = 0;
curLeadTime = 0;
curBassTime = 0;
while (!terminate)
{
playMusic();
drop();
wait_ms(500);
}
}
void parsePad(int pad_gpio) {
if (pad_gpio == PAD_TOP) {
rotate();
}
if (pad_gpio == PAD_BOTTOM) {
}
if (pad_gpio == PAD_PREVIOUS) {
move(LEFT);
}
if (pad_gpio == PAD_NEXT) {