-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreenLogic.cpp
More file actions
1241 lines (976 loc) · 31 KB
/
screenLogic.cpp
File metadata and controls
1241 lines (976 loc) · 31 KB
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
/**
// ____ _ _
// / __ \ | | | |
// | | | || |__| | ___ _ __ ___
// | | | || __ |/ _ \ '__/ _ \
// _\ \/ /_| | | | __/ | | (_) |
// (___||___)_| |_|\___|_| \___/
*/
//Created by Navras Kamal and Amir Shukayev for CMPUT 274 Final Project, December 2017
//Implements some code used in lectures and by TA's for the course
/* HOW THIS IS GONNA WORK
0. Menu: Base splash screen, main menu with difficulty selector, song selector and
high scores. Difficulty can be toggled between two states (easy / hard),
there is currently 7 songs in the list, which can be sorted based on
multiple parameters, and highscores are recorded after each games and
are displayed on a sorted list in the highscores tab
1. Scanner: Every second scans array of notes. if there is a match, note is added
to the screen.
2. Advancement: Every loop the pixels are advanced once. Array holds all notes
and goes through them and advanced them
3. Playing: If the button is pressed, the clicking segment is checked for
available note. If it is then the user is given one point
4. RANDOM SONGS: generates random music in C-minor (Amir's favourite key)
*/
#include <Arduino.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#include "menus.h"
#include "sorting.h"
// pin constants
const int TFT_DC = 9;
const int TFT_CS = 10;
// screen constants
const int ScreenWidth = 320;
const int ScreenHeight = 240;
// currently unused
const int BUTTON_1 = 11;
const int BUTTON_2 = 12;
const int BUTTON_3 = 13;
// for square sin wave frequency outputs.
// sounds like more harsh than normal tones
const int SPEAKER_PIN = 47;
// macros because A2 instead of just an int
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 5 // can be a digital pin
#define XP 4 // can be a digital pin
// width/height of the display when rotated horizontally
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
// 10 works well in terms of lag. shouldnt be more than 6 with current delays
const int MAX_RENDERED_NOTES = 10;
// Number of vertical Lines
int lines;
// game counter
// struct for a note moving across the screen
struct Note {
int progression;
uint8_t num;
};
// temporary song:
int song_1[10];
int song_2[] = {2, 6, 8, 12, 16, 19, 20, 22, 28, 30};
int song_3[] = {0, 4, 13, 14, 18, 21, 24, 26, 27, 29};
// these arrays holds all the notes on the screen to be rendered
// max of 30 notes to be rendered
Note screen_notes1[MAX_RENDERED_NOTES];
Note screen_notes2[MAX_RENDERED_NOTES];
Note screen_notes3[MAX_RENDERED_NOTES];
// goes through the first song for displaying note
int song_counter_1 = 0;
int song_counter_2 = 0;
int song_counter_3 = 0;
// these make sure that 1 tap doesnt give/take away 10+ points at once
boolean cooldown1 = false;
boolean cooldown2 = false;
boolean cooldown3 = false;
// creating the display screen
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// creating the touchscreen
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// gives us middle octave frequencies
const int note_c = 262;
const int note_d = 294;
const int note_e = 330;
const int note_f = 349;
const int note_g = 392;
const int note_a = 440;
const int note_b = 494;
const int node_C = 523;
// these two arrays hold the frequencies of notes.
// follows 440Hz A standard.
// C minor is 3 octaves for now
const int C_MIN[] = {147, 156, 175, 196, 208, 233, 262 /*c*/, 130 /*C*/,
147, 156, 175, 196, 208, 233, 262 /*c*/,
294, 311, 349, 392, 415, 466, 523 /*C*/ };
const int D_MIN[] = {147, 165, 175, 196, 220, 233, 262 /*c*/, 117 /*C*/,
147, 165, 175, 196, 220, 233, 262 /*c*/,
294, 330, 349, 392, 440, 466, 523 /*C*/ };
const int C_MAJ[] = {147, 165, 175, 196, 220, 245, 262 /*c*/, 117 /*C*/,
147, 165, 175, 196, 220, 245, 262 /*c*/,
294, 330, 349, 392, 440, 494, 523 /*C*/ };
const int Weird[] = {523, 587, 659, 698, 784, 880, 988 /*c*/, 1047 /*C*/,
1175, 1319, 1397, 1567, 1760, 1976, 2093 /*c*/,
294, 330, 349, 392, 440, 494, 523 /*C*/ };
const int Chinese[] = {93, 104, 117, 139, 156, 185, 208 /*c*/, 233 /*C*/,
277, 311, 370, 415, 466, 554, 622 /*c*/,
740, 831, 932, 1109, 1254, 1480, 1661 /*C*/ };
const int Grater[] = {93, 104, 117, 139, 93, 104, 117 /*c*/, 233 /*C*/,
277, 311, 370, 415, 277, 311, 370 /*c*/,
740, 831, 932, 1109, 740, 831, 932 /*C*/ };
const int Spouse[] = {156, 185, 117, 139, 156, 185, 208 /*c*/, 233 /*C*/,
466, 311, 622, 415, 466, 311, 622 /*c*/,
740, 831, 932, 440, 466, 523, 523 /*C*/ };
int FrequencyList[22];
// holds patterns for left hand (generally)
int C_MIN_PATTERN_1[] = { FrequencyList[7], FrequencyList[11], FrequencyList[9], FrequencyList[11] };
int C_MIN_PATTERN_2[] = { FrequencyList[7], FrequencyList[12], FrequencyList[9], FrequencyList[12] };
int C_MIN_PATTERN_3[] = { FrequencyList[7], FrequencyList[8], FrequencyList[9], FrequencyList[11] };
int C_MIN_PATTERN_4[] = { FrequencyList[11], FrequencyList[8], FrequencyList[10], FrequencyList[9] };
// holds lower scale patterns
int C_MIN_PATTERN_5[] = { FrequencyList[0], FrequencyList[1], FrequencyList[2], FrequencyList[3] };
int C_MIN_PATTERN_6[] = { FrequencyList[4], FrequencyList[3], FrequencyList[2], FrequencyList[1] };
int C_MIN_PATTERN_7[] = { FrequencyList[6], FrequencyList[5], FrequencyList[4], FrequencyList[3] };
int C_MIN_PATTERN_8[] = { FrequencyList[3], FrequencyList[4], FrequencyList[5], FrequencyList[6] };
// holds higher scale patterns
int C_MIN_PATTERN_9[] = { FrequencyList[14], FrequencyList[15], FrequencyList[16], FrequencyList[17] };
int C_MIN_PATTERN_10[] = { FrequencyList[15], FrequencyList[14], FrequencyList[13], FrequencyList[12] };
int C_MIN_PATTERN_11[] = { FrequencyList[16], FrequencyList[17], FrequencyList[18], FrequencyList[19] };
int C_MIN_PATTERN_12[] = { FrequencyList[18], FrequencyList[17], FrequencyList[18], FrequencyList[16] };
// holds repeated note patterns
int C_MIN_PATTERN_13[] = { FrequencyList[18], FrequencyList[18], FrequencyList[17], FrequencyList[18] };
int C_MIN_PATTERN_14[] = { FrequencyList[17], FrequencyList[17], FrequencyList[16], FrequencyList[17] };
int C_MIN_PATTERN_15[] = { FrequencyList[16], FrequencyList[16], FrequencyList[19], FrequencyList[16] };
int C_MIN_PATTERN_16[] = { FrequencyList[14], FrequencyList[18], FrequencyList[14], FrequencyList[14] };
// for the random number generator.
const int RANDOM_PIN = 33;
// this will count frames from 1-60
int counter1 = 0;
// this will count seconds for the songs;
int counter2 = 0;
// player points
int points = 0;
// for continuing after the song finishes
bool infinteMode = true;
// hard mode subtracts points for lives lost
// not that big of a difficulty increase
bool hardMode = false;
// holds generated music
// size of 20 tones is a good middle ground between using a lot of memory, and
// not making too many calls to the random music generator
int music[20];
// holds the length of the song
int length_music = 20;
// for progressing the music tones
int toneCounter = 0;
int song_rand[4];
// counts remaining lives. Lives lost during a mispress.
int lives = 3;
int songToPlay = 0;
// calibration data for the touch screen, obtained from documentation
// the minimum/maximum possible readings from the touch point
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// thresholds to determine if there was a touch
#define MINPRESSURE 10
#define MAXPRESSURE 1000
int numsongs = 7; //must be hardcoded, currently 7
song songs[7]; //size of this must also be hardcoded and should be equal to numsongs
//global vars for song listings
int pg = 1; //current page of songs
int maxpages = ceil(numsongs/3); //number of full pages of three songs that can be made from the listed number of songs
int displayedSongs; //number of songs on screen (1-3)
int sortMode = 0; //0: name, 1: length, 2: difficulty, 3: artist
// function declarations
void drawNote(Note n);
void drawNote(int progression, int n);
Note addNote(int num);
void advance(Note &n);
void advanceAllRenderedNotes();
void addNotesFromSong();
int randomNumber(int bits);
void returnMusic(int len);
void restart();
void addLine(int num);
point checkTouch();
int lengthString(String s);
void printString(String s);
void drawLives();
void selectFrequencies();
void setup_screen_notes(){
int screen_counter1 = 0; int screen_counter2 = 0; int screen_counter3 = 0;
int additionalCounter = 0;
for (int i = 0; i < 27; i++){
int a = randomNumber(4);
if (a % 3 == 0 && screen_counter1 != 10){
song_1[screen_counter1] = additionalCounter;
screen_counter1++;
additionalCounter++;
}
else if (a % 3 == 1 && screen_counter2 != 10){
song_2[screen_counter2] = additionalCounter;
screen_counter2++;
additionalCounter++;
}
else if (a % 3 == 2 && screen_counter3 != 10){
song_3[screen_counter3] = additionalCounter;
screen_counter3++;
additionalCounter++;
}
}
for (int i = 0; i < 10; i++){
Serial.print("yeah: ");
Serial.println(song_1[i]);
}
}
// initializes program
void setup(){
init();
Serial.begin(9600);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
// setting up structs for songs. To modify / add / remove songs change these values. Note that you must update the above numsongs and size of songs[]
String title[] = {"Symphony in C Minor", "Songé in C Major", "Piece in D Minor", "Disonance in no particular key", "Chinese", "Grater", "Spouse"};
String game[] = {"Cànadià.", "Seattle", "Mexico", "Calgary", "Somewhere in China", "Sychelles", "NYC"};
int length[] = {70, 33, 64, 65, 79, 31, 81}; //in seconds
int difficulty[] = {2, 1, 1, 2, 1, 3, 3}; //no current effect on game play
String artist[] = {"Amir", "Random Number generator", "voltage fluctuations", "code", "some header file", "integers", "Mozart"};
//Names of songs, games and artists are not our property and belong to their original owners. They are being used here for demonstation and educational purposes and their copyrighted music is not implemented
for (int i = 0; i < numsongs; i++){
songs[i] = {title[i], game[i], length[i], difficulty[i], artist[i], 0}; //builds structs
}
selectFrequencies();
if(numsongs%3){maxpages++;} //adds an extra page in case number of songs is not a multiple of three
Serial.println("");
Serial.println("~presort~");
for(int i = 0; i < numsongs; i++){
Serial.println(songs[i].name);
Serial.print(" L:");
Serial.print(songs[i].length);
Serial.print(" DIFF:");
Serial.print(songs[i].difficulty);
Serial.print(" GAME:");
Serial.println(songs[i].game);
}
splashScreen();
tft.setTextColor(ILI9341_WHITE);
// setting up screen
//tft.fillScreen(ILI9341_BLACK);
// setting up point counter at top left
tft.setCursor(0,0);
tft.setTextSize(2);
//tft.print(points);
// setting up buttons (NOT USED AS OF YET)
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
// setting up piezo speaker
pinMode(SPEAKER_PIN, OUTPUT);
// generates random music for the game
returnMusic(length_music);
/*
Serial.print("pointer: ");
int test = (int)&music;
Serial.println(test);*/
/*
Serial.println("these are the musical frequencies of the random song");
for (int i = 0; i < length_music; i++){
Serial.println(music[i]);
}
*/
/*
tft.fillRect(0,0, ScreenHeight,ScreenWidth, ILI9341_BLUE);
tft.fillRect(4,4, ScreenHeight-8,ScreenWidth-8, ILI9341_BLACK);
*/
// checks if num is -1, that means it shouldn't be rendered.
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
screen_notes1[i].progression = 0;
screen_notes1[i].num = -1;
screen_notes2[i].progression = 0;
screen_notes2[i].num = -1;
screen_notes3[i].progression = 0;
screen_notes3[i].num = -1;
}
}
point checkTouch(){
TSPoint touch = ts.getPoint();
if ((touch.z < MINPRESSURE) || (touch.z > MAXPRESSURE)) {
// no touch, just quit
point val = {-1, -1};
return val;
}
// get the y coordinate of where the display was touched
// remember the x-coordinate of touch is really our y-coordinate
// on the display
int touchX = map(touch.x, TS_MINX, TS_MAXX, 0, 240);
touchX = constrain(touchX, 0, 240);
// need to invert the x-axis, so reverse the
// range of the display coordinates
int touchY = map(touch.y, TS_MINY, TS_MAXY, 0, 320);
touchY = constrain(touchY, 0, 320);
point val = {touchX, touchY};
return val;
}
void printString(String s){
int len = lengthString(s);
if(len > 29){
s[27] = 46;
s[28] = 46;
s[29] = 46;
for(int i = 30; i <= len; i++){ //adds ellipses and hides remaining characters if name is too long to display as is
s[i] = 0;
}
tft.print(s);
}else{
tft.print(s);
}
}
int lengthString(String s){
int count = 0;
while (s[count]){count++;}
// Serial.println(count);
return count;
}
// selects frequencies for the different songs
void selectFrequencies(){
Serial.print("it is: ");
Serial.println(songToPlay);
if (songToPlay % 7 == 0){
for (int i = 0; i < 22; i++){
FrequencyList[i] = C_MIN[i];
}
}
else if (songToPlay % 7 == 1){
for (int i = 0; i < 22; i++){
FrequencyList[i] = C_MAJ[i];
}
}
else if (songToPlay % 7 == 2) {
for (int i = 0; i < 22; i++){
FrequencyList[i] = D_MIN[i];
}
}
else if (songToPlay % 7 == 3) {
for (int i = 0; i < 22; i++){
FrequencyList[i] = Weird[i];
}
}
else if (songToPlay % 7 == 4) {
for (int i = 0; i < 22; i++){
FrequencyList[i] = Chinese[i];
}
}
else if (songToPlay % 7 == 5) {
for (int i = 0; i < 22; i++){
FrequencyList[i] = Grater[i];
}
}
else if (songToPlay % 7 == 6) {
for (int i = 0; i < 22; i++){
FrequencyList[i] = Spouse[i];
}
}
C_MIN_PATTERN_1[0] = FrequencyList[7];
C_MIN_PATTERN_1[1] = FrequencyList[11];
C_MIN_PATTERN_1[2] = FrequencyList[9];
C_MIN_PATTERN_1[3] = FrequencyList[11];
C_MIN_PATTERN_2[0] = FrequencyList[7];
C_MIN_PATTERN_2[1] = FrequencyList[12];
C_MIN_PATTERN_2[2] = FrequencyList[9];
C_MIN_PATTERN_2[3] = FrequencyList[12];
C_MIN_PATTERN_3[0] = FrequencyList[7];
C_MIN_PATTERN_3[1] = FrequencyList[8];
C_MIN_PATTERN_3[2] = FrequencyList[9];
C_MIN_PATTERN_3[3] = FrequencyList[11];
C_MIN_PATTERN_4[0] = FrequencyList[11];
C_MIN_PATTERN_4[1] = FrequencyList[8];
C_MIN_PATTERN_4[2] = FrequencyList[10];
C_MIN_PATTERN_4[3] = FrequencyList[9];
C_MIN_PATTERN_5[0] = FrequencyList[0];
C_MIN_PATTERN_5[1] = FrequencyList[1];
C_MIN_PATTERN_5[2] = FrequencyList[2];
C_MIN_PATTERN_5[3] = FrequencyList[3];
C_MIN_PATTERN_6[0] = FrequencyList[4];
C_MIN_PATTERN_6[1] = FrequencyList[3];
C_MIN_PATTERN_6[2] = FrequencyList[2];
C_MIN_PATTERN_6[3] = FrequencyList[1];
C_MIN_PATTERN_7[0] = FrequencyList[6];
C_MIN_PATTERN_7[1] = FrequencyList[3];
C_MIN_PATTERN_7[2] = FrequencyList[6];
C_MIN_PATTERN_7[3] = FrequencyList[3];
C_MIN_PATTERN_8[0] = FrequencyList[3];
C_MIN_PATTERN_8[1] = FrequencyList[4];
C_MIN_PATTERN_8[2] = FrequencyList[5];
C_MIN_PATTERN_8[3] = FrequencyList[6];
C_MIN_PATTERN_9[0] = FrequencyList[14];
C_MIN_PATTERN_9[1] = FrequencyList[15];
C_MIN_PATTERN_9[2] = FrequencyList[16];
C_MIN_PATTERN_9[3] = FrequencyList[17];
C_MIN_PATTERN_10[0] = FrequencyList[14];
C_MIN_PATTERN_10[1] = FrequencyList[14];
C_MIN_PATTERN_10[2] = FrequencyList[12];
C_MIN_PATTERN_10[3] = FrequencyList[11];
C_MIN_PATTERN_11[0] = FrequencyList[16];
C_MIN_PATTERN_11[1] = FrequencyList[17];
C_MIN_PATTERN_11[2] = FrequencyList[18];
C_MIN_PATTERN_11[3] = FrequencyList[19];
C_MIN_PATTERN_12[0] = FrequencyList[18];
C_MIN_PATTERN_12[1] = FrequencyList[17];
C_MIN_PATTERN_12[2] = FrequencyList[18];
C_MIN_PATTERN_12[3] = FrequencyList[16];
C_MIN_PATTERN_13[0] = FrequencyList[18];
C_MIN_PATTERN_13[1] = FrequencyList[18];
C_MIN_PATTERN_13[2] = FrequencyList[16];
C_MIN_PATTERN_13[3] = FrequencyList[18];
C_MIN_PATTERN_14[0] = FrequencyList[17];
C_MIN_PATTERN_14[1] = FrequencyList[17];
C_MIN_PATTERN_14[2] = FrequencyList[15];
C_MIN_PATTERN_14[3] = FrequencyList[17];
C_MIN_PATTERN_15[0] = FrequencyList[16];
C_MIN_PATTERN_15[1] = FrequencyList[16];
C_MIN_PATTERN_15[2] = FrequencyList[19];
C_MIN_PATTERN_15[3] = FrequencyList[16];
C_MIN_PATTERN_16[0] = FrequencyList[14];
C_MIN_PATTERN_16[1] = FrequencyList[14];
C_MIN_PATTERN_16[2] = FrequencyList[18];
C_MIN_PATTERN_16[3] = FrequencyList[14];
}
// restarting from death screen, moving to play state
void restart(){
selectFrequencies();
setup_screen_notes();
Serial.print(C_MIN_PATTERN_9[0]);
tft.fillScreen(ILI9341_BLACK);
addLine(3);
tft.setTextSize(2);
// restarting all the numbers and stuff
// that make the game run
points = 0;
counter1 = 0;
counter2 = 0;
song_counter_1 = 0;
song_counter_2 = 0;
song_counter_3 = 0;
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
screen_notes1[i].num = -1;
screen_notes2[i].num = -1;
screen_notes3[i].num = -1;
}
if (hardMode){
lives = 1;
} else {
lives = 5;
}
// goes back to normal state
drawLives();
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.print(points);
while(true){
loop();
}
}
// uses piezo speaker to play a note
void playNote(){
// 0.6 seconds for normal notes
tone(SPEAKER_PIN, music[toneCounter], 600);
// for debugging purposes
Serial.println(music[toneCounter]);
// so next tone in the music generator is played
toneCounter++;
// resets the music, and gets some more tunes.
// doing this saves space, and reduces amount of calls to the random generator
// making the game smoother in general
if (toneCounter >= length_music){
// resets
toneCounter = 0;
// makes some more music!
returnMusic(length_music);
}
}
// Draws the circle at the bottom which are kind of instruments
void drawInstruments(){
tft.drawCircle((ScreenHeight/4)*1, 300, 5, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*1, 300, 6, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*1, 300, 7, ILI9341_YELLOW);
tft.drawCircle((ScreenHeight/4)*2, 300, 5, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*2, 300, 6, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*2, 300, 7, ILI9341_YELLOW);
tft.drawCircle((ScreenHeight/4)*3, 300, 5, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*3, 300, 6, ILI9341_RED);
tft.drawCircle((ScreenHeight/4)*3, 300, 7, ILI9341_YELLOW);
}
void drawLives(){
tft.fillRect(190, 4, 50, 8, ILI9341_BLACK);//refreshes life counter visuals
tft.setCursor(190, 5);
tft.setTextSize(1);
if(lives > 3){
tft.setTextColor(ILI9341_GREEN);
}
if(lives == 2){
tft.setTextColor(ILI9341_YELLOW);
}
if(lives == 1){
tft.setTextColor(ILI9341_RED);
}
tft.print("LIVES: ");
tft.print(lives);
}
// to proccess touches on the screen
void processTouch(){
// gets point
TSPoint touch = ts.getPoint();
// if they barely press it or if the wind blows on it or something
if (touch.z < MINPRESSURE || touch.z > MAXPRESSURE) {
// no touch, just quit
return;
}
// get the y coordinate of where the display was touched
// remember the x-coordinate of touch is really our y-coordinate
// on the display
int touchY = map(touch.x, TS_MINX, TS_MAXX, 0, TFT_HEIGHT - 1);
// need to invert the x-axis, so reverse the
// range of the display coordinates
int touchX = map(touch.y, TS_MINY, TS_MAXY, TFT_WIDTH - 1, 0);
// if they touch the first intstument
if (touchX < 100 && touchY < ScreenWidth/4){
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
if (screen_notes1[i].progression > 275 && screen_notes1[i].num != -1 && screen_notes1[i].progression < 310){
if (cooldown1){
// increase points
points++;
// make this instrument cool down a bit
cooldown1 = false;
//update score stuff
tft.fillRect(0,0, 20,20, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.print(points);
screen_notes1[i].num += 3;
// call tone player
playNote();
// dont need to go through other notes
break;
}
}
}
// this is if they mess up
if (cooldown1) {
if (hardMode){
// reduce points if in hard mode
points--;
}
// update score on display
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.print(points);
// starts up cooldown
cooldown1 = false;
// reduce lives left
lives--;
tone(SPEAKER_PIN, 988 , 100);
drawLives();
}
}
// if they touch the third instrument
else if (touchX < 100 && touchY > (ScreenWidth * 2)/4){
// go through all notes being rendered on the screen
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
// if the note is in the okay region
if (screen_notes3[i].progression > 275 && screen_notes3[i].num != -1 && screen_notes3[i].progression < 310){
// dont act if its cooling down so one press doesnt count as a bunch
if (cooldown3){
// increase points
points++;
// setup cooldown
cooldown3 = false;
screen_notes3[i].num += 3;
// print updated points to top left
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.print(points);
// play the sound
playNote();
// dont need to go through other notes
break;
}
}
}
// if the mess up
if (cooldown3) {
// subtract points if player is in hard mode
if (hardMode){
points--;
}
// update top left point counter
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.print(points);
// setup cooldown
cooldown3 = false;
// reduce lives
lives--;
tone(SPEAKER_PIN, 988 , 100);
drawLives();
}
}
// this logic is the same as above except for the 2nd (middle instrument)
else if (touchX < 100){
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
if (screen_notes2[i].progression > 275 && screen_notes2[i].num != -1 && screen_notes2[i].progression < 310){
if (cooldown2){
points++;
cooldown2 = false;
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.print(points);
screen_notes2[i].num += 3;
playNote();
break;
}
}
}
if (cooldown2) {
if (hardMode){
points--;
}
tft.fillRect(0,0, 11,11, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0,0);
tft.print(points);
cooldown2 = false;
lives--;
tone(SPEAKER_PIN, 988 , 100);
drawLives();
}
}
// this goes to the death screen state if player loses all their lvies
if (lives < 1){
onDeath();
}
}
// this gets reduced over time to increase difficulty marginally
// minimum value of 2
int delayVal = 8;
// game loop
void loop(){
processTouch();
// fast algorithm to move notes forward without wasting
// time redrawing pixels the same colour
advanceAllRenderedNotes();
// delay of 17 gives us approximately 60 frames per second.
delay(delayVal);
// counter1 counts frames
counter1++;
// one logic tic a second (or every 60 frames if delay isnt 17)
if (counter1 == 60){
// checks song arrays to see if a note should be added
addNotesFromSong();
// counter 2 counts logic frames
counter2++;
// RESETS SONG;
if (infinteMode){
if (counter2 > 20){
// resets counter so song replays every 20 logic frames
counter2 = 0;
// caps speed at delay of 2.
if (delayVal > 2){
delayVal--;
}
}
}
// resets to count until next logic frame
counter1 = 0;
}
// redraw instruments every 0.25 seconds or 15 frames
if (counter1 == 15){
drawInstruments();
}
// resets cooldowns every 20 logic frames. This is so game still goes fairly fast
// and gives enough time for player to react to multiple notes in a row
if (counter1 == 20){
cooldown1 = true;
cooldown2 = true;
cooldown3 = true;
}
/*
if (digitalRead(BUTTON_1) == HIGH){
Serial.println("button1");
}
if (digitalRead(BUTTON_2) == HIGH){
Serial.println("button2");
}
if (digitalRead(BUTTON_2) == HIGH){
Serial.println("button3");
}
*/
}
// this goes through the song and adds notes to the screen from it.
void addNotesFromSong(){
if (counter2 == 0){
song_counter_1 = 0;
song_counter_2 = 0;
song_counter_3 = 0;
}
// following 3 if statements check if the note should be added from each
// song array
if (counter2 == song_1[song_counter_1]){
// add note if it is in the array
screen_notes1[song_counter_1] = addNote(1);
// increase counter
song_counter_1++;
}
// same logic as above for 2nd instrument
if (counter2 == song_2[song_counter_2]){
screen_notes2[song_counter_2] = addNote(2);
song_counter_2++;
}
// same logic as above for 3rd instrument
if (counter2 == song_3[song_counter_3]){
screen_notes3[song_counter_3] = addNote(3);
song_counter_3++;
}
}
// Advances all notes currently on the board.
void advanceAllRenderedNotes(){
for (int i = 0; i < MAX_RENDERED_NOTES; i++){
// moves forward all currently rendered notes.
// unrendered notes have an insturment (num) value of -1
if (screen_notes1[i].num > 0){
advance(screen_notes1[i]);
}
if (screen_notes2[i].num > 0){
advance(screen_notes2[i]);
}
if (screen_notes2[i].num > 0){
advance(screen_notes3[i]);
}
/*
if (screen_notes1[i].progression > 340 && screen_notes1[i].progression < 350){
drawInstruments();
}
else if (screen_notes2[i].progression > 340 && screen_notes2[i].progression < 350){
drawInstruments();
}
else if (screen_notes3[i].progression > 340 && screen_notes3[i].progression < 350){
drawInstruments();
}
*/
}
}
// adds note to the board
Note addNote(int num){
// creates not structure and initializes it to given values
Note n;
n.progression = 0;
n.num = num;
return n;
}
// Adds vertical lines for the paths of the notes.
void addLine(int num) {
lines = num;
// n lines gives n+1 equal segments in between lines
num++;
//creates intervals
int interval = ScreenHeight / num;
// logic for adding to nice intervals
for (int i = 0; i < lines; i++){
tft.drawRect((interval + i*interval - 1),0, 4,ScreenWidth, ILI9341_BLUE);
}
}
// draws the note on the screen.
// uses basic OOP principles using structs.
void drawNote(int progression, int num){
// draws note at given progression
Note n;
n.progression = progression;
n.num = num;
drawNote(n);
}
// progression is from 0 to 320
// num is from 1 to @param lines
void drawNote(Note n){
// gets values from note passed to this function
int num = n.num;
int progression = n.progression;