3872 Play-live--test code - tempting to make this the main one

Dependencies:   mbed fastlib

Committer:
chenchen2020
Date:
Fri Oct 30 01:43:49 2020 +0000
Revision:
1:c704bea518d8
Parent:
0:e09703934ff4
Child:
2:9296823ea33d
Play_live+Recording - playback not working entirely, need to add servo class somewhere.  please work on this, thank you

Who changed what in which revision?

UserRevisionLine numberNew 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>
4180_1 0:e09703934ff4 26
4180_1 0:e09703934ff4 27 #include <mpr121.h>
chenchen2020 1:c704bea518d8 28 #include "SongPlayer.h"
4180_1 0:e09703934ff4 29
chenchen2020 1:c704bea518d8 30 DigitalIn record_mode(p14);
chenchen2020 1:c704bea518d8 31 DigitalIn play_back_mode(p16);
chenchen2020 1:c704bea518d8 32 DigitalIn play_live_mode(p15);
chenchen2020 1:c704bea518d8 33 SongPlayer mySpeaker(p21);
chenchen2020 1:c704bea518d8 34 Timer t;
chenchen2020 1:c704bea518d8 35 Timer t_rest;
chenchen2020 1:c704bea518d8 36
chenchen2020 1:c704bea518d8 37 enum Statetype {IDLE = 0, REC = 1, PLAY_BACK = 2, PLAY_LIVE = 3, STOP = 4, RESET = 5};
chenchen2020 1:c704bea518d8 38
chenchen2020 1:c704bea518d8 39 Statetype mode;
chenchen2020 1:c704bea518d8 40
chenchen2020 1:c704bea518d8 41 float mem_note[160];
chenchen2020 1:c704bea518d8 42 float mem_duration[160];
chenchen2020 1:c704bea518d8 43 int mem_ind;
4180_1 0:e09703934ff4 44
4180_1 0:e09703934ff4 45 // Create the interrupt receiver object on pin 26
4180_1 0:e09703934ff4 46 InterruptIn interrupt(p26);
4180_1 0:e09703934ff4 47
4180_1 0:e09703934ff4 48 // Setup the Serial to the PC for debugging
4180_1 0:e09703934ff4 49 Serial pc(USBTX, USBRX);
4180_1 0:e09703934ff4 50
4180_1 0:e09703934ff4 51 // Setup the i2c bus on pins 28 and 27
4180_1 0:e09703934ff4 52 I2C i2c(p9, p10);
4180_1 0:e09703934ff4 53
4180_1 0:e09703934ff4 54 // Setup the Mpr121:
4180_1 0:e09703934ff4 55 // constructor(i2c object, i2c address of the mpr121)
4180_1 0:e09703934ff4 56 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
4180_1 0:e09703934ff4 57
4180_1 0:e09703934ff4 58 void fallInterrupt() {
chenchen2020 1:c704bea518d8 59 //int key_code=0;
chenchen2020 1:c704bea518d8 60 //int i=0;
chenchen2020 1:c704bea518d8 61 uint16_t value=mpr121.read(0x00);
4180_1 0:e09703934ff4 62 value +=mpr121.read(0x01)<<8;
chenchen2020 1:c704bea518d8 63 float temp_value = float(value);
chenchen2020 1:c704bea518d8 64 t_rest.stop();
chenchen2020 1:c704bea518d8 65 if(mode == REC && mem_ind < 160){
chenchen2020 1:c704bea518d8 66 //Write duration and silence in object
chenchen2020 1:c704bea518d8 67 mem_note[mem_ind] = 0;
chenchen2020 1:c704bea518d8 68 mem_duration[mem_ind] = t_rest.read();
chenchen2020 1:c704bea518d8 69 pc.printf("New Data in memory: Note: %f Duration: %f\n", mem_note[mem_ind], mem_duration[mem_ind]);
chenchen2020 1:c704bea518d8 70 mem_ind++;
chenchen2020 1:c704bea518d8 71 }
chenchen2020 1:c704bea518d8 72 pc.printf("The time of silence seconds %f \n", t_rest.read());
chenchen2020 1:c704bea518d8 73 t_rest.reset();
chenchen2020 1:c704bea518d8 74 t.start();
chenchen2020 1:c704bea518d8 75 float note[] = {value + 150};
chenchen2020 1:c704bea518d8 76 if(mode == PLAY_LIVE || mode == REC){
chenchen2020 1:c704bea518d8 77 mySpeaker.Play_Note(note);
chenchen2020 1:c704bea518d8 78 }
chenchen2020 1:c704bea518d8 79 //sustaining note
chenchen2020 1:c704bea518d8 80 while(value > 0){
chenchen2020 1:c704bea518d8 81 if(mode == PLAY_LIVE || mode == REC){
chenchen2020 1:c704bea518d8 82 mySpeaker.Play_Note(note);
4180_1 0:e09703934ff4 83 }
chenchen2020 1:c704bea518d8 84 pc.printf("MPR value: %x \r\n", value);
chenchen2020 1:c704bea518d8 85 note[0] = value + 150;
chenchen2020 1:c704bea518d8 86 if(mode == PLAY_LIVE || mode == REC){
chenchen2020 1:c704bea518d8 87 mySpeaker.Play_Note(note);
chenchen2020 1:c704bea518d8 88 }
chenchen2020 1:c704bea518d8 89 value=mpr121.read(0x00);
chenchen2020 1:c704bea518d8 90 value +=mpr121.read(0x01)<<8;
chenchen2020 1:c704bea518d8 91 }
chenchen2020 1:c704bea518d8 92 mySpeaker.Play_Note(0);
chenchen2020 1:c704bea518d8 93 t.stop();
chenchen2020 1:c704bea518d8 94 if(mode == REC && mem_ind < 160){
chenchen2020 1:c704bea518d8 95 mem_note[mem_ind] = temp_value;
chenchen2020 1:c704bea518d8 96 mem_duration[mem_ind] = t.read();
chenchen2020 1:c704bea518d8 97 pc.printf("New Data in memory: Note: %f Duration: %f\n", mem_note[mem_ind], mem_duration[mem_ind]);
chenchen2020 1:c704bea518d8 98 mem_ind++;
chenchen2020 1:c704bea518d8 99 }
chenchen2020 1:c704bea518d8 100 t_rest.start();
chenchen2020 1:c704bea518d8 101 float duration[] = {t.read()};
chenchen2020 1:c704bea518d8 102 pc.printf("The of note seconds %f \n", t.read());
chenchen2020 1:c704bea518d8 103 t.reset();
4180_1 0:e09703934ff4 104 }
4180_1 0:e09703934ff4 105 int main() {
chenchen2020 1:c704bea518d8 106
4180_1 0:e09703934ff4 107 interrupt.fall(&fallInterrupt);
4180_1 0:e09703934ff4 108 interrupt.mode(PullUp);
4180_1 0:e09703934ff4 109
4180_1 0:e09703934ff4 110 while (1) {
chenchen2020 1:c704bea518d8 111 if(record_mode == 1){
chenchen2020 1:c704bea518d8 112 mode = REC;
chenchen2020 1:c704bea518d8 113 }
chenchen2020 1:c704bea518d8 114 if(play_back_mode == 1){
chenchen2020 1:c704bea518d8 115 mode = PLAY_BACK;
chenchen2020 1:c704bea518d8 116 }
chenchen2020 1:c704bea518d8 117 if(play_live_mode == 1){
chenchen2020 1:c704bea518d8 118 mode = PLAY_LIVE;
chenchen2020 1:c704bea518d8 119 }
chenchen2020 1:c704bea518d8 120 switch (mode){
chenchen2020 1:c704bea518d8 121 case REC:
chenchen2020 1:c704bea518d8 122 break;
chenchen2020 1:c704bea518d8 123 case PLAY_BACK:
chenchen2020 1:c704bea518d8 124 interrupt.fall(NULL);
chenchen2020 1:c704bea518d8 125 mySpeaker.PlaySong(mem_note, mem_duration);
chenchen2020 1:c704bea518d8 126 wait(1);
chenchen2020 1:c704bea518d8 127 interrupt.fall(&fallInterrupt);
chenchen2020 1:c704bea518d8 128 break;
chenchen2020 1:c704bea518d8 129 case PLAY_LIVE:
chenchen2020 1:c704bea518d8 130
chenchen2020 1:c704bea518d8 131 break;
chenchen2020 1:c704bea518d8 132 }
4180_1 0:e09703934ff4 133 }
4180_1 0:e09703934ff4 134 }
4180_1 0:e09703934ff4 135
4180_1 0:e09703934ff4 136
4180_1 0:e09703934ff4 137