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