Working Code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light_Demo by HM_IOT_Demo

Revision:
13:9b2a587ee7ac
Parent:
12:cd54180657ee
Child:
14:f84a9745c10a
diff -r cd54180657ee -r 9b2a587ee7ac main.cpp
--- a/main.cpp	Tue Jan 31 11:28:52 2017 +0000
+++ b/main.cpp	Wed Feb 01 12:38:31 2017 +0000
@@ -6,15 +6,14 @@
 #include "hm_config.h"
 //#include "rtos.h"
 
-//DiscoveredCharacteristic lightCharacteristic;
+DiscoveredCharacteristic lightCharacteristic;
 static s_serviceInfo lightChar = {NULL,NULL,NULL,NULL};
 Serial pc(USBTX, USBRX);
-
+//static uint8_t peer_addr[2] = {0x51,0x5A};
 void bleint();
-void SPI_Slave_read();
 void waitBleEvent();
 void ble_actions(int spi_data);
-
+uint8_t statusWR;
 SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10);
 
 
@@ -23,8 +22,8 @@
      //0x51 for Magic light 
     //0x5A
     int i;
-    //for(i=0; i<2; i++){
-        if ((params->peerAddr[0] == 0x51) || (params->peerAddr[0] == 0x5A)) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
+  //  for(i=0; i<2; i++){
+        if (params->peerAddr[0] == 0x51){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
        
            pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
@@ -34,13 +33,14 @@
            pc.printf("Connection Status : %d\r\n",con_status);
            connect_status = 1;
            i++;
-           //BLE::Instance().gap().stopScan();
+           BLE::Instance().gap().stopScan();
            
         }else{
             printf("Not Matched your device\r\n");
             return;
         }
-    //}
+   // }
+    
  }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
@@ -59,7 +59,7 @@
 
 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
     pc.printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
-    DiscoveredCharacteristic lightCharacteristic;
+    
     if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */
        lightCharacteristic = *characteristicP;
        pc.printf("Matched char UUID\r\n");
@@ -76,9 +76,10 @@
 void AddCharNodes(DiscoveredCharacteristic light_Characteristic)
 {
     printf("Add Char Nodes\r\n");
-    lightCharacteristic_t *ptr_temp_char = NULL;
+    lightCharacteristic_t *ptr_temp_char;
     ptr_temp_char = lightChar.services_Char[0];
 
+    
     if(NULL == ptr_temp_char)
     {
         ptr_temp_char = new lightCharacteristic_t;
@@ -149,7 +150,8 @@
 
 void light_actions(int service_char[]){
     lightCharacteristic_t *ptr_tmp = NULL;
-    ptr_tmp = lightChar.services_Char[service_char[1]-1];
+    ptr_tmp = lightChar.services_Char[0];
+    uint8_t numCount;
     if(connect_status)
     {
 
@@ -180,70 +182,26 @@
                 break;
                        
             case COLOR: // Color
-                switch(service_char[3])
-                {
-                    case RED:
-                        write_data[0]=0x56;
-                        write_data[1]=0xff;
-                        write_data[2]=0x00;
-                        write_data[3]=0x00;
-                        write_data[4]=0x00;
-                        write_data[5]=0xf0;
-                        write_data[6]=0xaa;
-                        datancpy((char *)pre_write_data, (char *)write_data,6);
-                        first_on=false;
-                        error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
-                        break;
-                        
-                    case GREEN:
-                        write_data[0]=0x56;
-                        write_data[1]=0x00;
-                        write_data[2]=0xff;
-                        write_data[3]=0x00;
-                        write_data[4]=0x00;
-                        write_data[5]=0xf0;
-                        write_data[6]=0xaa;
-                        datancpy((char *)pre_write_data, (char *)write_data,6);
-                        first_on=false;
-                        error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
-                        break;
-                    case BLUE:
-                        write_data[0]=0x56;
-                        write_data[1]=0x00;
-                        write_data[2]=0x00;
-                        write_data[3]=0xff;
-                        write_data[4]=0x00;
-                        write_data[5]=0xf0;
-                        write_data[6]=0xaa;
-                        datancpy((char *)pre_write_data, (char *)write_data,6);
-                        first_on=false;
-                        error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
-                        break;
-                    case YELLOW:
-                        write_data[0]=0x56;
-                        write_data[1]=0xff;
-                        write_data[2]=0xff;
-                        write_data[3]=0x00;
-                        write_data[4]=0x00;
-                        write_data[5]=0xf0;
-                        write_data[6]=0xaa;
-                        datancpy((char *)pre_write_data, (char *)write_data,6);
-                        first_on=false;
-                        error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
-                        break;
-                    default: 
-                        printf("Invalid\r\n");
+                write_data[0]=0x56;
+                write_data[1]=lightColor[service_char[3]][0];//0xff
+                write_data[2]=lightColor[service_char[3]][1];//0x00;
+                write_data[3]=lightColor[service_char[3]][2];//0x00;
+                write_data[4]=0x00;
+                write_data[5]=0xf0;
+                write_data[6]=0xaa;
+                for(numCount=0; numCount < 7; numCount++){
+                  pre_write_data[numCount] = write_data[numCount];
                 }
-                
+                first_on=false;
+                error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data);
                 break;
                        
             default:
                 printf("Invalid Options\r\n");
         } 
             printf("Error Status =%d\n\r",error_status);
-          
-          
-    } 
+    }
+    
 }
 
 void datancpy(char pre_write[], char writeData[], int data_size){
@@ -267,8 +225,9 @@
     pc.baud(9600);
     wait(8.0);
     pc.printf("Start\n\r");
-   // int options;
+
     spiSlave.reply(191);
+    statusWR=1;
     bleint();
     bufferSize = 0;
     while(1){
@@ -286,11 +245,23 @@
                 bufferSize=0;
                 light_actions(spiRX);
                 spiSlave.reply(0xAA);
+                
+                if(statusWR >=4)
+                {   wait_ms(500);
+                    BLE::Instance().shutdown();
+                    statusWR =1;
+                    connect_status =0;
+                    charDiscover =0;
+                    serviceDiscover = true;
+                    bleint();
+                }
+                statusWR++;
             }
             
         } 
         
-        wait_us(15);
+        
+        
     }
 
 }