160801

Dependencies:   BLE_API mbed nRF51822

Fork of 151006_1st_Scenario_normal by ramo

Committer:
ssenwkdw
Date:
Mon Aug 01 05:38:34 2016 +0000
Revision:
5:beffbecc70c6
Parent:
4:e80d411f87ae
121212

Who changed what in which revision?

UserRevisionLine numberNew 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 */
ssenwkdw 4:e80d411f87ae 16 #include <cstdlib>
ssenwkdw 4:e80d411f87ae 17 #include <ctime>
ssenwkdw 4:e80d411f87ae 18 #include <math.h>
DDUdui 0:482d3fcc94f4 19 #include "mbed.h"
DDUdui 0:482d3fcc94f4 20 #include "ble/BLE.h"
DDUdui 0:482d3fcc94f4 21 #include "ble/DiscoveredCharacteristic.h"
DDUdui 0:482d3fcc94f4 22 #include "ble/DiscoveredService.h"
ssenwkdw 1:c227f3064399 23
DDUdui 0:482d3fcc94f4 24 BLE ble;
ssenwkdw 1:c227f3064399 25 DigitalOut led(P0_19,1);
ssenwkdw 1:c227f3064399 26 DigitalOut myled(P0_8);
ssenwkdw 1:c227f3064399 27 DigitalOut neo(P0_6,1);
ssenwkdw 1:c227f3064399 28 DigitalOut pin4(P0_4);
ssenwkdw 1:c227f3064399 29 DigitalOut pin5(P0_5);
ssenwkdw 1:c227f3064399 30 DigitalOut pin15(P0_15);
ssenwkdw 1:c227f3064399 31 DigitalOut pin29(P0_29);
ssenwkdw 1:c227f3064399 32
ssenwkdw 1:c227f3064399 33 static const unsigned NUM_ROBOTS = 8;
ssenwkdw 4:e80d411f87ae 34 static const unsigned CLASS = 0;
ssenwkdw 1:c227f3064399 35
ssenwkdw 1:c227f3064399 36 uint16_t customServiceUUID = 0xA000;
ssenwkdw 1:c227f3064399 37 uint16_t readwriteCharUUID = 0xA001;
ssenwkdw 1:c227f3064399 38 static const uint16_t secure_code[] = {0xABCD};
ssenwkdw 1:c227f3064399 39
ssenwkdw 1:c227f3064399 40 uint8_t info[NUM_ROBOTS]= {0};
ssenwkdw 4:e80d411f87ae 41 uint8_t x[22] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};
ssenwkdw 4:e80d411f87ae 42 float y[22];
ssenwkdw 1:c227f3064399 43
ssenwkdw 4:e80d411f87ae 44 ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info);
ssenwkdw 1:c227f3064399 45
ssenwkdw 1:c227f3064399 46 GattCharacteristic *characteristics[] = {&readwriteChar};
ssenwkdw 1:c227f3064399 47
ssenwkdw 1:c227f3064399 48 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
ssenwkdw 1:c227f3064399 49
ssenwkdw 1:c227f3064399 50 DiscoveredCharacteristic characteristics_discovered;
DDUdui 0:482d3fcc94f4 51
ssenwkdw 4:e80d411f87ae 52 uint8_t com_p;
ssenwkdw 4:e80d411f87ae 53 bool movingTrigger = false;
ssenwkdw 1:c227f3064399 54 uint8_t info_t[NUM_ROBOTS]= {0,};
ssenwkdw 1:c227f3064399 55 uint8_t point;
ssenwkdw 1:c227f3064399 56
ssenwkdw 4:e80d411f87ae 57 float IRRead(PinName Pin) {
ssenwkdw 4:e80d411f87ae 58 AnalogIn AIn(Pin);
ssenwkdw 4:e80d411f87ae 59 float IRIn;
ssenwkdw 4:e80d411f87ae 60 IRIn=AIn;
ssenwkdw 4:e80d411f87ae 61 return(IRIn);
ssenwkdw 4:e80d411f87ae 62 }
ssenwkdw 4:e80d411f87ae 63
ssenwkdw 4:e80d411f87ae 64 void moveRandom(void)
ssenwkdw 4:e80d411f87ae 65 {
ssenwkdw 4:e80d411f87ae 66 uint8_t move_r1 = static_cast <int> (rand())%2; //0 or 1
ssenwkdw 4:e80d411f87ae 67 float move_r2 = 3 * static_cast <float> (rand()) / static_cast <float> (RAND_MAX); // 0~3
ssenwkdw 4:e80d411f87ae 68
ssenwkdw 4:e80d411f87ae 69 if( move_r1 > 0.5) {
ssenwkdw 4:e80d411f87ae 70 move_r1 = 1;
ssenwkdw 4:e80d411f87ae 71 } else {
ssenwkdw 4:e80d411f87ae 72 move_r1 = 0;
ssenwkdw 4:e80d411f87ae 73 }
ssenwkdw 4:e80d411f87ae 74
ssenwkdw 4:e80d411f87ae 75 if(move_r1==0) {
ssenwkdw 4:e80d411f87ae 76 pin4 = 1;
ssenwkdw 4:e80d411f87ae 77 pin5 = 1;
ssenwkdw 4:e80d411f87ae 78 pin15 = 0;
ssenwkdw 4:e80d411f87ae 79 pin29 = 0;
ssenwkdw 4:e80d411f87ae 80 } else {
ssenwkdw 4:e80d411f87ae 81 pin4 = 0;
ssenwkdw 4:e80d411f87ae 82 pin5 = 0;
ssenwkdw 4:e80d411f87ae 83 pin15 = 1;
ssenwkdw 4:e80d411f87ae 84 pin29 = 1;
ssenwkdw 4:e80d411f87ae 85 }
ssenwkdw 4:e80d411f87ae 86 wait(move_r2);
ssenwkdw 4:e80d411f87ae 87
ssenwkdw 4:e80d411f87ae 88 pin4 = 1;
ssenwkdw 4:e80d411f87ae 89 pin5 = 1;
ssenwkdw 4:e80d411f87ae 90 pin15 = 1;
ssenwkdw 4:e80d411f87ae 91 pin29 = 1;
ssenwkdw 4:e80d411f87ae 92 float move_r3 = 3 * static_cast <float> (rand()) / static_cast <float> (RAND_MAX); // 0~3
ssenwkdw 4:e80d411f87ae 93 wait(move_r3);
ssenwkdw 4:e80d411f87ae 94 }
ssenwkdw 4:e80d411f87ae 95
ssenwkdw 1:c227f3064399 96 void RGB_Show(uint8_t r, uint8_t g, uint8_t b)
ssenwkdw 1:c227f3064399 97 {
ssenwkdw 1:c227f3064399 98 uint8_t rgb[3] = {g, r, b};
ssenwkdw 1:c227f3064399 99 uint8_t *p = rgb;
ssenwkdw 1:c227f3064399 100 uint8_t *end = p + 3;
ssenwkdw 1:c227f3064399 101 while (p < end) {
ssenwkdw 1:c227f3064399 102 uint8_t pix = *p++;
ssenwkdw 1:c227f3064399 103 for (uint8_t mask = 0x80; mask; mask >>= 1) {
ssenwkdw 1:c227f3064399 104 if (pix & mask) {
ssenwkdw 1:c227f3064399 105 // T1H 760ns
ssenwkdw 1:c227f3064399 106 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 107 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 108 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 109 // T1L 660ns
ssenwkdw 1:c227f3064399 110 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 111 } else {
ssenwkdw 1:c227f3064399 112 // T0H 380ns
ssenwkdw 1:c227f3064399 113 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 114 // T0L 840ns
ssenwkdw 1:c227f3064399 115 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 116 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 117 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 118 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 119 }
ssenwkdw 1:c227f3064399 120 }
ssenwkdw 1:c227f3064399 121 }
ssenwkdw 1:c227f3064399 122 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 123 wait_us(100);
ssenwkdw 1:c227f3064399 124 }
DDUdui 0:482d3fcc94f4 125
DDUdui 0:482d3fcc94f4 126 void periodicCallback(void)
DDUdui 0:482d3fcc94f4 127 {
ssenwkdw 1:c227f3064399 128 led=!led;
ssenwkdw 1:c227f3064399 129 point = 0;
ssenwkdw 1:c227f3064399 130 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 1:c227f3064399 131 point+=info[i];
ssenwkdw 1:c227f3064399 132 }
ssenwkdw 1:c227f3064399 133 if(point==0) {
ssenwkdw 1:c227f3064399 134 RGB_Show(0x00,0x00,0xff);
ssenwkdw 1:c227f3064399 135 } else if(point==1) {
ssenwkdw 1:c227f3064399 136 RGB_Show(0xff,0xff,0xff);
ssenwkdw 1:c227f3064399 137 } else if(point==2) {
ssenwkdw 1:c227f3064399 138 RGB_Show(0x00,0xff,0x00);
ssenwkdw 1:c227f3064399 139 } else if(point==3) {
ssenwkdw 1:c227f3064399 140 RGB_Show(0x00,0xff,0xff);
ssenwkdw 1:c227f3064399 141 } else if(point==4) {
ssenwkdw 1:c227f3064399 142 RGB_Show(0xff,0x00,0x00);
ssenwkdw 1:c227f3064399 143 } else if(point==5) {
ssenwkdw 1:c227f3064399 144 RGB_Show(0xff,0x00,0xff);
ssenwkdw 1:c227f3064399 145 } else if(point==6) {
ssenwkdw 1:c227f3064399 146 RGB_Show(0xff,0xff,0x00);
ssenwkdw 1:c227f3064399 147 } else if(point==7) {
ssenwkdw 1:c227f3064399 148 RGB_Show(0xff,0xff,0xff);
ssenwkdw 1:c227f3064399 149 } else if(point==8) {
ssenwkdw 1:c227f3064399 150 RGB_Show(0x00,0x00,0xff);
ssenwkdw 1:c227f3064399 151 }
DDUdui 0:482d3fcc94f4 152 }
DDUdui 0:482d3fcc94f4 153
ssenwkdw 4:e80d411f87ae 154 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
ssenwkdw 1:c227f3064399 155 {
ssenwkdw 4:e80d411f87ae 156 float ran=static_cast <float> (rand()) / static_cast <float> (RAND_MAX); // 0~1 probability
ssenwkdw 4:e80d411f87ae 157 printf("com_p : %f OOOOOOOOOOOOOO ran : %f \r\n",y[com_p],ran);
ssenwkdw 4:e80d411f87ae 158 if( (params->rssi > -48) && (y[com_p] > ran) && (params->advertisingData[1] == 0xAF) ) {
ssenwkdw 4:e80d411f87ae 159 ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
ssenwkdw 4:e80d411f87ae 160 } else {
ssenwkdw 4:e80d411f87ae 161 movingTrigger = true;
DDUdui 0:482d3fcc94f4 162 }
DDUdui 0:482d3fcc94f4 163 }
DDUdui 0:482d3fcc94f4 164
ssenwkdw 4:e80d411f87ae 165 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
ssenwkdw 1:c227f3064399 166 {
ssenwkdw 4:e80d411f87ae 167 if (characteristicP->getUUID() == 0xA001) { /* !ALERT! Alter this filter to suit your device. */
ssenwkdw 4:e80d411f87ae 168 characteristics_discovered = *characteristicP;
ssenwkdw 4:e80d411f87ae 169 printf("characteristic discovered\r\n");
ssenwkdw 4:e80d411f87ae 170 characteristics_discovered.read();
ssenwkdw 1:c227f3064399 171 }
ssenwkdw 1:c227f3064399 172 }
ssenwkdw 1:c227f3064399 173
ssenwkdw 1:c227f3064399 174 void serviceDiscoveryCallback(const DiscoveredService *service)
ssenwkdw 1:c227f3064399 175 {
ssenwkdw 1:c227f3064399 176 }
ssenwkdw 1:c227f3064399 177
ssenwkdw 1:c227f3064399 178 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
ssenwkdw 1:c227f3064399 179 {
ssenwkdw 4:e80d411f87ae 180 info[7] += 1;
ssenwkdw 1:c227f3064399 181 ble.gap().stopAdvertising();
ssenwkdw 1:c227f3064399 182 ble.gap().stopScan();
ssenwkdw 4:e80d411f87ae 183
ssenwkdw 4:e80d411f87ae 184 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x]\r\n",
ssenwkdw 4:e80d411f87ae 185 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
ssenwkdw 4:e80d411f87ae 186 movingTrigger = false;
ssenwkdw 4:e80d411f87ae 187 printf("connected\r\n");
ssenwkdw 1:c227f3064399 188 if (params->role == Gap::CENTRAL) {
ssenwkdw 1:c227f3064399 189 printf("CENTRAL\r\n");
ssenwkdw 1:c227f3064399 190 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001);
ssenwkdw 1:c227f3064399 191 } else {
ssenwkdw 1:c227f3064399 192 printf("PERIPHERAL\r\n");
ssenwkdw 1:c227f3064399 193 }
DDUdui 0:482d3fcc94f4 194 }
ssenwkdw 1:c227f3064399 195
ssenwkdw 4:e80d411f87ae 196 void onDataReadCallback(const GattReadCallbackParams *response)
ssenwkdw 1:c227f3064399 197 {
ssenwkdw 1:c227f3064399 198 if (response->handle == characteristics_discovered.getValueHandle()) {
ssenwkdw 4:e80d411f87ae 199 printf("info_t : ");
ssenwkdw 1:c227f3064399 200 for(int i=0; i < NUM_ROBOTS; i++) {
ssenwkdw 4:e80d411f87ae 201 info_t[i] = response->data[i];
ssenwkdw 4:e80d411f87ae 202 printf("%u \t",info_t[i]);
ssenwkdw 1:c227f3064399 203 }
ssenwkdw 4:e80d411f87ae 204 printf("\r\n");
ssenwkdw 4:e80d411f87ae 205
ssenwkdw 4:e80d411f87ae 206 if(info[0] == 0) {
ssenwkdw 4:e80d411f87ae 207 if(response->data[0] == 0) {
ssenwkdw 4:e80d411f87ae 208 info[1] += 1; // +1 point
ssenwkdw 4:e80d411f87ae 209 info[2] = 0; // connection with normal
ssenwkdw 4:e80d411f87ae 210 info_t[1] += 1;
ssenwkdw 4:e80d411f87ae 211 info_t[2] = 0;
ssenwkdw 4:e80d411f87ae 212 characteristics_discovered.write(NUM_ROBOTS,info_t);
ssenwkdw 4:e80d411f87ae 213 } else {
ssenwkdw 4:e80d411f87ae 214 info[3] += 1; // -1 point
ssenwkdw 4:e80d411f87ae 215 info[2] = 1; // connection with malicious
ssenwkdw 4:e80d411f87ae 216 characteristics_discovered.write(NUM_ROBOTS,info_t);
ssenwkdw 4:e80d411f87ae 217 }
ssenwkdw 4:e80d411f87ae 218 } else {
ssenwkdw 4:e80d411f87ae 219 if(response->data[0] == 0) {
ssenwkdw 4:e80d411f87ae 220 info_t[3] += 1;
ssenwkdw 4:e80d411f87ae 221 info_t[2] = 1;
ssenwkdw 4:e80d411f87ae 222 characteristics_discovered.write(NUM_ROBOTS,info_t);
ssenwkdw 4:e80d411f87ae 223 printf("aaa\r\n");
ssenwkdw 4:e80d411f87ae 224 } else {
ssenwkdw 4:e80d411f87ae 225 characteristics_discovered.write(NUM_ROBOTS,info_t);
ssenwkdw 4:e80d411f87ae 226 }
ssenwkdw 4:e80d411f87ae 227 }
DDUdui 0:482d3fcc94f4 228 }
ssenwkdw 1:c227f3064399 229 }
ssenwkdw 1:c227f3064399 230
ssenwkdw 1:c227f3064399 231 void onDataWriteCallback(const GattWriteCallbackParams *params)
ssenwkdw 1:c227f3064399 232 {
ssenwkdw 4:e80d411f87ae 233 printf("bbb\r\n");
ssenwkdw 4:e80d411f87ae 234 ble.updateCharacteristicValue(readwriteChar.getValueHandle(),info,NUM_ROBOTS);
DDUdui 0:482d3fcc94f4 235 }
ssenwkdw 1:c227f3064399 236
ssenwkdw 1:c227f3064399 237 void onDataWrittenCallback(const GattWriteCallbackParams *params)
ssenwkdw 1:c227f3064399 238 {
ssenwkdw 4:e80d411f87ae 239 printf("ccc\r\n");
ssenwkdw 4:e80d411f87ae 240 for(int i=0; i<NUM_ROBOTS-1; i++) {
ssenwkdw 1:c227f3064399 241 info[i] = params->data[i];
ssenwkdw 1:c227f3064399 242 }
ssenwkdw 4:e80d411f87ae 243 printf("writecallbackparam : ");
ssenwkdw 4:e80d411f87ae 244 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 4:e80d411f87ae 245 printf("%u \t",params->data[i]);
ssenwkdw 4:e80d411f87ae 246 }
ssenwkdw 4:e80d411f87ae 247 printf("\r\n");
ssenwkdw 4:e80d411f87ae 248 ble.gattServer().write(readwriteChar.getValueHandle(),info,NUM_ROBOTS);
ssenwkdw 4:e80d411f87ae 249 ble.updateCharacteristicValue(readwriteChar.getValueHandle(),params->data,params->len);
ssenwkdw 1:c227f3064399 250 printf("data is written well\r\n");
ssenwkdw 1:c227f3064399 251
ssenwkdw 1:c227f3064399 252 ble.gap().disconnect(params->connHandle,Gap::REMOTE_USER_TERMINATED_CONNECTION);
ssenwkdw 1:c227f3064399 253 }
ssenwkdw 1:c227f3064399 254
DDUdui 0:482d3fcc94f4 255 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
DDUdui 0:482d3fcc94f4 256 {
ssenwkdw 4:e80d411f87ae 257 printf("Current info : ");
ssenwkdw 1:c227f3064399 258 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 4:e80d411f87ae 259 printf("%u \t",info[i]);
ssenwkdw 1:c227f3064399 260 }
ssenwkdw 1:c227f3064399 261 printf("\r\n");
ssenwkdw 4:e80d411f87ae 262 printf("disconnected\r\n");
ssenwkdw 4:e80d411f87ae 263 if(info[2] == 0) {
ssenwkdw 4:e80d411f87ae 264 if(com_p == 21) {
ssenwkdw 4:e80d411f87ae 265 com_p = 21;
ssenwkdw 4:e80d411f87ae 266 } else {
ssenwkdw 4:e80d411f87ae 267 com_p += 1;
ssenwkdw 4:e80d411f87ae 268 }
ssenwkdw 4:e80d411f87ae 269 } else if(info[2] == 1) {
ssenwkdw 4:e80d411f87ae 270 if(com_p == 0) {
ssenwkdw 4:e80d411f87ae 271 com_p = 0;
ssenwkdw 4:e80d411f87ae 272 } else {
ssenwkdw 4:e80d411f87ae 273 com_p -= 1;
ssenwkdw 4:e80d411f87ae 274 }
ssenwkdw 4:e80d411f87ae 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 int main(void)
ssenwkdw 4:e80d411f87ae 281 {
ssenwkdw 4:e80d411f87ae 282 srand (static_cast <unsigned> (time(0))); //seeding for random
ssenwkdw 4:e80d411f87ae 283 for(int i=0; i<22;i++){
ssenwkdw 4:e80d411f87ae 284 y[i] = 0.1 + 0.9*(1-exp(-0.2*x[i]));
ssenwkdw 4:e80d411f87ae 285 printf("%f ",y[i]);
ssenwkdw 4:e80d411f87ae 286 }
ssenwkdw 4:e80d411f87ae 287 printf("\r\n");
ssenwkdw 4:e80d411f87ae 288 info[0] = CLASS; // normal : 0 & malicious : 1
ssenwkdw 4:e80d411f87ae 289 info[1] = 0; // point
ssenwkdw 4:e80d411f87ae 290 com_p = 11;
ssenwkdw 1:c227f3064399 291
ssenwkdw 4:e80d411f87ae 292 srand (static_cast <unsigned> (time(0)));
ssenwkdw 1:c227f3064399 293
ssenwkdw 4:e80d411f87ae 294 Ticker ticker;
ssenwkdw 4:e80d411f87ae 295
ssenwkdw 1:c227f3064399 296 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 1:c227f3064399 297 printf("%u \t",info[i]);
ssenwkdw 1:c227f3064399 298 }
ssenwkdw 1:c227f3064399 299 printf("\r\n");
ssenwkdw 1:c227f3064399 300
ssenwkdw 1:c227f3064399 301 printf("communication for connection is started\r\n");
ssenwkdw 4:e80d411f87ae 302 ble.init();
ssenwkdw 3:3f4aa38b0fb7 303 ble.gap().onConnection(connectionCallback);
ssenwkdw 3:3f4aa38b0fb7 304 ble.gap().onDisconnection(disconnectionCallback);
DDUdui 0:482d3fcc94f4 305
ssenwkdw 4:e80d411f87ae 306 ble.gattClient().onDataRead(onDataReadCallback);
ssenwkdw 1:c227f3064399 307 ble.gattClient().onDataWrite(onDataWriteCallback);
ssenwkdw 1:c227f3064399 308 ble.gattServer().onDataWritten(onDataWrittenCallback);
DDUdui 0:482d3fcc94f4 309 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ssenwkdw 3:3f4aa38b0fb7 310 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)secure_code, sizeof(secure_code));
ssenwkdw 4:e80d411f87ae 311 ble.gap().setAdvertisingInterval(59);
ssenwkdw 1:c227f3064399 312
ssenwkdw 1:c227f3064399 313 ble.addService(customService);
ssenwkdw 4:e80d411f87ae 314
DDUdui 0:482d3fcc94f4 315 ble.gap().startAdvertising();
ssenwkdw 4:e80d411f87ae 316 ble.gap().setScanParams(125 /* scan interval */, 79 /* scan window */);
ssenwkdw 1:c227f3064399 317 ble.gap().startScan(advertisementCallback);
DDUdui 0:482d3fcc94f4 318
DDUdui 0:482d3fcc94f4 319 while (true) {
ssenwkdw 4:e80d411f87ae 320 /*
ssenwkdw 4:e80d411f87ae 321 if(connectionState == false) {
ssenwkdw 4:e80d411f87ae 322 ble.gap().startAdvertising();
ssenwkdw 4:e80d411f87ae 323 ble.gap().startScan(advertisementCallback);
ssenwkdw 4:e80d411f87ae 324 } else {
ssenwkdw 4:e80d411f87ae 325 ble.gap().stopAdvertising();
ssenwkdw 4:e80d411f87ae 326 ble.gap().stopScan();
ssenwkdw 4:e80d411f87ae 327 }
ssenwkdw 4:e80d411f87ae 328 */
DDUdui 0:482d3fcc94f4 329 ble.waitForEvent();
ssenwkdw 4:e80d411f87ae 330 if(movingTrigger == true) {
ssenwkdw 4:e80d411f87ae 331 moveRandom();
ssenwkdw 4:e80d411f87ae 332 } else {
ssenwkdw 4:e80d411f87ae 333 pin4 = 0;
ssenwkdw 4:e80d411f87ae 334 pin5 = 0;
ssenwkdw 4:e80d411f87ae 335 pin15 = 0;
ssenwkdw 4:e80d411f87ae 336 pin29 = 0;
ssenwkdw 4:e80d411f87ae 337 }
ssenwkdw 4:e80d411f87ae 338 if(info[1] == 20) {
ssenwkdw 4:e80d411f87ae 339 printf("Game over");
ssenwkdw 4:e80d411f87ae 340 break;
ssenwkdw 4:e80d411f87ae 341 }
DDUdui 0:482d3fcc94f4 342 }
ssenwkdw 4:e80d411f87ae 343 ble.shutdown();
ssenwkdw 4:e80d411f87ae 344 pin4 = 0;
ssenwkdw 4:e80d411f87ae 345 pin5 = 0;
ssenwkdw 4:e80d411f87ae 346 pin15 = 0;
ssenwkdw 4:e80d411f87ae 347 pin29 = 0;
ssenwkdw 1:c227f3064399 348 }