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

Dependencies:   mbed SDFileSystem_

Revision:
11:2d5fcf102778
Parent:
8:7f80139df48d
Child:
13:df67ca499051
--- a/main.cpp	Thu Aug 26 01:29:38 2021 +0000
+++ b/main.cpp	Mon Aug 30 08:40:28 2021 +0000
@@ -1,7 +1,6 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include <time.h>
-//#include "mbed_rtc_time.h"
 
 /*
    This basic example just shows how to read the ADC internal channels raw values.
@@ -11,9 +10,9 @@
 
 AnalogIn adc_temp(ADC_TEMP);
 AnalogIn adc_vref(ADC_VREF);
-/* UD-GS2 H/W define 
- PIO_SWin    PB_4 
- PIO_wkup    PA_4 
+/* UD-GS2 H/W define
+ PIO_SWin    PB_4
+ PIO_wkup    PA_4
  PIO_enable  PB_0
  PIO_intout1 PB_2
  PIO_led     PB_5
@@ -24,7 +23,7 @@
  PIO_battryMonEn   PA_5
  PB_7 SDA
  PB_6 SCL
- 
+
 */
 //DigitalOut hx_clk(PB_7);
 //DigitalIn hx_dt(PB_6);
@@ -32,160 +31,144 @@
 
 #include "H3LIS331DL.h"
 
+#define TIMEINTERVAL
+
 H3LIS331DL  h3dacc(PB_7,PB_6);
 
 RawSerial         pc(PA_9, PA_10,115200); //console UART
-//LowPowerTicker    interrput;
-Ticker    interrupt;
+LowPowerTicker    interrupt;
+//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     *fp1,*fp2,*fp;//fp1:LIS3DH, fp2:H3LIS331DL
+FILE     *fp1,*fp2;//fp1:H3LIS331DL, fp2:LIS3DH
 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;
+//char buffer1[512] = {};
+//char buffer2[512] = {};
+//char stracc[32] = {};
+//bool write1 = false,write2 = false;
+//double xyz[3] = {};
 
