static uint8 str_cmp(uint8 *p1,uint8 *p2,uint8 len)
{
uint8 i = 0;
while(i<len)
{
if (p1[i]!= p2[i])
{
return 0;
}
i++;
}
return 1;
}
//HalLedSet( m_led, HAL_LED_MODE_ON );
bool simple_ble_at_cmd_handle(uint8 *p_buffer,uint16 len)
{
bool ret = TRUE;
char str_temp[64];
if (4==len && strstr((char const *)p_buffer,"AT\r\n"))//if (4==len && str_cmp(p_buffer,"AT\r\n",4))
{
sprintf(str_temp,"OK\r\n");
NPI_WriteTransport((uint8*)str_temp,strlen(str_temp));
}
else if (9==len && strstr((char const *)p_buffer,"AT+LED"))//else if (9==len && str_cmp(p_buffer,"AT+LED",6))
{
/*
Para: 0 ~ 1
AT+LED0\r\n: 关灯
AT+LED1\r\n: 开灯
Default: 0
*/
switch(p_buffer[6])
{
case '?':
sprintf(str_temp,"OK+Get:%d\r\n",P1_0);
NPI_WriteTransport((uint8*)str_temp,strlen(str_temp));
break;
case '0':
P1_0=0;
HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
sprintf(str_temp,"OK+Set:%d\r\n",P1_0);
NPI_WriteTransport((uint8*)str_temp,strlen(str_temp));
break;
case '1':
P1_0=1;
HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
sprintf(str_temp,"OK+Set:%d\r\n",P1_0);
NPI_WriteTransport((uint8*)str_temp,strlen(str_temp));
break;
default:
ret = FALSE;
break;
}
} else
{
sprintf(str_temp,"ERROR\r\n");
NPI_WriteTransport((uint8*)str_temp,strlen(str_temp));
ret = FALSE;
}
return ret;
}
static void npi_serial_call_back(uint8_t port,uint8_t events)
{
(void)port;
static uint32 old_time=0;//老时间
static uint32 old_time_data_len=0;
uint32 new_time;
uint8 read_max_bytes = 20;
if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL))
{
uint8_t num_bytes=0;
num_bytes = NPI_RxBufLen();
if ( 0 == num_bytes )
{
old_time_data_len=0;
return;
}
if (old_time_data_len == 0)
{
old_time = osal_GetSystemClock();
old_time_data_len = num_bytes;
} else
{
new_time = osal_GetSystemClock();
if ( num_bytes>=read_max_bytes || (new_time-old_time)>20)
{
uint8 send_bytes=0;
uint8 *buffer = osal_mem_alloc(num_bytes);
if (num_bytes > read_max_bytes)
{
send_bytes = read_max_bytes;
} else
{
send_bytes = num_bytes;
}
if (buffer)
{
//读取读取串口缓冲区数据,释放串口数据
NPI_ReadTransport(buffer,num_bytes);
simple_ble_at_cmd_handle(buffer,send_bytes);
osal_mem_free(buffer);
}
old_time = new_time;
old_time_data_len = num_bytes - send_bytes;
}
}
}
}
测试效果;
image.png
网友评论