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