回転数計のクラスです。

Dependencies:   mbed

Committer:
tsumagari
Date:
Wed Nov 23 06:06:29 2016 +0000
Revision:
2:b492f029a2f4
Parent:
1:6bceac3446ff
Child:
4:2def7f5f7fed
????substr???????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsumagari 0:57308daf8dfe 1 #ifndef CADENCE_H
tsumagari 0:57308daf8dfe 2 #define CADENCE_H
tsumagari 0:57308daf8dfe 3
tsumagari 0:57308daf8dfe 4 #include "mbed.h"
tsumagari 0:57308daf8dfe 5 #include <string>
tsumagari 0:57308daf8dfe 6
tsumagari 0:57308daf8dfe 7 class Cadence : public Serial{
tsumagari 0:57308daf8dfe 8 private:
tsumagari 2:b492f029a2f4 9 static const int DATAS_NUM = 75;
tsumagari 0:57308daf8dfe 10
tsumagari 0:57308daf8dfe 11 protected:
tsumagari 0:57308daf8dfe 12
tsumagari 0:57308daf8dfe 13 public:
tsumagari 1:6bceac3446ff 14 string strC,strV;
tsumagari 1:6bceac3446ff 15 char data[DATAS_NUM];
tsumagari 1:6bceac3446ff 16 string strData;
tsumagari 1:6bceac3446ff 17 int data_count, data_num;
tsumagari 0:57308daf8dfe 18 double cadence, cadenceV;
tsumagari 0:57308daf8dfe 19 Cadence(PinName tx, PinName rx, const char* name = NULL) : Serial(tx, rx, NULL){
tsumagari 0:57308daf8dfe 20 for(int i=0;i<DATAS_NUM;i++) data[i]= NULL;
tsumagari 0:57308daf8dfe 21 data_num=0;
tsumagari 0:57308daf8dfe 22 data_count=0;
tsumagari 0:57308daf8dfe 23 baud(115200);
tsumagari 0:57308daf8dfe 24 cadence=0;
tsumagari 0:57308daf8dfe 25 cadenceV=0;
tsumagari 0:57308daf8dfe 26 }
tsumagari 2:b492f029a2f4 27 void readData(){ //Ticker で定期的に呼び出して値を更新
tsumagari 1:6bceac3446ff 28 data_count = 0;
tsumagari 1:6bceac3446ff 29 data_num = 0;
tsumagari 2:b492f029a2f4 30 cadence = 0;
tsumagari 0:57308daf8dfe 31 do{
tsumagari 0:57308daf8dfe 32 if(readable()) {
tsumagari 0:57308daf8dfe 33 data[data_count] = getc();
tsumagari 0:57308daf8dfe 34 data_count++;
tsumagari 0:57308daf8dfe 35 }
tsumagari 2:b492f029a2f4 36 }while(data[data_count-1] != '\n');
tsumagari 2:b492f029a2f4 37
tsumagari 2:b492f029a2f4 38 strData = data;
tsumagari 2:b492f029a2f4 39 if(strData.length() > DATAS_NUM-4 ){
tsumagari 1:6bceac3446ff 40 switch (strData.length()){
tsumagari 1:6bceac3446ff 41 case DATAS_NUM-3 :
tsumagari 1:6bceac3446ff 42 strData.erase(0,2);
tsumagari 1:6bceac3446ff 43 break;
tsumagari 1:6bceac3446ff 44 case DATAS_NUM-2 :
tsumagari 1:6bceac3446ff 45 strData.erase(0,3);
tsumagari 1:6bceac3446ff 46 break;
tsumagari 1:6bceac3446ff 47 case DATAS_NUM-1 :
tsumagari 1:6bceac3446ff 48 strData.erase(0,4);
tsumagari 1:6bceac3446ff 49 break;
tsumagari 1:6bceac3446ff 50 case DATAS_NUM :
tsumagari 1:6bceac3446ff 51 strData.erase(0,5);
tsumagari 1:6bceac3446ff 52 }
tsumagari 2:b492f029a2f4 53 strV = strData.substr(26,4);
tsumagari 1:6bceac3446ff 54 strC = strData.substr(64,4);
tsumagari 1:6bceac3446ff 55 sscanf(strV.c_str(),"%lf",&cadenceV);
tsumagari 1:6bceac3446ff 56 sscanf(strC.c_str(),"%lf",&cadence);
tsumagari 2:b492f029a2f4 57 //cadence /= 6.0;
tsumagari 2:b492f029a2f4 58 // cadenceV *= 0.001;
tsumagari 0:57308daf8dfe 59 }
tsumagari 0:57308daf8dfe 60 }
tsumagari 0:57308daf8dfe 61 };
tsumagari 0:57308daf8dfe 62 #endif