Working Code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light_Demo by HM_IOT_Demo

Revision:
12:cd54180657ee
Parent:
11:58d8a3129877
Child:
13:9b2a587ee7ac
--- a/main.cpp	Tue Jan 31 04:10:34 2017 +0000
+++ b/main.cpp	Tue Jan 31 11:28:52 2017 +0000
@@ -6,8 +6,8 @@
 #include "hm_config.h"
 //#include "rtos.h"
 
-DiscoveredCharacteristic lightCharacteristic;
-s_serviceInfo lightChar = {NULL,NULL,NULL,NULL};
+//DiscoveredCharacteristic lightCharacteristic;
+static s_serviceInfo lightChar = {NULL,NULL,NULL,NULL};
 Serial pc(USBTX, USBRX);
 
 void bleint();
@@ -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");
@@ -81,7 +81,7 @@
 
     if(NULL == ptr_temp_char)
     {
-        ptr_temp_char = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t));
+        ptr_temp_char = new lightCharacteristic_t;
         lightChar.services_Char[0] = ptr_temp_char;
     }
     else
@@ -92,7 +92,7 @@
                 }  
             
         /* add a new node */
-        ptr_temp_char->nextChar = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t));           
+        ptr_temp_char->nextChar = new lightCharacteristic_t;           
         ptr_temp_char = ptr_temp_char->nextChar;
     }
             
@@ -148,7 +148,6 @@
 }
 
 void light_actions(int service_char[]){
-    write_data = new uint8_t[8];
     lightCharacteristic_t *ptr_tmp = NULL;
     ptr_tmp = lightChar.services_Char[service_char[1]-1];
     if(connect_status)
@@ -181,27 +180,73 @@
                 break;
                        
             case COLOR: // Color
-                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;
-                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);
+                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");
+                }
+                
                 break;
                        
             default:
                 printf("Invalid Options\r\n");
         } 
             printf("Error Status =%d\n\r",error_status);
-            delete [] write_data;
+          
+          
     } 
 }
 
-void datancpy(char *pre_write, char *writeData, int data_size){
+void datancpy(char pre_write[], char writeData[], int data_size){
     int numCount;
     for(numCount=0; numCount <= data_size; numCount++){
         pre_write[numCount] = writeData[numCount];
@@ -223,7 +268,7 @@
     wait(8.0);
     pc.printf("Start\n\r");
    // int options;
-    //spiSlave.reply(191);
+    spiSlave.reply(191);
     bleint();
     bufferSize = 0;
     while(1){
@@ -231,7 +276,7 @@
      //   if(!(connect_status && charDiscover))
             waitBleEvent();
             
-    /*    if(spiSlave.receive()){
+        if(spiSlave.receive()){
             // printf("Slave Read loop1\r\n");
             spiRX[bufferSize] = spiSlave.read();
             spiSlave.reply(spiRX[bufferSize]);
@@ -243,7 +288,7 @@
                 spiSlave.reply(0xAA);
             }
             
-        } */
+        } 
         
         wait_us(15);
     }