Skip to content

Instantly share code, notes, and snippets.

@epietrowicz
Last active October 8, 2020 14:31
Show Gist options
  • Save epietrowicz/691f251730eb254ea65a6d759caffd94 to your computer and use it in GitHub Desktop.
Save epietrowicz/691f251730eb254ea65a6d759caffd94 to your computer and use it in GitHub Desktop.
main ble config
uint8 timerNotify = 0;
uint8 bleConnected = 0;
uint8 count = 0;
CYBLE_CONN_HANDLE_T connectionHandle;
static volatile uint32_t millisecondTimerCount = 0u;
/************************************************************
* This function is the handler for BLE stack events
*
* Arguments:
* eventCode is the type of event that triggered the callback
* eventParam contains parameters related to the event
************************************************************/
void Stack_Handler( uint32 eventCode, void *eventParam)
{
CYBLE_GATTS_WRITE_REQ_PARAM_T *wrReq;
switch ( eventCode )
{
case CYBLE_EVT_STACK_ON:
case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
/* Start advertising when the stack first turns on or on a disconnect event */
/* Start advertising when the stack first turns on or on a disconnect event */
CyBle_GappStartAdvertisement( CYBLE_ADVERTISING_FAST );
bleConnected = 0;
break;
case CYBLE_EVT_GATT_CONNECT_IND:
/* When the connection event happens, save the connection handle to be used later */
connectionHandle = *(CYBLE_CONN_HANDLE_T *)eventParam;
bleConnected = 1;
break;
case CYBLE_EVT_GATTS_WRITE_REQ:
/* If the client sends a write request to the CapSense Slider CCCD to enable or */
/* disable notifications, read that value and set capsenseNotify appropriately */
wrReq = (CYBLE_GATTS_WRITE_REQ_PARAM_T *)eventParam;
if( wrReq->handleValPair.attrHandle == CYBLE_TIMERSERVICE_TIMER_TIMERCCCD_DESC_HANDLE )
{
CyBle_GattsWriteAttributeValue( &wrReq->handleValPair, 0, &connectionHandle, CYBLE_GATT_DB_LOCALLY_INITIATED );
/* Set notifications based on whether the client turned them on or off */
timerNotify = wrReq->handleValPair.value.val[0];
}
/* Send a response so that the client can verify that the notification was set as requested */
CyBle_GattsWriteRsp( connectionHandle );
break;
default:
break;
}
}
int main(void)
{
UART_Start();
CyGlobalIntEnable; /* Enable global interrupts. */
/* Enable the Interrupt component connected to Timer interrupt */
Timer_1_Interrupt_StartEx(timer1InterruptHandler);
Timer_1_Start();
// Reset count
millisecondTimerCount = 0;
CyBle_Start( Stack_Handler );
uint32_t previousTimeInSeconds = millisecondTimerCount;
uint32_t interval = 5000;
for(;;)
{
uint32_t currentTimeInSeconds = millisecondTimerCount;
// Process any pending BLE events (both stack and IAS events)
CyBle_ProcessEvents();
if(bleConnected){
if (currentTimeInSeconds - previousTimeInSeconds > interval){
previousTimeInSeconds = currentTimeInSeconds;
count++;
CYBLE_GATTS_HANDLE_VALUE_IND_T timerHandle;
timerHandle.attrHandle = CYBLE_TIMERSERVICE_TIMER_CHAR_HANDLE;
timerHandle.value.val = &count;
timerHandle.value.len = 1;
if(timerNotify){
CyBle_GattsNotification( connectionHandle , &timerHandle );
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment