hi, i want to append data before sending. i am using ble_uart example. here suppose the data is available on uart buffer at 1data/sec. i want to sent some timer/counter which appends if there will be more data will be available in uart buffer. say 5data/per sencond then. i should be able to append all the data in a single packet.
currently i am using timeout to send data. here when i set timeout 500 ms (since 1000<500). then it send it porperly. but when the data is more say 5data/sec then program get restarted.
how should i approch the problem with approporate data appending before sending when there is more data available in uart buffer.
thanks!!
my code :
static void timer_handler(void * p_context)
{
preCount++;
uint32_t err_code;
//SEGGER_RTT_WriteString (0, "--> in timeout handler\n");
//SEGGER_RTT_printf (0, "preCount : %d\tIndex:%d\n",preCount,index);
SEGGER_RTT_printf(0, "Time Out. \n");
err_code = app_timer_stop(wait_timer_id);
APP_ERROR_CHECK(err_code);
if((datacnt%3==0)&(preCount!=datacnt/3))
//SEGGER_RTT_printf (0, "preCount : %d\tcount:%d\tdatacnt:%d\n",preCount,count,datacnt);
SEGGER_RTT_printf (0, "Add Data\n");
else
SEGGER_RTT_printf (0, "Send all Data\n");
SEGGER_RTT_printf (0, "preCount : %d\tcount:%d\tdatacnt:%d\n",preCount,count,datacnt/3);
}
// Create timers
static void init_timers()
{
uint32_t err_code;
// Initialize the Application timer Library.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
//SEGGER_RTT_printf(0, "APP_TIMER_INTI initialised. \n");
// Create timers
err_code = app_timer_create(&wait_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
timer_handler);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(wait_timer_id, APP_TIMER_TICKS(1500, APP_TIMER_PRESCALER), NULL);
APP_ERROR_CHECK(err_code);
//SEGGER_RTT_printf(0, "Timer Started \n");
}
/*@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' i.e '\n' (hex 0x0D) or if the string has reached a length of
* @ref NUS_MAX_DATA_LENGTH.
*/
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
uint8_t data[5];
static uint8_t index,push = 0;
//int index=0;
uint32_t err_code;
switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++;datacnt++;
//preCount = index;
//SEGGER_RTT_printf(0, "index:%d\tprecnt:%d\n",index,preCount);
if(index%3 == 0)
{
count++;
//preCount = count;
init_timers();
//preCount = count;
//SEGGER_RTT_printf(0, "index:%d\tprecnt:%d\tdatacnt:%d\n",index,preCount,datacnt);
//SEGGER_RTT_printf(0, "Application Timer Created. \n");
//SEGGER_RTT_printf(0, "Data Count :%d\n",count);
//SEGGER_RTT_printf(0, "index :%d\n",index);
const uint8_t* midiData = parseMIDItoAppleBle(3, data_array);
for(int i=0;i<5;i++)
{
data[i] = midiData[i];
}
//SEGGER_RTT_printf(0, "%x\t%x\t%x\t%x\t%x\tCount:%d%tindex:%d\n",data[0], data[1],data[2],data[3], data[4],count, index);
/*err_code = ble_nus_string_send(&m_nus, data, 5);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}*/
//count++;
index = 0;
}
break;
}