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

Dependencies:   mbed SDFileSystem_

Revision:
8:7f80139df48d
Parent:
7:b69fa9bb320d
Child:
11:2d5fcf102778
--- a/main.cpp	Thu Aug 19 08:38:59 2021 +0000
+++ b/main.cpp	Thu Aug 26 00:43:06 2021 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
-#include "time.h"
+#include <time.h>
+//#include "mbed_rtc_time.h"
+
 /*
    This basic example just shows how to read the ADC internal channels raw values.
    Please look in the corresponding device reference manual for a complete
@@ -32,110 +34,173 @@
 
 H3LIS331DL  h3dacc(PB_7,PB_6);
 
-
 RawSerial         pc(PA_9, PA_10,115200); //console UART
-LowPowerTicker    interrput;
+//LowPowerTicker    interrput;
+Ticker    interrupt;
 SPI               STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk
 DigitalOut        STSPICS(PB_12);
 
 SDFileSystem *sd = new SDFileSystem(PA_12, PA_11, PB_3, PA_15, "sd"); // mosi, miso, sclk, cs, name
 
 DigitalOut        led(PB_5);
+//int gettimeofday(struct timeval *tv, void *tz);
 int initLIS3DH();
 int read3axes(short *tx,short *ty,short *tz);
 //int readTemp(short *tmp);
-FILE     *fp;
+FILE     *fp1,*fp2,*fp;//fp1:LIS3DH, fp2:H3LIS331DL
+int timecount = 0;
+//static uint8_t buffer[512] = {};
+char buffer1[512] = {};
+char buffer2[512] = {};
+char buffer[512] = {};
+char stracc[32] = {};
+char stracc1[32] = {};
+char stracc2[32] = {};
+bool write1 = false,write2 = false,writeflag = false;
+
+void timer()
+{
+    short x=0,y=0,z=0;
+    float lis3dh_acc = 0;
+    double xyz[3]={},h3lis331dl_acc=0;
+    //char stracc[32] = {};
+
+ //H3LIS331DL
+    h3dacc.getAcceleration(xyz);
+    pc.printf("x:%lf,y:%lf,z:%lf\r\n",xyz[0],xyz[1],xyz[2]); //!!!If you read here, you cannot get acceleration!!!
+    //h3lis331dl_acc = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2]);
+    //sprintf(stracc2,"%ld,%lf\r\n",time(NULL),h3lis331dl_acc);
+    //pc.printf("%s",stracc2);
+    //if(strlen(buffer)+strlen(stracc2) > 512){
+        //pc.printf("buffer over. write file2\r\n");
+        //writeflag = true;
+    //}
+    //else{
+        //sprintf(buffer,"%s%s",buffer,stracc2);
+    //}
+    
+    
+    //if(timecount == 10){
+        //pc.printf("calc lis3dh acc\r\n");
+        //LIS3DH
+        /*
+        read3axes(&x,&y,&z);
+        lis3dh_acc = sqrt( (float)x/1024.0f * (float)x/1024 + (float)y/1024.0f * (float)y/1024 + (float)z/1024.0f * (float)z/1024);
+        sprintf(stracc1,"%ld,%lf\r\n",time(NULL),lis3dh_acc);
+        pc.printf("lis3dh:%s",stracc1);
+            //if(strlen(buffer1)+ strlen(stracc1)> 512){
+                if(strlen(buffer)+ strlen(stracc1)> 512){
+                //pc.printf("buffer1 over. write file1\r\n");
+                //write1 = true;
+                writeflag = true;
+            }
+            else{
+                sprintf(buffer1,"%s%s",buffer1,stracc1);
+            }
+            */
+        //timecount = 0;
+    //}
+    
+    //timecount++;
+}
 
 int main()
 {
-    short x,y,z,count=0;
-    float ox,oy,oz,oldacc = 0;
-    double xyz[3],newacc=0;
-    //int ret = 0;
-    char buf[512];
-    //char *retchar = NULL;
-    //time_t t;//24H = 86400s
+   
+    int lognum1 = 0, lognum2 = 0,lognum=0;
+    char filename[20] = {};
+    //char filename1[20]={};
+    //char filename2[20]={};
+    int ret = 0;
+    double abc[3]={};
  
     initLIS3DH();
  
     h3dacc.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G
     h3dacc.setHPFMode(H3LIS331DL_HPM_NORMAL_MODE_RES);//High Pass Filter ON
+    //h3dacc.setHPFCutOFF(H3LIS331DL_HPFCF_1);
  
     //pc.printf("\nSTM32 ADC internal channels reading example\r\n");
     //pc.printf("\nSTM32 3D acc sensor example\r\n");
     
-    fp = fopen("/sd/testlog.txt", "a");             
-         
-    if (fp == NULL)
-    {
-      //pc.printf("open error!!\r\n");
-
-    }     
-    else
-    {
-      //pc.printf("SD opened\r\n");
-      //fclose(fp);
-      wait_ms(10);
+    //sprintf(filename,"/sd/acclog_%d",lognum);
+    //sprintf(filename1,"/sd/lis3dh_%d",lognum1);
+    //sprintf(filename2,"/sd/h3lis331dl_%d",lognum2);
+    //sprintf(filename2,"/sd/h3lis_%d",lognum2);
+      
+      /*
+    fp = fopen(filename,"a");
+    if(!fp){
+        pc.printf("fp open failed:%s\r\n",filename);
     }
+    */
+ 
+ /*             
+    fp1 = fopen(filename1,"a");
+    if(!fp1){
+        pc.printf("fp1(%s) open failed\r\n",filename1);
+    }
+    fp2 = fopen(filename2,"a");
+    if(!fp2){
+        pc.printf("fp2(%s) open failed\r\n",filename2);
+    }
+    */
+     
+    interrupt.attach_us(&timer,200000);//200ms
     
     while(1) {
 //On board ACC sensor
-        x=y=z=0;
-        read3axes(&x,&y,&z);
-        //readTemp(&tmp);
-        //pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp);    
-        //t = time(NULL);
-        if(count == 5){
-            ox = (float)x/1024.0f;
-            oy = (float)y/1024.0f;
-            oz = (float)z/1024.0f;
-            ox = ox * ox;
-            oy = oy * oy;
-            oz = oz * oz;
-            oldacc = sqrt(ox + oy + oz);
-            
-            //pc.printf("oldacc %ld %f",time(NULL),sqrt(powf((float)x/1024.0f,2) + powf((float)y/1024.0f,2) + powf((float)z/1024.0f,2)));    
-            pc.printf("oldacc %ld, %f\r\n", time(NULL), oldacc);    
-            //pc.printf("oldacc %ld x=%f y=%f z=%f\r\n", time(NULL), (float)x/1024.0f,(float)y/1024.0f,(float)z/1024.0f);   
-            count = 0;
+        //pc.printf("write1:%d,write2:%d\r\n",write1,write2);
+        /*
+        if(write1){
+            pc.printf("write1\r\n");
+            ret = fprintf(fp1,"%s",buffer1);
+            //pc.printf("fp1 %d outputed\r\n",ret);
+            pc.printf("write buffer1:%d\r\n",ret);
+            memset(buffer1,0,sizeof(buffer1));
+            sprintf(buffer1,"%s",stracc1);
+            write1 = false;
         }
-// Extra ACC sensor        
-        //x=y=z=0;
-        //h3dacc.readXYZ(&x, &y, &z);
-        h3dacc.getAcceleration(xyz);
-        //t = time(NULL);
-        xyz[0] = xyz[0] * xyz[0];
-        xyz[1] = xyz[1] * xyz[1];
-        xyz[2] = xyz[2] * xyz[2];
-        newacc = xyz[0] + xyz[1] + xyz[2];
-        newacc = sqrt(newacc);
-        //pc.printf("newacc %ld x=%lf, y=%lf, z=%lf \r\n", time(NULL), xyz[0], xyz[1], xyz[2]);
-        pc.printf("newacc %ld, %lf\r\n", time(NULL), newacc);
-        //ret = fprintf(fp, "x=%lf, y=%lf, z=%lf \r\n", xyz[0], xyz[1], xyz[2]);
-        //pc.printf("fprintf ret:%d\r\n", ret);
-        /*
-        retchar = fgets(buf,64,fp);
-        if(!retchar){
-            if(fp){
-                fclose(fp);
-                fp = NULL;
-            }
+        //else{
+            //pc.printf("%d,%d\r\n",strlen(buffer1),timecount);
+        //}
+        */
+      
+// Extra ACC sensor 
+    //h3dacc.getAcceleration(abc); //If you read here, you can get acceleration
+    //pc.printf("new x:%lf,y:%lf,z:%lf\r\n",abc[0],abc[1],abc[2]);
+    /*
+        if(writeflag){
+            ret = fprintf(fp,"%s",buffer);
+            pc.printf("write buffer:%d\r\n",ret);
+            memset(buffer,0,sizeof(buffer));
+            sprintf(buffer,"%s",stracc2);
+            writeflag = false;
         }
         else{
-            pc.printf("%s\r\n",buf);
+            pc.printf("%d,%d\r\n",strlen(buffer),timecount);
+        }
+    */          
+        led = !led;
+        /*
+        if(time(NULL) > 86400){
+            //Create New File
+            set_time(NULL);
+            fclose(fp1);
+            fclose(fp2);
+            lognum1++;
+            lognum2++;
+            sprintf(filename1,"/sd/lis3dh_%d",lognum1);
+            sprintf(filename2,"/sd/h3lis331dl_%d",lognum2);
+            fp1 = fopen(filename1,"a");
+            if(!fp1){
+                pc.printf("fp1")
+                }
+            fp2 = fopen(filename2,"a");
         }
         */
-        
-        //pc.printf("High x=%d y=%d z=%d \r\n", x,y,z); 
-              
-        led = !led;
-        count++;
-        if(time(NULL) > 86400){
-            //Create New File
-            fclose(fp);
-            //fp = fopen();
-        }
-        wait(0.2);
+        wait_ms(200);
+        //wait(1);
     }
 }
 /*********** porting **************/   
@@ -154,4 +219,5 @@
 }   
 
 void error(const char* format, ...) {
+    return;
 }
\ No newline at end of file