Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSoftSerial2 INA226_ver1 mbed-rtos mbed SDFileSystem-RTOS
Fork of keiki2017 by
Cadence.h
00001 //;2648 ;00 00000 0;168 ;001; 10043 c1;31 10;00 00;00 00;10 63;08 39;I; -012; 0002; 0104;\r\n 00002 //;109 ;00 00000 0;123 ;011; 10008 e4;27 95;00 00;00 00;13 99;08 51;G; -068; -096; -192; 00003 #ifndef CADENCE_H 00004 #define CADENCE_H 00005 00006 #include "mbed.h" 00007 #include <string> 00008 DigitalOut led3(LED3); 00009 Timer cadenceT; 00010 RawSerial pc(USBTX,USBRX); 00011 class Cadence : public RawSerial 00012 { 00013 private: 00014 static const int DATAS_NUM = 75,BuffNum = 500; 00015 00016 protected: 00017 00018 public: 00019 char strV[5]; 00020 char strC[5]; 00021 char data[DATAS_NUM],myBuff[BuffNum],c; 00022 string strData; 00023 int dataCount, data_num, strlength; 00024 double cadence, voltage; 00025 Cadence(PinName tx, PinName rx, const char* name = NULL) : RawSerial(tx, rx) { 00026 for(int i=0; i<DATAS_NUM; i++) data[i]= '\0'; 00027 for(int i = 0; i<BuffNum; i++) myBuff[i] = '\0'; 00028 data_num=0; 00029 dataCount=0; 00030 baud(115200); 00031 cadence=0; 00032 voltage=0; 00033 strcpy(strC,"0000"); 00034 strcpy(strV,"0000"); 00035 cadenceT.start(); 00036 attach(this, &Cadence::readData, RawSerial::RxIrq); 00037 } 00038 int checkInt(const char c[]) { 00039 for(int i = 0; i<strlen(c); i++) { 00040 if( c[0] == '-' ) continue; 00041 if( c[i] - '0' > 9 || c[i] - '0' <0 ) return -1; 00042 } 00043 return 1; 00044 } 00045 void readData() { 00046 if(readable()) { 00047 led3 = 1; 00048 cadenceT.reset(); 00049 cadenceT.start(); 00050 dataCount = 0; 00051 do { 00052 myBuff[dataCount] = getc(); 00053 dataCount++; 00054 } while(cadenceT.read_ms()<10 && myBuff[dataCount-1] != '\n'); 00055 if(dataCount < DATAS_NUM-4) return; 00056 } 00057 if(dataCount != 0) { 00058 // led3 = 1;//Ticker内でTimer&stringを使うとフリーズ 00059 // strData = "hello"; 00060 // pc.printf("%s\n\r",strData.c_str()); 00061 // for(int i = 0; i<strlen(myBuff); i++) { 00062 // pc.printf("mybuff[%d]:%c\n\r",i,myBuff[i]); 00063 // strData += myBuff[i]; 00064 // led3 = 0; 00065 // } 00066 // strData = string(myBuff,DATAS_NUM); 00067 strlength = strlen(myBuff)/*strData.length()*/; 00068 if( strlength > DATAS_NUM-4 && strlength <= DATAS_NUM ) { 00069 for(int i = 0; i<4; i++){ 00070 strC[i] = myBuff[strlength-7+i]; 00071 strV[i] = myBuff[strlength-44+i]; 00072 } 00073 if( checkInt(strC) ) { 00074 sscanf(strC,"%lf",&cadence); 00075 cadence /= 6.0; 00076 } 00077 if( checkInt(strV) ) { 00078 sscanf(strV,"%lf",&voltage); 00079 voltage *= 0.001; 00080 } 00081 // switch (strlength) { 00082 // case DATAS_NUM-3 : 00083 // strData.erase(0,2); 00084 // break; 00085 // case DATAS_NUM-2 : 00086 // strData.erase(0,3); 00087 // break; 00088 // case DATAS_NUM-1 : 00089 // strData.erase(0,4); 00090 // break; 00091 // case DATAS_NUM : 00092 // strData.erase(0,5); 00093 // } 00094 // strV = strData.substr(27,4); 00095 // strC = strData.substr(64,4); 00096 // if( checkInt( strV.c_str() ) ) sscanf(strV.c_str(),"%lf",&voltage); 00097 // if( checkInt( strC.c_str() ) ) sscanf(strC.c_str(),"%lf",&cadence); 00098 00099 // pc.printf("%d\n\r",strlen(strV)); 00100 }//strlength 00101 pc.printf("\n\rmyBuff:%s(%d)\n\r",myBuff,strlength); 00102 pc.printf("strC:%s\n\r",strC); 00103 for (int i = 0; i < strlength; i++) myBuff[i] = '\0'; 00104 dataCount = 0; 00105 }//if(dataCount != 0) 00106 led3 = 0; 00107 }//readData 00108 }; 00109 #endif
Generated on Wed Jul 13 2022 17:48:43 by
1.7.2
