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 zzzzzzzzzzzzzzzzzzzzzz 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 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.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ 00133 ble.gap().startAdvertising();//BLEの送信 00134 00135 00136 timer.start(); 00137 while(1){ 00138 myled3 = !myled3; 00139 time_t seconds = time(NULL); 00140 accelerometer.getOutput(readings); 00141 //13-bit, sign extended values. 00142 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)); 00143 printf(": %d\r\n",seconds); 00144 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)); 00145 fprintf(kp, ",%d\n",seconds); 00146 00147 00148 if(timer.read() > wt1/1000){ 00149 timer.stop(); 00150 timer.reset(); 00151 fclose(kp); 00152 break; 00153 } 00154 } 00155 00156 } 00157 Thread::wait(wt); 00158 one_slot.release(); 00159 } 00160 } 00161 00162 // const static char DEVICE_NAME[] = "BLE1"; 00163 00164 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00165 { 00166 BLE &ble= params->ble; 00167 ble_error_t error = params->error; 00168 00169 if (error != BLE_ERROR_NONE) { 00170 return; 00171 } 00172 const uint8_t address1[] = {AA,AA,AA,AA,AA,AA}; 00173 ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1); 00174 /* ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00175 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00176 ble.gap().setAdvertisingInterval(1000);*/ 00177 } 00178 00179 00180 00181 00182 int main (void) { 00183 00184 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 }
Generated on Thu Jul 14 2022 08:17:45 by
1.7.2
