Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem PinDetect
main.cpp
00001 #include "mbed.h" 00002 #include "Speaker.h" 00003 #include "uLCD_4DGL.h" 00004 #include "SDFileSystem.h" 00005 #include "PinDetect.h" 00006 #include "mpr121.h" 00007 #include "rtos.h" 00008 #include "wave_player.h" 00009 #include <algorithm> 00010 00011 //Volume level 00012 BusOut myleds(LED1, LED2, LED3, LED4); 00013 00014 //Frequency one of song 00015 Speaker speaker1(p21); 00016 //Frequency two of song 00017 Speaker speaker2(p21); 00018 00019 //Help guide recording if recording 00020 //If not recording then show notes being played 00021 uLCD_4DGL uLCD(p9, p10, p11); 00022 00023 //Save recording and play recording from 00024 SDFileSystem sd(p5, p6, p7, p8, "sd"); 00025 00026 //Push button to switch instruments 00027 PinDetect pianoPB(p19); 00028 PinDetect synthPB(p20); 00029 PinDetect stringsPB(p16); 00030 00031 //Red LEDs to show current instrument 00032 BusOut redLEDs(p22, p23, p24); 00033 00034 // ======= INIT FOR TOUCH PAD ======= 00035 // Create the interrupt receiver object on pin 26 00036 InterruptIn interrupt(p26); 00037 00038 // Setup the Serial to the PC for debugging 00039 Serial pc(USBTX, USBRX); 00040 00041 // Setup the i2c bus on pins 28 and 27 00042 I2C i2c(p28, p27); 00043 00044 // Setup the Mpr121: 00045 // constructor(i2c object, i2c address of the mpr121) 00046 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); 00047 // ======= INIT FOR TOUCH PAD ======= 00048 00049 //Bluetooth 00050 RawSerial bluemod(p13,p14); 00051 00052 //Mutex Locks 00053 //uLCD 00054 Mutex stdio_mutex; 00055 Mutex currState_mutex; 00056 00057 // ===== GLOBAL VARIABLES ====== 00058 enum instr{PIANO, SYNTH, STRINGS}; 00059 enum state{STARTING, IDLE, PLAYING, LOADING, SAVING}; 00060 enum playback{PAUSE, PLAY}; 00061 00062 //Check how many places change state -> be sure to add mutex 00063 volatile int currInstr = PIANO; 00064 volatile int currState = STARTING; 00065 volatile int volume = 0.5; 00066 volatile int playback = PLAY; 00067 00068 volatile int gridPosX = 0; 00069 volatile int gridPosY = 0; 00070 // ===== GLOBAL VARIABLES ====== 00071 00072 void fallInterrupt() { 00073 int key_code=0; 00074 int i=0; 00075 int value=mpr121.read(0x00); 00076 value +=mpr121.read(0x01)<<8; 00077 for (i=0; i<12; i++) { 00078 if (((value>>i)&0x01)==1) key_code=i+1; 00079 } 00080 switch (key_code) { 00081 case 1: //Right 00082 gridPosX = std::min(8, gridPosX + 1); 00083 break; 00084 case 4: //Up 00085 gridPosY = std::max(8, gridPosY + 1); 00086 break; 00087 case 5: //Select 00088 break; 00089 case 6: //Down 00090 gridPosY = std::min(0, gridPosY - 1); 00091 break; 00092 case 9: //Left 00093 gridPosX = std::min(0, gridPosX - 1); 00094 break; 00095 default: 00096 break; 00097 } 00098 } 00099 00100 void piano_hit_callback (void){ 00101 redLEDs.write(0x04); 00102 currInstr = PIANO; 00103 } 00104 00105 void synth_hit_callback (void){ 00106 redLEDs.write(0x02); 00107 currInstr = SYNTH; 00108 } 00109 00110 void strings_hit_callback (void){ 00111 redLEDs.write(0x01); 00112 currInstr = STRINGS; 00113 } 00114 00115 void lcdThread(void const *args) { 00116 while(1) { 00117 // switch(currState) { 00118 // case STARTING: 00119 // stdio_mutex.lock(); 00120 // uLCD.text_width(4); 00121 // uLCD.text_height(4); 00122 // uLCD.printf("Welcome to\n"); 00123 // uLCD.printf("BUST A BEAT\n"); 00124 // stdio_mutex.unlock(); 00125 // break; 00126 // case IDLE: 00127 // break; 00128 // case PLAYING: 00129 // break; 00130 // case LOADING: 00131 // break; 00132 // case SAVING: 00133 // break; 00134 // } 00135 Thread::wait(1000.0*0.2); 00136 } 00137 } 00138 00139 void beatThread(void const *args) { 00140 while(1) { 00141 // if (currState == PLAYING) { 00142 // 00143 // } 00144 Thread::wait(1000.0*0.2); 00145 } 00146 } 00147 00148 void noteThread(void const *args) { 00149 while(1) { 00150 if (currState == PLAYING) { 00151 00152 } 00153 Thread::wait(1000.0*0.2); 00154 } 00155 } 00156 00157 void blueToothThread(void const *args) { 00158 char bnum=0; 00159 char bhit=0; 00160 while(1) { 00161 if (bluemod.getc()=='!') { 00162 if (bluemod.getc()=='B') { //color data packet 00163 bnum = bluemod.getc(); //button number 00164 bhit = bluemod.getc(); //1=hit, 0=release 00165 if (bluemod.getc()==char(~('!' + 'B' + bnum + bhit))) { 00166 switch (bnum) { 00167 case '1': //Playback Start 00168 playback = PLAY; 00169 break; 00170 case '2': //Playback Stop 00171 playback = PAUSE; 00172 break; 00173 case '3': //Save recording 00174 currState = SAVING; 00175 break; 00176 case '4': //Load recording 00177 currState = LOADING; 00178 break; 00179 case '5': //Volume Up 00180 volume = std::min(1.0, volume + 0.1); 00181 break; 00182 case '6': //Volume Down 00183 volume = std::max(0.0, volume - 0.1); 00184 break; 00185 default: 00186 break; 00187 } 00188 } 00189 } 00190 } 00191 if (volume >= 1) { 00192 myleds.write(0x0F); 00193 } else if (volume >= 0.75) { 00194 myleds.write(0x07); 00195 } else if (volume >= 0.50) { 00196 myleds.write(0x03); 00197 } else if (volume >= 0.25) { 00198 myleds.write(0x01); 00199 } else { 00200 myleds.write(0x00); 00201 } 00202 myleds.write(0x0F); 00203 Thread::wait(1000.0*0.2); 00204 } 00205 } 00206 00207 void init() { 00208 pianoPB.mode(PullUp); 00209 synthPB.mode(PullUp); 00210 stringsPB.mode(PullUp); 00211 interrupt.mode(PullUp); 00212 wait(0.01); 00213 pc.printf("I am here\n"); 00214 pianoPB.attach_deasserted(&piano_hit_callback); 00215 synthPB.attach_deasserted(&synth_hit_callback); 00216 stringsPB.attach_deasserted(&strings_hit_callback); 00217 interrupt.fall(&fallInterrupt); 00218 00219 pianoPB.setSampleFrequency(); 00220 synthPB.setSampleFrequency(); 00221 stringsPB.setSampleFrequency(); 00222 00223 Thread thread2(lcdThread); 00224 Thread thread3(beatThread); 00225 Thread thread4(noteThread); 00226 Thread thread5(blueToothThread); 00227 } 00228 00229 int main() { 00230 init(); 00231 //Touchpad 00232 currState = IDLE; 00233 while(1) { 00234 wait(0.2); 00235 } 00236 }
Generated on Wed Sep 28 2022 16:40:41 by
1.7.2