Child pages
  • Getting Started with PSoC 6 - CY8CKIT-062-BLE
Skip to end of metadata
Go to start of metadata

Introduction

This page will show you step-by-step how to interface a create a simple BLE Weather Station with the Cypress PSoC 6.

Hardware Requirements

Software Requirements

I2C Setup

Open PSoC Creator:

  • File → New → Project
  • Target Kit: CY8CKIT-062-BLE → Next
  • Empty Schematic → Next
  • CMSIS Pack: Disable, IAR EW-ARM: Disable, Makefile: Disable → Next
  • Create your Project Name → Next

From the TopDesign.cysch, add the I2C (SCB) (v2.0) ip block, right click and click configure.

Configure Basic and Pins Properties as shown:

Add two Digital Bidirectional Pin (v1.0) and connect them to scl_b/sda_b

Configure both as:

  • Name: (scl_b/sda_b)
  • General: Bidirectional
  • Drive mode: Resistive Pull Up
  • Max frequency: I2C SM (100kbps)

 

 

From the Design Wide Resources, select the Pins option configure the pins as:

  • scl_b : P6[0] K8
  • sda_b : P6[1] J9
 Click here to expand... i2c si7005 test application
main_cm0p.c
#include "project.h"
#include "I2C_1.h"

#define I2C_TIMEOUT 100
#define SI7005_ADDR 0x40
#define SI7005_DEV_ID 0x50

/* SI7005 Registers */
#define SI7005_REG_STATUS         0x00
#define SI7005_REG_DATA           0x01
#define SI7005_REG_CONFIG         0x03
#define SI7005_REG_ID             0x11

/* SI7005 Status Register */
#define SI7005_STATUS_NOT_READY   0x01

/* SI7005 Config Register */
#define SI7005_CONFIG_START       0x01
#define SI7005_CONFIG_HEAT        0x02
#define SI7005_CONFIG_HUMIDITY    0x00
#define SI7005_CONFIG_HUMIDITY_START    0x01
#define SI7005_CONFIG_TEMPERATURE 0x10
#define SI7005_CONFIG_TEMPERATURE_START 0x11
#define SI7005_CONFIG_FAST        0x20

int main(void)
{
    __enable_irq(); /* Enable global interrupts. */
    
    I2C_1_Start();
    volatile uint8_t tmp0 = 0;
    volatile uint8_t si7005_connected = 0;
    
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_ID, I2C_TIMEOUT);
    I2C_1_MasterSendReStart(SI7005_ADDR, CY_SCB_I2C_READ_XFER, I2C_TIMEOUT);    
    I2C_1_MasterReadByte(CY_SCB_I2C_NAK, &tmp0, I2C_TIMEOUT);
    I2C_1_MasterSendStop(I2C_TIMEOUT);
       
    if ( tmp0 == SI7005_DEV_ID )
        si7005_connected=1;
   
    for(;;)
    {
        /* Place your application code here. */
    }
}
  • Insert a breakpoint on line 40:
  • Build → Build CYC8CKIT-062-I2C
  • Debug → Debug → PSoC 63 CY8C6347BZI-BLD53 (CM0p) → OK/Connect
  • add watch on "tmp0"
  • Debug → Resume Execution

 

BLE Setup

From the TopDesign.cysch, add the Bluetooth Low Energy (BLE) (v2.0) ip block, right click and click configure:

Configuration:

General:

  • Complete BLE Protocol
  • GAP role (X) Peripheral
  • CPU core (Single core)

Gatt Settings:

From Server -> (right click) Add Service -> add "Custom Service"

Rename as "Weather Station"

Delete default "Custom Characteristic".

Add Temperature Charateristic:

  • Right Click (Weather Station) → Add Characteristic → Enviromental Sensing → Temperature
  • Set the Read and Notify Properites as shown:

Add Characteristic -> Enviromental Sensing -> Humidty

Add Humidty Charateristic:

  • Right Click (Weather Station) → Add Characteristic → Enviromental Sensing → Humidty
  • Set the Read and Notify Properites as shown:

Gap Settings:

General:

  • Device Name: PSoC6 Weather
  • Appearance: Generic Tag

Advertisement settings:

  • Discovery mode: General
  • Disable: Timeout

Advertisement packet:

  • Enable: Local Name
  • Enable: TX Power Level
  • Enable: Appearance

Security configuration 0:

  • I/O capabilities: No Input No Output
  • Bonding requirement: No Bonding

Click OK, done with configuring the BLE module for now.

Design Wide Resources: Clocks:

From the Design Wide Resources select the Clocks option, we need WCO enabled for Bluetooth.

Source Clocks:

  • Enable: WCO

Miscellaneous Clocks:

  • LFClk → WCO
  • BakClk → WCO

Design Wide Resources: Interrupts:

Enable both Interrupts to teh ARM CM0+ Enable core

Rebuild the application.

Next take a look in your generated sources folder for "BLE_config.h", these defines will be used in the BLE stack function calls:

/* Below are the indexes and handles of the defined Custom Services and their characteristics */
#define CY_BLE_WEATHER_STATION_SERVICE_INDEX   (0x00u) /* Index of Weather Station service in the cy_ble_customs array */
#define CY_BLE_WEATHER_STATION_TEMPERATURE_CHAR_INDEX   (0x00u) /* Index of Temperature characteristic */
#define CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX   (0x00u) /* Index of Client Characteristic Configuration descriptor */
#define CY_BLE_WEATHER_STATION_HUMIDITY_CHAR_INDEX   (0x01u) /* Index of Humidity characteristic */
#define CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX   (0x00u) /* Index of Client Characteristic Configuration descriptor */


#define CY_BLE_WEATHER_STATION_SERVICE_HANDLE   (0x0010u) /* Handle of Weather Station service */
#define CY_BLE_WEATHER_STATION_TEMPERATURE_DECL_HANDLE   (0x0011u) /* Handle of Temperature characteristic declaration */
#define CY_BLE_WEATHER_STATION_TEMPERATURE_CHAR_HANDLE   (0x0012u) /* Handle of Temperature characteristic */
#define CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE   (0x0013u) /* Handle of Client Characteristic Configuration descriptor */
#define CY_BLE_WEATHER_STATION_HUMIDITY_DECL_HANDLE   (0x0014u) /* Handle of Humidity characteristic declaration */
#define CY_BLE_WEATHER_STATION_HUMIDITY_CHAR_HANDLE   (0x0015u) /* Handle of Humidity characteristic */
#define CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE   (0x0016u) /* Handle of Client Characteristic Configuration descriptor */

 

