Snake and Physics based jumping game with high scores and different difficulties.
Dependencies: N5110 SDFileSystem mbed
sounds.h@5:ae641b1d04fa, 2016-05-04 (annotated)
- Committer:
- el14jw
- Date:
- Wed May 04 15:01:20 2016 +0000
- Revision:
- 5:ae641b1d04fa
- Parent:
- 4:c2d920b17b14
Snake and Physics based jumping game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14jw | 0:23a749719479 | 1 | /** |
el14jw | 0:23a749719479 | 2 | @file sounds.h |
el14jw | 0:23a749719479 | 3 | |
el14jw | 0:23a749719479 | 4 | @brief Header file for containing defines of note frequencies and sound arrays |
el14jw | 5:ae641b1d04fa | 5 | @brief Revision 1.1 |
el14jw | 0:23a749719479 | 6 | @author Joel W. Webb |
el14jw | 0:23a749719479 | 7 | @date March 2016 |
el14jw | 0:23a749719479 | 8 | */ |
el14jw | 0:23a749719479 | 9 | |
el14jw | 0:23a749719479 | 10 | /// Note definitions from Arduino.cc |
el14jw | 0:23a749719479 | 11 | #define NOTE_B0 31 |
el14jw | 0:23a749719479 | 12 | #define NOTE_C1 33 |
el14jw | 0:23a749719479 | 13 | #define NOTE_CS1 35 |
el14jw | 0:23a749719479 | 14 | #define NOTE_D1 37 |
el14jw | 0:23a749719479 | 15 | #define NOTE_DS1 39 |
el14jw | 0:23a749719479 | 16 | #define NOTE_E1 41 |
el14jw | 0:23a749719479 | 17 | #define NOTE_F1 44 |
el14jw | 0:23a749719479 | 18 | #define NOTE_FS1 46 |
el14jw | 0:23a749719479 | 19 | #define NOTE_G1 49 |
el14jw | 0:23a749719479 | 20 | #define NOTE_GS1 52 |
el14jw | 0:23a749719479 | 21 | #define NOTE_A1 55 |
el14jw | 0:23a749719479 | 22 | #define NOTE_AS1 58 |
el14jw | 0:23a749719479 | 23 | #define NOTE_B1 62 |
el14jw | 0:23a749719479 | 24 | #define NOTE_C2 65 |
el14jw | 0:23a749719479 | 25 | #define NOTE_CS2 69 |
el14jw | 0:23a749719479 | 26 | #define NOTE_D2 73 |
el14jw | 0:23a749719479 | 27 | #define NOTE_DS2 78 |
el14jw | 0:23a749719479 | 28 | #define NOTE_E2 82 |
el14jw | 0:23a749719479 | 29 | #define NOTE_F2 87 |
el14jw | 0:23a749719479 | 30 | #define NOTE_FS2 93 |
el14jw | 0:23a749719479 | 31 | #define NOTE_G2 98 |
el14jw | 0:23a749719479 | 32 | #define NOTE_GS2 104 |
el14jw | 0:23a749719479 | 33 | #define NOTE_A2 110 |
el14jw | 0:23a749719479 | 34 | #define NOTE_AS2 117 |
el14jw | 0:23a749719479 | 35 | #define NOTE_B2 123 |
el14jw | 0:23a749719479 | 36 | #define NOTE_C3 131 |
el14jw | 0:23a749719479 | 37 | #define NOTE_CS3 139 |
el14jw | 0:23a749719479 | 38 | #define NOTE_D3 147 |
el14jw | 0:23a749719479 | 39 | #define NOTE_DS3 156 |
el14jw | 0:23a749719479 | 40 | #define NOTE_E3 165 |
el14jw | 0:23a749719479 | 41 | #define NOTE_F3 175 |
el14jw | 0:23a749719479 | 42 | #define NOTE_FS3 185 |
el14jw | 0:23a749719479 | 43 | #define NOTE_G3 196 |
el14jw | 0:23a749719479 | 44 | #define NOTE_GS3 208 |
el14jw | 0:23a749719479 | 45 | #define NOTE_A3 220 |
el14jw | 0:23a749719479 | 46 | #define NOTE_AS3 233 |
el14jw | 0:23a749719479 | 47 | #define NOTE_B3 247 |
el14jw | 0:23a749719479 | 48 | #define NOTE_C4 262 |
el14jw | 0:23a749719479 | 49 | #define NOTE_CS4 277 |
el14jw | 0:23a749719479 | 50 | #define NOTE_D4 294 |
el14jw | 0:23a749719479 | 51 | #define NOTE_DS4 311 |
el14jw | 0:23a749719479 | 52 | #define NOTE_E4 330 |
el14jw | 0:23a749719479 | 53 | #define NOTE_F4 349 |
el14jw | 0:23a749719479 | 54 | #define NOTE_FS4 370 |
el14jw | 0:23a749719479 | 55 | #define NOTE_G4 392 |
el14jw | 0:23a749719479 | 56 | #define NOTE_GS4 415 |
el14jw | 0:23a749719479 | 57 | #define NOTE_A4 440 |
el14jw | 0:23a749719479 | 58 | #define NOTE_AS4 466 |
el14jw | 0:23a749719479 | 59 | #define NOTE_B4 494 |
el14jw | 0:23a749719479 | 60 | #define NOTE_C5 523 |
el14jw | 0:23a749719479 | 61 | #define NOTE_CS5 554 |
el14jw | 0:23a749719479 | 62 | #define NOTE_D5 587 |
el14jw | 0:23a749719479 | 63 | #define NOTE_DS5 622 |
el14jw | 0:23a749719479 | 64 | #define NOTE_E5 659 |
el14jw | 0:23a749719479 | 65 | #define NOTE_F5 698 |
el14jw | 0:23a749719479 | 66 | #define NOTE_FS5 740 |
el14jw | 0:23a749719479 | 67 | #define NOTE_G5 784 |
el14jw | 0:23a749719479 | 68 | #define NOTE_GS5 831 |
el14jw | 0:23a749719479 | 69 | #define NOTE_A5 880 |
el14jw | 0:23a749719479 | 70 | #define NOTE_AS5 932 |
el14jw | 0:23a749719479 | 71 | #define NOTE_B5 988 |
el14jw | 0:23a749719479 | 72 | #define NOTE_C6 1047 |
el14jw | 0:23a749719479 | 73 | #define NOTE_CS6 1109 |
el14jw | 0:23a749719479 | 74 | #define NOTE_D6 1175 |
el14jw | 0:23a749719479 | 75 | #define NOTE_DS6 1245 |
el14jw | 0:23a749719479 | 76 | #define NOTE_E6 1319 |
el14jw | 0:23a749719479 | 77 | #define NOTE_F6 1397 |
el14jw | 0:23a749719479 | 78 | #define NOTE_FS6 1480 |
el14jw | 0:23a749719479 | 79 | #define NOTE_G6 1568 |
el14jw | 0:23a749719479 | 80 | #define NOTE_GS6 1661 |
el14jw | 0:23a749719479 | 81 | #define NOTE_A6 1760 |
el14jw | 0:23a749719479 | 82 | #define NOTE_AS6 1865 |
el14jw | 0:23a749719479 | 83 | #define NOTE_B6 1976 |
el14jw | 0:23a749719479 | 84 | #define NOTE_C7 2093 |
el14jw | 0:23a749719479 | 85 | #define NOTE_CS7 2217 |
el14jw | 0:23a749719479 | 86 | #define NOTE_D7 2349 |
el14jw | 0:23a749719479 | 87 | #define NOTE_DS7 2489 |
el14jw | 0:23a749719479 | 88 | #define NOTE_E7 2637 |
el14jw | 0:23a749719479 | 89 | #define NOTE_F7 2794 |
el14jw | 0:23a749719479 | 90 | #define NOTE_FS7 2960 |
el14jw | 0:23a749719479 | 91 | #define NOTE_G7 3136 |
el14jw | 0:23a749719479 | 92 | #define NOTE_GS7 3322 |
el14jw | 0:23a749719479 | 93 | #define NOTE_A7 3520 |
el14jw | 0:23a749719479 | 94 | #define NOTE_AS7 3729 |
el14jw | 0:23a749719479 | 95 | #define NOTE_B7 3951 |
el14jw | 0:23a749719479 | 96 | #define NOTE_C8 4186 |
el14jw | 0:23a749719479 | 97 | #define NOTE_CS8 4435 |
el14jw | 0:23a749719479 | 98 | #define NOTE_D8 4699 |
el14jw | 0:23a749719479 | 99 | #define NOTE_DS8 4978 |
el14jw | 0:23a749719479 | 100 | |
el14jw | 0:23a749719479 | 101 | |
el14jw | 0:23a749719479 | 102 | /** |
el14jw | 0:23a749719479 | 103 | @note Tunes are defined as float arrays with a structure {Note, Time of Note, Note2, Time of Note 2... etc, NULL} |
el14jw | 0:23a749719479 | 104 | @note Must end in a NULL or we start to read from memory outside the note array |
el14jw | 0:23a749719479 | 105 | */ |
el14jw | 0:23a749719479 | 106 | const float tune_intro[] = {NOTE_E4,0.2,NOTE_GS4,0.2,NOTE_B4,0.2,NOTE_E5,0.2,NOTE_B4,0.2,NOTE_GS4,0.2,NOTE_E4,0.3,1,0.1,NOTE_GS4,0.2,NOTE_E4,0.3,NULL}; |
el14jw | 0:23a749719479 | 107 | const float tune_snakeintro[] = {NOTE_C4,0.2,NOTE_CS4,0.2,NOTE_D4,0.2,NOTE_DS4,0.2,NOTE_E4,0.2,NOTE_B3,0.2,NOTE_E4,0.4,NOTE_C4,0.2,NOTE_CS4,0.2,NOTE_D4,0.2,NOTE_DS4,0.2,NOTE_E4,0.2,NOTE_B3,0.2,NOTE_E4,0.4,NULL}; |
el14jw | 1:c4928de1f922 | 108 | const float tune_plinkintro[] = {NOTE_C5,0.05,NOTE_D5,0.05,NOTE_E5,0.05,NOTE_F5,0.05,NOTE_G5,0.1,NULL}; |
el14jw | 0:23a749719479 | 109 | const float tune_snakeEat[] = {NOTE_D5,0.1,NOTE_FS5,0.1,NULL}; |
el14jw | 2:80a91a737e17 | 110 | const float tune_plinkjump_small[] = {NOTE_C6,0.05,1,0.05,NOTE_C6,0.05,NULL}; |
el14jw | 2:80a91a737e17 | 111 | const float tune_plinkjump_normal[] = {NOTE_C3,0.05,1,0.05,NOTE_C3,0.05,NULL}; |
el14jw | 2:80a91a737e17 | 112 | const float tune_plinkjump_big[] = {NOTE_C1,0.05,1,0.05,NOTE_C1,0.05,NULL}; |
el14jw | 0:23a749719479 | 113 | const float tune_gameOver[] = {NOTE_E5,0.2,NOTE_DS5,0.2,NOTE_D5,0.2,NOTE_CS5,0.4,NULL}; |
el14jw | 0:23a749719479 | 114 | |
el14jw | 0:23a749719479 | 115 | const float tune_test1[] = {NOTE_C4,0.5,NOTE_D4,0.5,NOTE_E4,0.5,NOTE_F4,0.5,NOTE_G4,0.5,NOTE_A4,0.5,NOTE_B4,0.5,NOTE_C5,0.5,NULL}; |
el14jw | 0:23a749719479 | 116 | const float tune_test2[] = {NOTE_C5,0.25,NOTE_B4,0.25,NOTE_A4,0.25,NOTE_G4,0.25,NOTE_F4,0.25,NOTE_E4,0.25,NOTE_D4,0.25,NULL}; |
el14jw | 0:23a749719479 | 117 | |
el14jw | 0:23a749719479 | 118 | |
el14jw | 0:23a749719479 | 119 | |
el14jw | 4:c2d920b17b14 | 120 | /// The Timout object used originally has been re-designed as a Ticker because the poll-joystick interrupt was interferring with the Timout and breaking it |
el14jw | 0:23a749719479 | 121 | Ticker note; |
el14jw | 0:23a749719479 | 122 | |
el14jw | 0:23a749719479 | 123 | /// playSound is the function to make calling the sound_isr easier form within our code |
el14jw | 0:23a749719479 | 124 | void playSound(const float* sound) /// const float* sound is a pointer to the first element of the tune array you wish to play |
el14jw | 0:23a749719479 | 125 | { |
el14jw | 0:23a749719479 | 126 | noteArray = sound; /// It is stored in the global const float pointer noteArray |
el14jw | 0:23a749719479 | 127 | note.attach(&sound_isr,0.005); |
el14jw | 0:23a749719479 | 128 | } |
el14jw | 0:23a749719479 | 129 | |
el14jw | 0:23a749719479 | 130 | /// The sound_isr is a Timeout interrupt service routine that calls itself with varying time delays |
el14jw | 0:23a749719479 | 131 | void sound_isr() |
el14jw | 0:23a749719479 | 132 | { |
el14jw | 0:23a749719479 | 133 | /// If the float located at *noteArray is a NULL then we have reached the end of the tune |
el14jw | 0:23a749719479 | 134 | if (*noteArray != NULL) { |
el14jw | 0:23a749719479 | 135 | |
el14jw | 0:23a749719479 | 136 | if (*noteArray == 1) { /// If the frequency is 0 then we have a pause |
el14jw | 0:23a749719479 | 137 | buzzer = 0.0; |
el14jw | 0:23a749719479 | 138 | } else { /// PWM signal with frequency equal to *noteArray |
el14jw | 0:23a749719479 | 139 | buzzer = 0.5; |
el14jw | 0:23a749719479 | 140 | buzzer.period((float) 1.0/(*noteArray)); |
el14jw | 0:23a749719479 | 141 | } |
el14jw | 1:c4928de1f922 | 142 | if ((*(noteArray+1)>5.0F) || (*(noteArray+1) == 0)){ |
el14jw | 0:23a749719479 | 143 | error(); |
el14jw | 0:23a749719479 | 144 | } |
el14jw | 0:23a749719479 | 145 | note.detach(); |
el14jw | 0:23a749719479 | 146 | note.attach(&sound_isr,*(noteArray+1)); /// Recall sound_isr with the time delay located one array index after the note frequency *(noteArray+1) |
el14jw | 0:23a749719479 | 147 | noteArray += 2; /// Increment the noteArray pointer to point at the next note in the array to be played (or NULL); |
el14jw | 0:23a749719479 | 148 | |
el14jw | 0:23a749719479 | 149 | } else { |
el14jw | 0:23a749719479 | 150 | noteArray = NULL; /// If we reach the end of the tune array then we clear to noteArray pointer of any memory value to avoid conflicts |
el14jw | 0:23a749719479 | 151 | buzzer = 0; /// and set the duty cycle to 0 |
el14jw | 0:23a749719479 | 152 | note.detach(); |
el14jw | 0:23a749719479 | 153 | } |
el14jw | 0:23a749719479 | 154 | } |