acc and mbed

Dependencies:   ADXL345_I2C BLE_API SDFileSystem mbed-rtos mbed nRF51822

Fork of AAAAandACCELACARTION by Shuta Nakamae

Committer:
nakamae
Date:
Thu Dec 01 03:23:32 2016 +0000
Revision:
0:315d43b70cfe
kasokudo with ble

Who changed what in which revision?

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