natsu tsumagari
/
test_cadenceClass
回転数計のクラスです。
Cadence.h@0:57308daf8dfe, 2016-11-23 (annotated)
- Committer:
- tsumagari
- Date:
- Wed Nov 23 04:40:06 2016 +0000
- Revision:
- 0:57308daf8dfe
- Child:
- 1:6bceac3446ff
Cadence class ?main????????
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 | #define CADENCE_READ_TIME 0.1 |
tsumagari | 0:57308daf8dfe | 8 | |
tsumagari | 0:57308daf8dfe | 9 | //Serial cadenceTwe(p13,p14); |
tsumagari | 0:57308daf8dfe | 10 | Ticker cadenceReadTicker; |
tsumagari | 0:57308daf8dfe | 11 | |
tsumagari | 0:57308daf8dfe | 12 | class Cadence : public Serial{ |
tsumagari | 0:57308daf8dfe | 13 | private: |
tsumagari | 0:57308daf8dfe | 14 | static const int DATAS_NUM = 74; |
tsumagari | 0:57308daf8dfe | 15 | int data_count, data_num; |
tsumagari | 0:57308daf8dfe | 16 | char data[DATAS_NUM]; |
tsumagari | 0:57308daf8dfe | 17 | string strData; |
tsumagari | 0:57308daf8dfe | 18 | int gomi; |
tsumagari | 0:57308daf8dfe | 19 | |
tsumagari | 0:57308daf8dfe | 20 | protected: |
tsumagari | 0:57308daf8dfe | 21 | |
tsumagari | 0:57308daf8dfe | 22 | public: |
tsumagari | 0:57308daf8dfe | 23 | double cadence, cadenceV; |
tsumagari | 0:57308daf8dfe | 24 | Cadence(PinName tx, PinName rx, const char* name = NULL) : Serial(tx, rx, NULL){ |
tsumagari | 0:57308daf8dfe | 25 | for(int i=0;i<DATAS_NUM;i++) data[i]= NULL; |
tsumagari | 0:57308daf8dfe | 26 | data_num=0; |
tsumagari | 0:57308daf8dfe | 27 | data_count=0; |
tsumagari | 0:57308daf8dfe | 28 | baud(115200); |
tsumagari | 0:57308daf8dfe | 29 | cadence=0; |
tsumagari | 0:57308daf8dfe | 30 | cadenceV=0; |
tsumagari | 0:57308daf8dfe | 31 | } |
tsumagari | 0:57308daf8dfe | 32 | // void Init(){ |
tsumagari | 0:57308daf8dfe | 33 | // for(int i=0;i<DATAS_NUM;i++) data[i]= NULL; |
tsumagari | 0:57308daf8dfe | 34 | // data_num=0; |
tsumagari | 0:57308daf8dfe | 35 | // data_count=0; |
tsumagari | 0:57308daf8dfe | 36 | // baud(115200); |
tsumagari | 0:57308daf8dfe | 37 | // } |
tsumagari | 0:57308daf8dfe | 38 | void cadenceRead(){ //Ticker で定期的に呼び出して値を更新 |
tsumagari | 0:57308daf8dfe | 39 | do{ |
tsumagari | 0:57308daf8dfe | 40 | if(readable()) { |
tsumagari | 0:57308daf8dfe | 41 | data[data_count] = getc(); |
tsumagari | 0:57308daf8dfe | 42 | data_count++; |
tsumagari | 0:57308daf8dfe | 43 | } |
tsumagari | 0:57308daf8dfe | 44 | }while(data[--data_count] == '\r'); |
tsumagari | 0:57308daf8dfe | 45 | |
tsumagari | 0:57308daf8dfe | 46 | if(data_count > DATAS_NUM-4 ){ |
tsumagari | 0:57308daf8dfe | 47 | strData = data; |
tsumagari | 0:57308daf8dfe | 48 | } |
tsumagari | 0:57308daf8dfe | 49 | switch (strData.length()){ |
tsumagari | 0:57308daf8dfe | 50 | case DATAS_NUM-3 : |
tsumagari | 0:57308daf8dfe | 51 | strData.erase(0,1); |
tsumagari | 0:57308daf8dfe | 52 | break; |
tsumagari | 0:57308daf8dfe | 53 | case DATAS_NUM-2 : |
tsumagari | 0:57308daf8dfe | 54 | strData.erase(0,2); |
tsumagari | 0:57308daf8dfe | 55 | break; |
tsumagari | 0:57308daf8dfe | 56 | case DATAS_NUM-1 : |
tsumagari | 0:57308daf8dfe | 57 | strData.erase(0,3); |
tsumagari | 0:57308daf8dfe | 58 | break; |
tsumagari | 0:57308daf8dfe | 59 | case DATAS_NUM : |
tsumagari | 0:57308daf8dfe | 60 | strData.erase(0,4); |
tsumagari | 0:57308daf8dfe | 61 | } |
tsumagari | 0:57308daf8dfe | 62 | sscanf(strData.substr(27,4).c_str(),"%f",&cadenceV); |
tsumagari | 0:57308daf8dfe | 63 | sscanf(strData.substr(64,4).c_str(),"%f",&cadence); |
tsumagari | 0:57308daf8dfe | 64 | cadence /= 6.0; |
tsumagari | 0:57308daf8dfe | 65 | } |
tsumagari | 0:57308daf8dfe | 66 | }; |
tsumagari | 0:57308daf8dfe | 67 | #endif |