1154
Dependencies: BLE_API Peripheral_1 mbed nRF51822
Fork of Peripheral_1_serial by
main.cpp@3:3f4aa38b0fb7, 2015-10-01 (annotated)
- Committer:
- ssenwkdw
- Date:
- Thu Oct 01 17:33:32 2015 +0000
- Revision:
- 3:3f4aa38b0fb7
- Parent:
- 1:c227f3064399
aa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DDUdui | 0:482d3fcc94f4 | 1 | /* mbed Microcontroller Library |
DDUdui | 0:482d3fcc94f4 | 2 | * Copyright (c) 2006-2015 ARM Limited |
DDUdui | 0:482d3fcc94f4 | 3 | * |
DDUdui | 0:482d3fcc94f4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DDUdui | 0:482d3fcc94f4 | 5 | * you may not use this file except in compliance with the License. |
DDUdui | 0:482d3fcc94f4 | 6 | * You may obtain a copy of the License at |
DDUdui | 0:482d3fcc94f4 | 7 | * |
DDUdui | 0:482d3fcc94f4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DDUdui | 0:482d3fcc94f4 | 9 | * |
DDUdui | 0:482d3fcc94f4 | 10 | * Unless required by applicable law or agreed to in writing, software |
DDUdui | 0:482d3fcc94f4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DDUdui | 0:482d3fcc94f4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DDUdui | 0:482d3fcc94f4 | 13 | * See the License for the specific language governing permissions and |
DDUdui | 0:482d3fcc94f4 | 14 | * limitations under the License. |
DDUdui | 0:482d3fcc94f4 | 15 | */ |
DDUdui | 0:482d3fcc94f4 | 16 | |
DDUdui | 0:482d3fcc94f4 | 17 | #include "mbed.h" |
DDUdui | 0:482d3fcc94f4 | 18 | #include "ble/BLE.h" |
DDUdui | 0:482d3fcc94f4 | 19 | #include "ble/DiscoveredCharacteristic.h" |
DDUdui | 0:482d3fcc94f4 | 20 | #include "ble/DiscoveredService.h" |
ssenwkdw | 1:c227f3064399 | 21 | |
DDUdui | 0:482d3fcc94f4 | 22 | BLE ble; |
ssenwkdw | 1:c227f3064399 | 23 | DigitalOut led(P0_19,1); |
ssenwkdw | 1:c227f3064399 | 24 | DigitalOut myled(P0_8); |
ssenwkdw | 1:c227f3064399 | 25 | DigitalOut neo(P0_6,1); |
ssenwkdw | 1:c227f3064399 | 26 | DigitalOut pin4(P0_4); |
ssenwkdw | 1:c227f3064399 | 27 | DigitalOut pin5(P0_5); |
ssenwkdw | 1:c227f3064399 | 28 | DigitalOut pin15(P0_15); |
ssenwkdw | 1:c227f3064399 | 29 | DigitalOut pin29(P0_29); |
ssenwkdw | 1:c227f3064399 | 30 | |
ssenwkdw | 1:c227f3064399 | 31 | static const unsigned NUM_ROBOTS = 8; |
ssenwkdw | 1:c227f3064399 | 32 | static const unsigned ADDR_LEN = 6; |
ssenwkdw | 1:c227f3064399 | 33 | |
ssenwkdw | 1:c227f3064399 | 34 | uint16_t customServiceUUID = 0xA000; |
ssenwkdw | 1:c227f3064399 | 35 | uint16_t readwriteCharUUID = 0xA001; |
ssenwkdw | 1:c227f3064399 | 36 | static const uint16_t secure_code[] = {0xABCD}; |
ssenwkdw | 1:c227f3064399 | 37 | |
ssenwkdw | 1:c227f3064399 | 38 | uint8_t info[NUM_ROBOTS]= {0}; |
ssenwkdw | 1:c227f3064399 | 39 | |
ssenwkdw | 1:c227f3064399 | 40 | ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
ssenwkdw | 1:c227f3064399 | 41 | |
ssenwkdw | 1:c227f3064399 | 42 | //GattCharacteristic readwriteChar (readwriteCharUUID, info, NUM_ROBOTS, NUM_ROBOTS, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
ssenwkdw | 1:c227f3064399 | 43 | |
ssenwkdw | 1:c227f3064399 | 44 | GattCharacteristic *characteristics[] = {&readwriteChar}; |
ssenwkdw | 1:c227f3064399 | 45 | |
ssenwkdw | 1:c227f3064399 | 46 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
ssenwkdw | 1:c227f3064399 | 47 | |
ssenwkdw | 1:c227f3064399 | 48 | bool triggerLedCharacteristic = false; |
ssenwkdw | 1:c227f3064399 | 49 | DiscoveredCharacteristic characteristics_discovered; |
DDUdui | 0:482d3fcc94f4 | 50 | |
ssenwkdw | 1:c227f3064399 | 51 | int8_t temp; |
ssenwkdw | 1:c227f3064399 | 52 | uint8_t pt_addr = 0; |
ssenwkdw | 1:c227f3064399 | 53 | uint8_t list_addr[NUM_ROBOTS][ADDR_LEN+NUM_ROBOTS]= {0,}; |
ssenwkdw | 1:c227f3064399 | 54 | int8_t list_rssi[NUM_ROBOTS]; |
ssenwkdw | 1:c227f3064399 | 55 | uint8_t initial_list_addr[NUM_ROBOTS][ADDR_LEN + 1]= {0,}; |
ssenwkdw | 1:c227f3064399 | 56 | // 1st Device, 2nd device has '10(2)' information |
ssenwkdw | 1:c227f3064399 | 57 | uint8_t info_t[NUM_ROBOTS]= {0,}; |
ssenwkdw | 1:c227f3064399 | 58 | uint8_t point; |
ssenwkdw | 1:c227f3064399 | 59 | |
ssenwkdw | 1:c227f3064399 | 60 | void RGB_Show(uint8_t r, uint8_t g, uint8_t b) |
ssenwkdw | 1:c227f3064399 | 61 | { |
ssenwkdw | 1:c227f3064399 | 62 | uint8_t rgb[3] = {g, r, b}; |
ssenwkdw | 1:c227f3064399 | 63 | uint8_t *p = rgb; |
ssenwkdw | 1:c227f3064399 | 64 | uint8_t *end = p + 3; |
ssenwkdw | 1:c227f3064399 | 65 | while (p < end) { |
ssenwkdw | 1:c227f3064399 | 66 | uint8_t pix = *p++; |
ssenwkdw | 1:c227f3064399 | 67 | for (uint8_t mask = 0x80; mask; mask >>= 1) { |
ssenwkdw | 1:c227f3064399 | 68 | if (pix & mask) { |
ssenwkdw | 1:c227f3064399 | 69 | // T1H 760ns |
ssenwkdw | 1:c227f3064399 | 70 | NRF_GPIO->OUTSET = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 71 | NRF_GPIO->OUTSET = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 72 | NRF_GPIO->OUTSET = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 73 | // T1L 660ns |
ssenwkdw | 1:c227f3064399 | 74 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 75 | } else { |
ssenwkdw | 1:c227f3064399 | 76 | // T0H 380ns |
ssenwkdw | 1:c227f3064399 | 77 | NRF_GPIO->OUTSET = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 78 | // T0L 840ns |
ssenwkdw | 1:c227f3064399 | 79 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 80 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 81 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 82 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 83 | } |
ssenwkdw | 1:c227f3064399 | 84 | } |
ssenwkdw | 1:c227f3064399 | 85 | } |
ssenwkdw | 1:c227f3064399 | 86 | NRF_GPIO->OUTCLR = (1UL << 8); |
ssenwkdw | 1:c227f3064399 | 87 | wait_us(100); |
ssenwkdw | 1:c227f3064399 | 88 | } |
DDUdui | 0:482d3fcc94f4 | 89 | |
DDUdui | 0:482d3fcc94f4 | 90 | void periodicCallback(void) |
DDUdui | 0:482d3fcc94f4 | 91 | { |
ssenwkdw | 1:c227f3064399 | 92 | led=!led; |
ssenwkdw | 1:c227f3064399 | 93 | point = 0; |
ssenwkdw | 1:c227f3064399 | 94 | for(int i=0; i<NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 95 | point+=info[i]; |
ssenwkdw | 1:c227f3064399 | 96 | } |
ssenwkdw | 1:c227f3064399 | 97 | if(point==0) { |
ssenwkdw | 1:c227f3064399 | 98 | RGB_Show(0x00,0x00,0xff); |
ssenwkdw | 1:c227f3064399 | 99 | } else if(point==1) { |
ssenwkdw | 1:c227f3064399 | 100 | RGB_Show(0xff,0xff,0xff); |
ssenwkdw | 1:c227f3064399 | 101 | } else if(point==2) { |
ssenwkdw | 1:c227f3064399 | 102 | RGB_Show(0x00,0xff,0x00); |
ssenwkdw | 1:c227f3064399 | 103 | } else if(point==3) { |
ssenwkdw | 1:c227f3064399 | 104 | RGB_Show(0x00,0xff,0xff); |
ssenwkdw | 1:c227f3064399 | 105 | } else if(point==4) { |
ssenwkdw | 1:c227f3064399 | 106 | RGB_Show(0xff,0x00,0x00); |
ssenwkdw | 1:c227f3064399 | 107 | } else if(point==5) { |
ssenwkdw | 1:c227f3064399 | 108 | RGB_Show(0xff,0x00,0xff); |
ssenwkdw | 1:c227f3064399 | 109 | } else if(point==6) { |
ssenwkdw | 1:c227f3064399 | 110 | RGB_Show(0xff,0xff,0x00); |
ssenwkdw | 1:c227f3064399 | 111 | } else if(point==7) { |
ssenwkdw | 1:c227f3064399 | 112 | RGB_Show(0xff,0xff,0xff); |
ssenwkdw | 1:c227f3064399 | 113 | } else if(point==8) { |
ssenwkdw | 1:c227f3064399 | 114 | RGB_Show(0x00,0x00,0xff); |
ssenwkdw | 1:c227f3064399 | 115 | } |
DDUdui | 0:482d3fcc94f4 | 116 | } |
DDUdui | 0:482d3fcc94f4 | 117 | |
ssenwkdw | 1:c227f3064399 | 118 | void initialSetup(const Gap::AdvertisementCallbackParams_t *params) |
ssenwkdw | 1:c227f3064399 | 119 | { |
ssenwkdw | 1:c227f3064399 | 120 | if ( params->advertisingData[1] == 0xAF ) { |
ssenwkdw | 1:c227f3064399 | 121 | for(int i = 0; i < NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 122 | if(initial_list_addr[i][5] == params->peerAddr[5] |
ssenwkdw | 1:c227f3064399 | 123 | && initial_list_addr[i][4] == params->peerAddr[4] |
ssenwkdw | 1:c227f3064399 | 124 | && initial_list_addr[i][3] == params->peerAddr[3] |
ssenwkdw | 1:c227f3064399 | 125 | && initial_list_addr[i][2] == params->peerAddr[2] |
ssenwkdw | 1:c227f3064399 | 126 | && initial_list_addr[i][1] == params->peerAddr[1] |
ssenwkdw | 1:c227f3064399 | 127 | && initial_list_addr[i][0] == params->peerAddr[0]) { |
ssenwkdw | 1:c227f3064399 | 128 | break; |
ssenwkdw | 1:c227f3064399 | 129 | } |
ssenwkdw | 1:c227f3064399 | 130 | if(initial_list_addr[i][5] == 0 && initial_list_addr[i][4] == 0 |
ssenwkdw | 1:c227f3064399 | 131 | && initial_list_addr[i][3] == 0 && initial_list_addr[i][2] == 0 |
ssenwkdw | 1:c227f3064399 | 132 | && initial_list_addr[i][1] == 0 && initial_list_addr[i][0] == 0) { |
ssenwkdw | 1:c227f3064399 | 133 | initial_list_addr[i][5] = params->peerAddr[5]; |
ssenwkdw | 1:c227f3064399 | 134 | initial_list_addr[i][4] = params->peerAddr[4]; |
ssenwkdw | 1:c227f3064399 | 135 | initial_list_addr[i][3] = params->peerAddr[3]; |
ssenwkdw | 1:c227f3064399 | 136 | initial_list_addr[i][2] = params->peerAddr[2]; |
ssenwkdw | 1:c227f3064399 | 137 | initial_list_addr[i][1] = params->peerAddr[1]; |
ssenwkdw | 1:c227f3064399 | 138 | initial_list_addr[i][0] = params->peerAddr[0]; |
DDUdui | 0:482d3fcc94f4 | 139 | |
ssenwkdw | 1:c227f3064399 | 140 | initial_list_addr[i][6] = 1; |
ssenwkdw | 1:c227f3064399 | 141 | break; |
ssenwkdw | 1:c227f3064399 | 142 | } |
ssenwkdw | 1:c227f3064399 | 143 | } |
ssenwkdw | 1:c227f3064399 | 144 | printf("initialsetup\r\n"); |
ssenwkdw | 1:c227f3064399 | 145 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x\r\n", |
ssenwkdw | 1:c227f3064399 | 146 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
ssenwkdw | 1:c227f3064399 | 147 | params->rssi, params->isScanResponse, params->type, params->advertisingData[1], params->advertisingData[0]); |
DDUdui | 0:482d3fcc94f4 | 148 | } |
DDUdui | 0:482d3fcc94f4 | 149 | } |
DDUdui | 0:482d3fcc94f4 | 150 | |
ssenwkdw | 1:c227f3064399 | 151 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
ssenwkdw | 1:c227f3064399 | 152 | { |
ssenwkdw | 1:c227f3064399 | 153 | if ( params->rssi > -50 && params->advertisingData[1] == 0xAF ) { |
ssenwkdw | 1:c227f3064399 | 154 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x %0x\r\n", |
ssenwkdw | 1:c227f3064399 | 155 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
ssenwkdw | 1:c227f3064399 | 156 | params->rssi, params->isScanResponse, params->type, params->advertisingData[3], params->advertisingData[2], params->advertisingData[1]); |
ssenwkdw | 1:c227f3064399 | 157 | |
ssenwkdw | 1:c227f3064399 | 158 | ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
ssenwkdw | 1:c227f3064399 | 159 | } |
ssenwkdw | 1:c227f3064399 | 160 | else{ |
ssenwkdw | 1:c227f3064399 | 161 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u, %0x %0x %0x\r\n", |
ssenwkdw | 1:c227f3064399 | 162 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
ssenwkdw | 1:c227f3064399 | 163 | params->rssi, params->isScanResponse, params->type, params->advertisingData[3], params->advertisingData[2], params->advertisingData[1]); |
ssenwkdw | 1:c227f3064399 | 164 | } |
ssenwkdw | 1:c227f3064399 | 165 | /* |
ssenwkdw | 1:c227f3064399 | 166 | int i; |
ssenwkdw | 1:c227f3064399 | 167 | |
ssenwkdw | 1:c227f3064399 | 168 | for(i = 0; i < NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 169 | if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3] |
ssenwkdw | 1:c227f3064399 | 170 | && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) { |
ssenwkdw | 1:c227f3064399 | 171 | list_rssi[i] = params->rssi; |
ssenwkdw | 1:c227f3064399 | 172 | break; |
DDUdui | 0:482d3fcc94f4 | 173 | } |
ssenwkdw | 1:c227f3064399 | 174 | if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) { |
ssenwkdw | 1:c227f3064399 | 175 | list_addr[i][5] = params->peerAddr[5]; |
ssenwkdw | 1:c227f3064399 | 176 | list_addr[i][4] = params->peerAddr[4]; |
ssenwkdw | 1:c227f3064399 | 177 | list_addr[i][3] = params->peerAddr[3]; |
ssenwkdw | 1:c227f3064399 | 178 | list_addr[i][2] = params->peerAddr[2]; |
ssenwkdw | 1:c227f3064399 | 179 | list_addr[i][1] = params->peerAddr[1]; |
ssenwkdw | 1:c227f3064399 | 180 | list_addr[i][0] = params->peerAddr[0]; |
ssenwkdw | 1:c227f3064399 | 181 | |
ssenwkdw | 1:c227f3064399 | 182 | list_addr[i][6] = params->advertisingData[0]; |
ssenwkdw | 1:c227f3064399 | 183 | |
ssenwkdw | 1:c227f3064399 | 184 | list_rssi[i] = params->rssi; |
ssenwkdw | 1:c227f3064399 | 185 | |
ssenwkdw | 1:c227f3064399 | 186 | if ( i==NUM_ROBOTS-1 ) { |
ssenwkdw | 1:c227f3064399 | 187 | for(unsigned j=0; j<NUM_ROBOTS; j++) { |
ssenwkdw | 1:c227f3064399 | 188 | for(unsigned k=j ; k<NUM_ROBOTS ; k++) { |
ssenwkdw | 1:c227f3064399 | 189 | if(list_rssi[j] < list_rssi[k]) { |
ssenwkdw | 1:c227f3064399 | 190 | temp = list_rssi[j]; |
ssenwkdw | 1:c227f3064399 | 191 | list_rssi[j] = list_rssi[k]; |
ssenwkdw | 1:c227f3064399 | 192 | list_rssi[k] = temp; |
ssenwkdw | 1:c227f3064399 | 193 | for(unsigned l = 0; l < 6 ; l++) { |
ssenwkdw | 1:c227f3064399 | 194 | temp = list_addr[j][l]; |
ssenwkdw | 1:c227f3064399 | 195 | list_addr[j][l] = list_addr[k][l]; |
ssenwkdw | 1:c227f3064399 | 196 | list_addr[k][l] = temp; |
ssenwkdw | 1:c227f3064399 | 197 | } |
ssenwkdw | 1:c227f3064399 | 198 | } |
ssenwkdw | 1:c227f3064399 | 199 | } |
ssenwkdw | 1:c227f3064399 | 200 | } |
ssenwkdw | 1:c227f3064399 | 201 | break; |
ssenwkdw | 1:c227f3064399 | 202 | } |
ssenwkdw | 1:c227f3064399 | 203 | } |
ssenwkdw | 1:c227f3064399 | 204 | |
ssenwkdw | 1:c227f3064399 | 205 | if( i == NUM_ROBOTS-1 ) { |
ssenwkdw | 1:c227f3064399 | 206 | if( i == 2 ) { |
ssenwkdw | 1:c227f3064399 | 207 | ble.gap().stopScan(); |
ssenwkdw | 1:c227f3064399 | 208 | ble.gap().connect(list_addr[0], Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
ssenwkdw | 1:c227f3064399 | 209 | } |
ssenwkdw | 1:c227f3064399 | 210 | } |
ssenwkdw | 1:c227f3064399 | 211 | } |
ssenwkdw | 1:c227f3064399 | 212 | */ |
ssenwkdw | 1:c227f3064399 | 213 | } |
ssenwkdw | 1:c227f3064399 | 214 | |
ssenwkdw | 1:c227f3064399 | 215 | void serviceDiscoveryCallback(const DiscoveredService *service) |
ssenwkdw | 1:c227f3064399 | 216 | { |
ssenwkdw | 1:c227f3064399 | 217 | } |
ssenwkdw | 1:c227f3064399 | 218 | |
ssenwkdw | 1:c227f3064399 | 219 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) |
ssenwkdw | 1:c227f3064399 | 220 | { |
ssenwkdw | 1:c227f3064399 | 221 | if (characteristicP->getUUID() == 0xA001) { /* !ALERT! Alter this filter to suit your device. */ |
ssenwkdw | 1:c227f3064399 | 222 | characteristics_discovered = *characteristicP; |
ssenwkdw | 1:c227f3064399 | 223 | printf("characteristic discovered\r\t\n"); |
ssenwkdw | 1:c227f3064399 | 224 | characteristics_discovered.read(); |
DDUdui | 0:482d3fcc94f4 | 225 | } |
DDUdui | 0:482d3fcc94f4 | 226 | } |
ssenwkdw | 1:c227f3064399 | 227 | |
ssenwkdw | 1:c227f3064399 | 228 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
ssenwkdw | 1:c227f3064399 | 229 | { |
ssenwkdw | 1:c227f3064399 | 230 | printf("connected\r\n"); |
ssenwkdw | 1:c227f3064399 | 231 | ble.gap().stopAdvertising(); |
ssenwkdw | 1:c227f3064399 | 232 | ble.gap().stopScan(); |
ssenwkdw | 1:c227f3064399 | 233 | if (params->role == Gap::CENTRAL) { |
ssenwkdw | 1:c227f3064399 | 234 | printf("CENTRAL\r\n"); |
ssenwkdw | 1:c227f3064399 | 235 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001); |
ssenwkdw | 1:c227f3064399 | 236 | } else { |
ssenwkdw | 1:c227f3064399 | 237 | printf("PERIPHERAL\r\n"); |
ssenwkdw | 1:c227f3064399 | 238 | } |
DDUdui | 0:482d3fcc94f4 | 239 | } |
ssenwkdw | 1:c227f3064399 | 240 | |
ssenwkdw | 1:c227f3064399 | 241 | void triggerToggledWrite(const GattReadCallbackParams *response) |
ssenwkdw | 1:c227f3064399 | 242 | { |
ssenwkdw | 1:c227f3064399 | 243 | if (response->handle == characteristics_discovered.getValueHandle()) { |
ssenwkdw | 1:c227f3064399 | 244 | for(int i=0; i < NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 245 | info_t[i] = info[i] || response->data[i]; |
ssenwkdw | 1:c227f3064399 | 246 | info[i] = info_t[i]; |
ssenwkdw | 1:c227f3064399 | 247 | } |
ssenwkdw | 1:c227f3064399 | 248 | characteristics_discovered.write(NUM_ROBOTS, info); |
DDUdui | 0:482d3fcc94f4 | 249 | } |
ssenwkdw | 1:c227f3064399 | 250 | } |
ssenwkdw | 1:c227f3064399 | 251 | |
ssenwkdw | 1:c227f3064399 | 252 | void onDataWriteCallback(const GattWriteCallbackParams *params) |
ssenwkdw | 1:c227f3064399 | 253 | { |
ssenwkdw | 1:c227f3064399 | 254 | characteristics_discovered.read(); |
DDUdui | 0:482d3fcc94f4 | 255 | } |
ssenwkdw | 1:c227f3064399 | 256 | |
ssenwkdw | 1:c227f3064399 | 257 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
ssenwkdw | 1:c227f3064399 | 258 | { |
ssenwkdw | 1:c227f3064399 | 259 | for(int i=0; i<NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 260 | info[i] = params->data[i]; |
ssenwkdw | 1:c227f3064399 | 261 | } |
ssenwkdw | 1:c227f3064399 | 262 | printf("data is written well\r\n"); |
ssenwkdw | 1:c227f3064399 | 263 | |
ssenwkdw | 1:c227f3064399 | 264 | ble.gap().disconnect(params->connHandle,Gap::REMOTE_USER_TERMINATED_CONNECTION); |
ssenwkdw | 1:c227f3064399 | 265 | } |
ssenwkdw | 1:c227f3064399 | 266 | |
DDUdui | 0:482d3fcc94f4 | 267 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
DDUdui | 0:482d3fcc94f4 | 268 | { |
ssenwkdw | 1:c227f3064399 | 269 | printf("disconnected\r\n"); |
ssenwkdw | 1:c227f3064399 | 270 | |
ssenwkdw | 1:c227f3064399 | 271 | for(int i=0; i<NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 272 | printf(" %u", info[i]); |
ssenwkdw | 1:c227f3064399 | 273 | } |
ssenwkdw | 1:c227f3064399 | 274 | printf("\r\n"); |
ssenwkdw | 1:c227f3064399 | 275 | |
ssenwkdw | 1:c227f3064399 | 276 | ble.gap().startAdvertising(); |
ssenwkdw | 1:c227f3064399 | 277 | ble.gap().startScan(advertisementCallback); |
DDUdui | 0:482d3fcc94f4 | 278 | } |
ssenwkdw | 1:c227f3064399 | 279 | |
DDUdui | 0:482d3fcc94f4 | 280 | |
DDUdui | 0:482d3fcc94f4 | 281 | int main(void) |
DDUdui | 0:482d3fcc94f4 | 282 | { |
DDUdui | 0:482d3fcc94f4 | 283 | Ticker ticker; |
ssenwkdw | 1:c227f3064399 | 284 | ticker.attach(periodicCallback, 0.5); |
ssenwkdw | 1:c227f3064399 | 285 | |
ssenwkdw | 1:c227f3064399 | 286 | int num = 0; |
ssenwkdw | 1:c227f3064399 | 287 | |
ssenwkdw | 1:c227f3064399 | 288 | ble.init(); |
ssenwkdw | 1:c227f3064399 | 289 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED); |
ssenwkdw | 1:c227f3064399 | 290 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ssenwkdw | 3:3f4aa38b0fb7 | 291 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)secure_code, sizeof(secure_code)); |
ssenwkdw | 3:3f4aa38b0fb7 | 292 | ble.setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED); |
ssenwkdw | 1:c227f3064399 | 293 | ble.gap().setAdvertisingInterval(32); |
ssenwkdw | 1:c227f3064399 | 294 | ble.gap().startAdvertising(); |
ssenwkdw | 1:c227f3064399 | 295 | |
ssenwkdw | 1:c227f3064399 | 296 | ble.gap().setScanParams(1014 , 884); |
ssenwkdw | 1:c227f3064399 | 297 | ble.gap().startScan(initialSetup); |
ssenwkdw | 1:c227f3064399 | 298 | |
ssenwkdw | 1:c227f3064399 | 299 | wait(5); |
ssenwkdw | 1:c227f3064399 | 300 | |
ssenwkdw | 1:c227f3064399 | 301 | ble.gap().stopAdvertising(); |
ssenwkdw | 1:c227f3064399 | 302 | ble.gap().stopScan(); |
ssenwkdw | 1:c227f3064399 | 303 | |
ssenwkdw | 1:c227f3064399 | 304 | ble.gap().clearAdvertisingPayload(); |
ssenwkdw | 1:c227f3064399 | 305 | |
ssenwkdw | 1:c227f3064399 | 306 | for(int i = 0; i< NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 307 | num += initial_list_addr[i][6]; |
ssenwkdw | 1:c227f3064399 | 308 | } |
ssenwkdw | 1:c227f3064399 | 309 | info[num] = 1; |
ssenwkdw | 1:c227f3064399 | 310 | |
ssenwkdw | 1:c227f3064399 | 311 | printf("initial setting is done\r\n"); |
ssenwkdw | 1:c227f3064399 | 312 | |
ssenwkdw | 1:c227f3064399 | 313 | printf("initial info : "); |
ssenwkdw | 1:c227f3064399 | 314 | for(int i=0; i<NUM_ROBOTS; i++) { |
ssenwkdw | 1:c227f3064399 | 315 | printf("%u \t",info[i]); |
ssenwkdw | 1:c227f3064399 | 316 | } |
ssenwkdw | 1:c227f3064399 | 317 | printf("\r\n"); |
ssenwkdw | 1:c227f3064399 | 318 | |
ssenwkdw | 1:c227f3064399 | 319 | |
ssenwkdw | 1:c227f3064399 | 320 | //advertising for real experiment |
ssenwkdw | 1:c227f3064399 | 321 | printf("communication for connection is started\r\n"); |
ssenwkdw | 1:c227f3064399 | 322 | |
ssenwkdw | 3:3f4aa38b0fb7 | 323 | ble.gap().onConnection(connectionCallback); |
ssenwkdw | 3:3f4aa38b0fb7 | 324 | ble.gap().onDisconnection(disconnectionCallback); |
DDUdui | 0:482d3fcc94f4 | 325 | |
ssenwkdw | 1:c227f3064399 | 326 | ble.gattClient().onDataRead(triggerToggledWrite); |
ssenwkdw | 1:c227f3064399 | 327 | ble.gattClient().onDataWrite(onDataWriteCallback); |
ssenwkdw | 1:c227f3064399 | 328 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
DDUdui | 0:482d3fcc94f4 | 329 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ssenwkdw | 3:3f4aa38b0fb7 | 330 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)secure_code, sizeof(secure_code)); |
ssenwkdw | 1:c227f3064399 | 331 | ble.gap().setAdvertisingInterval(29); |
ssenwkdw | 1:c227f3064399 | 332 | |
ssenwkdw | 1:c227f3064399 | 333 | ble.addService(customService); |
DDUdui | 0:482d3fcc94f4 | 334 | ble.gap().startAdvertising(); |
ssenwkdw | 1:c227f3064399 | 335 | ble.gap().setScanParams(190 /* scan interval */, 181 /* scan window */); |
ssenwkdw | 1:c227f3064399 | 336 | ble.gap().startScan(advertisementCallback); |
DDUdui | 0:482d3fcc94f4 | 337 | |
DDUdui | 0:482d3fcc94f4 | 338 | while (true) { |
DDUdui | 0:482d3fcc94f4 | 339 | ble.waitForEvent(); |
DDUdui | 0:482d3fcc94f4 | 340 | } |
ssenwkdw | 1:c227f3064399 | 341 | } |