med ble switch science

Dependencies:   BLE_API SDFileSystem mbed-rtos mbed nRF51822

Committer:
kataokashunpei
Date:
Thu Nov 10 07:30:49 2016 +0000
Revision:
5:f8734ee36ec9
Parent:
4:c1ac302982b7
????????????????????; ????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kataokashunpei 0:1ad445491c35 1 #include "mbed.h"
kataokashunpei 0:1ad445491c35 2 #include "ble/BLE.h"
kataokashunpei 0:1ad445491c35 3 #include "ble/services/HeartRateService.h"
kataokashunpei 0:1ad445491c35 4 #include "ble/services/BatteryService.h"
kataokashunpei 0:1ad445491c35 5 #include "ble/services/DeviceInformationService.h"
kataokashunpei 0:1ad445491c35 6 #include "rtos.h"
kataokashunpei 0:1ad445491c35 7 #include <string.h>
kataokashunpei 0:1ad445491c35 8 #include "SDFileSystem.h"
kataokashunpei 4:c1ac302982b7 9 #define N 100//addressを保存する個数
kataokashunpei 4:c1ac302982b7 10 #define M 12//addressとtimeを入れる
kataokashunpei 1:fa329dd713be 11 #define GET_TIME 10000
kataokashunpei 1:fa329dd713be 12 #define SEND_TIME 20000
kataokashunpei 1:fa329dd713be 13 #define AA 0xAA
kataokashunpei 2:592364ca5fb2 14 #define BB 0xBB
kataokashunpei 5:f8734ee36ec9 15 #define CC 0xCC
kataokashunpei 5:f8734ee36ec9 16 #define DD 0xDD
kataokashunpei 5:f8734ee36ec9 17
kataokashunpei 2:592364ca5fb2 18 DigitalOut myled3(LED3);
kataokashunpei 1:fa329dd713be 19
kataokashunpei 0:1ad445491c35 20 Semaphore one_slot(1);
kataokashunpei 0:1ad445491c35 21
kataokashunpei 5:f8734ee36ec9 22 Timer timer;
kataokashunpei 0:1ad445491c35 23 BLE ble;
kataokashunpei 4:c1ac302982b7 24 char address[N][M]={};//保存する配列を初期化
kataokashunpei 0:1ad445491c35 25 const GapScanningParams scanningParams;
kataokashunpei 0:1ad445491c35 26 int wt;//threadの時間
kataokashunpei 1:fa329dd713be 27 int wt1=0;//central mode の時間
kataokashunpei 3:8fcc0e904fd6 28 int ran;
kataokashunpei 5:f8734ee36ec9 29
kataokashunpei 0:1ad445491c35 30 SDFileSystem sd(p25, p28, p29, p21, "sd"); // the pinout on the mbed Cool Components workshop board
kataokashunpei 0:1ad445491c35 31 FILE *fp;
kataokashunpei 0:1ad445491c35 32 int counter=0,i,b,flag;
kataokashunpei 5:f8734ee36ec9 33
kataokashunpei 0:1ad445491c35 34 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params){
kataokashunpei 0:1ad445491c35 35 time_t seconds = time(NULL); // JST
kataokashunpei 0:1ad445491c35 36 struct tm *t = localtime(&seconds);
kataokashunpei 2:592364ca5fb2 37
kataokashunpei 0:1ad445491c35 38 /*printf("%04d/%02d/%02d %02d:%02d:%02d\r\n",
kataokashunpei 0:1ad445491c35 39 t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
kataokashunpei 0:1ad445491c35 40 */
kataokashunpei 0:1ad445491c35 41 flag=0;
kataokashunpei 5:f8734ee36ec9 42
kataokashunpei 0:1ad445491c35 43 for(i=0;i<counter;i++){
kataokashunpei 0:1ad445491c35 44 if(address[i][0] == params->peerAddr[0]){
kataokashunpei 0:1ad445491c35 45 flag=1;
kataokashunpei 0:1ad445491c35 46 break;
kataokashunpei 0:1ad445491c35 47 }
kataokashunpei 0:1ad445491c35 48 }
kataokashunpei 0:1ad445491c35 49
kataokashunpei 0:1ad445491c35 50 if(flag==0){
kataokashunpei 0:1ad445491c35 51 for(i=0; i<6; i++){
kataokashunpei 0:1ad445491c35 52 address[counter][i]=params->peerAddr[i];
kataokashunpei 0:1ad445491c35 53 }
kataokashunpei 0:1ad445491c35 54 time_t seconds = time(NULL);
kataokashunpei 0:1ad445491c35 55 struct tm *t = localtime(&seconds);
kataokashunpei 1:fa329dd713be 56 address[counter][6]= t->tm_year - 70;
kataokashunpei 1:fa329dd713be 57 address[counter][7]= t->tm_mon;
kataokashunpei 1:fa329dd713be 58 address[counter][8]= t->tm_mday - 1;
kataokashunpei 0:1ad445491c35 59 address[counter][9]= t->tm_hour;
kataokashunpei 0:1ad445491c35 60 address[counter][10]= t->tm_min;
kataokashunpei 0:1ad445491c35 61 address[counter][11]= t->tm_sec;
kataokashunpei 0:1ad445491c35 62
kataokashunpei 1:fa329dd713be 63 for(b=0; b<6 ; b++){
kataokashunpei 1:fa329dd713be 64 fprintf(fp,"%02x", address[counter][b]);
kataokashunpei 1:fa329dd713be 65 printf("%02x", address[counter][b]);
kataokashunpei 4:c1ac302982b7 66 if(b==5)printf(" ");
kataokashunpei 1:fa329dd713be 67 }
kataokashunpei 4:c1ac302982b7 68
kataokashunpei 4:c1ac302982b7 69 fprintf(fp,",%04d",address[counter][b]);
kataokashunpei 4:c1ac302982b7 70 b++;
kataokashunpei 4:c1ac302982b7 71
kataokashunpei 4:c1ac302982b7 72 for(; b<12; b++){
kataokashunpei 4:c1ac302982b7 73 fprintf(fp,"%02d",address[counter][b]);
kataokashunpei 4:c1ac302982b7 74 printf("%02d",address[counter][b]);
kataokashunpei 4:c1ac302982b7 75 if(b==11){
kataokashunpei 4:c1ac302982b7 76 fprintf(fp,"\n");
kataokashunpei 4:c1ac302982b7 77 printf("\r\n");
kataokashunpei 4:c1ac302982b7 78 }
kataokashunpei 4:c1ac302982b7 79 }
kataokashunpei 4:c1ac302982b7 80 //fprintf(fp,",%04d,%02d,%02d,%02d,%02d,%02d\n",address[counter][6],address[counter][7],address[counter][8],address[counter][9],address[counter][10],address[counter][11]);
kataokashunpei 4:c1ac302982b7 81 //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]);
kataokashunpei 4:c1ac302982b7 82
kataokashunpei 1:fa329dd713be 83 counter++;
kataokashunpei 5:f8734ee36ec9 84
kataokashunpei 0:1ad445491c35 85 /*
kataokashunpei 0:1ad445491c35 86 printf("DEV:");
kataokashunpei 0:1ad445491c35 87 for(b=0; b<6 ; b++)printf("%02x ", address[counter][b]);
kataokashunpei 0:1ad445491c35 88 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]);
kataokashunpei 0:1ad445491c35 89 */
kataokashunpei 5:f8734ee36ec9 90
kataokashunpei 0:1ad445491c35 91 /*配列の中身をすべて表示
kataokashunpei 0:1ad445491c35 92 for(a=0; a<counter; a++){
kataokashunpei 0:1ad445491c35 93 for(b=0; b<6 ; b++){
kataokashunpei 0:1ad445491c35 94 if(b==0)printf("DEV:");
kataokashunpei 0:1ad445491c35 95 printf("%02x ", address[a][b]);
kataokashunpei 0:1ad445491c35 96 if(b==5){
kataokashunpei 0:1ad445491c35 97 printf("%04d/%02d/%02d %02d:%02d:%02d \r\n",address[a][6],address[a][7],address[a][8],address[a][9],address[a][10],address[a][11]);
kataokashunpei 0:1ad445491c35 98 }
kataokashunpei 0:1ad445491c35 99 }
kataokashunpei 0:1ad445491c35 100 if(a==counter-1)printf("----------\n\r");
kataokashunpei 0:1ad445491c35 101 }
kataokashunpei 0:1ad445491c35 102 */
kataokashunpei 0:1ad445491c35 103 }
kataokashunpei 0:1ad445491c35 104 }
kataokashunpei 5:f8734ee36ec9 105
kataokashunpei 0:1ad445491c35 106 void test_thread(void const *name) {
kataokashunpei 0:1ad445491c35 107
kataokashunpei 0:1ad445491c35 108 while (true) {
kataokashunpei 2:592364ca5fb2 109 //Timer timer;
kataokashunpei 0:1ad445491c35 110 one_slot.wait();
kataokashunpei 0:1ad445491c35 111
kataokashunpei 0:1ad445491c35 112 if(!strcmp((const char*)name, "1")){
kataokashunpei 4:c1ac302982b7 113 //printf("**startAdvertising thread**\n\r");
kataokashunpei 3:8fcc0e904fd6 114 wt=1000;
kataokashunpei 0:1ad445491c35 115 ble.gap().stopAdvertising();
kataokashunpei 0:1ad445491c35 116 ble.startScan(&onScanCallback);
kataokashunpei 0:1ad445491c35 117 }
kataokashunpei 0:1ad445491c35 118 //2
kataokashunpei 0:1ad445491c35 119 if(!strcmp((const char*)name, "2")){
kataokashunpei 5:f8734ee36ec9 120 memset(address, 0, sizeof(address));//配列の初期化
kataokashunpei 2:592364ca5fb2 121 counter = 0;
kataokashunpei 4:c1ac302982b7 122 printf("**get**\n\r");
kataokashunpei 2:592364ca5fb2 123 myled3 = 0;
kataokashunpei 0:1ad445491c35 124 wt=0;//central modeの際はthreadの時間は0sec
kataokashunpei 3:8fcc0e904fd6 125 wt1 = GET_TIME ;//centralmode の時間はこっち
kataokashunpei 1:fa329dd713be 126 fp = fopen("/sd/test.csv", "a");
kataokashunpei 0:1ad445491c35 127
kataokashunpei 3:8fcc0e904fd6 128 printf("%d秒間の受信\n\r",wt1/1000);
kataokashunpei 0:1ad445491c35 129 timer.start();
kataokashunpei 5:f8734ee36ec9 130 while(1){
kataokashunpei 0:1ad445491c35 131 ble.waitForEvent();
kataokashunpei 0:1ad445491c35 132 if(timer.read() > wt1/1000){
kataokashunpei 0:1ad445491c35 133 timer.stop();
kataokashunpei 0:1ad445491c35 134 timer.reset();
kataokashunpei 0:1ad445491c35 135 ble.stopScan();
kataokashunpei 0:1ad445491c35 136 fclose(fp);
kataokashunpei 0:1ad445491c35 137 break;
kataokashunpei 0:1ad445491c35 138 }
kataokashunpei 0:1ad445491c35 139 }
kataokashunpei 5:f8734ee36ec9 140
kataokashunpei 5:f8734ee36ec9 141 }
kataokashunpei 5:f8734ee36ec9 142
kataokashunpei 5:f8734ee36ec9 143 //3
kataokashunpei 5:f8734ee36ec9 144 if(!strcmp((const char*)name, "3")){
kataokashunpei 5:f8734ee36ec9 145 printf("**send**\n\r");
kataokashunpei 5:f8734ee36ec9 146 myled3 = 1;
kataokashunpei 5:f8734ee36ec9 147 ran = rand() % 10;
kataokashunpei 5:f8734ee36ec9 148 wt=SEND_TIME - ran*1000;
kataokashunpei 5:f8734ee36ec9 149 printf("%d秒間の送信\n\r",wt/1000);
kataokashunpei 5:f8734ee36ec9 150 ble.gap().startAdvertising();//BLEの送信
kataokashunpei 1:fa329dd713be 151 }
kataokashunpei 0:1ad445491c35 152 Thread::wait(wt);
kataokashunpei 0:1ad445491c35 153 one_slot.release();
kataokashunpei 1:fa329dd713be 154 }
kataokashunpei 0:1ad445491c35 155 }
kataokashunpei 0:1ad445491c35 156
kataokashunpei 0:1ad445491c35 157 // const static char DEVICE_NAME[] = "BLE1";
kataokashunpei 0:1ad445491c35 158
kataokashunpei 0:1ad445491c35 159 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
kataokashunpei 0:1ad445491c35 160 {
kataokashunpei 0:1ad445491c35 161 BLE &ble= params->ble;
kataokashunpei 0:1ad445491c35 162 ble_error_t error = params->error;
kataokashunpei 5:f8734ee36ec9 163
kataokashunpei 0:1ad445491c35 164 if (error != BLE_ERROR_NONE) {
kataokashunpei 0:1ad445491c35 165 return;
kataokashunpei 0:1ad445491c35 166 }
kataokashunpei 5:f8734ee36ec9 167 const uint8_t address1[] = {AA,AA,AA,AA,AA,AA};
kataokashunpei 0:1ad445491c35 168 ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1);
kataokashunpei 0:1ad445491c35 169 // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
kataokashunpei 0:1ad445491c35 170 // ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
kataokashunpei 0:1ad445491c35 171 // ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
kataokashunpei 0:1ad445491c35 172 }
kataokashunpei 0:1ad445491c35 173
kataokashunpei 0:1ad445491c35 174 int main (void) {
kataokashunpei 0:1ad445491c35 175 ble.init(bleInitComplete);
kataokashunpei 1:fa329dd713be 176 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,GapScanningParams::SCAN_WINDOW_MAX,0);
kataokashunpei 5:f8734ee36ec9 177
kataokashunpei 0:1ad445491c35 178 Thread t2(test_thread, (void *)"2");
kataokashunpei 0:1ad445491c35 179 Thread t3(test_thread, (void *)"3");
kataokashunpei 0:1ad445491c35 180
kataokashunpei 0:1ad445491c35 181 test_thread((void *)"1");
kataokashunpei 0:1ad445491c35 182 }