aa

Dependencies:   ADXL345_I2C BLE_API SDFileSystem mbed-rtos mbed nRF51822

Fork of 1__ by Shuta Nakamae

Committer:
nakamae
Date:
Tue Jan 10 11:23:52 2017 +0000
Revision:
2:e103492f1a0b
Parent:
1:005a4d3ec64b
pp;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nakamae 0:07880269e91a 1 #include "mbed.h"
nakamae 0:07880269e91a 2 #include "ble/BLE.h"
nakamae 0:07880269e91a 3 #include "ble/services/HeartRateService.h"
nakamae 0:07880269e91a 4 #include "ble/services/BatteryService.h"
nakamae 0:07880269e91a 5 #include "ble/services/DeviceInformationService.h"
nakamae 0:07880269e91a 6 #include "rtos.h"
nakamae 0:07880269e91a 7 #include "ADXL345_I2C.h"
nakamae 0:07880269e91a 8
nakamae 0:07880269e91a 9 #include <string.h>
nakamae 0:07880269e91a 10 #include "SDFileSystem.h"
nakamae 0:07880269e91a 11 #define N 100//addressを保存する個数
nakamae 0:07880269e91a 12 #define M 6//addressとtimeを入れる
nakamae 0:07880269e91a 13 #define GET_TIME 10000
nakamae 0:07880269e91a 14 #define SEND_TIME 20000
nakamae 2:e103492f1a0b 15 #define AD 0xAA
nakamae 2:e103492f1a0b 16
nakamae 0:07880269e91a 17 DigitalOut myled3(LED3);
nakamae 0:07880269e91a 18 Semaphore one_slot(1);
nakamae 0:07880269e91a 19 Timer timer;
nakamae 0:07880269e91a 20 BLE ble;
nakamae 0:07880269e91a 21 char address[N][M]={};//保存する配列を初期化
nakamae 0:07880269e91a 22 const GapScanningParams scanningParams;
nakamae 0:07880269e91a 23 int wt;//threadの時間
nakamae 0:07880269e91a 24 int wt1=0;//central mode の時間
nakamae 0:07880269e91a 25 int ran;
nakamae 2:e103492f1a0b 26 int countfp=0;
nakamae 2:e103492f1a0b 27 char id[] = "/sd/aaaaaaaaaaaa.csv";
nakamae 2:e103492f1a0b 28 char acc[] = "/sd/aaaaaaaaaaaa_acc.csv";
nakamae 0:07880269e91a 29
nakamae 0:07880269e91a 30 //加速度センサー
nakamae 0:07880269e91a 31 ADXL345_I2C accelerometer(p7, p30);
nakamae 0:07880269e91a 32 SDFileSystem sd(p25, p28, p29, p21, "sd"); // the pinout on the mbed Cool Components workshop board
nakamae 0:07880269e91a 33 FILE *fp, *kp;
nakamae 0:07880269e91a 34 int counter=0,i,b,flag;
nakamae 0:07880269e91a 35 int readings[3] = {0, 0, 0};
nakamae 0:07880269e91a 36
nakamae 0:07880269e91a 37 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params){
nakamae 0:07880269e91a 38 flag=0;//重複確認のためのフラグ
nakamae 0:07880269e91a 39 for(i=0;i<counter;i++){
nakamae 0:07880269e91a 40 if(address[i][0] == params->peerAddr[0]){
nakamae 0:07880269e91a 41 flag=1;
nakamae 0:07880269e91a 42 break;
nakamae 0:07880269e91a 43 }
nakamae 0:07880269e91a 44 }
nakamae 0:07880269e91a 45 if(flag==0){
nakamae 0:07880269e91a 46 for(i=0; i<6; i++){
nakamae 0:07880269e91a 47 address[counter][i]=params->peerAddr[i];
nakamae 0:07880269e91a 48 }
nakamae 0:07880269e91a 49 time_t seconds = time(NULL);
nakamae 0:07880269e91a 50
nakamae 0:07880269e91a 51 char buff[10]={};
nakamae 0:07880269e91a 52 sprintf(buff, "%d", seconds);
nakamae 0:07880269e91a 53 strcat(address[counter],buff);
nakamae 0:07880269e91a 54
nakamae 0:07880269e91a 55 for(b=0; b<6 ; b++){
nakamae 0:07880269e91a 56 fprintf(fp,"%02x", address[counter][b]);
nakamae 2:e103492f1a0b 57 //printf("%02x", address[counter][b]);
nakamae 0:07880269e91a 58 }
nakamae 0:07880269e91a 59 fprintf(fp, ",%d\r\n",seconds);
nakamae 2:e103492f1a0b 60 //printf(" %d\r\n",seconds);
nakamae 0:07880269e91a 61 counter++;
nakamae 0:07880269e91a 62
nakamae 0:07880269e91a 63 /* printf("DEV:");
nakamae 0:07880269e91a 64 for(b=0; b<6 ; b++)printf("%02x ", address[counter][b]);
nakamae 0:07880269e91a 65 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]);*/
nakamae 0:07880269e91a 66
nakamae 0:07880269e91a 67
nakamae 0:07880269e91a 68 }
nakamae 0:07880269e91a 69 }
nakamae 0:07880269e91a 70
nakamae 0:07880269e91a 71 void test_thread(void const *name) {
nakamae 0:07880269e91a 72
nakamae 0:07880269e91a 73 while (true) {
nakamae 0:07880269e91a 74 //Timer timer;
nakamae 0:07880269e91a 75 one_slot.wait();
nakamae 0:07880269e91a 76
nakamae 0:07880269e91a 77 if(!strcmp((const char*)name, "1")){
nakamae 0:07880269e91a 78 //printf("**startAdvertising thread**\n\r");
nakamae 0:07880269e91a 79 wt=1000;
nakamae 0:07880269e91a 80 ble.gap().stopAdvertising();
nakamae 0:07880269e91a 81 ble.startScan(&onScanCallback);
nakamae 0:07880269e91a 82 }
nakamae 0:07880269e91a 83 //2
nakamae 0:07880269e91a 84 if(!strcmp((const char*)name, "2")){
nakamae 2:e103492f1a0b 85 memset(address, 0, sizeof(address));//配列の初期化
nakamae 0:07880269e91a 86 counter = 0;
nakamae 2:e103492f1a0b 87 //printf("**get**\n\r");
nakamae 0:07880269e91a 88 myled3 = 0;
nakamae 0:07880269e91a 89 wt=0;//central modeの際はthreadの時間は0sec
nakamae 0:07880269e91a 90 wt1 = GET_TIME ;//centralmode の時間はこっち
nakamae 0:07880269e91a 91
nakamae 2:e103492f1a0b 92 fp(countfp == 0){
nakamae 2:e103492f1a0b 93 fp = fopen( id , "w");
nakamae 2:e103492f1a0b 94 fclose(fp);
nakamae 2:e103492f1a0b 95 kp = fopen( acc , "w");
nakamae 2:e103492f1a0b 96 fclose(kp);
nakamae 2:e103492f1a0b 97 countfp = 1;
nakamae 2:e103492f1a0b 98 }
nakamae 0:07880269e91a 99
nakamae 2:e103492f1a0b 100 fp = fopen( id , "a");
nakamae 2:e103492f1a0b 101 kp = fopen( acc , "a");
nakamae 2:e103492f1a0b 102
nakamae 2:e103492f1a0b 103 //printf("%d seconds receive\n\r",wt1/1000);
nakamae 0:07880269e91a 104 timer.start();
nakamae 2:e103492f1a0b 105
nakamae 0:07880269e91a 106 while(1){
nakamae 0:07880269e91a 107 myled3 = !myled3;
nakamae 0:07880269e91a 108 time_t seconds = time(NULL);
nakamae 0:07880269e91a 109 accelerometer.getOutput(readings);
nakamae 0:07880269e91a 110 //13-bit, sign extended values.
nakamae 0:07880269e91a 111 // 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));
nakamae 0:07880269e91a 112 // printf(": %d\r\n",seconds);
nakamae 0:07880269e91a 113 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));
nakamae 0:07880269e91a 114 fprintf(kp, ",%d\n",seconds);
nakamae 0:07880269e91a 115
nakamae 0:07880269e91a 116 ble.waitForEvent();
nakamae 0:07880269e91a 117 if(timer.read() > wt1/1000){
nakamae 0:07880269e91a 118 timer.stop();
nakamae 0:07880269e91a 119 timer.reset();
nakamae 0:07880269e91a 120 ble.stopScan();
nakamae 0:07880269e91a 121 fclose(fp);
nakamae 0:07880269e91a 122 fclose(kp);
nakamae 0:07880269e91a 123
nakamae 0:07880269e91a 124 break;
nakamae 0:07880269e91a 125 }
nakamae 0:07880269e91a 126 }
nakamae 0:07880269e91a 127
nakamae 0:07880269e91a 128 }
nakamae 0:07880269e91a 129
nakamae 0:07880269e91a 130 //3
nakamae 0:07880269e91a 131 if(!strcmp((const char*)name, "3")){
nakamae 2:e103492f1a0b 132 //printf("**send**\n\r");
nakamae 0:07880269e91a 133 ran = rand() % 10;
nakamae 0:07880269e91a 134 wt= 0;
nakamae 0:07880269e91a 135 wt1 = SEND_TIME - ran*1000;
nakamae 2:e103492f1a0b 136 //printf("%d seconds sendin\n\r",wt1/1000);
nakamae 2:e103492f1a0b 137 // ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */
nakamae 0:07880269e91a 138 ble.gap().startAdvertising();//BLEの送信
nakamae 0:07880269e91a 139
nakamae 0:07880269e91a 140 timer.start();
nakamae 0:07880269e91a 141 while(1){
nakamae 0:07880269e91a 142 myled3 = !myled3;
nakamae 0:07880269e91a 143 time_t seconds = time(NULL);
nakamae 0:07880269e91a 144 accelerometer.getOutput(readings);
nakamae 0:07880269e91a 145 //13-bit, sign extended values.
nakamae 0:07880269e91a 146 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));
nakamae 0:07880269e91a 147 printf(": %d\r\n",seconds);
nakamae 0:07880269e91a 148 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));
nakamae 0:07880269e91a 149 fprintf(kp, ",%d\n",seconds);
nakamae 0:07880269e91a 150
nakamae 0:07880269e91a 151
nakamae 0:07880269e91a 152 if(timer.read() > wt1/1000){
nakamae 0:07880269e91a 153 timer.stop();
nakamae 0:07880269e91a 154 timer.reset();
nakamae 0:07880269e91a 155 fclose(kp);
nakamae 0:07880269e91a 156 break;
nakamae 0:07880269e91a 157 }
nakamae 0:07880269e91a 158 }
nakamae 0:07880269e91a 159
nakamae 0:07880269e91a 160 }
nakamae 0:07880269e91a 161 Thread::wait(wt);
nakamae 0:07880269e91a 162 one_slot.release();
nakamae 0:07880269e91a 163 }
nakamae 0:07880269e91a 164 }
nakamae 0:07880269e91a 165
nakamae 0:07880269e91a 166 // const static char DEVICE_NAME[] = "BLE1";
nakamae 0:07880269e91a 167
nakamae 0:07880269e91a 168 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
nakamae 0:07880269e91a 169 {
nakamae 0:07880269e91a 170 BLE &ble= params->ble;
nakamae 0:07880269e91a 171 ble_error_t error = params->error;
nakamae 0:07880269e91a 172
nakamae 0:07880269e91a 173 if (error != BLE_ERROR_NONE) {
nakamae 0:07880269e91a 174 return;
nakamae 0:07880269e91a 175 }
nakamae 2:e103492f1a0b 176 const uint8_t address1[] = {AD,AD,AD,AD,AD,AD};
nakamae 0:07880269e91a 177 ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1);
nakamae 0:07880269e91a 178 /* ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
nakamae 0:07880269e91a 179 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
nakamae 0:07880269e91a 180 ble.gap().setAdvertisingInterval(1000);*/
nakamae 0:07880269e91a 181 }
nakamae 0:07880269e91a 182
nakamae 0:07880269e91a 183
nakamae 0:07880269e91a 184 int main (void) {
nakamae 0:07880269e91a 185 //------------------------------------------------------
nakamae 0:07880269e91a 186 //加速度センサーの初期化
nakamae 0:07880269e91a 187
nakamae 0:07880269e91a 188 //Go into standby mode to configure the device.
nakamae 0:07880269e91a 189 accelerometer.setPowerControl(0x00);
nakamae 0:07880269e91a 190 //Full resolution, +/-16g, 4mg/LSB.
nakamae 0:07880269e91a 191 accelerometer.setDataFormatControl(0x0B);
nakamae 0:07880269e91a 192 //3.2kHz data rate.
nakamae 0:07880269e91a 193 accelerometer.setDataRate(ADXL345_3200HZ);
nakamae 0:07880269e91a 194 //Measurement mode.
nakamae 0:07880269e91a 195 accelerometer.setPowerControl(0x08);
nakamae 0:07880269e91a 196 //-------------------------------------------------------
nakamae 0:07880269e91a 197
nakamae 0:07880269e91a 198 ble.init(bleInitComplete);
nakamae 0:07880269e91a 199 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,GapScanningParams::SCAN_WINDOW_MAX,0);
nakamae 0:07880269e91a 200
nakamae 0:07880269e91a 201 Thread t2(test_thread, (void *)"2");
nakamae 0:07880269e91a 202 Thread t3(test_thread, (void *)"3");
nakamae 0:07880269e91a 203
nakamae 0:07880269e91a 204 test_thread((void *)"1");
nakamae 0:07880269e91a 205 }