2 Replies Latest reply on Mar 3, 2017 11:39 PM by user_14997725

    How to read the status....

    user_14997725

      Hi, 

      I have realized the BTLE device (attached the code). I need to send back 2 responses (a) one for status of a switch (b) and Timeout (by default it is 5 secs). 

      How do I do this? 
      a. When the device starts (it should update the switch status - and timeout value which is default). 
      b. How do I update when the value changes - I need to modify the switch status when the position is changed and when the timeout changed (this value is set by the central - so I would be getting CYBLE_EVT_GATTS_WRITE_REQ event) 

      With best regards, 
      Phani.

         

      My main.c looks like as below....

         

      unsigned char BTState;
      unsigned char LockOpenDuration = 5; //5 secs the lock is open
      uint8 UpperByte, LowerByte;
      uint16 WriteVal;
      unsigned char applicationPower;
      CYBLE_API_RESULT_T apiResult;

         

      void OpenTheLock(void)
      {
          SCB_1_UartPutString("Opening the lock\n\r");
          //LED_2_Write (1); //Turn off the blue LED if on...
          LED_2_Write (1);
          CyDelay(LockOpenDuration * 1000);
          LED_2_Write (0);
          /* if you are done with everything and ready to go to sleep,
          then set it up to go to sleep. Update the code inside if() specific
          to your application*/
          if(0)
          {
          applicationPower = SLEEP;
          }
          /* if you are done with everything and ready to go to deepsleep,
          then set it up to go to deepsleep. Update the code inside if() specific
          to your application*/
          if (1)
          {
          applicationPower = DEEPSLEEP;
          }
      }

         

      void AppCallBack (uint32 event, void * eventParam)
      {   
      /* Structure to store data written by Client */
      CYBLE_GATTS_WRITE_REQ_PARAM_T *wrReqParam;
      CYBLE_BLESS_CLK_CFG_PARAMS_T clockConfig;
          
          switch(event)
          {
              case CYBLE_EVT_STACK_ON:
              /* This event is generated when BLE stack is ON */
              /* BLE stack is on. Start BLE advertisement */
              //In the final version this will depend on Switch to turn on the advertisement or not
              SCB_1_UartPutString("CYBLE_EVT_STACK_ON\n\r");
              /* C8. Get the configured clock parameters for BLE subsystem */
              CyBle_GetBleClockCfgParam(&clockConfig);
              /* C8. Set the device sleep-clock accuracy (SCA) based on the tuned ppm
              of the WCO */
              clockConfig.bleLlSca = CYBLE_LL_SCA_000_TO_020_PPM;
              /* C8. Set the clock parameter of BLESS with updated values */
              CyBle_SetBleClockCfgParam(&clockConfig);
              /* Put the device into discoverable mode so that a Central device can
              connect to it. */
              apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
              CY_SET_XTND_REG32((void CYFAR *)(CYREG_BLE_BLERD_BB_XO_CAPTRIM), 0x0000BCBC);
              break;
              
              case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
              /* This event is generated at GAP disconnection. */
              /* Restart BLE advertisement */
              //In the final version this will depend on Switch to turn on the advertisement or not
              SCB_1_UartPutString("CYBLE_EVT_GAP_DEVICE_DISCONNECTED\n\r");
              CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
              break;
              
              case CYBLE_EVT_GATTS_WRITE_REQ:
              /* This event is generated when the connected Central */
              /* device sends a Write request. */
              /* The parameter ‘eventParam’ contains the data written */
              /* Extract the Write data sent by Client */
              SCB_1_UartPutString("CYBLE_EVT_GATTS_WRITE_REQ\n\r");
              wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam;
              UpperByte = *((uint8 *)(wrReqParam->handleValPair.value.val));
              SCB_1_UartPutString("Upper Byte = 0x"); PrintChar(UpperByte);SCB_1_UartPutString("\n\r");
              LowerByte = *(((uint8 *)(wrReqParam->handleValPair.value.val))+1);
              SCB_1_UartPutString("Lower byte = 0x"); PrintChar(LowerByte); SCB_1_UartPutString("\n\r");

         

              //if( CYBLE_BT_LOCK_SERVICE_HANDLE == *(uint8 *)(wrReqParam->handleValPair.value.val))
              if (UpperByte == 0x0AA)
              {
                  /* Open the lock */
                  OpenTheLock();
              }
              else if (UpperByte == 0x55) //This is where I am setting the duration
              {
                  /* Set the durarion */
                  LockOpenDuration = LowerByte;
                  WriteVal = UpperByte << 8; WriteVal = WriteVal | LowerByte;
                  SCB_1_UartPutString("WriteVal = 0x"); 
                  PrintChar(WriteVal>>8); //&0x00ff); 
                  PrintChar(WriteVal); //&0xff);
                  SCB_1_UartPutString("\n\r");
                  CyBle_GattsWriteAttributeValue (&(wrReqParam->handleValPair),0, &(wrReqParam->connHandle), WriteVal);
              }
              else
                  SCB_1_UartPutString("Wrong data written. Check your data\n\r");

         

                  
              /* Send the response to the write request received. */
              CyBle_GattsWriteRsp(cyBle_connHandle);
              break;
              
              default:
              break;
          }
      }

         

      int main()
      {
      /* Variable declarations for low power*/
      CYBLE_LP_MODE_T lpMode;
      CYBLE_BLESS_STATE_T blessState;
      uint8 interruptStatus;    
          
          /* Enable global interrupts */
          CyGlobalIntEnable;  /* Uncomment this line to enable global interrupts. */

         

          /* Place your initialization/startup code here (e.g. MyInst_Start()) */

          
          /* For these C1, C2 etc Cx states - refer to AN92584 */
          /* Designing for Low Power and Estimating Battery Life for BLE Applications */
          /* C1. Stop the ILO to reduce current consumption */
          CySysClkIloStop();
          /* C2. Configure the divider values for the ECO, so that a 3-MHz ECO divided
      c   lock can be provided to the CPU in Sleep mode */
          CySysClkWriteEcoDiv(CY_SYS_CLK_ECO_DIV8);
          /* Start the BLE Component and register the generic event handler */
          apiResult = CyBle_Start(AppCallBack);

         

          /* Wait for BLE Component to initialize */
          while (CyBle_GetState() == CYBLE_STATE_INITIALIZING)
          {
              CyBle_ProcessEvents();
          }
          
          applicationPower = ACTIVE;
          
          /* main while loop of the application */    
          for(;;)
          {
              /* Process all pending BLE events in the stack */
              CyBle_ProcessEvents();

         

              //Is below code needed?
              applicationPower = DEEPSLEEP; 

         

              /* C3. Call the function that manages the BLESS power modes */
              CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
              
              /*C4. Run your application specific codehere */
              //??? Nothing I (Phani)  believe as of now...
              //if(applicationPower == ACTIVE)
              //{
              //    RunApplication();
              //}
              
              /*C6. Manage Application power mode */
              //ManageApplicationPower();
              /*C7. Manage System power mode */
              //ManageSystemPower();
          }
      }

        • 1. Re: How to read the status....
          yssu

          Hi Phani,

             

          a) Use the macro "CYBLE_GATT_DB_ATTR_SET_GEN_VALUE(handle,src,length)" to set the value of an attribute, where

             

          'Handle' is the handle of the attribute, 'src' is the pointer to the data and 'length' is the length of the data.

             

           

             

          b) You can use this macro whenever you want to update the data of the attribute but do not use this before the stack is on. With respect to the value written by the central, you are handling it correctly except that you are not checking for the handle(you have commented it out).

             

          What is the exact issue you are facing?

          • 2. Re: How to read the status....
            user_14997725

            Hi Sujay,

               

            Yes, this CYBLE_GATT_DB_ATTR_SET_GEN_VALUE(handle,src,length) helped and works fine. Thanks a lot.

               

            With best regards,

               

            Phani.