I-O DATA DEV2 / Mbed 2 deprecated ud-gs4-R_400G_SD_Log_No1

Dependencies:   mbed SDFileSystem_

Revision:
37:1b459e0ef847
Parent:
36:709c6fae0b2e
Child:
38:87b7b3de6e88
--- a/main.cpp	Wed Sep 08 09:39:42 2021 +0000
+++ b/main.cpp	Fri Sep 10 04:35:22 2021 +0000
@@ -1,6 +1,9 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include <errno.h>
+#include <stdio.h>
+#include <FileHandle.h>
+#include <mbed_poll.h>
 
 /*
    This basic example just shows how to read the ADC internal channels raw values.
@@ -66,6 +69,9 @@
 SDFileSystem *sd = new SDFileSystem(PA_12, PA_11, PB_3, PA_15, "sd"); // mosi, miso, sclk, cs, name
 
 DigitalOut        led(PB_5);
+DigitalIn         button(PB_4);
+int               btn_flag = 0;
+
 int initLIS3DH();
 int read3axes(short *tx,short *ty,short *tz);
 int initLIS331();
@@ -142,8 +148,8 @@
     //平均(/s)
     if (newcount == 500) {
         float asc = (float)as / 500.0f;
-        //pc.printf("avarage(new) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
-            //(float)ax/1000.0f*0.005,(float)ay/1000.0f*0.005,(float)az/1000.0f*0.005,(float)as/1000.0f*0.005, (float)tmp_max*0.005);
+        pc.printf("avarage(new) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
+            (float)ax/500.0f*0.005,(float)ay/500.0f*0.005,(float)az/500.0f*0.005,(float)as/500.0f*0.005, (float)tmp_max*0.005);
         sprintf(stracc1,"%d,%d,%2.2f,%2.2f\r\n",timecount,time(NULL),asc*0.005,(float)tmp_max*0.005);
         
         if((strlen(buffer1)+strlen(stracc1)) > DATA_SIZE) {
@@ -153,9 +159,12 @@
             if(ret < strlen(buffer1)){
                 pc.printf("fp1 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
                 NVIC_SystemReset();
-            }    
+            }   
+            //ret = fflush(fp1);
+            //if(ret != 0){
+                //pc.printf("fp1 fflush failed:%d,%d\r\n",ret,errno);
+            //} 
             memset(buffer1,0,sizeof(buffer1));
-            //bufferIndex = buffer1;
             sprintf(buffer1,"%s",stracc1);
         } else {
             sprintf(buffer1,"%s%s",buffer1,stracc1);
@@ -183,8 +192,8 @@
         //pc.printf("lis3dh:%s",stracc2);
         if(oldcount == 5000){
             float asc2 = (float)as2 / 500.0f;
-            //pc.printf("avarage(old) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
-            //(float)ax2/100.0f*0.005,(float)ay2/100.0f*0.005,(float)az2/100.0f*0.005,(float)as2/100.0f*0.005, (float)tmp_max2*0.005);
+            pc.printf("avarage(old) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
+            (float)ax2/500.0f*0.005,(float)ay2/500.0f*0.005,(float)az2/500.0f*0.005,(float)as2/500.0f*0.005, (float)tmp_max2*0.005);
             sprintf(stracc2,"%d,%d,%2.2f,%2.2f\r\n",timecount,time(NULL),asc2*0.005,(float)tmp_max2*0.005);
             
             if( (strlen(buffer2)+ strlen(stracc2)) > DATA_SIZE) {
@@ -194,6 +203,10 @@
                     pc.printf("fp2 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
                     NVIC_SystemReset();
                 }
+                //ret = fflush(fp2);
+                //if(ret != 0){
+                    //pc.printf("fp2 fflush failed:%d,%d\r\n",ret,errno);
+                //} 
             
                 memset(buffer2,0,sizeof(buffer2));
                 sprintf(buffer2,"%s",stracc2);
@@ -237,6 +250,28 @@
             }
             pc.printf("Create New File:%d,%d",timecount,time(NULL));
         }
+        
+        //Button Check
+        if(button.read() == 1){
+            btn_flag++;
+            //pc.printf("Button pressed:%d\r\n",btn_flag);
+        }
+        else{
+            btn_flag = 0;
+        }
+        
+        if(btn_flag > 200){
+            //pc.printf("Button Pressed. Close fp\r\n");
+            if(fp1){
+                fclose(fp1);
+                fp1 = NULL;
+            }
+            if(fp2){
+                fclose(fp2);
+                fp2 = NULL;
+            }
+        }
+        
 }
 
 int main()
@@ -280,7 +315,6 @@
         wait_ms(100);
     }
     //interrupt.attach_us(&timer,20000);//20ms
-    //bufferIndex = buffer1;
     while(1)
     {
         timecount++;