BLE Example 1: Basic BLE Advertisement

The customEventHandler and startAdvertisement functions are based off: Cypress CE218134_BLE_CapSense.pdf

Using Cypress's CySmart

 BLE Example 1 project files...
main_cm0p.c
#include "project.h"
#include "BLE_applications.h"
#include "I2C_1.h"
 
int main(void)
{
    __enable_irq(); /* Enable global interrupts. */
    
    /* Disable the Watchdog Timer to avoid CPU resets */
    Cy_WDT_Unlock();
    Cy_WDT_Disable();    
    
    I2C_1_Start();
 
    /* Start BLE component and register the customEventHandler function. This 
	   function exposes the events from BLE component for application use */
    Cy_BLE_Start(customEventHandler);
 
    for(;;)
    {
        /* Process event callback to handle BLE events. The events generated 
		   and used for this application are inside the 'customEventHandler' 
           routine */
        Cy_BLE_ProcessEvents();
        
        /* Start the BLE advertisement if required */
        startAdvertisement();        
    }
}
BLE_applications.h
/* Include guard */
#ifndef BLE_APPLICATIONS_H
#define BLE_APPLICATIONS_H
 
/* Header file includes */
#include <project.h>
 
/* Bit mask for the notification bit in CCCD (Client Characteristic Configuration 
   Descriptor), which is written by the client device */
#define CCCD_NOTIFY_BIT_MASK    (uint8_t) (0x01u)
 
/* Event handler function for the three custom services in this project */
void    customEventHandler(uint32_t event, void *eventParameter);
 
/* Function that checks for events from the handler and starts the BLE advertisement
  if required */
void    startAdvertisement(void);
 
#endif
BLE_applications.c
/* Header file includes */
#include "BLE_applications.h"
 
/* Client Characteristic Configuration descriptor (CCCD) data length */
#define CCCD_DATA_LEN   (uint8_t) (0x02u)
 
/* Indexes of a two-byte CCCD array */
#define CCCD_INDEX_0    (uint8_t) (0x00u)
#define CCCD_INDEX_1    (uint8_t) (0x01u)
 
/* Null byte used to initialize CCCD values */
#define CCCD_NULL       (uint8_t) (0x00u)
 
/* 'connectionHandle' stores the BLE connection parameters */
cy_stc_ble_conn_handle_t connectionHandle;
 
/* This flag is used by application to know whether a Central device has been 
   connected. This value is continuously updated in BLE event callback
   function */
bool                deviceConnected = false;
 
/* This is used to restart advertisements in the main firmware loop */
bool                restartAdvertisement = true;
 
/* Status flag for the Stack Busy state. This flag is used to notify the 
   application if there is stack buffer free to push more data or not */
bool                busyStatus = false;
 
/*******************************************************************************
* Function Name: void startAdvertisement(void)
********************************************************************************
* Summary:
*  Check the "restartAdvertisement" flag and start the BLE advertisement if
*  the flag is "true"
*
* Parameters:
*  Void
*
* Return:
*  void
*
*******************************************************************************/
void startAdvertisement(void)
{
    /* Check if the restartAdvertisement flag is set by the event handler */
    if(restartAdvertisement)
	{
		/* Reset 'restartAdvertisement' flag */
		restartAdvertisement = false;
		
		/* Start Advertisement and enter discoverable mode */
		Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST,
                       CY_BLE_PERIPHERAL_CONFIGURATION_0_INDEX);	
	}
}
 
/*******************************************************************************
* Function Name: void customEventHandler(uint32_t event, void *eventParameter)
********************************************************************************
* Summary:
*  Call back event function to handle various events from the BLE stack
*
* Parameters:
*  event            :	event returned
*  eventParameter   :	link to value of the events returned
*
* Return:
*  void
*
*******************************************************************************/
void customEventHandler(uint32_t event, void *eventParameter)
{
    /* Local variable to store the data received as part of the write request
       events */
    cy_stc_ble_gatts_write_cmd_req_param_t   *writeReqParameter;
    
    /* Take an action based on the current event */
    switch (event)
    {
        /* This event is received when the BLE stack is Started */
        case CY_BLE_EVT_STACK_ON:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
               API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GAP EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
            
        /* If the current BLE state is Disconnected, then the Advertisement
           Start-Stop event implies that advertisement has stopped */
        case CY_BLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
            
            /* Check if the advertisement has stopped */
            if (Cy_BLE_GetState() == CY_BLE_STATE_STOPPED)
            {
                /* Set restartAdvertisement flag to allow calling Advertisement
                   API from the main function */
                restartAdvertisement = true;
            }
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
             API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GATT EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        
        /* This event is received when device is connected over GATT level */    
        case CY_BLE_EVT_GATT_CONNECT_IND:
            
            /* Update attribute handle on GATT Connection*/
            connectionHandle = *(cy_stc_ble_conn_handle_t *) eventParameter;
            /* This flag is used by the application to check the connection
               status */
            deviceConnected = true;
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GATT_DISCONNECT_IND:
           
            /* Update deviceConnected flag*/
            deviceConnected = false;
            
            /* Call the functions that handle the disconnect events for all 
               custom services */
            //FIXME: handleDisconnectEvent_X();
            break;
        
        /* This event is received when Central device sends a Write command
           on an Attribute */
        case CY_BLE_EVT_GATTS_WRITE_REQ:
            
            /* Read the write request parameter */
            writeReqParameter = (cy_stc_ble_gatts_write_cmd_req_param_t *) 
                                eventParameter;
            /* When this event is triggered, the peripheral has received a 
               write command on the custom  characteristic. Check if command
               fits any of the custom attributes and update the flag for
               sending notifications by the respective service */
            //FIXME: handleWriteRequestX();
            /* Send the response to the write request received. */
            Cy_BLE_GATTS_WriteRsp(connectionHandle);
            break;
                
        /* This event is generated when the internal stack buffer is full and no
           more data can be accepted or the stack has buffer available and can 
           accept data. This event is used by application to prevent pushing lot
           of data to the BLE stack. */
        case CY_BLE_EVT_STACK_BUSY_STATUS:
            
            /* Extract the present stack status */
            busyStatus = *(uint8_t *) eventParameter;
            break;
        
        /* Do nothing for all other events */
        default:
            break;
    }
}

BLE Example 2: BLE Si7005 Temperature Data

