![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test publish
Dependencies: mbed GroveEarbudSensor
Revision 8:e1beb2a9454e, committed 2016-04-11
- Comitter:
- age2pierre
- Date:
- Mon Apr 11 13:30:35 2016 +0000
- Parent:
- 7:d497a01156ef
- Child:
- 10:af58557a4d6c
- Commit message:
- Updated main
Changed in this revision
--- a/GypsyScale.h Mon Apr 11 11:18:46 2016 +0000 +++ b/GypsyScale.h Mon Apr 11 13:30:35 2016 +0000 @@ -6,6 +6,7 @@ class GypsyScale : public Scale { public: + GypsyScale(Notes arg) : Scale(arg){}; Notes getSupertonic(); Notes getMediant(); Notes getSubdominant();
--- a/MajorScale.h Mon Apr 11 11:18:46 2016 +0000 +++ b/MajorScale.h Mon Apr 11 13:30:35 2016 +0000 @@ -6,6 +6,7 @@ class MajorScale : public Scale { public: + MajorScale(Notes arg) : Scale(arg){}; Notes getSupertonic(); Notes getMediant(); Notes getSubdominant();
--- a/MelodyGenerator.cpp Mon Apr 11 11:18:46 2016 +0000 +++ b/MelodyGenerator.cpp Mon Apr 11 13:30:35 2016 +0000 @@ -12,18 +12,19 @@ this->melody = myMelody; } -vector<Notes> MelodyGenerator::getMeasure(Scale& mode) +vector<Notes>* MelodyGenerator::getMeasure(Scale& mode) { int length = melody.length(); - vector<Notes> result; + vector<Notes>* result = new vector<Notes>(); char currentChar = melody[cursor]; - bool octaveUp = false; - bool octaveDown = false; - bool flat = false; - bool sharp = false; + while(currentChar != '|') { - while(currentChar != '|') { + bool octaveUp = false; + bool octaveDown = false; + bool flat = false; + bool sharp = false; + if (currentChar == 'u') octaveUp = true; else if (currentChar == 'd') @@ -72,14 +73,20 @@ else if (octaveDown) currentNote = mode.applyOctaveDown(currentNote); - result.push_back(currentNote); + result->push_back(currentNote); } - } + + cursor++; + if (cursor == length) + cursor = 0; - if (cursor == length) - cursor = 0; - else + currentChar = melody[cursor]; + } + + cursor++; + if (cursor == length) + cursor = 0; return result; } @@ -88,4 +95,3 @@ { this->melody = myMelody; } -
--- a/MelodyGenerator.h Mon Apr 11 11:18:46 2016 +0000 +++ b/MelodyGenerator.h Mon Apr 11 13:30:35 2016 +0000 @@ -12,7 +12,7 @@ public : MelodyGenerator(); MelodyGenerator(string myMelody); - vector<Notes> getMeasure(Scale& mode); + vector<Notes>* getMeasure(Scale& mode); void changeMelody(string myMelody); protected : private :
--- a/main.cpp Mon Apr 11 11:18:46 2016 +0000 +++ b/main.cpp Mon Apr 11 13:30:35 2016 +0000 @@ -1,20 +1,38 @@ #include "mbed.h" #include "GroveEarbudSensor.h" - -InterruptIn earSensor(p5); -DigitalOut led(LED1); +#include "GypsyScale.h" +#include "MajorScale.h" +#include "Speaker.h" +#include "MelodyGenerator.h" +#include "Notes.h" +#include <vector> -void printHeartRate(float heartrate,void *data) { - printf("Callback: heartrate = %.1f\r\n",heartrate); -} +using namespace std; + +InterruptIn earSensorPin(p5); +//DigitalOut led(LED1); +PwmOut speakerPin(p21); int main() { - GroveEarbudSensor earbud(&earSensor); - earbud.registerCallback(printHeartRate); - + GroveEarbudSensor earbud(&earSensorPin); + Speaker speaker(&speakerPin); + MelodyGenerator melodyGen; + + MajorScale scale1(SOL_4); + GypsyScale scale2(MI_4); + while(true) { - led = !led; - wait(0.5); + float heartRate = earbud.getHeartRate(); + if(heartRate <30) { + speaker.play(SILENCE); + heartRate = earbud.getHeartRate(); + } else { + vector<Notes>* melo = melodyGen.getMeasure(scale1); + for(vector<Notes>::iterator it = melo->begin(); it != melo->end(); ++it) { + speaker.play(*it); + wait( heartRate * 0.006); + } + } } }