Part Number:CC2640R2F
Hello,
I am trying to build my BLE application on the CC2640R2 with the simple_central and simple_peripheral examples. I would like to very quickly and reliably send a current button state on the simple_central side to the simple_peripheral side to turn on an LED. I have added this code to the SimpleCentral_handleKeys function along with supporting interrupt and callback functions to send a 1 when my button is actively pushed and then a 0 when it is released:
if (keys & KEY_SELECT_PRESSED)
{
switch (keyPressConnOpt)
{
case GATT_RW:
if (charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t status;
// Do a write
attWriteReq_t req;
req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
if ( req.pValue != NULL )
{
req.handle = charHdl;
req.len = 1;
req.pValue[0] = 1;
req.sig = 0;
req.cmd = 0;
status = GATT_WriteCharValue(connHandle, &req, selfEntity);
if ( status != SUCCESS )
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); //should not get here unless error
}
else
displayText("Write Sent 1");
}
else
{
status = bleMemAllocError; //Should never get here unless error
}
break;
}
}
}
if (keys & KEY_SELECT_RELEASED)
{
switch (keyPressConnOpt)
{
case GATT_RW:
if (charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t status;
// Do a write
attWriteReq_t req;
req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
if ( req.pValue != NULL )
{
req.handle = charHdl;
req.len = 1;
req.pValue[0] = 0;
req.sig = 0;
req.cmd = 0;
//retry:
status = GATT_WriteCharValue(connHandle, &req, selfEntity);
if ( status != SUCCESS && status != blePending )
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); //should not get here unless error
}
if ( status == blePending ) //Can't accept command at this time, recommended to wait some time and retry until success
{
//goto retry;
displayText("BLE Pending...");
}
else
displayText("Write Sent 0");
}
else
{
status = bleMemAllocError; //Should never get here unless error
}
break;
}
}
}This works fine when the button is pushed and held for a moment before releasing, but if it is pushed and released immediately, less than about 200ms, then the second GATT_WriteCharValue returns a 0x16 which is a blePending error. I tried implementing a simple retry system until it results in SUCCESS but it is still very slow and misses button presses and releases if rapidly pressed. Is there a better way I should be handling this to quickly send a boolean value from the central side to the peripheral side?
Thanks!