This is also based on Still based off Cypress CE218134_BLE_CapSense.pdf, but we wire up the si7005 Temperature data

 BLE Example 2 project files...
main_cm0p.c
#include "project.h"
#include "BLE_applications.h"
#include "I2C_1.h"
 
/* This flag is used by application to know whether a Central device has been 
   connected. This is updated in BLE event callback function */ 
extern bool deviceConnected;
 
/*These flags are set when the Central device writes to CCCD of the 
  Sensor Characteristic to enable notifications */
extern bool sendSi7005TempNotifications;
 
int main(void)
{
    __enable_irq(); /* Enable global interrupts. */
    
    /* Disable the Watchdog Timer to avoid CPU resets */
    Cy_WDT_Unlock();
    Cy_WDT_Disable();    
    
    I2C_1_Start();
    
    /* Start BLE component and register the customEventHandler function. This 
	   function exposes the events from BLE component for application use */
    Cy_BLE_Start(customEventHandler);
    
    for(;;)
    {
        /* Process event callback to handle BLE events. The events generated 
		   and used for this application are inside the 'customEventHandler' 
           routine */
        Cy_BLE_ProcessEvents();
        
        /* If a connection is detected, handle Sensor data transmission */
		if(deviceConnected == true)
		{
			/* Send Si7005 Temperature data when respective notification is 
               enabled */
            if(sendSi7005TempNotifications == CCCD_NOTIFY_BIT_MASK)
			{
				/* Send Si7005 Temperature data when respective notification is 
                   enabled */
                handleSi7005Temp();
			}
		}        
        
        /* Start the BLE advertisement if required */
        startAdvertisement();        
    }
}
BLE_applications.h
#ifndef BLE_APPLICATIONS_H
#define BLE_APPLICATIONS_H
 
/* Header file includes */
#include <project.h>
 
/* Bit mask for the notification bit in CCCD (Client Characteristic Configuration 
   Descriptor), which is written by the client device */
#define CCCD_NOTIFY_BIT_MASK    (uint8_t) (0x01u)
 
/* Event handler function for the three custom services in this project */
void    customEventHandler(uint32_t event, void *eventParameter);
 
void    handleSi7005Temp(void);
 
/* Function that checks for events from the handler and starts the BLE advertisement
  if required */
void    startAdvertisement(void);
 
#endif
BLE_applications.c
/* Header file includes */
#include "BLE_applications.h"
 
/* Client Characteristic Configuration descriptor (CCCD) data length */
#define CCCD_DATA_LEN   (uint8_t) (0x02u)
 
/* Indexes of a two-byte CCCD array */
#define CCCD_INDEX_0    (uint8_t) (0x00u)
#define CCCD_INDEX_1    (uint8_t) (0x01u)
 
/* Null byte used to initialize CCCD values */
#define CCCD_NULL       (uint8_t) (0x00u)
 
/* 'connectionHandle' stores the BLE connection parameters */
cy_stc_ble_conn_handle_t connectionHandle;
 
/* Read and write length of the si7005 data */
#define SI7005_DATA_LEN    (uint8_t) (0x02u)
 
/* Respective indexes of the si7005 */
#define SI7005_TEMP0_INDEX   (uint8_t) (0x00u)
#define SI7005_TEMP1_INDEX   (uint8_t) (0x01u)
 
/* Data lengths of si7005 Temperature data sent over BLE notifications */
#define SI7005_TEMP    (uint8_t) (0x01u)
#define SI7005_HUMIDITY    (uint8_t) (0x01u)
 
/* Array to store the present si7005 Temperature data. */
uint8_t             Si7005TempDataArray[SI7005_DATA_LEN];
 
/* These flags are set when the Central device writes to CCCD (Client 
   Characteristic Configuration Descriptor) of the si7005 Temperature 
   Characteristic to enable notifications */
bool                sendSi7005TempNotifications = false;
 
/* This flag is used by application to know whether a Central device has been 
   connected. This value is continuously updated in BLE event callback
   function */
bool                deviceConnected = false;
 
/* This is used to restart advertisements in the main firmware loop */
bool                restartAdvertisement = true;
 
/* Status flag for the Stack Busy state. This flag is used to notify the 
   application if there is stack buffer free to push more data or not */
bool                busyStatus = false;
 
#include "I2C_1.h"
 
#define I2C_TIMEOUT 100
 
#define SI7005_ADDR 0x40
#define SI7005_DEV_ID 0x50
 
/* SI7005 Registers */
#define SI7005_REG_STATUS         0x00
#define SI7005_REG_DATA           0x01
#define SI7005_REG_CONFIG         0x03
#define SI7005_REG_ID             0x11
 
/* Status Register */
#define SI7005_STATUS_NOT_READY   0x01
 
/* Config Register */
#define SI7005_CONFIG_START       0x01
#define SI7005_CONFIG_HEAT        0x02
#define SI7005_CONFIG_HUMIDITY    0x00
#define SI7005_CONFIG_HUMIDITY_START    0x01
#define SI7005_CONFIG_TEMPERATURE 0x10
#define SI7005_CONFIG_TEMPERATURE_START 0x11
#define SI7005_CONFIG_FAST        0x20
 
/*******************************************************************************
* Function Name: void startAdvertisement(void)
********************************************************************************
* Summary:
*  Check the "restartAdvertisement" flag and start the BLE advertisement if
*  the flag is "true"
*
* Parameters:
*  Void
*
* Return:
*  void
*
*******************************************************************************/
void startAdvertisement(void)
{
    /* Check if the restartAdvertisement flag is set by the event handler */
    if(restartAdvertisement)
	{
		/* Reset 'restartAdvertisement' flag */
		restartAdvertisement = false;
		
		/* Start Advertisement and enter discoverable mode */
		Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST,
                       CY_BLE_PERIPHERAL_CONFIGURATION_0_INDEX);	
	}
}
 
