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