回転数計のクラスです。

Dependencies:   mbed

main.cpp

Committer:
tsumagari
Date:
2016-11-23
Revision:
0:57308daf8dfe
Child:
1:6bceac3446ff

File content as of revision 0:57308daf8dfe:

#include "mbed.h"
#include <string>
//#include "Cadence.h"

DigitalOut myled(LED1);
Ticker cadenceTicker;
Serial pc(USBTX,USBRX); //for debug

class Cadence : public Serial{
    private:
    static const int DATAS_NUM = 74;
    string strData;
    int gomi;
    
    protected:
    
    public:
    char data[DATAS_NUM];
    int data_count, data_num;
    double cadence, cadenceV;
    string strV,strC;
    
    Cadence(PinName tx, PinName rx, const char* name = NULL) : Serial(tx, rx, NULL){
        for(int i=0;i<DATAS_NUM;i++) data[i]= NULL;
        data_num=0;
        data_count=0;
        baud(115200);
        cadence=0;
        cadenceV=0;
    }
//    void Init(){
//        for(int i=0;i<DATAS_NUM;i++) data[i]= NULL;
//        data_num=0;
//        data_count=0;
//        baud(115200);
//    }
    void cadenceRead(){ //Ticker で定期的に呼び出して値を更新
        data_count=0;
        data_num=0;
        do{
            if(readable()) {
                data[data_count] = getc();
                data_count++;
            }
        }while(data[data_count-1] != '\r');
        
        if(data_count > DATAS_NUM-4 ){
            strData = data;
            switch (strData.length()){
                case DATAS_NUM-3 :
                    strData.erase(0,1);
                    break;
                case DATAS_NUM-2 :
                    strData.erase(0,2);
                    break;
                case DATAS_NUM-1 :
                    strData.erase(0,3);
                    break;
                case DATAS_NUM :
                    strData.erase(0,4);
            }
            strV = strData.substr(27,4);
            strC = strData.substr(64,4);
            sscanf(strC.c_str(),"%lf",&cadence);
            sscanf(strV.c_str(),"%lf",&cadenceV);
            cadence /= 6.0;
            cadenceV *=0.001;
        }
    }
};



Cadence cadence(p13,p14);

int main() {
    pc.baud(115200);
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        pc.printf("test\n\r");
        pc.printf("cadence: %f\n\r",cadence.cadence);
        pc.printf("voltage: %f\n\r",cadence.cadenceV);
        pc.printf("%d,\n\r",cadence.data_count);
        cadence.cadenceRead();
    }
}