Evan Zhang / Mbed 2 deprecated StepperMotorSong4180

Dependencies:   mbed mbed-rtos SDFileSystem11

main.cpp

Committer:
cawald18
Date:
2021-04-26
Revision:
3:869a56651e96
Parent:
2:39d41fd0f52b
Child:
4:f14ca9203f62

File content as of revision 3:869a56651e96:

#include "mbed.h"
#include "rtos.h"
#include "notes.h"
#include "SDFileSystem.h"
#include <string>
#include <sstream>
#include <iostream>
#include <cstdlib>

Thread motor;
SDFileSystem sd(p5, p6, p7, p8, "sd");
Serial pc(USBTX, USBRX); // tx, rx
class Note {
public:
    int midiNote;
    float start;
    float stop;
    Note(int midi, float sta, float sto) {
        start = sta;
        stop = sto;
        midiNote = midi;
        }
    };
        


class Flipper {
public:
    Flipper(PinName pin) : _pin(pin) {
        _pin = 0;
    }
    void flip() {
        _pin = !_pin;
    }
private:
    DigitalOut _pin;
};
 
DigitalOut led1(LED1);
Flipper f(p21);
Flipper f1(p22);
Ticker t;
Ticker t1;
 
int main() {
     bool holdNotes = 0; //Stop reading notes file
     bool holdStart = 0; //Stop reading start times
     bool holdFinish = 0; //Stop reading in finish times
     pc.baud(115200);
     const char * d;
     std::string str = "123.4567";
     d = str.c_str();
    // convert string to float
    float num_float = std::atof(d);

    // convert string to double
    pc.printf("%f\n", num_float-1.0);
    int temp = 0;

    pc.printf("Hello World!");
    string output = "";

    FILE *fpn = fopen("/sd/notesmatrix.txt", "r");
    FILE *fpstart = fopen("/sd/notesstart.txt", "r");
    FILE *fpfinish = fopen("/sd/notesfinish.txt","r");
    unsigned char c;
    pc.printf("I'm here\n");
    Note myNote = Note(0, 0, 0);
    while (!feof(fpn)){                        // while not end of file
        while(!holdNotes) {
           c = fgetc(fpn);                         // get a character/byte from the file
           if(c == ','){
               stringstream degree(output);
               degree >> temp;
               pc.printf("%d\n", temp);
               output = "";
               holdNotes = 1;
           } else {
          output += c;
          }
        }
    myNote.midiNote = temp;
    output = "";
        while(!holdStart) {
            c = fgetc(fpstart);
            if(c == ','){
                d = output.c_str();
                num_float = std::atof(d);
                pc.printf("%f\n", num_float);
                output = "";  
                myNote.start = num_float;
                holdStart = 1;
            } else {
                output+= c;
             }
           
        }
    output = "";
    while(!holdFinish){
        c = fgetc(fpfinish);
        if(c == ','){
            d = output.c_str();
            num_float = std::atof(d);
            pc.printf("%f\n", num_float);
            output = "";  
            myNote.stop = num_float;
            holdFinish = 1;
        } else {
           output+= c;
           }
           
    } //ONCE WE REACH THIS POINT ALL PARTS OF THE NOTE OBJECT SHOULD BE SET
    pc.printf("Hello World!\n");
    pc.printf("%d", myNote.midiNote);
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
      
    }
    
}    
      
      
   
}