natsu tsumagari
/
test_cadenceClass
回転数計のクラスです。
Cadence.h@1:6bceac3446ff, 2016-11-23 (annotated)
- Committer:
- tsumagari
- Date:
- Wed Nov 23 05:31:01 2016 +0000
- Revision:
- 1:6bceac3446ff
- Parent:
- 0:57308daf8dfe
- Child:
- 2:b492f029a2f4
ho
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:57308daf8dfe | 9 | static const int DATAS_NUM = 74; |
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 | 0:57308daf8dfe | 27 | void cadenceRead(){ //Ticker で定期的に呼び出して値を更新 |
tsumagari | 1:6bceac3446ff | 28 | data_count = 0; |
tsumagari | 1:6bceac3446ff | 29 | data_num = 0; |
tsumagari | 0:57308daf8dfe | 30 | do{ |
tsumagari | 0:57308daf8dfe | 31 | if(readable()) { |
tsumagari | 0:57308daf8dfe | 32 | data[data_count] = getc(); |
tsumagari | 0:57308daf8dfe | 33 | data_count++; |
tsumagari | 0:57308daf8dfe | 34 | } |
tsumagari | 1:6bceac3446ff | 35 | }while(data[data_count-1] != '\r' || data[data_count-1] != '\n'); |
tsumagari | 1:6bceac3446ff | 36 | |
tsumagari | 0:57308daf8dfe | 37 | if(data_count > DATAS_NUM-4 ){ |
tsumagari | 0:57308daf8dfe | 38 | strData = data; |
tsumagari | 1:6bceac3446ff | 39 | switch (strData.length()){ |
tsumagari | 1:6bceac3446ff | 40 | case DATAS_NUM-3 : |
tsumagari | 1:6bceac3446ff | 41 | strData.erase(0,2); |
tsumagari | 1:6bceac3446ff | 42 | break; |
tsumagari | 1:6bceac3446ff | 43 | case DATAS_NUM-2 : |
tsumagari | 1:6bceac3446ff | 44 | strData.erase(0,3); |
tsumagari | 1:6bceac3446ff | 45 | break; |
tsumagari | 1:6bceac3446ff | 46 | case DATAS_NUM-1 : |
tsumagari | 1:6bceac3446ff | 47 | strData.erase(0,4); |
tsumagari | 1:6bceac3446ff | 48 | break; |
tsumagari | 1:6bceac3446ff | 49 | case DATAS_NUM : |
tsumagari | 1:6bceac3446ff | 50 | strData.erase(0,5); |
tsumagari | 1:6bceac3446ff | 51 | } |
tsumagari | 1:6bceac3446ff | 52 | strV = strData.substr(27,4); |
tsumagari | 1:6bceac3446ff | 53 | strC = strData.substr(64,4); |
tsumagari | 1:6bceac3446ff | 54 | sscanf(strV.c_str(),"%lf",&cadenceV); |
tsumagari | 1:6bceac3446ff | 55 | sscanf(strC.c_str(),"%lf",&cadence); |
tsumagari | 1:6bceac3446ff | 56 | cadence /= 6.0; |
tsumagari | 1:6bceac3446ff | 57 | cadenceV *= 0.001; |
tsumagari | 0:57308daf8dfe | 58 | } |
tsumagari | 0:57308daf8dfe | 59 | } |
tsumagari | 0:57308daf8dfe | 60 | }; |
tsumagari | 0:57308daf8dfe | 61 | #endif |