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