Fengrui Chenchen Zuo
/
3872_play_live
3872 Play-live--test code - tempting to make this the main one
Diff: main.cpp
- Revision:
- 1:c704bea518d8
- Parent:
- 0:e09703934ff4
- Child:
- 2:9296823ea33d
diff -r e09703934ff4 -r c704bea518d8 main.cpp --- a/main.cpp Wed Mar 16 01:49:13 2011 +0000 +++ b/main.cpp Fri Oct 30 01:43:49 2020 +0000 @@ -25,11 +25,22 @@ #include <list> #include <mpr121.h> +#include "SongPlayer.h" -DigitalOut led1(LED1); -DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); +DigitalIn record_mode(p14); +DigitalIn play_back_mode(p16); +DigitalIn play_live_mode(p15); +SongPlayer mySpeaker(p21); +Timer t; +Timer t_rest; + +enum Statetype {IDLE = 0, REC = 1, PLAY_BACK = 2, PLAY_LIVE = 3, STOP = 4, RESET = 5}; + +Statetype mode; + +float mem_note[160]; +float mem_duration[160]; +int mem_ind; // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); @@ -45,79 +56,80 @@ Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); void fallInterrupt() { - int key_code=0; - int i=0; - int value=mpr121.read(0x00); + //int key_code=0; + //int i=0; + uint16_t value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; - // LED demo mod by J. Hamblen - //pc.printf("MPR value: %x \r\n", value); - i=0; - // puts key number out to LEDs for demo - for (i=0; i<12; i++) { - if (((value>>i)&0x01)==1) key_code=i+1; + float temp_value = float(value); + t_rest.stop(); + if(mode == REC && mem_ind < 160){ + //Write duration and silence in object + mem_note[mem_ind] = 0; + mem_duration[mem_ind] = t_rest.read(); + pc.printf("New Data in memory: Note: %f Duration: %f\n", mem_note[mem_ind], mem_duration[mem_ind]); + mem_ind++; + } + pc.printf("The time of silence seconds %f \n", t_rest.read()); + t_rest.reset(); + t.start(); + float note[] = {value + 150}; + if(mode == PLAY_LIVE || mode == REC){ + mySpeaker.Play_Note(note); + } + //sustaining note + while(value > 0){ + if(mode == PLAY_LIVE || mode == REC){ + mySpeaker.Play_Note(note); } - led4=key_code & 0x01; - led3=(key_code>>1) & 0x01; - led2=(key_code>>2) & 0x01; - led1=(key_code>>3) & 0x01; + pc.printf("MPR value: %x \r\n", value); + note[0] = value + 150; + if(mode == PLAY_LIVE || mode == REC){ + mySpeaker.Play_Note(note); + } + value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + } + mySpeaker.Play_Note(0); + t.stop(); + if(mode == REC && mem_ind < 160){ + mem_note[mem_ind] = temp_value; + mem_duration[mem_ind] = t.read(); + pc.printf("New Data in memory: Note: %f Duration: %f\n", mem_note[mem_ind], mem_duration[mem_ind]); + mem_ind++; + } + t_rest.start(); + float duration[] = {t.read()}; + pc.printf("The of note seconds %f \n", t.read()); + t.reset(); } int main() { - - pc.printf("\nHello from the mbed & mpr121\n\r"); - - unsigned char dataArray[2]; - int key; - int count = 0; - - pc.printf("Test 1: read a value: \r\n"); - dataArray[0] = mpr121.read(AFE_CFG); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 2: read a value: \r\n"); - dataArray[0] = mpr121.read(0x5d); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 3: write & read a value: \r\n"); - mpr121.read(ELE0_T); - mpr121.write(ELE0_T,0x22); - dataArray[0] = mpr121.read(ELE0_T); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 4: Write many values: \r\n"); - unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; - mpr121.writeMany(0x42,data,7); - - // Now read them back .. - key = 0x42; - count = 0; - while (count < 7) { - char result = mpr121.read(key); - key++; - count++; - pc.printf("Read value: '%x'=%x\n\r",key,result); - } - - pc.printf("Test 5: Read Electrodes:\r\n"); - key = ELE0_T; - count = 0; - while (count < 24) { - char result = mpr121.read(key); - pc.printf("Read key:%x value:%x\n\r",key,result); - key++; - count++; - } - pc.printf("--------- \r\n\n"); - - // mpr121.setProximityMode(true); - - pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); - + interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); while (1) { - wait(5); - pc.printf("."); + if(record_mode == 1){ + mode = REC; + } + if(play_back_mode == 1){ + mode = PLAY_BACK; + } + if(play_live_mode == 1){ + mode = PLAY_LIVE; + } + switch (mode){ + case REC: + break; + case PLAY_BACK: + interrupt.fall(NULL); + mySpeaker.PlaySong(mem_note, mem_duration); + wait(1); + interrupt.fall(&fallInterrupt); + break; + case PLAY_LIVE: + + break; + } } }