/*******************************************************************************
* Function Name: void sendSi7005TempNotification(uint8_t *Si7005TempDataArray)
********************************************************************************
* Summary:
*  Send Si7005Temp data as BLE Notifications. This function updates
*  the notification handle with data and triggers the BLE component to send
*  notification
*
* Parameters:
*  Si7005TempDataArray:	 Si7005Temp value
*
* Return:
*  void
*
*******************************************************************************/
void sendSi7005TempNotification(uint8_t *Si7005TempDataArray)
{
    cy_stc_ble_gatts_handle_value_ntf_t  Si7005TempnotificationHandle;
 
    /* If stack is not busy, then send the notification */
    if (busyStatus == CY_BLE_STACK_STATE_FREE)
    {
        /* Update notification handle with Si7005 Temperature data */
        Si7005TempnotificationHandle.connHandle = connectionHandle;
        Si7005TempnotificationHandle.handleValPair.attrHandle = 
                            CY_BLE_WEATHER_STATION_TEMPERATURE_CHAR_HANDLE;
        Si7005TempnotificationHandle.handleValPair.value.val = 
                            Si7005TempDataArray;
        Si7005TempnotificationHandle.handleValPair.value.len =
                            SI7005_DATA_LEN;
 
        /* Send the updated handle as part of attribute for notifications */
        Cy_BLE_GATTS_Notification(&Si7005TempnotificationHandle);
    }
}
 
/*******************************************************************************
* Function Name: void handleSi7005Temp(void)
********************************************************************************
* Summary:
*  Read Si7005 Tempearature data
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleSi7005Temp(void)
{
    /* Si7005 Temperature value from the previous scan */
    static uint16_t   previousSi7005Temp;
 
    /*  Si7005 Temperature value from the current scan */
    uint16_t          currentSi7005Temp;
    int16_t           broadcastSi7005Temp;    
    float             Si7005Temp;
 
    static uint8_t tmp0 = 0;
    static uint8_t tmp1 = 0;    
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_CONFIG, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_CONFIG_TEMPERATURE_START, I2C_TIMEOUT);
    I2C_1_MasterSendStop(I2C_TIMEOUT);
 
    CyDelay(100);
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_DATA, I2C_TIMEOUT);        
    I2C_1_MasterSendReStart(SI7005_ADDR, CY_SCB_I2C_READ_XFER, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_ACK, &tmp0, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_NAK, &tmp1, I2C_TIMEOUT);        
    I2C_1_MasterSendStop(I2C_TIMEOUT);
    currentSi7005Temp = (tmp0 << 8);
    currentSi7005Temp = (currentSi7005Temp | (tmp1 & 0xFF));
    currentSi7005Temp = (currentSi7005Temp >> 2);
 
    //org.bluetooth.characteristic.temperature sint16, Celsius with 0.01 degress resolution.
    Si7005Temp = (float)(currentSi7005Temp * 100.0);
    Si7005Temp = (float)(Si7005Temp / 32);
    Si7005Temp = (float)(Si7005Temp - 5000);
    broadcastSi7005Temp = (int16_t)Si7005Temp;
 
    //org.bluetooth.characteristic.temperature is in little Endian..
    Si7005TempDataArray[SI7005_TEMP1_INDEX] = (int8_t)((broadcastSi7005Temp & 0xFF00) >> 8);
    Si7005TempDataArray[SI7005_TEMP0_INDEX] = (int8_t)(broadcastSi7005Temp & 0x00FF);
 
    if (currentSi7005Temp != previousSi7005Temp)
    {    
        /* Send data over Si7005 Temperature notification */
        sendSi7005TempNotification(Si7005TempDataArray);
 
        /* Update the local static variable with the present finger
		    position on the slider */
        previousSi7005Temp = currentSi7005Temp;
    };
}
/*******************************************************************************
* Function Name: void handleDisconnectEventforSi7005Temp(void)
********************************************************************************
* Summary:
*  This functions handles the 'disconnect' event for the Si7005Temp service
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleDisconnectEventforSi7005Temp(void)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005TempNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t Si7005TempCCCDvalue[CCCD_DATA_LEN];
 
    /* Reset Si7005 notification flag to prevent further notifications
        being sent to Central device after next connection. */
    sendSi7005TempNotifications = false;
 
    /* Reset the Si7005 CCCD value to disable notifications */
    /* Write the present CapSense notification status to the local variable */
    Si7005TempCCCDvalue[CCCD_INDEX_0] = sendSi7005TempNotifications;
    Si7005TempCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
    /* Update the Si7005 CCCD handle with notification status data*/
    Si7005TempNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
    Si7005TempNotificationCCCDhandle.value.val = Si7005TempCCCDvalue;
    Si7005TempNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report the Si7005 data to BLE component for sending data when read by 
       the Central device */
    attributeHandle.handleValuePair = Si7005TempNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
 
