Integrated Music Player (Basic Drums & Piano)
Dependencies: 4DGL-uLCD-SE mbed
Fork of MPR121_Demo by
main.cpp@1:2af7b5d46614, 2014-03-23 (annotated)
- Committer:
- ajayke
- Date:
- Sun Mar 23 07:18:02 2014 +0000
- Revision:
- 1:2af7b5d46614
- Parent:
- 0:e09703934ff4
Integrated Music Player
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:e09703934ff4 | 1 | /* |
4180_1 | 0:e09703934ff4 | 2 | Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) |
4180_1 | 0:e09703934ff4 | 3 | |
4180_1 | 0:e09703934ff4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
4180_1 | 0:e09703934ff4 | 5 | of this software and associated documentation files (the "Software"), to deal |
4180_1 | 0:e09703934ff4 | 6 | in the Software without restriction, including without limitation the rights |
4180_1 | 0:e09703934ff4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
4180_1 | 0:e09703934ff4 | 8 | copies of the Software, and to permit persons to whom the Software is |
4180_1 | 0:e09703934ff4 | 9 | furnished to do so, subject to the following conditions: |
4180_1 | 0:e09703934ff4 | 10 | |
4180_1 | 0:e09703934ff4 | 11 | The above copyright notice and this permission notice shall be included in |
4180_1 | 0:e09703934ff4 | 12 | all copies or substantial portions of the Software. |
4180_1 | 0:e09703934ff4 | 13 | |
4180_1 | 0:e09703934ff4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4180_1 | 0:e09703934ff4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
4180_1 | 0:e09703934ff4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
4180_1 | 0:e09703934ff4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
4180_1 | 0:e09703934ff4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
4180_1 | 0:e09703934ff4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
4180_1 | 0:e09703934ff4 | 20 | THE SOFTWARE. |
4180_1 | 0:e09703934ff4 | 21 | */ |
4180_1 | 0:e09703934ff4 | 22 | |
4180_1 | 0:e09703934ff4 | 23 | #include <mbed.h> |
4180_1 | 0:e09703934ff4 | 24 | #include <string> |
4180_1 | 0:e09703934ff4 | 25 | #include <list> |
ajayke | 1:2af7b5d46614 | 26 | #include "uLCD_4DGL.h" |
4180_1 | 0:e09703934ff4 | 27 | #include <mpr121.h> |
4180_1 | 0:e09703934ff4 | 28 | |
4180_1 | 0:e09703934ff4 | 29 | InterruptIn interrupt(p26); |
ajayke | 1:2af7b5d46614 | 30 | InterruptIn interrupt1(p25); |
ajayke | 1:2af7b5d46614 | 31 | uLCD_4DGL uLCD(p13,p14,p11); |
ajayke | 1:2af7b5d46614 | 32 | I2C i2c(p9, p10); |
ajayke | 1:2af7b5d46614 | 33 | I2C i2c1(p28, p27); |
ajayke | 1:2af7b5d46614 | 34 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
ajayke | 1:2af7b5d46614 | 35 | Mpr121 mpr121_1(&i2c1, Mpr121::ADD_VSS); |
ajayke | 1:2af7b5d46614 | 36 | PwmOut speaker(p21); |
ajayke | 1:2af7b5d46614 | 37 | PwmOut speaker1(p22); |
4180_1 | 0:e09703934ff4 | 38 | Serial pc(USBTX, USBRX); |
4180_1 | 0:e09703934ff4 | 39 | |
ajayke | 1:2af7b5d46614 | 40 | int key_code=0; |
ajayke | 1:2af7b5d46614 | 41 | int key_code1=0; |
ajayke | 1:2af7b5d46614 | 42 | float beats = 0; |
ajayke | 1:2af7b5d46614 | 43 | float beats_var = 0; |
ajayke | 1:2af7b5d46614 | 44 | void pianoInterrupt() { |
ajayke | 1:2af7b5d46614 | 45 | |
4180_1 | 0:e09703934ff4 | 46 | int i=0; |
4180_1 | 0:e09703934ff4 | 47 | int value=mpr121.read(0x00); |
4180_1 | 0:e09703934ff4 | 48 | value +=mpr121.read(0x01)<<8; |
ajayke | 1:2af7b5d46614 | 49 | |
4180_1 | 0:e09703934ff4 | 50 | i=0; |
ajayke | 1:2af7b5d46614 | 51 | |
4180_1 | 0:e09703934ff4 | 52 | for (i=0; i<12; i++) { |
4180_1 | 0:e09703934ff4 | 53 | if (((value>>i)&0x01)==1) key_code=i+1; |
4180_1 | 0:e09703934ff4 | 54 | } |
4180_1 | 0:e09703934ff4 | 55 | } |
4180_1 | 0:e09703934ff4 | 56 | |
ajayke | 1:2af7b5d46614 | 57 | void drumsInterrupt() { |
ajayke | 1:2af7b5d46614 | 58 | |
ajayke | 1:2af7b5d46614 | 59 | int i=0; |
ajayke | 1:2af7b5d46614 | 60 | int value=mpr121_1.read(0x00); |
ajayke | 1:2af7b5d46614 | 61 | value +=mpr121_1.read(0x01)<<8; |
ajayke | 1:2af7b5d46614 | 62 | i=0; |
ajayke | 1:2af7b5d46614 | 63 | |
ajayke | 1:2af7b5d46614 | 64 | |
ajayke | 1:2af7b5d46614 | 65 | for (i=0; i<12; i++) { |
ajayke | 1:2af7b5d46614 | 66 | if (((value>>i)&0x01)==1) key_code1=i+1; |
ajayke | 1:2af7b5d46614 | 67 | } |
ajayke | 1:2af7b5d46614 | 68 | |
ajayke | 1:2af7b5d46614 | 69 | } |
4180_1 | 0:e09703934ff4 | 70 | |
ajayke | 1:2af7b5d46614 | 71 | void piano() { |
ajayke | 1:2af7b5d46614 | 72 | speaker.period(1.0/ (float (key_code*75.1) ) ); |
ajayke | 1:2af7b5d46614 | 73 | speaker = float(key_code*2)/50.0; |
ajayke | 1:2af7b5d46614 | 74 | wait(.5); |
ajayke | 1:2af7b5d46614 | 75 | } |
4180_1 | 0:e09703934ff4 | 76 | |
ajayke | 1:2af7b5d46614 | 77 | void drums() { |
4180_1 | 0:e09703934ff4 | 78 | |
ajayke | 1:2af7b5d46614 | 79 | /*speaker1.period(1.0/ (float (key_code1*5.1) ) ); |
ajayke | 1:2af7b5d46614 | 80 | speaker1 = float(key_code1*7)/ 30.0;*/ |
ajayke | 1:2af7b5d46614 | 81 | wait(.5); |
ajayke | 1:2af7b5d46614 | 82 | |
ajayke | 1:2af7b5d46614 | 83 | switch(key_code1) { |
ajayke | 1:2af7b5d46614 | 84 | case 1: |
ajayke | 1:2af7b5d46614 | 85 | beats = 1.0/ ( float (10.1) ); |
ajayke | 1:2af7b5d46614 | 86 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 87 | speaker1.pulsewidth( beats ); |
ajayke | 1:2af7b5d46614 | 88 | speaker1 = 0.009; |
ajayke | 1:2af7b5d46614 | 89 | break; |
ajayke | 1:2af7b5d46614 | 90 | case 2: |
ajayke | 1:2af7b5d46614 | 91 | beats = 1.0/ ( float (15.1) ); |
ajayke | 1:2af7b5d46614 | 92 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 93 | |
ajayke | 1:2af7b5d46614 | 94 | speaker1 = 0.9; |
ajayke | 1:2af7b5d46614 | 95 | break; |
ajayke | 1:2af7b5d46614 | 96 | case 3: |
ajayke | 1:2af7b5d46614 | 97 | beats = 1.0/ ( float (20.1) ); |
ajayke | 1:2af7b5d46614 | 98 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 99 | speaker1 = 0.9; |
ajayke | 1:2af7b5d46614 | 100 | break; |
ajayke | 1:2af7b5d46614 | 101 | case 4: |
ajayke | 1:2af7b5d46614 | 102 | beats = 1.0/ ( float (25.1) ); |
ajayke | 1:2af7b5d46614 | 103 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 104 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 105 | break; |
ajayke | 1:2af7b5d46614 | 106 | case 5: |
ajayke | 1:2af7b5d46614 | 107 | beats = 1.0/ ( float (30.1) ); |
ajayke | 1:2af7b5d46614 | 108 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 109 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 110 | break; |
ajayke | 1:2af7b5d46614 | 111 | case 6: |
ajayke | 1:2af7b5d46614 | 112 | beats = 1.0/ ( float (33.1) ); |
ajayke | 1:2af7b5d46614 | 113 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 114 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 115 | break; |
ajayke | 1:2af7b5d46614 | 116 | case 7: |
ajayke | 1:2af7b5d46614 | 117 | beats = 1.0/ ( float (40.1) ); |
ajayke | 1:2af7b5d46614 | 118 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 119 | |
ajayke | 1:2af7b5d46614 | 120 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 121 | break; |
ajayke | 1:2af7b5d46614 | 122 | case 8: |
ajayke | 1:2af7b5d46614 | 123 | beats = 1.0/ ( float (45.1) ); |
ajayke | 1:2af7b5d46614 | 124 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 125 | speaker1 = 0.9; |
ajayke | 1:2af7b5d46614 | 126 | break; |
ajayke | 1:2af7b5d46614 | 127 | case 9: |
ajayke | 1:2af7b5d46614 | 128 | beats = 1.0/ ( float (8.1) ); |
ajayke | 1:2af7b5d46614 | 129 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 130 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 131 | break; |
ajayke | 1:2af7b5d46614 | 132 | case 10: |
ajayke | 1:2af7b5d46614 | 133 | beats = 1.0/ ( float (5.1) ); |
ajayke | 1:2af7b5d46614 | 134 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 135 | |
ajayke | 1:2af7b5d46614 | 136 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 137 | break; |
ajayke | 1:2af7b5d46614 | 138 | case 11: |
ajayke | 1:2af7b5d46614 | 139 | beats = 1.0/ ( float (30.1) ); |
ajayke | 1:2af7b5d46614 | 140 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 141 | speaker1.pulsewidth( 1.0/ ( float (10.1) ) ); |
ajayke | 1:2af7b5d46614 | 142 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 143 | break; |
ajayke | 1:2af7b5d46614 | 144 | case 12: |
ajayke | 1:2af7b5d46614 | 145 | beats = 1.0/ ( float (10.1) ); |
ajayke | 1:2af7b5d46614 | 146 | speaker1.period( beats ); |
ajayke | 1:2af7b5d46614 | 147 | speaker1.pulsewidth( 0.9 ); |
ajayke | 1:2af7b5d46614 | 148 | speaker1 = float(key_code1*2)/50.0; |
ajayke | 1:2af7b5d46614 | 149 | break; |
ajayke | 1:2af7b5d46614 | 150 | |
ajayke | 1:2af7b5d46614 | 151 | default: |
ajayke | 1:2af7b5d46614 | 152 | break; |
ajayke | 1:2af7b5d46614 | 153 | } |
ajayke | 1:2af7b5d46614 | 154 | |
ajayke | 1:2af7b5d46614 | 155 | } |
ajayke | 1:2af7b5d46614 | 156 | int d; |
ajayke | 1:2af7b5d46614 | 157 | void LCD_Display() { |
ajayke | 1:2af7b5d46614 | 158 | uLCD.cls(); |
ajayke | 1:2af7b5d46614 | 159 | uLCD.locate(0,0); |
ajayke | 1:2af7b5d46614 | 160 | |
ajayke | 1:2af7b5d46614 | 161 | |
ajayke | 1:2af7b5d46614 | 162 | |
ajayke | 1:2af7b5d46614 | 163 | |
ajayke | 1:2af7b5d46614 | 164 | uLCD.color(GREEN); |
ajayke | 1:2af7b5d46614 | 165 | uLCD.text_bold(ON); |
ajayke | 1:2af7b5d46614 | 166 | |
ajayke | 1:2af7b5d46614 | 167 | uLCD.printf("MUSIC "); |
ajayke | 1:2af7b5d46614 | 168 | uLCD.text_bold(ON); |
ajayke | 1:2af7b5d46614 | 169 | uLCD.printf("PLAYER" ); |
ajayke | 1:2af7b5d46614 | 170 | uLCD.printf("\n\n" ); |
ajayke | 1:2af7b5d46614 | 171 | |
ajayke | 1:2af7b5d46614 | 172 | uLCD.text_bold(OFF); |
ajayke | 1:2af7b5d46614 | 173 | uLCD.color(WHITE); |
ajayke | 1:2af7b5d46614 | 174 | uLCD.textbackground_color(BLUE); |
ajayke | 1:2af7b5d46614 | 175 | uLCD.printf("\nPiano\nKey Pressed %D", key_code ); |
ajayke | 1:2af7b5d46614 | 176 | uLCD.color(GREEN); |
ajayke | 1:2af7b5d46614 | 177 | uLCD.textbackground_color(BLACK); |
ajayke | 1:2af7b5d46614 | 178 | uLCD.printf("\n\n" ); |
ajayke | 1:2af7b5d46614 | 179 | uLCD.printf("Timbre: %0.2f", (float) speaker ); |
ajayke | 1:2af7b5d46614 | 180 | uLCD.printf("\nPitch: %.2f", (float (key_code*75.1)) ); |
ajayke | 1:2af7b5d46614 | 181 | uLCD.printf("\n" ); |
ajayke | 1:2af7b5d46614 | 182 | uLCD.color(WHITE); |
ajayke | 1:2af7b5d46614 | 183 | uLCD.textbackground_color(BLUE); |
ajayke | 1:2af7b5d46614 | 184 | uLCD.printf("\nDrums\nKey Pressed %D", key_code1 ); |
ajayke | 1:2af7b5d46614 | 185 | uLCD.color(GREEN); |
ajayke | 1:2af7b5d46614 | 186 | uLCD.textbackground_color(BLACK); |
ajayke | 1:2af7b5d46614 | 187 | uLCD.printf("\n\n" ); |
ajayke | 1:2af7b5d46614 | 188 | uLCD.printf("Loudness: %0.2f", (float) speaker1 ); |
ajayke | 1:2af7b5d46614 | 189 | beats_var = (beats == 0) ? 0 : (1.0 / (float)beats ); |
ajayke | 1:2af7b5d46614 | 190 | d = (int) beats_var; |
ajayke | 1:2af7b5d46614 | 191 | uLCD.printf("\nBeats/Sec: %d", d ); |
ajayke | 1:2af7b5d46614 | 192 | uLCD.printf("\n" ); |
ajayke | 1:2af7b5d46614 | 193 | uLCD.printf("\n\n" ); |
ajayke | 1:2af7b5d46614 | 194 | |
ajayke | 1:2af7b5d46614 | 195 | } |
4180_1 | 0:e09703934ff4 | 196 | |
ajayke | 1:2af7b5d46614 | 197 | int main() { |
ajayke | 1:2af7b5d46614 | 198 | |
ajayke | 1:2af7b5d46614 | 199 | |
ajayke | 1:2af7b5d46614 | 200 | interrupt.fall(&pianoInterrupt); |
ajayke | 1:2af7b5d46614 | 201 | interrupt.mode(PullUp); |
ajayke | 1:2af7b5d46614 | 202 | interrupt1.fall(&drumsInterrupt); |
ajayke | 1:2af7b5d46614 | 203 | interrupt1.mode(PullUp); |
4180_1 | 0:e09703934ff4 | 204 | |
ajayke | 1:2af7b5d46614 | 205 | uLCD.background_color(BLACK); |
ajayke | 1:2af7b5d46614 | 206 | uLCD.cls(); |
ajayke | 1:2af7b5d46614 | 207 | uLCD.color(WHITE); |
ajayke | 1:2af7b5d46614 | 208 | uLCD.textbackground_color(BLUE); |
ajayke | 1:2af7b5d46614 | 209 | uLCD.set_font(FONT_7X8); |
ajayke | 1:2af7b5d46614 | 210 | //uLCD.text_mode(OPAQUE); |
ajayke | 1:2af7b5d46614 | 211 | uLCD.text_mode(TRANSPARENT); |
ajayke | 1:2af7b5d46614 | 212 | while (1) { |
ajayke | 1:2af7b5d46614 | 213 | |
ajayke | 1:2af7b5d46614 | 214 | piano(); |
ajayke | 1:2af7b5d46614 | 215 | drums(); |
ajayke | 1:2af7b5d46614 | 216 | LCD_Display(); |
ajayke | 1:2af7b5d46614 | 217 | } |
4180_1 | 0:e09703934ff4 | 218 | |
4180_1 | 0:e09703934ff4 | 219 | |
4180_1 | 0:e09703934ff4 | 220 | } |
4180_1 | 0:e09703934ff4 | 221 | |
4180_1 | 0:e09703934ff4 | 222 | |
4180_1 | 0:e09703934ff4 | 223 |