aaaaada

Dependencies:   ADXL345_I2C BLE_API SDFileSystem mbed-rtos mbed nRF51822

Fork of zzzzzzzzzzzzzzzzzzzzzz by Shuta Nakamae

Committer:
nakamae
Date:
Fri Dec 30 06:28:37 2016 +0000
Revision:
0:07880269e91a
aa

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