PNI / Mbed 2 deprecated VSG_DataLogger1_VariableSpeed

Dependencies:   SDFileSystemVSG mbed

Fork of VSG_DataLogger1 by PNI

Revision:
5:e4b1de08bb87
Parent:
4:ea7c4356f84c
Child:
6:bd675690d831
--- a/main.cpp	Fri Aug 10 18:48:19 2018 +0000
+++ b/main.cpp	Mon Aug 13 21:19:30 2018 +0000
@@ -6,7 +6,7 @@
 #define SAMPLE_RATE     (4)    //Hz
 #define SAMPLE_RATE_HIGH (240)    //Hz
 #define TMRC_VLOW       (0x99)    // TMRC hex value corresponding to reading/rate
-#define TMRC_VHIGH      (0x94)    // TMRC hex value corresponding to reading/rate
+#define TMRC_VHIGH      (0x93)    // TMRC hex value corresponding to reading/rate
 #define SAMPLE_PERIOD   (60)      // seconds
 #define SAMPLE_DELAY    (5)      // seconds -to allow time to seal unit and place in the field
 #define CYCLE_COUNT     (50)
@@ -91,9 +91,11 @@
  
     int response;
     unsigned int count = 0;
-    char haveMeasurements = 0;
     char SDOK;
-    int X[1],Y[1],Z[1];
+    char disp_buffer[128];
+    char disp_char[32];
+    int dbuf_size = 0;
+    int X,Y,Z;
     mag.lock();
 //    magTimer.attach(&sampletimer,SAMPLE_INTERVAL);
     
@@ -196,6 +198,10 @@
     response = mag.write(CMM_REG);     // Mag single measurement Register
     response = mag.write(ALL_AXES+START_BIT);     // start single measurement - all Axes
     sel1a = 1;
+    //Now go read the STM internal channel selected above
+    HAL_ADC_ConfigChannel(&hadc1, &sConfig);
+    HAL_ADC_Start(&hadc1); // Start conversion
+    
 
     while(count < MAX_COUNT) {
         if (islow > 0) {
@@ -228,79 +234,66 @@
         }*/
         takeAsample_F = 0;
 
-        //Now go read the STM internal channel selected above
-        HAL_ADC_ConfigChannel(&hadc1, &sConfig);
-        HAL_ADC_Start(&hadc1); // Start conversion
-    
         // Wait end of conversion and get value
         //if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
         uint16_t value = HAL_ADC_GetValue(&hadc1); //}
+        //Now go read the STM internal channel selected above
+        HAL_ADC_Start(&hadc1); // Start conversion
+    
+        
 
         sel1a = 0;
             response = mag.write(MX_REG);     // Read back mag data
             response = mag.write(0); 
-            X[0] = response << 16;
+            X = response << 16;
             response = mag.write(0); 
-            X[0] += (response << 8);
+            X += (response << 8);
             response = mag.write(0); 
-            X[0] += response;
+            X += response;
             response = mag.write(0); 
-            Y[0] = response << 16;
-            response = mag.write(0); 
-            Y[0] += (response << 8);
+            Y = response << 16;
             response = mag.write(0); 
-            Y[0] += response;
+            Y += (response << 8);
             response = mag.write(0); 
-            Z[0] = response << 16;
+            Y += response;
             response = mag.write(0); 
-            Z[0] += (response << 8);
+            Z = response << 16;
             response = mag.write(0); 
-            Z[0] += response;
+            Z += (response << 8);
+            response = mag.write(0); 
+            Z += response;
         sel1a = 1;
-        haveMeasurements = 1;
 
         // send/save data after receiving it
-        if (haveMeasurements) {       
-                
-                if (SDOK) {
-                    fprintf(sd,"%d,",count);
-                    fprintf(sd,"%0.3f,",t.read()); //-tprev
-                    fprintf(sd,"%i,   ",value);
-                }
-                else {
-                    printf("%d,",count);
-                    printf("%0.3f,",t.read()); //-tprev
-                    printf("%i,   ",value);
-                }
-            tprev = t.read();
-            // sign adjust and publish data
-            for (char i = 0; i<1; i++) {
-                // 24 bit sign adjust
-                if (X[i] > (0x07FFFFF)) X[i] -= 0x1000000;
-                if (Y[i] > (0x07FFFFF)) Y[i] -= 0x1000000;
-                if (Z[i] > (0x07FFFFF)) Z[i] -= 0x1000000;
-
-                if (SDOK) {
-                    fprintf(sd,"%d,%d,%d",X[i],Y[i],Z[i]);
-                }
-                else printf("%d,%d,%d",X[i],Y[i],Z[i]);
+        if (X > (0x07FFFFF)) X -= 0x1000000;
+        if (Y > (0x07FFFFF)) Y -= 0x1000000;
+        if (Z > (0x07FFFFF)) Z -= 0x1000000;
+        
+        
+        if (SDOK) {
+            sprintf(disp_char,"%d,%0.3f,%i, %d,%d,%d\r\n",count,t.read(),value,X,Y,Z);
+        } 
+        else {
+            sprintf(disp_char,"%d,%0.3f,%i, %d,%d,%d\r\n",count,t.read(),value,X,Y,Z);
+        }
         
-                if(i<0) {
-                    if (SDOK) {
-                        fprintf(sd,",");   // more data comming
-                    }
-                    else printf(",  ");
-                }
-                else {
-                    if (SDOK) {
-                        fprintf(sd,"\n");  // last set of data
-                    }
-                    else printf("\r\n");
-                }            
+        if (nhigh == 0) {
+            strcpy(disp_buffer,disp_char);
+        }
+        else {
+            strcat(disp_buffer,disp_char);
+        }
+        
+        if (nhigh > 2) {
+            if (debugPin) { //SDOK
+                printf("%s",disp_buffer);
             }
-        count++;
+            else {
+                fprintf(sd,"%s",disp_buffer);
+                //printf("%s",disp_buffer);
+            }
         }
-     
+        count++;
         LED = !LED;
         
     } // end of while(1)