-void timer()
+/*
+void timer(void)
 {
     short x=0,y=0,z=0;
     float lis3dh_acc = 0;
-    double xyz[3]={},h3lis331dl_acc=0;
+    double xyz[3]= {},h3lis331dl_acc=0;
     //char stracc[32] = {};
 
- //H3LIS331DL
+//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("x:%f,y:%f,z:%f\r\n",(float)xyz[0],(float)xyz[1],(float)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(stracc1,"%ld,%lf\r\n",timecount*200,h3lis331dl_acc);
+    pc.printf("h3lis331dl:%s",stracc1);
+    if(strlen(buffer1)+strlen(stracc1) > 512) {
         //pc.printf("buffer over. write file2\r\n");
-        //writeflag = true;
-    //}
-    //else{
-        //sprintf(buffer,"%s%s",buffer,stracc2);
-    //}
-    
-    
-    //if(timecount == 10){
+        write1 = true;
+    } else {
+        sprintf(buffer1,"%s%s",buffer1,stracc1);
+    }
+
+    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++;
+        sprintf(stracc2,"%ld,%lf\r\n",time(NULL),lis3dh_acc);
+        pc.printf("lis3dh:%s",stracc2);
+        if(strlen(buffer1)+ strlen(stracc1)> 512) {
+            write2 = true;
+        } else {
+            sprintf(buffer2,"%s%s",buffer2,stracc2);
+        }
+        timecount = 0;
+    }
+
+    timecount++;
 }
+*/
 
 int main()
 {
-   
-    int lognum1 = 0, lognum2 = 0,lognum=0;
-    char filename[20] = {};
-    //char filename1[20]={};
-    //char filename2[20]={};
+    int lognum1 = 0, lognum2 = 0;
+    char filename1[32]= {};
+    char filename2[32]= {};
     int ret = 0;
-    double abc[3]={};
- 
+    short x=0,y=0,z=0;
+    double xyz[3] = {}, h3lis331dl_acc = 0;
+    float lis3dh_acc = 0;
+    int count = 0;
+    char buffer1[512] = {};
+    char buffer2[512] = {};
+    char stracc1[32] = {}, stracc2[32] = {};
+
     initLIS3DH();
- 
-    h3dacc.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G
+
+    h3dacc.init(H3LIS331DL_ODR_50Hz, 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");
-    
-    //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);
-    }
-    */
- 
- /*             
+
+    sprintf(filename1,"/sd/lis3dh_%d",lognum1);
+    sprintf(filename2,"/sd/h3lis_%d",lognum2);
+
     fp1 = fopen(filename1,"a");
-    if(!fp1){
+    if(!fp1) {
         pc.printf("fp1(%s) open failed\r\n",filename1);
     }
     fp2 = fopen(filename2,"a");
-    if(!fp2){
+    if(!fp2) {
         pc.printf("fp2(%s) open failed\r\n",filename2);
     }
-    */
-     
-    interrupt.attach_us(&timer,200000);//200ms
-    
+
+    //interrupt.attach_us(&timer,200000);//200ms
+
     while(1) {
-//On board ACC sensor
-        //pc.printf("write1:%d,write2:%d\r\n",write1,write2);
-        /*
-        if(write1){
-            pc.printf("write1\r\n");
+        //H3LIS331DL
+        h3dacc.getAcceleration(xyz);
+        //pc.printf("x:%f,y:%f,z:%f\r\n",(float)xyz[0],(float)xyz[1],(float)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(stracc1,"%ld,%lf\r\n",count*20,h3lis331dl_acc);
+        //pc.printf("h3lis331dl:%s",stracc1);
+        if(strlen(buffer1)+strlen(stracc1) > 512) {
+            //pc.printf("buffer over. write file2\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;
+            //write1 = false;
+        
+        } else {
+            sprintf(buffer1,"%s%s",buffer1,stracc1);
         }
-        //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;
+        
+        if(count == 10) {
+        //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(stracc2,"%ld,%lf\r\n",timecount*20*10,lis3dh_acc);
+        //pc.printf("lis3dh:%s",stracc2);
+        if(strlen(buffer2)+ strlen(stracc2)> 512) {
+            ret = fprintf(fp2,"%s",buffer2);
+            //pc.printf("write buffer:%d\r\n",ret);
+            memset(buffer2,0,sizeof(buffer2));
+            sprintf(buffer2,"%s",stracc2);
+        } else {
+            sprintf(buffer2,"%s%s",buffer2,stracc2);
         }
-        else{
-            pc.printf("%d,%d\r\n",strlen(buffer),timecount);
-        }
-    */          
-        led = !led;
-        /*
+        count = 0;
+    }
+        
         if(time(NULL) > 86400){
             //Create New File
             set_time(NULL);
+            timecount = 0;
             fclose(fp1);
             fclose(fp2);
             lognum1++;
@@ -194,30 +177,40 @@
             sprintf(filename2,"/sd/h3lis331dl_%d",lognum2);
             fp1 = fopen(filename1,"a");
             if(!fp1){
-                pc.printf("fp1")
+                pc.printf("fp1 create file failed\r\n");
                 }
             fp2 = fopen(filename2,"a");
+            if(!fp2){
+                pc.printf("fp2 create file failed\r\n");
+            }
         }
-        */
-        wait_ms(200);
-        //wait(1);
+        
+        led = !led;
+        timecount++;
+        count++;
+        wait_ms(20);
     }
 }
-/*********** porting **************/   
+/*********** porting **************/
 
-void spiFormat(int b,int m) {
-   STSPI.format(b,m);  /* 8bit */
+void spiFormat(int b,int m)
+{
+    STSPI.format(b,m);  /* 8bit */
 }
-void spiFrequency(int f){
+void spiFrequency(int f)
+{
     STSPI.frequency(f); /* 1Mbps */
 }
-void spiWriteCS(int cs) {
+void spiWriteCS(int cs)
+{
     STSPICS=cs;
 }
-int spiWrite(int wd) {
-    return ( STSPI.write(wd)); 
-}   
+int spiWrite(int wd)
+{
+    return ( STSPI.write(wd));
+}
 
-void error(const char* format, ...) {
+void error(const char* format, ...)
+{
     return;
 }
\ No newline at end of file