med ble switch science

Dependencies:   BLE_API SDFileSystem mbed-rtos mbed nRF51822

Committer:
kataokashunpei
Date:
Tue Oct 25 05:32:14 2016 +0000
Revision:
2:592364ca5fb2
Parent:
1:fa329dd713be
Child:
3:8fcc0e904fd6
demo SSmbed

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