/******************************************************************************* * Function Name: main ******************************************************************************** * * Summary: * Main function. * * Parameters: * None * * Return: * None * *******************************************************************************/ int main() { /* Local variables */ char8 command; int8 intTxPowerLevel; CYBLE_API_RESULT_T apiResult; CYBLE_BLESS_PWR_IN_DB_T txPower; CyGlobalIntEnable; isr_DIS_StartEx(DIS_Interrupt); UART_Start(); CyBle_Start(StackEventHandler); /*Start BLE*/ /* Register the event handler for DIS specific events */ CyBle_DisRegisterAttrCallback(DisEventHandler); /* Register the event handler for TPS specific events */ CyBle_TpsRegisterAttrCallback(TpsServiceAppEventHandler); for(;;) { CyBle_ProcessEvents(); HandleLeds(); /*If SW2 is pressed then start to discover the services of server*/ if(discoverServer==TRUE) { CyBle_GattcStartDiscovery(cyBle_connHandle); discoverServer=FALSE; } command = UART_UartGetChar(); if( command != 0u) {/*Client related functions*/ switch(command) { if(disSupport) { case '1': /*To read the Manufacturer Name */ printf("Manufacturer Name:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_MANUFACTURER_NAME); break; case '2': /*To read the Model number*/ printf("Model Number:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_MODEL_NUMBER); break; case '3': /*To read the serial number*/ printf("Serial Number:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_SERIAL_NUMBER); break; case '4': /*To read hardware Revision*/ printf("Hardware Rev:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_HARDWARE_REV); break; case '5': /*To read Firmware Revision*/ printf("Firmware Rev:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_FIRMWARE_REV); break; case '6': /*To read Software Revision*/ printf("Software Rev:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_SOFTWARE_REV); break; case '7': /*To read System ID*/ printf("System ID:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_SYSTEM_ID); break; case '8': /*To read IEEE 11073 -20601 certifications details*/ printf("IEEE 11073-20601:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_REG_CERT_DATA); break; case '9': /*To read PNP ID*/ printf("PNP ID:"); CyBle_DiscGetCharacteristicValue(cyBle_connHandle,CYBLE_DIS_PNP_ID); break; } else { printf("Client hasn't discovered the services of server or server doesn't support DIS service\r\n"); } /*Server related fucntions*/ /* Decrease Tx power level of BLE radio if button is pressed */ case'd': /* Specify connection channel for reading Tx power level */ txPower.bleSsChId = CYBLE_LL_CONN_CH_TYPE; /* Get current Tx power level */ CyBle_GetTxPowerLevel(&txPower); /* Decrease the Tx power level by one scale */ DecreaseTxPowerLevelValue(&txPower.blePwrLevelInDbm); /* Set the new Tx power level */ apiResult = CyBle_SetTxPowerLevel(&txPower); if(CYBLE_ERROR_OK == apiResult) { /* Convert power level to numeric int8 value */ intTxPowerLevel = ConvertTxPowerlevelToInt8(txPower.blePwrLevelInDbm); (void) CyBle_TpssSetCharacteristicValue(CYBLE_TPS_TX_POWER_LEVEL, CYBLE_TPS_TX_POWER_LEVEL_SIZE, &intTxPowerLevel); /* Display new Tx Power Level value */ printf("Tx power level is set to %d dBm\r\n", intTxPowerLevel); } break; case 'n': /*Send notification to the client about the TX power level*/ CyBle_TpssSendNotification(cyBle_connHandle,CYBLE_TPS_TX_POWER_LEVEL,CYBLE_TPS_TX_POWER_LEVEL_SIZE,&intTxPowerLevel); break; } } } }
int main() { CYBLE_LP_MODE_T lpMode; CYBLE_BLESS_STATE_T blessState; CYBLE_STACK_LIB_VERSION_T stackVersion; CyGlobalIntEnable; UART_DEB_Start(); /* Start communication component */ printf("BLE Heart Rate Collector Example Project \r\n"); Disconnect_LED_Write(LED_OFF); Scanning_LED_Write(LED_OFF); Notification_LED_Write(LED_OFF); apiResult = CyBle_Start(AppCallBack); if(apiResult != CYBLE_ERROR_OK) { printf("CyBle_Start API Error: 0x%x \r\n", apiResult); } apiResult = CyBle_GetStackLibraryVersion(&stackVersion); if(apiResult != CYBLE_ERROR_OK) { printf("CyBle_GetStackLibraryVersion API Error: 0x%x \r\n", apiResult); } else { printf("Stack Version: %d.%d.%d.%d \r\n", stackVersion.majorVersion, stackVersion.minorVersion, stackVersion.patch, stackVersion.buildNumber); } CyBle_BasRegisterAttrCallback(BasCallBack); HrsInit(); while(1) { if(CyBle_GetState() != CYBLE_STATE_INITIALIZING) { /* Enter DeepSleep mode between connection intervals */ lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); CyGlobalIntDisable; blessState = CyBle_GetBleSsState(); if(lpMode == CYBLE_BLESS_DEEPSLEEP) { if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP) { /* Put the device into the DeepSleep mode only when all debug information has been sent */ if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u) { CySysPmDeepSleep(); } else { CySysPmSleep(); } } } else { if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE) { CySysPmSleep(); } } CyGlobalIntEnable; /* Handle advertising led blinking */ HandleLeds(); } /* Store bonding data to flash only when all debug information has been sent */ if((cyBle_pendingFlashWrite != 0u) && ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)) { apiResult = CyBle_StoreBondingData(0u); printf("Store bonding data, status: %x \r\n", apiResult); } /******************************************************************* * Processes all pending BLE events in the stack *******************************************************************/ CyBle_ProcessEvents(); } }
int main() { #ifdef LOW_POWER_MODE CYBLE_LP_MODE_T lpMode; CYBLE_BLESS_STATE_T blessState; #endif CYBLE_API_RESULT_T bleApiResult; CyGlobalIntEnable; /* Start UART and BLE component and display project information */ UART_Start(); bleApiResult = CyBle_Start(AppCallBack); if(bleApiResult == CYBLE_ERROR_OK) { #ifdef PRINT_MESSAGE_LOG UART_UartPutString("\n\r************************************************************"); UART_UartPutString("\n\r***************** BLE UART example project *****************"); UART_UartPutString("\n\r************************************************************\n\r"); UART_UartPutString("\n\rDevice role \t: CENTRAL"); #ifdef LOW_POWER_MODE UART_UartPutString("\n\rLow Power Mode \t: ENABLED"); #else UART_UartPutString("\n\rLow Power Mode \t: DISABLED"); #endif #ifdef FLOW_CONTROL UART_UartPutString("\n\rFlow Control \t: ENABLED"); #else UART_UartPutString("\n\rFlow Control \t: DISABLED"); #endif #endif } else { #ifdef PRINT_MESSAGE_LOG UART_UartPutString("\n\r\t\tCyBle stack initilization FAILED!!! \n\r "); #endif /* Enter infinite loop */ while(1); } CyBle_ProcessEvents(); /*************************************************************************** * Main polling loop ***************************************************************************/ while(1) { #ifdef LOW_POWER_MODE if((CyBle_GetState() != CYBLE_STATE_INITIALIZING) && (CyBle_GetState() != CYBLE_STATE_DISCONNECTED)) { /* Enter DeepSleep mode between connection intervals */ lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); CyGlobalIntDisable; blessState = CyBle_GetBleSsState(); if(lpMode == CYBLE_BLESS_DEEPSLEEP) { if((blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP) && \ (UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u) { #ifdef FLOW_CONTROL EnableUartRxInt(); #endif CySysPmSleep(); #ifdef FLOW_CONTROL DisableUartRxInt(); #endif } } else { if((blessState != CYBLE_BLESS_STATE_EVENT_CLOSE) && \ (UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u) { #ifdef FLOW_CONTROL EnableUartRxInt(); #endif CySysPmSleep(); #ifdef FLOW_CONTROL DisableUartRxInt(); #endif } } CyGlobalIntEnable; /* Handle advertising led blinking */ HandleLeds(); } #else HandleLeds(); #endif /******************************************************************* * Process all pending BLE events in the stack *******************************************************************/ HandleBleProcessing(); CyBle_ProcessEvents(); } }