BLE_DEMO_SPI_HARDCODE
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo_23 by
main.cpp
00001 /************************************************************************ 00002 * * 00003 * * 00004 ************************************************************************/ 00005 00006 #include "hm_config.h" 00007 00008 DiscoveredCharacteristic lightCharacteristic; 00009 Gap::Handle_t connectionHandle = 0xFFFF; 00010 //static s_serviceInfo lightChar = {NULL,NULL}; 00011 Serial pc(USBTX, USBRX); 00012 void bleint(); 00013 void waitBleEvent(); 00014 void ble_actions(int spi_data); 00015 uint8_t statusWR; 00016 SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); 00017 lightCharacteristic_t ptr_temp_char; 00018 00019 //DiscoveredCharacteristic ptr_temp_char; 00020 00021 bool connection_handle = 0; 00022 00023 uint8_t Ble_address[6]; 00024 00025 00026 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { 00027 uint8_t con_status =0; 00028 int i; 00029 //0x51 for Magic light 00030 //0x5A 00031 00032 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n", 00033 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00034 params->rssi, params->isScanResponse, params->type, params->advertisingData); 00035 //if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ 00036 if (params->peerAddr[0] == 0xE5){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ 00037 00038 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", 00039 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00040 params->rssi, params->isScanResponse, params->type); 00041 pc.printf("Data Length : %d\r\n",params->advertisingDataLen); 00042 for (i = 0; i < 6; i++) 00043 Ble_address[i] = params->peerAddr[i]; 00044 00045 con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); 00046 pc.printf("Connection Status : %d\r\n",con_status); 00047 connect_status = 1; 00048 BLE::Instance().gap().stopScan(); 00049 00050 } 00051 else{ 00052 printf("Not Matched your device\r\n"); 00053 return; 00054 } 00055 } 00056 00057 void serviceDiscoveryCallback(const DiscoveredService *service) { 00058 //pc.printf("Service Discovery Callback\r\n"); 00059 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 00060 //pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 00061 } else { 00062 //pc.printf("S UUID-"); 00063 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 00064 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 00065 printf("%02x", longUUIDBytes[i]); 00066 } 00067 //pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); 00068 } 00069 } 00070 00071 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { 00072 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); 00073 00074 if (characteristicP->getUUID().getShortUUID() == 0xec0e) { /* !ALERT! Alter this filter to suit your device. */ 00075 lightCharacteristic = *characteristicP; 00076 pc.printf("Matched char UUID\r\n"); 00077 printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); 00078 printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); 00079 AddCharNodes(lightCharacteristic); 00080 charDiscover = 1; 00081 serviceDiscover = true; 00082 }else { 00083 printf("Not Matched char UUID\r\n"); 00084 } 00085 } 00086 00087 void AddCharNodes(DiscoveredCharacteristic light_Characteristic) 00088 { 00089 //printf("Add Char Nodes\r\n"); 00090 // lightCharacteristic_t ptr_temp_char; 00091 /* 00092 ptr_temp_char = lightChar.services_Char[0]; 00093 00094 00095 if(NULL == ptr_temp_char) 00096 { 00097 ptr_temp_char = new lightCharacteristic_t; 00098 lightChar.services_Char[0] = ptr_temp_char; 00099 } 00100 else 00101 { 00102 while(NULL != ptr_temp_char->nextChar) 00103 { 00104 ptr_temp_char = ptr_temp_char->nextChar; 00105 } 00106 00107 /* add a new node 00108 ptr_temp_char->nextChar = new lightCharacteristic_t; 00109 ptr_temp_char = ptr_temp_char->nextChar; 00110 }*/ 00111 00112 /* assign data to the node */ 00113 ptr_temp_char.u_characteristicID = 1; 00114 pc.printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); 00115 pc.printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); 00116 ptr_temp_char.connHandle = light_Characteristic.getConnectionHandle(); 00117 ptr_temp_char.valueHandle = light_Characteristic.getValueHandle(); 00118 //ptr_temp_char.nextChar = NULL; 00119 } 00120 00121 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { 00122 pc.printf("terminated SD for handle %u\r\n", connectionHandle); 00123 serviceDiscover = false; 00124 } 00125 00126 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 00127 //pc.printf("Connection Callback\n\r"); 00128 if (params->role == Gap::CENTRAL) { 00129 connectionHandle = params->handle; 00130 //pc.printf("Service and characterstics discovery callback\r\n"); 00131 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 00132 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9); 00133 // 0xffe5 --> Services UUID 00134 // 0xffe9 --> Characteristics UUID 00135 } 00136 } 00137 00138 void writeCallback(const GattWriteCallbackParams *eventDataP) 00139 { 00140 pc.printf("written callback\r\n"); 00141 } 00142 00143 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { 00144 pc.printf("disconnected\r\n"); 00145 BLE::Instance().gap().startScan(advertisementCallback); 00146 } 00147 00148 00149 void hvxCallback(const GattHVXCallbackParams *params) { 00150 printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); 00151 for (unsigned index = 0; index < params->len; index++) { 00152 printf(" %02x", params->data[index]); 00153 } 00154 printf("\r\n"); 00155 } 00156 void triggerRead(const GattReadCallbackParams *response) 00157 { 00158 printf("reading data : \r\n"); 00159 // if (response->handle == GattCharacteristic.getValueHandle()) 00160 // if (response->handle == characteristicP->getValueHandle()) 00161 { 00162 printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); 00163 for (unsigned index = 0; index < response->len; index++) 00164 { 00165 printf("%c[%02x]", response->data[index], response->data[index]); 00166 } 00167 printf("\r\n"); 00168 } 00169 } 00170 00171 /** 00172 * Callback triggered when the ble initialization process has finished 00173 */ 00174 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00175 { 00176 BLE& ble = params->ble; 00177 00178 pc.printf("Ble Init\n\r"); 00179 /* Ensure that it is the default instance of BLE */ 00180 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00181 return; 00182 } 00183 00184 ble.gap().onConnection(connectionCallback); 00185 ble.gap().onDisconnection(disconnectionCallback); 00186 ble.gap().setScanParams(1000, 800); 00187 ble.gap().startScan(advertisementCallback); 00188 //ble.gattClient().onHVX(hvxCallback); 00189 ble.gattClient().onDataRead(triggerRead); 00190 00191 } 00192 00193 void light_actions(int * service_char) 00194 { 00195 uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00}, 00196 {0x00,0x00,0xFF},{0xFF,0xFF,0x00}, 00197 {0x00,0xFF,0xFF},{0xFF,0x00,0xFF} 00198 }; 00199 uint8_t pre_write_data[16]; 00200 uint8_t write_data[16]; 00201 00202 //lightCharacteristic_t *ptr_tmp = NULL; 00203 //ptr_tmp = lightChar.services_Char[0]; 00204 uint8_t numCount; 00205 if(connect_status) 00206 { 00207 00208 switch(service_char[2]) 00209 { 00210 case TURN_OFF: // off 00211 write_data[0]=0x56; 00212 write_data[1]=0x00; 00213 write_data[2]=0x00; 00214 write_data[3]=0x00; 00215 write_data[4]=0x00; 00216 write_data[5]=0xf0; 00217 write_data[6]=0xaa; 00218 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); 00219 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); 00220 BLE::Instance().processEvents(); 00221 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); 00222 //BLE::Instance().updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), write_data, 7); 00223 break; 00224 00225 case TURN_ON: //on 00226 pre_write_data[0]=0x56; 00227 pre_write_data[1]=0xDE; 00228 pre_write_data[2]=0x8E; 00229 pre_write_data[3]=0xEE; 00230 pre_write_data[4]=0x00; 00231 pre_write_data[5]=0xf0; 00232 pre_write_data[6]=0xaa; 00233 00234 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data); 00235 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,pre_write_data); 00236 BLE::Instance().processEvents(); 00237 //error_status = BLE::Instance().DiscoveredCharacteristic().write(7,pre_write_data); 00238 break; 00239 00240 case COLOR: // Color 00241 write_data[0]=0x56; 00242 write_data[1]=lightColor[service_char[3]][0];//0xff 00243 write_data[2]=lightColor[service_char[3]][1];//0x00; 00244 write_data[3]=lightColor[service_char[3]][2];//0x00; 00245 write_data[4]=0x00; 00246 write_data[5]=0xf0; 00247 write_data[6]=0xaa; 00248 for(numCount=0; numCount < 7; numCount++){ 00249 pre_write_data[numCount] = write_data[numCount]; 00250 } 00251 first_on=false; 00252 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); 00253 error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); 00254 BLE::Instance().processEvents(); 00255 //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); 00256 00257 break; 00258 00259 default: 00260 printf("Invalid Options\r\n"); 00261 } 00262 printf("Error Status =%d\n\r",error_status); 00263 } 00264 } 00265 00266 void datancpy(char pre_write[], char writeData[], int data_size){ 00267 int numCount; 00268 for(numCount=0; numCount <= data_size; numCount++){ 00269 pre_write[numCount] = writeData[numCount]; 00270 } 00271 00272 } 00273 00274 void bleint() 00275 { 00276 printf("Init\r\n"); 00277 BLE &ble = BLE::Instance(); 00278 BLE::Instance().init(bleInitComplete); 00279 } 00280 00281 int main() 00282 { 00283 // Serial port configuration 00284 pc.baud(9600); 00285 wait(8.0); 00286 pc.printf("Start\n\r"); 00287 00288 spiSlave.reply(191); 00289 statusWR=1; 00290 bleint(); 00291 bufferSize = 0; 00292 int spiRX[5]; 00293 00294 int temp = 0x00; 00295 int color = 0x00; 00296 int con_status,i; 00297 while(1){ 00298 pc.printf("loop\r\n"); 00299 wait(3.0); 00300 if(!(connect_status && charDiscover)) 00301 waitBleEvent(); 00302 00303 if (temp == 2 && color == 3) { 00304 temp = 0x00; 00305 color = 0x00; 00306 } 00307 else if (temp == 2 && color < 3) { 00308 color++; 00309 } 00310 else if (temp < 2) 00311 temp++; 00312 00313 spiRX[0] = 0xB3; 00314 spiRX[1] = 1; 00315 spiRX[2] = temp; 00316 spiRX[3] = color; 00317 pc.printf("spix buffer is: "); 00318 for (i = 0; i < 4; i++) { 00319 pc.printf("%d ", spiRX[i]); 00320 } 00321 pc.printf("\n"); 00322 00323 //if(spiSlave.receive()){ 00324 //printf("Slave Read loop1\r\n"); 00325 //spiRX[bufferSize] = spiSlave.read(); 00326 //spiSlave.reply(spiRX[bufferSize]); 00327 //bufferSize++; 00328 bufferSize = 5; 00329 00330 if(bufferSize >= 5){ 00331 bufferSize=0; 00332 light_actions(spiRX); 00333 //BLE::Instance().gattClient().reset(); 00334 spiSlave.reply(spiRX[2]); //(0xAA); 00335 00336 00337 if(error_status > 7) 00338 { 00339 error_status = BLE_ERROR_NONE; 00340 //wait_ms(500); 00341 BLE::Instance().shutdown(); 00342 statusWR =1; 00343 connect_status =0; 00344 charDiscover =0; 00345 serviceDiscover = true; 00346 bleint(); 00347 } 00348 //statusWR++; 00349 wait_ms(3000); 00350 } 00351 00352 //} 00353 00354 } 00355 00356 } 00357 00358 void waitBleEvent() 00359 { 00360 while(serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ 00361 BLE::Instance().waitForEvent(); 00362 } 00363 } 00364
Generated on Fri Jul 22 2022 09:46:03 by
1.7.2