/*******************************************************************************
* Function Name: void handleWriteRequestforSi7005Temp
*                     (cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
********************************************************************************
* Summary:
*  This functions handles the 'write request' event for the Si7005Temp service
*
* Parameters:
*  writeRequest : pointer to the write request parameters from the central       
*
* Return:
*  void
*
*******************************************************************************/
void handleWriteRequestforSi7005Temp(cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005TempNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t                         Si7005TempCCCDvalue[CCCD_DATA_LEN];
 
    /* Extract the Write value sent by the Client for CapSense Button CCCD */
    if (writeRequest->handleValPair.value.val
        [CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
         == true)
    {
        sendSi7005TempNotifications = true;
    }
    else if (writeRequest->handleValPair.value.val
             [CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
             == false)
    {
        sendSi7005TempNotifications = false;
    }
 
    /* Write the present CapSense notification status to the local variable */
    Si7005TempCCCDvalue[CCCD_INDEX_0] = sendSi7005TempNotifications;
    Si7005TempCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
 
    /* Update CCCD handle with notification status data */
    Si7005TempNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
 
    Si7005TempNotificationCCCDhandle.value.val = Si7005TempCCCDvalue;
    Si7005TempNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report data to BLE component for sending data when read by the central
       device */
    attributeHandle.handleValuePair = Si7005TempNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
/*******************************************************************************
* Function Name: void customEventHandler(uint32_t event, void *eventParameter)
********************************************************************************
* Summary:
*  Call back event function to handle various events from the BLE stack
*
* Parameters:
*  event            :	event returned
*  eventParameter   :	link to value of the events returned
*
* Return:
*  void
*
*******************************************************************************/
void customEventHandler(uint32_t event, void *eventParameter)
{
    /* Local variable to store the data received as part of the write request
       events */
    cy_stc_ble_gatts_write_cmd_req_param_t   *writeReqParameter;
    
    /* Take an action based on the current event */
    switch (event)
    {
        /* This event is received when the BLE stack is Started */
        case CY_BLE_EVT_STACK_ON:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
               API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GAP EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
            
        /* If the current BLE state is Disconnected, then the Advertisement
           Start-Stop event implies that advertisement has stopped */
        case CY_BLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
            
            /* Check if the advertisement has stopped */
            if (Cy_BLE_GetState() == CY_BLE_STATE_STOPPED)
            {
                /* Set restartAdvertisement flag to allow calling Advertisement
                   API from the main function */
                restartAdvertisement = true;
            }
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
             API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GATT EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        
        /* This event is received when device is connected over GATT level */    
        case CY_BLE_EVT_GATT_CONNECT_IND:
            
            /* Update attribute handle on GATT Connection*/
            connectionHandle = *(cy_stc_ble_conn_handle_t *) eventParameter;
            /* This flag is used by the application to check the connection
               status */
            deviceConnected = true;
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GATT_DISCONNECT_IND:
           
            /* Update deviceConnected flag*/
            deviceConnected = false;
            
            /* Call the functions that handle the disconnect events for all 
               custom services */
            handleDisconnectEventforSi7005Temp();
            break;
        
        /* This event is received when Central device sends a Write command
           on an Attribute */
        case CY_BLE_EVT_GATTS_WRITE_REQ:
            
            /* Read the write request parameter */
            writeReqParameter = (cy_stc_ble_gatts_write_cmd_req_param_t *) 
                                eventParameter;
            /* When this event is triggered, the peripheral has received a 
               write command on the custom  characteristic. Check if command
               fits any of the custom attributes and update the flag for
               sending notifications by the respective service */
            if (CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE
                == writeReqParameter->handleValPair.attrHandle)
            {
                handleWriteRequestforSi7005Temp(writeReqParameter);
            }
            /* Send the response to the write request received. */
            Cy_BLE_GATTS_WriteRsp(connectionHandle);
            break;
        
        /* This event is generated when the internal stack buffer is full and no
           more data can be accepted or the stack has buffer available and can 
           accept data. This event is used by application to prevent pushing lot
           of data to the BLE stack. */
        case CY_BLE_EVT_STACK_BUSY_STATUS:
            
            /* Extract the present stack status */
            busyStatus = *(uint8_t *) eventParameter;
            break;
        
        /* Do nothing for all other events */
        default:
            break;
    }
}

BLE Example 3: BLE Si7005 Humidity Data

This is also based on Still based off Cypress CE218134_BLE_CapSense.pdf, but we also wire up the si7005 Humidity data

 BLE Example 3 project files...
main_cm0p.c
#include "project.h"
#include "BLE_applications.h"
#include "I2C_1.h"
 
/* This flag is used by application to know whether a Central device has been 
   connected. This is updated in BLE event callback function */ 
extern bool deviceConnected;
 
/*These flags are set when the Central device writes to CCCD of the 
  Sensor Characteristic to enable notifications */
extern bool sendSi7005TempNotifications;
extern bool sendSi7005HumidityNotifications;
 
int main(void)
{
    __enable_irq(); /* Enable global interrupts. */
    
    /* Disable the Watchdog Timer to avoid CPU resets */
    Cy_WDT_Unlock();
    Cy_WDT_Disable();    
    
    I2C_1_Start();
    
    /* Start BLE component and register the customEventHandler function. This 
	   function exposes the events from BLE component for application use */
    Cy_BLE_Start(customEventHandler);
    
    for(;;)
    {
        /* Process event callback to handle BLE events. The events generated 
		   and used for this application are inside the 'customEventHandler' 
           routine */
        Cy_BLE_ProcessEvents();
        
        /* If a connection is detected, handle Sensor data transmission */
		if(deviceConnected == true)
		{
			/* Send Si7005 Temperature data when respective notification is 
               enabled */
            if(sendSi7005TempNotifications == CCCD_NOTIFY_BIT_MASK)
			{
				/* Send Si7005 Temperature data when respective notification is 
                   enabled */
                handleSi7005Temp();
			}
            if(sendSi7005HumidityNotifications == CCCD_NOTIFY_BIT_MASK)
			{
				/* Send Si7005 Humidity data when respective notification is 
                   enabled */
				handleSi7005Humidity();
			}                  
		}        
        
        /* Start the BLE advertisement if required */
        startAdvertisement();        
    }
}

BLE_applications.h
/* Include guard */
#ifndef BLE_APPLICATIONS_H
#define BLE_APPLICATIONS_H
 
/* Header file includes */
#include <project.h>
 
/* Bit mask for the notification bit in CCCD (Client Characteristic Configuration 
   Descriptor), which is written by the client device */
#define CCCD_NOTIFY_BIT_MASK    (uint8_t) (0x01u)
 
/* Event handler function for the three custom services in this project */
void    customEventHandler(uint32_t event, void *eventParameter);
 
void    handleSi7005Temp(void);
void    handleSi7005Humidity(void);
 
/* Function that checks for events from the handler and starts the BLE advertisement
  if required */
void    startAdvertisement(void);
 
#endif
BLE_applications.c
/* Header file includes */
#include "BLE_applications.h"
 
/* Client Characteristic Configuration descriptor (CCCD) data length */
#define CCCD_DATA_LEN   (uint8_t) (0x02u)
 
/* Indexes of a two-byte CCCD array */
#define CCCD_INDEX_0    (uint8_t) (0x00u)
#define CCCD_INDEX_1    (uint8_t) (0x01u)
 
/* Null byte used to initialize CCCD values */
#define CCCD_NULL       (uint8_t) (0x00u)
 
/* 'connectionHandle' stores the BLE connection parameters */
cy_stc_ble_conn_handle_t connectionHandle;
 
/* Read and write length of the si7005 data */
#define SI7005_DATA_LEN    (uint8_t) (0x02u)
 
/* Respective indexes of the si7005 */
#define SI7005_TEMP0_INDEX   (uint8_t) (0x00u)
#define SI7005_TEMP1_INDEX   (uint8_t) (0x01u)
 
/* Data lengths of si7005 Temperature data sent over BLE notifications */
#define SI7005_TEMP    (uint8_t) (0x01u)
#define SI7005_HUMIDITY    (uint8_t) (0x01u)
 
/* Array to store the present si7005 Temperature data. */
uint8_t             Si7005TempDataArray[SI7005_DATA_LEN];
uint8_t             Si7005HumidityDataArray[SI7005_DATA_LEN];
 
/* These flags are set when the Central device writes to CCCD (Client 
   Characteristic Configuration Descriptor) of the si7005 Temperature 
   Characteristic to enable notifications */
bool                sendSi7005TempNotifications = false;
bool                sendSi7005HumidityNotifications = false;
 
/* This flag is used by application to know whether a Central device has been 
   connected. This value is continuously updated in BLE event callback
   function */
bool                deviceConnected = false;
 
/* This is used to restart advertisements in the main firmware loop */
bool                restartAdvertisement = true;
 
/* Status flag for the Stack Busy state. This flag is used to notify the 
   application if there is stack buffer free to push more data or not */
bool                busyStatus = false;
 
#include "I2C_1.h"
 
#define I2C_TIMEOUT 100
 
#define SI7005_ADDR 0x40
#define SI7005_DEV_ID 0x50
 
/* SI7005 Registers */
#define SI7005_REG_STATUS         0x00
#define SI7005_REG_DATA           0x01
#define SI7005_REG_CONFIG         0x03
#define SI7005_REG_ID             0x11
 
/* Status Register */
#define SI7005_STATUS_NOT_READY   0x01
 
/* Config Register */
#define SI7005_CONFIG_START       0x01
#define SI7005_CONFIG_HEAT        0x02
#define SI7005_CONFIG_HUMIDITY    0x00
#define SI7005_CONFIG_HUMIDITY_START    0x01
#define SI7005_CONFIG_TEMPERATURE 0x10
#define SI7005_CONFIG_TEMPERATURE_START 0x11
#define SI7005_CONFIG_FAST        0x20
 
/*******************************************************************************
* Function Name: void startAdvertisement(void)
********************************************************************************
* Summary:
*  Check the "restartAdvertisement" flag and start the BLE advertisement if
*  the flag is "true"
*
* Parameters:
*  Void
*
* Return:
*  void
*
*******************************************************************************/
void startAdvertisement(void)
{
    /* Check if the restartAdvertisement flag is set by the event handler */
    if(restartAdvertisement)
	{
		/* Reset 'restartAdvertisement' flag */
		restartAdvertisement = false;
		
		/* Start Advertisement and enter discoverable mode */
		Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST,
                       CY_BLE_PERIPHERAL_CONFIGURATION_0_INDEX);	
	}
}
 
/*******************************************************************************
* Function Name: void sendSi7005HumidityNotification(uint8_t *Si7005HumidityDataArray)
********************************************************************************
* Summary:
*  Send Si7005Humidity data as BLE Notifications. This function updates
*  the notification handle with data and triggers the BLE component to send
*  notification
*
* Parameters:
*  Si7005HumidityDataArray:	 Si7005Humidity value
*
* Return:
*  void
*
*******************************************************************************/
void sendSi7005HumidityNotification(uint8_t *Si7005HumidityDataArray)
{
    /* 'Si7005HumiditynotificationHandle' stores  Si7005Humidit notification data
       parameters */
    cy_stc_ble_gatts_handle_value_ntf_t  Si7005HumiditynotificationHandle;
 
    /* If stack is not busy, then send the notification */
    if (busyStatus == CY_BLE_STACK_STATE_FREE)
    {
        /* Update notification handle with CapSense slider data */
        Si7005HumiditynotificationHandle.connHandle = connectionHandle;
        Si7005HumiditynotificationHandle.handleValPair.attrHandle = 
                            CY_BLE_WEATHER_STATION_HUMIDITY_CHAR_HANDLE;
        Si7005HumiditynotificationHandle.handleValPair.value.val = 
                            Si7005HumidityDataArray;
        Si7005HumiditynotificationHandle.handleValPair.value.len =
                            SI7005_DATA_LEN;
 
        /* Send the updated handle as part of attribute for notifications */
        Cy_BLE_GATTS_Notification(&Si7005HumiditynotificationHandle);
    }
}
 
/*******************************************************************************
* Function Name: void handleSi7005Humidity(void)
********************************************************************************
* Summary:
*  Read Si7005 Tempearature data
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleSi7005Humidity(void)
{
    /* CapSense Humidity value from the previous scan */
    static uint16_t   previousSi7005Humidity;
 
    /*  CapSense Humidity value from the current scan */
    uint16_t          currentSi7005Humidity;
    uint16_t          broadcastSi7005Humidity;    
    float             Si7005Humidity;
 
    static uint8_t tmp0 = 0;
    static uint8_t tmp1 = 0;    
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_CONFIG, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_CONFIG_HUMIDITY_START, I2C_TIMEOUT);
    I2C_1_MasterSendStop(I2C_TIMEOUT);
 
    CyDelay(100);
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_DATA, I2C_TIMEOUT);        
    I2C_1_MasterSendReStart(SI7005_ADDR, CY_SCB_I2C_READ_XFER, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_ACK, &tmp0, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_NAK, &tmp1, I2C_TIMEOUT);        
    I2C_1_MasterSendStop(I2C_TIMEOUT);
    currentSi7005Humidity = (tmp0 << 8);
    currentSi7005Humidity = (currentSi7005Humidity | (tmp1 & 0xFF));
    currentSi7005Humidity = (currentSi7005Humidity >> 4);   
    
    //org.bluetooth.characteristic.humidity int16, % with 0.01 resolution.
    Si7005Humidity = (float)(currentSi7005Humidity * 100.0);
    Si7005Humidity = (float)(Si7005Humidity / 16);
    Si7005Humidity = (float)(Si7005Humidity - 2400);
    broadcastSi7005Humidity = (uint16_t)Si7005Humidity;
    
    //org.bluetooth.characteristic.humidity is in little Endian...
    Si7005HumidityDataArray[SI7005_TEMP1_INDEX] = (int8_t)((broadcastSi7005Humidity & 0xFF00) >> 8);
    Si7005HumidityDataArray[SI7005_TEMP0_INDEX] = (int8_t)(broadcastSi7005Humidity & 0x00FF);    
    
    if (currentSi7005Humidity != previousSi7005Humidity)
    {    
        /* Send data over slider notification */
        sendSi7005HumidityNotification(Si7005HumidityDataArray);
        /* Update the local static variable with the present Humidity */
        previousSi7005Humidity = currentSi7005Humidity;
    };
}
 
