回転数計のクラスです。

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tsumagari
Date:
Sat Feb 18 07:57:27 2017 +0000
Parent:
5:871c2d6c1129
Commit message:
RawSerial?attach

Changed in this revision

BufferedSoftSerial.lib Show diff for this revision Revisions of this file
Cadence.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/BufferedSoftSerial.lib	Sat Jan 28 01:07:59 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/Sissors/code/BufferedSoftSerial/#84516c88c817
--- a/Cadence.h	Sat Jan 28 01:07:59 2017 +0000
+++ b/Cadence.h	Sat Feb 18 07:57:27 2017 +0000
@@ -1,53 +1,77 @@
+//;2648 ;00 00000 0;168 ;001; 10043 c1;31 10;00 00;00 00;10 63;08 39;I; -012; 0002; 0104;\r\n
+//;109  ;00 00000 0;123 ;011; 10008 e4;27 95;00 00;00 00;13 99;08 51;G; -068; -096; -192;
 #ifndef CADENCE_H
 #define CADENCE_H
 
 #include "mbed.h"
-#include "BufferedSoftSerial.h"
 #include <string>
-
-
-class Cadence : public /*BufferedSoft*/RawSerial{
+DigitalOut led3(LED3);
+class Cadence : public RawSerial{
     private:
-    static const int DATAS_NUM = 69 + 4 + 1; //経過時間の文字数は最大4
-//;1816; 00 00000 0;168 ;001; 10133 e9;34 00;00 0e;00 00;10 70;06 89;G; -005; -009; -006;    //2+4+67+2(\r\n)
-//                                     -- --                                        ----
+    static const int DATAS_NUM = 75;
+    
     protected:
     
     public:
-    char strC[4],strV[4];
+    string strC,strV;
     char data[DATAS_NUM];
     string strData;
     int data_count, data_num;
-    double cadence, voltage, safeflag;
-    int cadence_i, voltage_i;
-    Cadence(PinName tx, PinName rx, const char* name = NULL) : /*BufferedSoft*/RawSerial(tx, rx, NULL){
+    double cadence, voltage;
+    Cadence(PinName tx, PinName rx, const char* name = NULL) : RawSerial(tx, rx){
         for(int i=0;i<DATAS_NUM;i++) data[i]= NULL;
+        data_num=0;
         data_count=0;
         baud(115200);
         cadence=0;
         voltage=0;
-        strC[0] = '0';
-        strV[0] = '0';
+    }
+    int checkInt(const char c[]){
+        for(int i = 0; i<strlen(c); i++){
+            if( c[0] == '-' ) continue;
+            if( c[i] - '0' > 9 || c[i] - '0' <0 ) return -1;
+        }return 1;
     }
-    void readData(){ //Ticker で定期的に呼び出して値を更新
-      if(readable()){ 
+    void readData(){
+      if(readable()){
         data_count = 0;
+        for (int i = 0; i<DATAS_NUM; i++) data[i] = NULL;
         do{
-            if(readable()){
+            if(readable()) {
                 data[data_count] = getc();
                 if(data[data_count] != '\n') data_count++;
+                led3 = !led3;
             }
-        }while( data[data_count-1] != '\r' && data_count < DATAS_NUM );
-        if( data_count > 71 ) {
-            for(int i = 0; i<4; i++){
-                strC[i] = data[data_count - 6 + i]; // 7 = 5 + 1
-                pc.putc(strC[i]);
-                strV[i] = data[data_count - 43 + i]; // 44 = 42 + 1
+        }while(data[data_count] != '\n' && data_count<DATAS_NUM);
+        strData = data;
+        if( int strlength = strData.length() > DATAS_NUM-4 ){
+//            for(int i = 0; i<4; i++){
+//                strC += data[data_count-6+i];
+//                strV += data[data_count-43+i];
+//            }
+//            if( checkInt(strC) ) sscanf(strC,"%lf",&cadence);
+//            if( checkInt(strV) ) sscanf(strV,"%lf",&voltage);
+
+            switch (strData.length()){
+                case DATAS_NUM-3 :
+                    strData.erase(0,2);
+                    break;
+                case DATAS_NUM-2 :
+                    strData.erase(0,3);
+                    break;
+                case DATAS_NUM-1 :
+                    strData.erase(0,4);
+                    break;
+                case DATAS_NUM :
+                    strData.erase(0,5);
             }
-            sscanf(strC,"%4d",&cadence_i);
-            sscanf(strV,"%4d",&voltage_i);
-            cadence = cadence_i/6.0;
-            voltage = voltage_i/1000.0;
+            strV = strData.substr(27,4);
+            strC = strData.substr(64,4);
+            if( checkInt( strV.c_str() ) ) sscanf(strV.c_str(),"%lf",&voltage);
+            if( checkInt( strC.c_str() ) ) sscanf(strC.c_str(),"%lf",&cadence);
+            
+            cadence /= 6.0;
+            voltage *= 0.001;
         }
       }
     }
--- a/main.cpp	Sat Jan 28 01:07:59 2017 +0000
+++ b/main.cpp	Sat Feb 18 07:57:27 2017 +0000
@@ -7,9 +7,13 @@
 #include "Cadence.h"
 Cadence ar(p13,p14);
 
+void for_attach(){
+    ar.readData();
+}
+
 int main() {
-//    ar.format(8, Serial::None, 1);
     ar.baud(115200);
+    ar.attach(for_attach);
     while(1) {
         pc.printf("data:");
         ar.readData();
@@ -17,12 +21,11 @@
         for(int i = 0; i<75; i++){
             if(ar.data[i] == '\r') {
                 pc.putc('!');
-                continue;
             }else if(ar.data[i] == '\n'){
                 pc.putc('$');
-                continue;
+            }else{
+                pc.printf("%c",ar.data[i]);
             }
-            pc.printf("%c",ar.data[i]);
         }
         pc.printf("test\n\r");
         pc.printf("cadence:%4.2f\n\r",ar.cadence);
@@ -32,6 +35,6 @@
         pc.printf("\n\rstrV:");
         for(int i=0; i<4; i++) pc.printf("%c",ar.strV[i]);
         pc.printf("\n\r");
-//        pc.printf("'%s'\n\r",ar.strC.c_str());
+        wait(0.4);
     }
 }
--- a/mbed.bld	Sat Jan 28 01:07:59 2017 +0000
+++ b/mbed.bld	Sat Feb 18 07:57:27 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/ef9c61f8c49f
\ No newline at end of file