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

Dependencies:   mbed fastlib

main.cpp

Committer:
chenchen2020
Date:
2020-10-30
Revision:
1:c704bea518d8
Parent:
0:e09703934ff4
Child:
2:9296823ea33d

File content as of revision 1:c704bea518d8:

/*
Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <mbed.h>
#include <string>
#include <list>

#include <mpr121.h>
#include "SongPlayer.h"

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);

// Setup the Serial to the PC for debugging
Serial pc(USBTX, USBRX);

// Setup the i2c bus on pins 28 and 27
I2C i2c(p9, p10);

// Setup the Mpr121:
// constructor(i2c object, i2c address of the mpr121)
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);

void fallInterrupt() {
    //int key_code=0;
    //int i=0;
    uint16_t value=mpr121.read(0x00);
    value +=mpr121.read(0x01)<<8;
    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);
        }
        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() {
    
    interrupt.fall(&fallInterrupt);
    interrupt.mode(PullUp);

    while (1) {
        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;
        }
    }
}