med ble switch science

Dependencies:   BLE_API SDFileSystem mbed-rtos mbed nRF51822

Revision:
1:fa329dd713be
Parent:
0:1ad445491c35
Child:
2:592364ca5fb2
--- a/main.cpp	Mon Oct 24 04:43:07 2016 +0000
+++ b/main.cpp	Mon Oct 24 11:01:50 2016 +0000
@@ -8,14 +8,20 @@
 #include "SDFileSystem.h"
 #define n 10//addressを保存する個数
 #define m 12//addressとtimeを入れる
+#define GET_TIME 10000
+#define SEND_TIME 20000
+#define AA 0xAA
+
+//DigitalOut myled3(LED3); 
+//DigitalOut myled2(LED2);
+
 Semaphore one_slot(1);
  
 BLE  ble;
 char address[n][m]={};//保存する配列を初期化
 const GapScanningParams scanningParams;
 int wt;//threadの時間
-int wt1=0;
-int d=0;
+int wt1=0;//central mode の時間
 
 SDFileSystem sd(p25, p28, p29, p21, "sd"); // the pinout on the mbed Cool Components workshop board
 FILE *fp;
@@ -42,37 +48,27 @@
          }
         time_t seconds = time(NULL);
         struct tm *t = localtime(&seconds);
-        address[counter][6]=t->tm_year;
-        address[counter][7]=t->tm_mon;
-        address[counter][8]= t->tm_mday;
+        address[counter][6]= t->tm_year - 70;
+        address[counter][7]= t->tm_mon;
+        address[counter][8]= t->tm_mday - 1;
         address[counter][9]= t->tm_hour;
         address[counter][10]= t->tm_min;
         address[counter][11]= t->tm_sec;
     
-//ファイルをオープン
-        //fp = fopen("/sd/test.txt", "a");
-
-        fprintf(fp,"DEV:");
-        printf("DEV:");
-        for(b=0; b<6 ; b++)fprintf(fp,"%02x ", address[counter][b]);
-        for(b=0; b<6 ; b++)printf("%02x ", address[counter][b]);
-        fprintf(fp,"%04d/%02d/%02d %02d:%02d:%02d \r\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
-        printf("%04d/%02d/%02d %02d:%02d:%02d \r\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
-        printf("written\n\r");
-
-        //printf("Ok");
-        //fflush(stdin);
-        //fflush(stdout);
-        //fflush(fp);
-
+        //fprintf(fp,"DEV:");
+        //printf("DEV:");
+        for(b=0; b<6 ; b++){
+            fprintf(fp,"%02x", address[counter][b]);
+            printf("%02x", address[counter][b]);
+        }    
+        fprintf(fp,",%04d,%02d,%02d,%02d,%02d,%02d\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
+        printf(",%04d/%02d/%02d %02d:%02d:%02d\r\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
+        counter++;
 /*      
         printf("DEV:");
         for(b=0; b<6 ; b++)printf("%02x ", address[counter][b]);   
         printf("%04d/%02d/%02d %02d:%02d:%02d \r\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
 */
-        
-        counter++;
-
 
 /*配列の中身をすべて表示     
         for(a=0; a<counter; a++){
@@ -94,39 +90,38 @@
     while (true) {
         Timer timer;
         one_slot.wait();
-        //printf("%s\n\r", (const char*)name);
         
         if(!strcmp((const char*)name, "1")){
-            printf("**this is startAdvertising thread**\n\r");
-            wt=1000;
+            printf("**startAdvertising thread**\n\r");
+            wt=100;
             ble.gap().stopAdvertising();
             ble.startScan(&onScanCallback);
         }
-
-
 //2        
         if(!strcmp((const char*)name, "2")){
-            printf("**this is advertising thread**\n\r");        
-             wt=1000;
-             printf("%d秒間の送信\n\r",wt/1000);
+            printf("**send thread**\n\r"); 
+            //myled3 = 1;
+             wt=SEND_TIME;
+             //printf("%d秒間の送信\n\r",wt/1000);
             ble.gap().startAdvertising();//BLEの送信
         }
         
 //3        
         if(!strcmp((const char*)name, "3")){
-            //memset(address, 0, sizeof(address));//配列の初期化
-            printf("**this is central thread**\n\r");
+            memset(address, 0, sizeof(address));//配列の初期化
+            printf("**get thread**\n\r");
+            //myled3 = 0;
             wt=0;//central modeの際はthreadの時間は0sec
-            wt1=10000;//centralmode の時間はこっち
+            wt1=GET_TIME;//centralmode の時間はこっち
+            fp = fopen("/sd/test.csv", "a");
             
-            fp = fopen("/sd/test.txt", "a");
-            
-            printf("%d秒間の受信\n\r",wt1/1000);
+            //printf("%d秒間の受信\n\r",wt1/1000);
             timer.start();
             while(1){
+                //myled3 = 1; 
                 ble.waitForEvent();
-                //printf("%d\n\r",timer.read());
                 if(timer.read() > wt1/1000){
+                        //myled3 = 0;
                         timer.stop();
                         timer.reset();
                         ble.stopScan();
@@ -134,11 +129,10 @@
                         break;
                 }
             }
-        }
-        
+        }      
         Thread::wait(wt);
         one_slot.release();
-    }
+    }        
 }
  
 // const static char     DEVICE_NAME[]        = "BLE1";
@@ -151,7 +145,7 @@
     if (error != BLE_ERROR_NONE) {
         return;
     }
-    const uint8_t address1[] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
+    const uint8_t address1[] = {AA,AA,AA,AA,AA,AA};
     ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1);
 //  ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
 //   ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -159,16 +153,11 @@
 }
  
 int main (void) {
-    //ble.init();
     ble.init(bleInitComplete);
-    ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,
-                      GapScanningParams::SCAN_WINDOW_MAX,
-                      0);
-
+    ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,GapScanningParams::SCAN_WINDOW_MAX,0);
 
     Thread t2(test_thread, (void *)"2");
     Thread t3(test_thread, (void *)"3");
     
     test_thread((void *)"1");
-
 }
\ No newline at end of file