/*******************************************************************************
* Function Name: void sendSi7005TempNotification(uint8_t *Si7005TempDataArray)
********************************************************************************
* Summary:
*  Send Si7005Temp data as BLE Notifications. This function updates
*  the notification handle with data and triggers the BLE component to send
*  notification
*
* Parameters:
*  Si7005TempDataArray:	 Si7005Temp value
*
* Return:
*  void
*
*******************************************************************************/
void sendSi7005TempNotification(uint8_t *Si7005TempDataArray)
{
    cy_stc_ble_gatts_handle_value_ntf_t  Si7005TempnotificationHandle;
 
    /* If stack is not busy, then send the notification */
    if (busyStatus == CY_BLE_STACK_STATE_FREE)
    {
        /* Update notification handle with Si7005 Temperature data */
        Si7005TempnotificationHandle.connHandle = connectionHandle;
        Si7005TempnotificationHandle.handleValPair.attrHandle = 
                            CY_BLE_WEATHER_STATION_TEMPERATURE_CHAR_HANDLE;
        Si7005TempnotificationHandle.handleValPair.value.val = 
                            Si7005TempDataArray;
        Si7005TempnotificationHandle.handleValPair.value.len =
                            SI7005_DATA_LEN;
 
        /* Send the updated handle as part of attribute for notifications */
        Cy_BLE_GATTS_Notification(&Si7005TempnotificationHandle);
    }
}
 
