Ble Demo with Raspberry PI

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_DEMO_SPI_HARDCODE by HM_IOT_Demo

Committer:
varshaberya123
Date:
Thu Oct 12 09:07:57 2017 +0000
Revision:
17:2e0273d799e2
Parent:
16:5bd717644078
Child:
18:0eefac249a4b
BLE_DEMO_SPI_HARDCODE_UPDATED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmiot 7:66586d2d7cb5 1 /************************************************************************
hmiot 7:66586d2d7cb5 2 * *
hmiot 7:66586d2d7cb5 3 * *
hmiot 7:66586d2d7cb5 4 ************************************************************************/
hmiot 6:71e7a446ae6a 5
hmiot 7:66586d2d7cb5 6 #include "hm_config.h"
hmiot 7:66586d2d7cb5 7
hmiot 13:9b2a587ee7ac 8 DiscoveredCharacteristic lightCharacteristic;
hariom 16:5bd717644078 9 Gap::Handle_t connectionHandle = 0xFFFF;
hariom 16:5bd717644078 10 //static s_serviceInfo lightChar = {NULL,NULL};
hmiot 6:71e7a446ae6a 11 Serial pc(USBTX, USBRX);
hmiot 9:b967a01810e1 12 void bleint();
hmiot 10:5e5fa5bf77b5 13 void waitBleEvent();
hmiot 10:5e5fa5bf77b5 14 void ble_actions(int spi_data);
hmiot 13:9b2a587ee7ac 15 uint8_t statusWR;
hmiot 8:285ebd0e83fb 16 SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10);
hariom 16:5bd717644078 17 lightCharacteristic_t ptr_temp_char;
hariom 16:5bd717644078 18
hariom 16:5bd717644078 19 //DiscoveredCharacteristic ptr_temp_char;
hariom 16:5bd717644078 20
hariom 16:5bd717644078 21 bool connection_handle = 0;
hariom 16:5bd717644078 22
hariom 16:5bd717644078 23 uint8_t Ble_address[6];
hmiot 8:285ebd0e83fb 24
mbed_tw_hoehoe 3:d6f80e11a7f4 25
hmiot 6:71e7a446ae6a 26 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 27 uint8_t con_status =0;
hariom 16:5bd717644078 28 int i;
hmiot 6:71e7a446ae6a 29 //0x51 for Magic light
hmiot 6:71e7a446ae6a 30 //0x5A
hariom 16:5bd717644078 31
hariom 16:5bd717644078 32 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n",
hariom 16:5bd717644078 33 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
hariom 16:5bd717644078 34 params->rssi, params->isScanResponse, params->type, params->advertisingData);
hariom 16:5bd717644078 35 //if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
varshaberya123 17:2e0273d799e2 36 if (params->peerAddr[0] == 0xE5){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
hmiot 8:285ebd0e83fb 37
hmiot 8:285ebd0e83fb 38 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 39 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 40 params->rssi, params->isScanResponse, params->type);
hmiot 6:71e7a446ae6a 41 pc.printf("Data Length : %d\r\n",params->advertisingDataLen);
hariom 16:5bd717644078 42 for (i = 0; i < 6; i++)
hariom 16:5bd717644078 43 Ble_address[i] = params->peerAddr[i];
hariom 16:5bd717644078 44
hmiot 8:285ebd0e83fb 45 con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL);
hmiot 8:285ebd0e83fb 46 pc.printf("Connection Status : %d\r\n",con_status);
hmiot 8:285ebd0e83fb 47 connect_status = 1;
hmiot 13:9b2a587ee7ac 48 BLE::Instance().gap().stopScan();
hariom 16:5bd717644078 49
hariom 16:5bd717644078 50 }
hariom 16:5bd717644078 51 else{
hmiot 11:58d8a3129877 52 printf("Not Matched your device\r\n");
hmiot 11:58d8a3129877 53 return;
hmiot 11:58d8a3129877 54 }
hmiot 8:285ebd0e83fb 55 }
mbed_tw_hoehoe 0:83b5c6efd8d7 56
mbed_tw_hoehoe 0:83b5c6efd8d7 57 void serviceDiscoveryCallback(const DiscoveredService *service) {
hariom 16:5bd717644078 58 //pc.printf("Service Discovery Callback\r\n");
hmiot 6:71e7a446ae6a 59 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
hariom 16:5bd717644078 60 //pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 61 } else {
hariom 16:5bd717644078 62 //pc.printf("S UUID-");
hmiot 6:71e7a446ae6a 63 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
hmiot 6:71e7a446ae6a 64 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
hmiot 6:71e7a446ae6a 65 printf("%02x", longUUIDBytes[i]);
hmiot 6:71e7a446ae6a 66 }
hariom 16:5bd717644078 67 //pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 68 }
mbed_tw_hoehoe 0:83b5c6efd8d7 69 }
mbed_tw_hoehoe 0:83b5c6efd8d7 70
mbed_tw_hoehoe 0:83b5c6efd8d7 71 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
hmiot 6:71e7a446ae6a 72 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
hmiot 13:9b2a587ee7ac 73
varshaberya123 17:2e0273d799e2 74 if (characteristicP->getUUID().getShortUUID() == 0xec0e) { /* !ALERT! Alter this filter to suit your device. */
hmiot 8:285ebd0e83fb 75 lightCharacteristic = *characteristicP;
hmiot 6:71e7a446ae6a 76 pc.printf("Matched char UUID\r\n");
hmiot 8:285ebd0e83fb 77 printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle());
hmiot 8:285ebd0e83fb 78 printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle());
hmiot 10:5e5fa5bf77b5 79 AddCharNodes(lightCharacteristic);
hmiot 9:b967a01810e1 80 charDiscover = 1;
hmiot 9:b967a01810e1 81 serviceDiscover = true;
hmiot 9:b967a01810e1 82 }else {
hmiot 8:285ebd0e83fb 83 printf("Not Matched char UUID\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 84 }
mbed_tw_hoehoe 0:83b5c6efd8d7 85 }
mbed_tw_hoehoe 0:83b5c6efd8d7 86
hmiot 8:285ebd0e83fb 87 void AddCharNodes(DiscoveredCharacteristic light_Characteristic)
hmiot 8:285ebd0e83fb 88 {
hariom 16:5bd717644078 89 //printf("Add Char Nodes\r\n");
hariom 16:5bd717644078 90 // lightCharacteristic_t ptr_temp_char;
hariom 16:5bd717644078 91 /*
hmiot 8:285ebd0e83fb 92 ptr_temp_char = lightChar.services_Char[0];
hmiot 8:285ebd0e83fb 93
hmiot 13:9b2a587ee7ac 94
hmiot 8:285ebd0e83fb 95 if(NULL == ptr_temp_char)
hmiot 8:285ebd0e83fb 96 {
hmiot 12:cd54180657ee 97 ptr_temp_char = new lightCharacteristic_t;
hariom 16:5bd717644078 98 lightChar.services_Char[0] = ptr_temp_char;
hmiot 8:285ebd0e83fb 99 }
hmiot 8:285ebd0e83fb 100 else
hmiot 8:285ebd0e83fb 101 {
hmiot 8:285ebd0e83fb 102 while(NULL != ptr_temp_char->nextChar)
hmiot 8:285ebd0e83fb 103 {
hmiot 8:285ebd0e83fb 104 ptr_temp_char = ptr_temp_char->nextChar;
hmiot 8:285ebd0e83fb 105 }
hmiot 8:285ebd0e83fb 106
hariom 16:5bd717644078 107 /* add a new node
hariom 16:5bd717644078 108 ptr_temp_char->nextChar = new lightCharacteristic_t;
hmiot 8:285ebd0e83fb 109 ptr_temp_char = ptr_temp_char->nextChar;
hariom 16:5bd717644078 110 }*/
hmiot 8:285ebd0e83fb 111
hmiot 8:285ebd0e83fb 112 /* assign data to the node */
hariom 16:5bd717644078 113 ptr_temp_char.u_characteristicID = 1;
hariom 16:5bd717644078 114 pc.printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle());
hariom 16:5bd717644078 115 pc.printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle());
hariom 16:5bd717644078 116 ptr_temp_char.connHandle = light_Characteristic.getConnectionHandle();
hariom 16:5bd717644078 117 ptr_temp_char.valueHandle = light_Characteristic.getValueHandle();
hariom 16:5bd717644078 118 //ptr_temp_char.nextChar = NULL;
hmiot 8:285ebd0e83fb 119 }
hmiot 8:285ebd0e83fb 120
mbed_tw_hoehoe 0:83b5c6efd8d7 121 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 122 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
hmiot 8:285ebd0e83fb 123 serviceDiscover = false;
mbed_tw_hoehoe 0:83b5c6efd8d7 124 }
mbed_tw_hoehoe 0:83b5c6efd8d7 125
hmiot 6:71e7a446ae6a 126 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
hariom 16:5bd717644078 127 //pc.printf("Connection Callback\n\r");
hmiot 6:71e7a446ae6a 128 if (params->role == Gap::CENTRAL) {
hariom 16:5bd717644078 129 connectionHandle = params->handle;
hariom 16:5bd717644078 130 //pc.printf("Service and characterstics discovery callback\r\n");
hmiot 6:71e7a446ae6a 131 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
hmiot 6:71e7a446ae6a 132 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9);
hmiot 10:5e5fa5bf77b5 133 // 0xffe5 --> Services UUID
hmiot 10:5e5fa5bf77b5 134 // 0xffe9 --> Characteristics UUID
mbed_tw_hoehoe 0:83b5c6efd8d7 135 }
hmiot 6:71e7a446ae6a 136 }
hmiot 6:71e7a446ae6a 137
hariom 16:5bd717644078 138 void writeCallback(const GattWriteCallbackParams *eventDataP)
hariom 16:5bd717644078 139 {
hariom 16:5bd717644078 140 pc.printf("written callback\r\n");
hariom 16:5bd717644078 141 }
hariom 16:5bd717644078 142
hmiot 6:71e7a446ae6a 143 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 144 pc.printf("disconnected\r\n");
hmiot 8:285ebd0e83fb 145 BLE::Instance().gap().startScan(advertisementCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 146 }
mbed_tw_hoehoe 0:83b5c6efd8d7 147
hmiot 6:71e7a446ae6a 148
hariom 16:5bd717644078 149 void hvxCallback(const GattHVXCallbackParams *params) {
hariom 16:5bd717644078 150 printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication");
hariom 16:5bd717644078 151 for (unsigned index = 0; index < params->len; index++) {
hariom 16:5bd717644078 152 printf(" %02x", params->data[index]);
hariom 16:5bd717644078 153 }
hariom 16:5bd717644078 154 printf("\r\n");
hariom 16:5bd717644078 155 }
varshaberya123 17:2e0273d799e2 156 void triggerRead(const GattReadCallbackParams *response)
varshaberya123 17:2e0273d799e2 157 {
varshaberya123 17:2e0273d799e2 158 printf("reading data : \r\n");
varshaberya123 17:2e0273d799e2 159 // if (response->handle == GattCharacteristic.getValueHandle())
varshaberya123 17:2e0273d799e2 160 // if (response->handle == characteristicP->getValueHandle())
varshaberya123 17:2e0273d799e2 161 {
varshaberya123 17:2e0273d799e2 162 printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
varshaberya123 17:2e0273d799e2 163 for (unsigned index = 0; index < response->len; index++)
varshaberya123 17:2e0273d799e2 164 {
varshaberya123 17:2e0273d799e2 165 printf("%c[%02x]", response->data[index], response->data[index]);
varshaberya123 17:2e0273d799e2 166 }
varshaberya123 17:2e0273d799e2 167 printf("\r\n");
varshaberya123 17:2e0273d799e2 168 }
varshaberya123 17:2e0273d799e2 169 }
hariom 16:5bd717644078 170
hmiot 6:71e7a446ae6a 171 /**
hmiot 6:71e7a446ae6a 172 * Callback triggered when the ble initialization process has finished
hmiot 6:71e7a446ae6a 173 */
hmiot 6:71e7a446ae6a 174 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
hmiot 6:71e7a446ae6a 175 {
hmiot 6:71e7a446ae6a 176 BLE& ble = params->ble;
hmiot 8:285ebd0e83fb 177
hmiot 6:71e7a446ae6a 178 pc.printf("Ble Init\n\r");
hmiot 9:b967a01810e1 179 /* Ensure that it is the default instance of BLE */
hmiot 6:71e7a446ae6a 180 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
hmiot 6:71e7a446ae6a 181 return;
hmiot 9:b967a01810e1 182 }
hmiot 6:71e7a446ae6a 183
hmiot 9:b967a01810e1 184 ble.gap().onConnection(connectionCallback);
hmiot 9:b967a01810e1 185 ble.gap().onDisconnection(disconnectionCallback);
hmiot 9:b967a01810e1 186 ble.gap().setScanParams(1000, 800);
hmiot 9:b967a01810e1 187 ble.gap().startScan(advertisementCallback);
hariom 16:5bd717644078 188 //ble.gattClient().onHVX(hvxCallback);
varshaberya123 17:2e0273d799e2 189 ble.gattClient().onDataRead(triggerRead);
hmiot 10:5e5fa5bf77b5 190
hmiot 8:285ebd0e83fb 191 }
hmiot 8:285ebd0e83fb 192
hariom 16:5bd717644078 193 void light_actions(int * service_char)
hariom 16:5bd717644078 194 {
hariom 16:5bd717644078 195 uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00},
hariom 16:5bd717644078 196 {0x00,0x00,0xFF},{0xFF,0xFF,0x00},
hariom 16:5bd717644078 197 {0x00,0xFF,0xFF},{0xFF,0x00,0xFF}
hariom 16:5bd717644078 198 };
hariom 16:5bd717644078 199 uint8_t pre_write_data[16];
hariom 16:5bd717644078 200 uint8_t write_data[16];
hariom 16:5bd717644078 201
hariom 16:5bd717644078 202 //lightCharacteristic_t *ptr_tmp = NULL;
hariom 16:5bd717644078 203 //ptr_tmp = lightChar.services_Char[0];
hmiot 13:9b2a587ee7ac 204 uint8_t numCount;
hmiot 10:5e5fa5bf77b5 205 if(connect_status)
hmiot 10:5e5fa5bf77b5 206 {
hmiot 9:b967a01810e1 207
hmiot 10:5e5fa5bf77b5 208 switch(service_char[2])
hmiot 10:5e5fa5bf77b5 209 {
hmiot 10:5e5fa5bf77b5 210 case TURN_OFF: // off
hmiot 10:5e5fa5bf77b5 211 write_data[0]=0x56;
hmiot 10:5e5fa5bf77b5 212 write_data[1]=0x00;
hmiot 10:5e5fa5bf77b5 213 write_data[2]=0x00;
hmiot 10:5e5fa5bf77b5 214 write_data[3]=0x00;
hmiot 10:5e5fa5bf77b5 215 write_data[4]=0x00;
hmiot 10:5e5fa5bf77b5 216 write_data[5]=0xf0;
hmiot 10:5e5fa5bf77b5 217 write_data[6]=0xaa;
hariom 16:5bd717644078 218 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
hariom 16:5bd717644078 219 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data);
hariom 16:5bd717644078 220 BLE::Instance().processEvents();
hariom 16:5bd717644078 221 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data);
hariom 16:5bd717644078 222 //BLE::Instance().updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), write_data, 7);
hmiot 10:5e5fa5bf77b5 223 break;
hmiot 8:285ebd0e83fb 224
hmiot 10:5e5fa5bf77b5 225 case TURN_ON: //on
hmiot 10:5e5fa5bf77b5 226 pre_write_data[0]=0x56;
hmiot 10:5e5fa5bf77b5 227 pre_write_data[1]=0xDE;
hmiot 10:5e5fa5bf77b5 228 pre_write_data[2]=0x8E;
hmiot 10:5e5fa5bf77b5 229 pre_write_data[3]=0xEE;
hmiot 10:5e5fa5bf77b5 230 pre_write_data[4]=0x00;
hmiot 10:5e5fa5bf77b5 231 pre_write_data[5]=0xf0;
hmiot 10:5e5fa5bf77b5 232 pre_write_data[6]=0xaa;
hmiot 15:ef3cf37827d2 233
hariom 16:5bd717644078 234 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data);
hariom 16:5bd717644078 235 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,pre_write_data);
hariom 16:5bd717644078 236 BLE::Instance().processEvents();
hariom 16:5bd717644078 237 //error_status = BLE::Instance().DiscoveredCharacteristic().write(7,pre_write_data);
hmiot 10:5e5fa5bf77b5 238 break;
hmiot 8:285ebd0e83fb 239
hmiot 10:5e5fa5bf77b5 240 case COLOR: // Color
hmiot 13:9b2a587ee7ac 241 write_data[0]=0x56;
hmiot 13:9b2a587ee7ac 242 write_data[1]=lightColor[service_char[3]][0];//0xff
hmiot 13:9b2a587ee7ac 243 write_data[2]=lightColor[service_char[3]][1];//0x00;
hmiot 13:9b2a587ee7ac 244 write_data[3]=lightColor[service_char[3]][2];//0x00;
hmiot 13:9b2a587ee7ac 245 write_data[4]=0x00;
hmiot 13:9b2a587ee7ac 246 write_data[5]=0xf0;
hmiot 13:9b2a587ee7ac 247 write_data[6]=0xaa;
hmiot 13:9b2a587ee7ac 248 for(numCount=0; numCount < 7; numCount++){
hmiot 13:9b2a587ee7ac 249 pre_write_data[numCount] = write_data[numCount];
hmiot 12:cd54180657ee 250 }
hmiot 13:9b2a587ee7ac 251 first_on=false;
hariom 16:5bd717644078 252 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
hariom 16:5bd717644078 253 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data);
hariom 16:5bd717644078 254 BLE::Instance().processEvents();
hariom 16:5bd717644078 255 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data);
hariom 16:5bd717644078 256
hmiot 10:5e5fa5bf77b5 257 break;
hmiot 8:285ebd0e83fb 258
hmiot 10:5e5fa5bf77b5 259 default:
hmiot 10:5e5fa5bf77b5 260 printf("Invalid Options\r\n");
hmiot 10:5e5fa5bf77b5 261 }
hmiot 10:5e5fa5bf77b5 262 printf("Error Status =%d\n\r",error_status);
hmiot 13:9b2a587ee7ac 263 }
hmiot 8:285ebd0e83fb 264 }
hmiot 8:285ebd0e83fb 265
hmiot 12:cd54180657ee 266 void datancpy(char pre_write[], char writeData[], int data_size){
hmiot 8:285ebd0e83fb 267 int numCount;
hmiot 8:285ebd0e83fb 268 for(numCount=0; numCount <= data_size; numCount++){
hmiot 8:285ebd0e83fb 269 pre_write[numCount] = writeData[numCount];
hmiot 8:285ebd0e83fb 270 }
hmiot 8:285ebd0e83fb 271
mbed_tw_hoehoe 2:4b53d13d9851 272 }
mbed_tw_hoehoe 2:4b53d13d9851 273
hmiot 8:285ebd0e83fb 274 void bleint()
hmiot 8:285ebd0e83fb 275 {
hmiot 8:285ebd0e83fb 276 printf("Init\r\n");
hmiot 9:b967a01810e1 277 BLE &ble = BLE::Instance();
hmiot 8:285ebd0e83fb 278 BLE::Instance().init(bleInitComplete);
hmiot 8:285ebd0e83fb 279 }
hmiot 8:285ebd0e83fb 280
hmiot 8:285ebd0e83fb 281 int main()
hmiot 8:285ebd0e83fb 282 {
hmiot 8:285ebd0e83fb 283 // Serial port configuration
mbed_tw_hoehoe 1:2f1203d70643 284 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 285 wait(8.0);
hmiot 9:b967a01810e1 286 pc.printf("Start\n\r");
hmiot 13:9b2a587ee7ac 287
hmiot 12:cd54180657ee 288 spiSlave.reply(191);
hmiot 13:9b2a587ee7ac 289 statusWR=1;
hmiot 9:b967a01810e1 290 bleint();
hmiot 10:5e5fa5bf77b5 291 bufferSize = 0;
hariom 16:5bd717644078 292 int spiRX[5];
hariom 16:5bd717644078 293
hariom 16:5bd717644078 294 int temp = 0x00;
hariom 16:5bd717644078 295 int color = 0x00;
hariom 16:5bd717644078 296 int con_status,i;
hmiot 10:5e5fa5bf77b5 297 while(1){
hariom 16:5bd717644078 298 pc.printf("loop\r\n");
hariom 16:5bd717644078 299 wait(3.0);
hmiot 14:f84a9745c10a 300 if(!(connect_status && charDiscover))
hariom 16:5bd717644078 301 waitBleEvent();
hmiot 10:5e5fa5bf77b5 302
hariom 16:5bd717644078 303 if (temp == 2 && color == 3) {
hariom 16:5bd717644078 304 temp = 0x00;
hariom 16:5bd717644078 305 color = 0x00;
hariom 16:5bd717644078 306 }
hariom 16:5bd717644078 307 else if (temp == 2 && color < 3) {
hariom 16:5bd717644078 308 color++;
hariom 16:5bd717644078 309 }
hariom 16:5bd717644078 310 else if (temp < 2)
hariom 16:5bd717644078 311 temp++;
hariom 16:5bd717644078 312
hariom 16:5bd717644078 313 spiRX[0] = 0xB3;
hariom 16:5bd717644078 314 spiRX[1] = 1;
hariom 16:5bd717644078 315 spiRX[2] = temp;
hariom 16:5bd717644078 316 spiRX[3] = color;
hariom 16:5bd717644078 317 pc.printf("spix buffer is: ");
hariom 16:5bd717644078 318 for (i = 0; i < 4; i++) {
hariom 16:5bd717644078 319 pc.printf("%d ", spiRX[i]);
hariom 16:5bd717644078 320 }
hariom 16:5bd717644078 321 pc.printf("\n");
hariom 16:5bd717644078 322
hariom 16:5bd717644078 323 //if(spiSlave.receive()){
hariom 16:5bd717644078 324 //printf("Slave Read loop1\r\n");
hariom 16:5bd717644078 325 //spiRX[bufferSize] = spiSlave.read();
hariom 16:5bd717644078 326 //spiSlave.reply(spiRX[bufferSize]);
hariom 16:5bd717644078 327 //bufferSize++;
hariom 16:5bd717644078 328 bufferSize = 5;
hmiot 10:5e5fa5bf77b5 329
hmiot 10:5e5fa5bf77b5 330 if(bufferSize >= 5){
hmiot 10:5e5fa5bf77b5 331 bufferSize=0;
hmiot 10:5e5fa5bf77b5 332 light_actions(spiRX);
hariom 16:5bd717644078 333 //BLE::Instance().gattClient().reset();
hmiot 15:ef3cf37827d2 334 spiSlave.reply(spiRX[2]); //(0xAA);
hariom 16:5bd717644078 335
hmiot 13:9b2a587ee7ac 336
hariom 16:5bd717644078 337 if(error_status > 7)
hariom 16:5bd717644078 338 {
hariom 16:5bd717644078 339 error_status = BLE_ERROR_NONE;
hariom 16:5bd717644078 340 //wait_ms(500);
hmiot 13:9b2a587ee7ac 341 BLE::Instance().shutdown();
hmiot 13:9b2a587ee7ac 342 statusWR =1;
hmiot 13:9b2a587ee7ac 343 connect_status =0;
hmiot 13:9b2a587ee7ac 344 charDiscover =0;
hmiot 13:9b2a587ee7ac 345 serviceDiscover = true;
hmiot 13:9b2a587ee7ac 346 bleint();
hmiot 13:9b2a587ee7ac 347 }
hariom 16:5bd717644078 348 //statusWR++;
hariom 16:5bd717644078 349 wait_ms(3000);
hmiot 10:5e5fa5bf77b5 350 }
hmiot 10:5e5fa5bf77b5 351
hariom 16:5bd717644078 352 //}
hmiot 13:9b2a587ee7ac 353
hmiot 9:b967a01810e1 354 }
hmiot 9:b967a01810e1 355
hmiot 9:b967a01810e1 356 }
hmiot 9:b967a01810e1 357
hmiot 10:5e5fa5bf77b5 358 void waitBleEvent()
hmiot 10:5e5fa5bf77b5 359 {
hmiot 10:5e5fa5bf77b5 360 while(serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){
hmiot 10:5e5fa5bf77b5 361 BLE::Instance().waitForEvent();
hmiot 10:5e5fa5bf77b5 362 }
hmiot 9:b967a01810e1 363 }
hmiot 10:5e5fa5bf77b5 364