test publish

Dependencies:   mbed GroveEarbudSensor

Application.cpp

Committer:
age2pierre
Date:
2016-04-14
Revision:
12:a56f9fb318de
Child:
13:879d678baf64

File content as of revision 12:a56f9fb318de:

#include "Application.h"

Application::Application(InterruptIn* earSensorPin, AnalogIn* GSRPin, PwmOut* speakerPin) : earbud(earSensorPin), speaker(speakerPin), GSRSens(GSRPin)
{
    thresholdHR = 0.0;
    thresholdGSR = 0.0;
}

void Application::Init()
{
    printf("Initialization.. waiting 14s");
    fflush(stdout);
    wait(4); // Time for GSR measure to stabilize

    float sumGSR, sumHR = 0.0;
    for(int i = 0 ; i<NBGSRVALUE ; i++) {
        sumGSR += GSRSens.getGSRValue();
        sumHR += earbud.getHeartRate();
        wait(0.5);

    }
    thresholdGSR = sumGSR/NBGSRVALUE;
    thresholdHR = sumHR/NBGSRVALUE;

}

void Application::Run()
{
    MelodyGenerator melodyGen;
    MajorScale scale1(SOL_4);
    GypsyScale scale2(MI_4);
    vector<Notes>* melo;
    float heartRate, measureGSR;

    while(true) {

        heartRate = earbud.getHeartRate();
        measureGSR = GSRSens.getGSRValue();
        printf("\r\nHeartrate : %0.0f (nominal : %0.3f) \r\nGSR       : %0.4f (nominal : %0.4f) \r\n", heartRate, thresholdHR, measureGSR, thresholdGSR); // for debug purpose

        if(heartRate <30) {
            speaker.play(SILENCE);
            heartRate = earbud.getHeartRate();
            wait(1);
        } else {
            if (measureGSR < thresholdGSR) {
                melo = melodyGen.getMeasure(scale1);
                printf("\tMajor scale\r\n");
            } else {
                melo = melodyGen.getMeasure(scale2);
                printf("\tGypsy scale\r\n");
            }

            for(vector<Notes>::iterator it = melo->begin(); it != melo->end(); ++it) {
                speaker.play(*it);
                wait( 0.3 - ((heartRate - thresholdHR) * 0.0015));
            }
        }
    }
}