/*******************************************************************************
* Function Name: void handleSi7005Temp(void)
********************************************************************************
* Summary:
*  Read Si7005 Tempearature data
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleSi7005Temp(void)
{
    /* Si7005 Temperature value from the previous scan */
    static uint16_t   previousSi7005Temp;
 
    /*  Si7005 Temperature value from the current scan */
    uint16_t          currentSi7005Temp;
    int16_t           broadcastSi7005Temp;    
    float             Si7005Temp;
 
    static uint8_t tmp0 = 0;
    static uint8_t tmp1 = 0;    
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_CONFIG, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_CONFIG_TEMPERATURE_START, I2C_TIMEOUT);
    I2C_1_MasterSendStop(I2C_TIMEOUT);
 
    CyDelay(100);
 
    I2C_1_MasterSendStart(SI7005_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT);
    I2C_1_MasterWriteByte(SI7005_REG_DATA, I2C_TIMEOUT);        
    I2C_1_MasterSendReStart(SI7005_ADDR, CY_SCB_I2C_READ_XFER, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_ACK, &tmp0, I2C_TIMEOUT);
    I2C_1_MasterReadByte(CY_SCB_I2C_NAK, &tmp1, I2C_TIMEOUT);        
    I2C_1_MasterSendStop(I2C_TIMEOUT);
    currentSi7005Temp = (tmp0 << 8);
    currentSi7005Temp = (currentSi7005Temp | (tmp1 & 0xFF));
    currentSi7005Temp = (currentSi7005Temp >> 2);
 
    //org.bluetooth.characteristic.temperature sint16, Celsius with 0.01 degress resolution.
    Si7005Temp = (float)(currentSi7005Temp * 100.0);
    Si7005Temp = (float)(Si7005Temp / 32);
    Si7005Temp = (float)(Si7005Temp - 5000);
    broadcastSi7005Temp = (int16_t)Si7005Temp;
 
    //org.bluetooth.characteristic.temperature is in little Endian..
    Si7005TempDataArray[SI7005_TEMP1_INDEX] = (int8_t)((broadcastSi7005Temp & 0xFF00) >> 8);
    Si7005TempDataArray[SI7005_TEMP0_INDEX] = (int8_t)(broadcastSi7005Temp & 0x00FF);
 
    if (currentSi7005Temp != previousSi7005Temp)
    {    
        /* Send data over Si7005 Temperature notification */
        sendSi7005TempNotification(Si7005TempDataArray);
 
        /* Update the local static variable with the present Temperature */
        previousSi7005Temp = currentSi7005Temp;
    };
}
/*******************************************************************************
* Function Name: void handleDisconnectEventforSi7005Humidity(void)
********************************************************************************
* Summary:
*  This functions handles the 'disconnect' event for the Si7005Humidity service
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleDisconnectEventforSi7005Humidity(void)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005HumidityNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t Si7005HumidityCCCDvalue[CCCD_DATA_LEN];
 
    /* Reset Si7005 notification flag to prevent further notifications
        being sent to Central device after next connection. */
    sendSi7005HumidityNotifications = false;
 
    /* Reset the Si7005 CCCD value to disable notifications */
    /* Write the present Si7005Humidity notification status to the local variable */
    Si7005HumidityCCCDvalue[CCCD_INDEX_0] = sendSi7005HumidityNotifications;
    Si7005HumidityCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
 
    /* Update the Si7005 CCCD handle with notification status data*/
    Si7005HumidityNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
    Si7005HumidityNotificationCCCDhandle.value.val = Si7005HumidityCCCDvalue;
    Si7005HumidityNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report the Si7005 data to BLE component for sending data when read by 
       the Central device */
    attributeHandle.handleValuePair = Si7005HumidityNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
 
/*******************************************************************************
* Function Name: void handleDisconnectEventforSi7005Temp(void)
********************************************************************************
* Summary:
*  This functions handles the 'disconnect' event for the Si7005Temp service
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void handleDisconnectEventforSi7005Temp(void)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005TempNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t Si7005TempCCCDvalue[CCCD_DATA_LEN];
 
    /* Reset Si7005 notification flag to prevent further notifications
        being sent to Central device after next connection. */
    sendSi7005TempNotifications = false;
 
    /* Reset the Si7005 CCCD value to disable notifications */
    /* Write the present Si7005Temp notification status to the local variable */
    Si7005TempCCCDvalue[CCCD_INDEX_0] = sendSi7005TempNotifications;
    Si7005TempCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
    /* Update the Si7005 CCCD handle with notification status data*/
    Si7005TempNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
    Si7005TempNotificationCCCDhandle.value.val = Si7005TempCCCDvalue;
    Si7005TempNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report the Si7005 data to BLE component for sending data when read by 
       the Central device */
    attributeHandle.handleValuePair = Si7005TempNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
