Shuta Nakamae / Mbed 2 deprecated 01

Dependencies:   ADXL345_I2C BLE_API SDFileSystem mbed-rtos mbed nRF51822

Fork of 1__ by Shuta Nakamae

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include "ble/services/HeartRateService.h"
00004 #include "ble/services/BatteryService.h"
00005 #include "ble/services/DeviceInformationService.h"
00006 #include "rtos.h"
00007 #include "ADXL345_I2C.h"
00008 
00009 #include <string.h>
00010 #include "SDFileSystem.h"
00011 #define N 100//addressを保存する個数
00012 #define M 6//addressとtimeを入れる
00013 #define GET_TIME 10000
00014 #define SEND_TIME 20000
00015 #define AD 0xAA
00016 
00017 DigitalOut myled3(LED3); 
00018 Semaphore one_slot(1);
00019 Timer timer;
00020 BLE  ble;
00021 char address[N][M]={};//保存する配列を初期化
00022 const GapScanningParams scanningParams;
00023 int wt;//threadの時間
00024 int wt1=0;//central mode の時間
00025 int ran;
00026 int countfp=0;
00027 char id[] = "/sd/aaaaaaaaaaaa.csv"; 
00028 char acc[] = "/sd/aaaaaaaaaaaa_acc.csv";
00029  
00030  //加速度センサー
00031 ADXL345_I2C accelerometer(p7, p30);
00032 SDFileSystem sd(p25, p28, p29, p21, "sd"); // the pinout on the mbed Cool Components workshop board
00033 FILE *fp, *kp;
00034 int counter=0,i,b,flag;
00035 int readings[3] = {0, 0, 0}; 
00036  
00037 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params){
00038     flag=0;//重複確認のためのフラグ
00039     for(i=0;i<counter;i++){
00040         if(address[i][0] == params->peerAddr[0]){
00041             flag=1;
00042             break;
00043         }
00044     }       
00045     if(flag==0){
00046         for(i=0; i<6; i++){
00047             address[counter][i]=params->peerAddr[i];
00048          }
00049         time_t seconds = time(NULL);
00050         
00051         char buff[10]={};
00052         sprintf(buff, "%d", seconds);
00053         strcat(address[counter],buff);
00054 
00055         for(b=0; b<6 ; b++){
00056                 fprintf(fp,"%02x", address[counter][b]);
00057                 //printf("%02x", address[counter][b]);
00058         }
00059         fprintf(fp, ",%d\r\n",seconds);
00060         //printf("    %d\r\n",seconds);
00061         counter++; 
00062       
00063 /*        printf("DEV:");
00064         for(b=0; b<6 ; b++)printf("%02x ", address[counter][b]);   
00065         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]);*/
00066 
00067  
00068     }  
00069 }      
00070  
00071 void test_thread(void const *name) {    
00072     
00073     while (true) {
00074         //Timer timer;
00075         one_slot.wait();
00076         
00077         if(!strcmp((const char*)name, "1")){
00078             //printf("**startAdvertising thread**\n\r");
00079             wt=1000;
00080             ble.gap().stopAdvertising();
00081             ble.startScan(&onScanCallback);
00082         }
00083 //2        
00084         if(!strcmp((const char*)name, "2")){
00085             memset(address, 0, sizeof(address));//配列の初期化
00086             counter = 0;
00087             //printf("**get**\n\r");
00088             myled3 = 0;
00089             wt=0;//central modeの際はthreadの時間は0sec
00090             wt1 = GET_TIME ;//centralmode の時間はこっち
00091             
00092             fp(countfp == 0){
00093                 fp = fopen( id , "w");
00094                 fclose(fp);
00095                 kp = fopen( acc , "w");
00096                 fclose(kp);
00097                 countfp = 1;
00098             }
00099             
00100             fp = fopen( id , "a");
00101             kp = fopen( acc , "a");
00102             
00103             //printf("%d seconds receive\n\r",wt1/1000);
00104             timer.start();
00105             
00106             while(1){ 
00107                 myled3 = !myled3;       
00108                 time_t seconds = time(NULL);
00109                 accelerometer.getOutput(readings);
00110                 //13-bit, sign extended values.
00111             //    printf("%f, %f, %f",(int16_t)readings[0]*(9.8/256), (int16_t)readings[1]*(9.8/256), (int16_t)readings[2]*(9.8/256));
00112             //    printf(":    %d\r\n",seconds);
00113                 fprintf(kp,"%f,%f,%f",(int16_t)readings[0]*(9.8/256), (int16_t)readings[1]*(9.8/256), (int16_t)readings[2]*(9.8/256));
00114                 fprintf(kp, ",%d\n",seconds);
00115                     
00116                 ble.waitForEvent();
00117                 if(timer.read() > wt1/1000){
00118                         timer.stop();
00119                         timer.reset();
00120                         ble.stopScan();
00121                         fclose(fp);
00122                         fclose(kp);
00123                         
00124                         break;
00125                 }
00126             }
00127         
00128         }
00129         
00130 //3        
00131         if(!strcmp((const char*)name, "3")){
00132             //printf("**send**\n\r"); 
00133             ran = rand() % 10;
00134             wt= 0;
00135             wt1 = SEND_TIME - ran*1000;
00136             //printf("%d seconds sendin\n\r",wt1/1000);
00137             // ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */
00138             ble.gap().startAdvertising();//BLEの送信
00139 
00140             timer.start();
00141             while(1){ 
00142                 myled3 = !myled3;       
00143                 time_t seconds = time(NULL);
00144                 accelerometer.getOutput(readings);
00145                 //13-bit, sign extended values.
00146                 printf("%f, %f, %f",(int16_t)readings[0]*(9.8/256), (int16_t)readings[1]*(9.8/256), (int16_t)readings[2]*(9.8/256));
00147                 printf(":    %d\r\n",seconds);
00148                 fprintf(kp,"%f,%f,%f",(int16_t)readings[0]*(9.8/256), (int16_t)readings[1]*(9.8/256), (int16_t)readings[2]*(9.8/256));
00149                 fprintf(kp, ",%d\n",seconds);
00150         
00151                 
00152                 if(timer.read() > wt1/1000){
00153                         timer.stop();
00154                         timer.reset();
00155                         fclose(kp);
00156                         break;
00157                 }
00158             }
00159 
00160         }      
00161         Thread::wait(wt);
00162         one_slot.release();
00163     }        
00164 }
00165  
00166 // const static char     DEVICE_NAME[]        = "BLE1";
00167     
00168 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00169 {
00170     BLE &ble= params->ble;
00171     ble_error_t error = params->error;
00172  
00173     if (error != BLE_ERROR_NONE) {
00174         return;
00175     }
00176     const uint8_t address1[] = {AD,AD,AD,AD,AD,AD};
00177     ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1);
00178 /*  ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00179   ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00180   ble.gap().setAdvertisingInterval(1000);*/
00181 }
00182  
00183  
00184 int main (void) {
00185 //------------------------------------------------------
00186 //加速度センサーの初期化
00187 
00188     //Go into standby mode to configure the device.
00189     accelerometer.setPowerControl(0x00);
00190     //Full resolution, +/-16g, 4mg/LSB.
00191     accelerometer.setDataFormatControl(0x0B);
00192     //3.2kHz data rate.
00193     accelerometer.setDataRate(ADXL345_3200HZ);
00194     //Measurement mode.
00195     accelerometer.setPowerControl(0x08);
00196 //-------------------------------------------------------
00197     
00198     ble.init(bleInitComplete);
00199     ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,GapScanningParams::SCAN_WINDOW_MAX,0);
00200  
00201     Thread t2(test_thread, (void *)"2");
00202     Thread t3(test_thread, (void *)"3");
00203     
00204     test_thread((void *)"1");
00205 }