/*******************************************************************************
* Function Name: void handleWriteRequestforSi7005Humidity
*                     (cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
********************************************************************************
* Summary:
*  This functions handles the 'write request' event for the Si7005Humidity service
*
* Parameters:
*  writeRequest : pointer to the write request parameters from the central       
*
* Return:
*  void
*
*******************************************************************************/
void handleWriteRequestforSi7005Humidity(cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005HumidityNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t                         Si7005HumidityCCCDvalue[CCCD_DATA_LEN];
 
    /* Extract the Write value sent by the Client for CapSense Button CCCD */
    if (writeRequest->handleValPair.value.val
        [CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
         == true)
    {
        sendSi7005HumidityNotifications = true;
    }
    else if (writeRequest->handleValPair.value.val
             [CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
             == false)
    {
        sendSi7005HumidityNotifications = false;
    }
 
    /* Write the present CapSense notification status to the local variable */
    Si7005HumidityCCCDvalue[CCCD_INDEX_0] = sendSi7005HumidityNotifications;
    Si7005HumidityCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
 
    /* Update CCCD handle with notification status data */
    Si7005HumidityNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
    
    Si7005HumidityNotificationCCCDhandle.value.val = Si7005HumidityCCCDvalue;
    Si7005HumidityNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report data to BLE component for sending data when read by the central
       device */
    attributeHandle.handleValuePair = Si7005HumidityNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
 
/*******************************************************************************
* Function Name: void handleWriteRequestforSi7005Temp
*                     (cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
********************************************************************************
* Summary:
*  This functions handles the 'write request' event for the Si7005Temp service
*
* Parameters:
*  writeRequest : pointer to the write request parameters from the central       
*
* Return:
*  void
*
*******************************************************************************/
void handleWriteRequestforSi7005Temp(cy_stc_ble_gatts_write_cmd_req_param_t *writeRequest)
{
    /* Local variable 'attributeHandle' stores attribute parameters*/
    cy_stc_ble_gatts_db_attr_val_info_t  attributeHandle;
 
    /* Handle value to update the CCCD */
    cy_stc_ble_gatt_handle_value_pair_t  Si7005TempNotificationCCCDhandle;
 
    /* Local variable to store the current CCCD value */
    uint8_t                         Si7005TempCCCDvalue[CCCD_DATA_LEN];
 
    /* Extract the Write value sent by the Client for CapSense Button CCCD */
    if (writeRequest->handleValPair.value.val
        [CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
         == true)
    {
        sendSi7005TempNotifications = true;
    }
    else if (writeRequest->handleValPair.value.val
             [CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_INDEX]
             == false)
    {
        sendSi7005TempNotifications = false;
    }
 
    /* Write the present CapSense notification status to the local variable */
    Si7005TempCCCDvalue[CCCD_INDEX_0] = sendSi7005TempNotifications;
    Si7005TempCCCDvalue[CCCD_INDEX_1] = CCCD_NULL;
 
    /* Update CCCD handle with notification status data */
    Si7005TempNotificationCCCDhandle.attrHandle 
    = CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
 
    Si7005TempNotificationCCCDhandle.value.val = Si7005TempCCCDvalue;
    Si7005TempNotificationCCCDhandle.value.len = CCCD_DATA_LEN;
 
    /* Report data to BLE component for sending data when read by the central
       device */
    attributeHandle.handleValuePair = Si7005TempNotificationCCCDhandle;
    attributeHandle.offset = CCCD_NULL;
    attributeHandle.connHandle = connectionHandle;
    attributeHandle.flags = CY_BLE_GATT_DB_PEER_INITIATED;
    Cy_BLE_GATTS_WriteAttributeValueCCCD(&attributeHandle);
}
/*******************************************************************************
* Function Name: void customEventHandler(uint32_t event, void *eventParameter)
********************************************************************************
* Summary:
*  Call back event function to handle various events from the BLE stack
*
* Parameters:
*  event            :	event returned
*  eventParameter   :	link to value of the events returned
*
* Return:
*  void
*
*******************************************************************************/
void customEventHandler(uint32_t event, void *eventParameter)
{
    /* Local variable to store the data received as part of the write request
       events */
    cy_stc_ble_gatts_write_cmd_req_param_t   *writeReqParameter;
    
    /* Take an action based on the current event */
    switch (event)
    {
        /* This event is received when the BLE stack is Started */
        case CY_BLE_EVT_STACK_ON:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
               API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GAP EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
            
        /* If the current BLE state is Disconnected, then the Advertisement
           Start-Stop event implies that advertisement has stopped */
        case CY_BLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
            
            /* Check if the advertisement has stopped */
            if (Cy_BLE_GetState() == CY_BLE_STATE_STOPPED)
            {
                /* Set restartAdvertisement flag to allow calling Advertisement
                   API from the main function */
                restartAdvertisement = true;
            }
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED:
            
            /* Set restartAdvertisement flag to allow calling Advertisement
             API from the main function */
            restartAdvertisement = true;
            break;
        /* ~~~~~~~~~~~~~~~~~~~~~~GATT EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        
        /* This event is received when device is connected over GATT level */    
        case CY_BLE_EVT_GATT_CONNECT_IND:
            
            /* Update attribute handle on GATT Connection*/
            connectionHandle = *(cy_stc_ble_conn_handle_t *) eventParameter;
            /* This flag is used by the application to check the connection
               status */
            deviceConnected = true;
            break;
        
        /* This event is received when device is disconnected */
        case CY_BLE_EVT_GATT_DISCONNECT_IND:
           
            /* Update deviceConnected flag*/
            deviceConnected = false;
            
            /* Call the functions that handle the disconnect events for all 
               custom services */
            handleDisconnectEventforSi7005Temp();
            handleDisconnectEventforSi7005Humidity();       
            break;
        
        /* This event is received when Central device sends a Write command
           on an Attribute */
        case CY_BLE_EVT_GATTS_WRITE_REQ:
            
            /* Read the write request parameter */
            writeReqParameter = (cy_stc_ble_gatts_write_cmd_req_param_t *) 
                                eventParameter;
            /* When this event is triggered, the peripheral has received a 
               write command on the custom  characteristic. Check if command
               fits any of the custom attributes and update the flag for
               sending notifications by the respective service */
            if (CY_BLE_WEATHER_STATION_TEMPERATURE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE
                == writeReqParameter->handleValPair.attrHandle)
            {
                handleWriteRequestforSi7005Temp(writeReqParameter);
            }
            if (CY_BLE_WEATHER_STATION_HUMIDITY_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE
                == writeReqParameter->handleValPair.attrHandle)
            {
                handleWriteRequestforSi7005Humidity(writeReqParameter);
            }            
            /* Send the response to the write request received. */
            Cy_BLE_GATTS_WriteRsp(connectionHandle);
            break;
        
        /* This event is generated when the internal stack buffer is full and no
           more data can be accepted or the stack has buffer available and can 
           accept data. This event is used by application to prevent pushing lot
           of data to the BLE stack. */
        case CY_BLE_EVT_STACK_BUSY_STATUS:
            
            /* Extract the present stack status */
            busyStatus = *(uint8_t *) eventParameter;
            break;
        
        /* Do nothing for all other events */
        default:
            break;
    }
}

 

Comments

Comments, feedback, and questions can be sent to: eewiki@digikey.com
Please use the Digi-Key's TechForum: TechForum

 

  • No labels