btstack协议栈实战篇–Hello World example

作者 : admin 本文共1467个字,预计阅读时间需要4分钟 发布时间: 2024-06-9 共3人阅读

btstack协议栈—总目录-CSDN博客

目录

      1.定时计时器设置

      2.主要应用程序设置

        3.运行log如下图


        该示例演示了如何提供周期性定时器来切换LED并将调试消息作为最小BTstack测试发送到控制台。

      1.定时计时器设置

        由于BTstack中的计时器是单触发的,因此通过在心跳中重新注册计时器源来实现周期计数器
处理程序回调函数。这里的列举了适用于周期性切换LED和打印切换次数的心跳处理程序。

/* @section Periodic Timer Setup 
 *
 * @text As timers in BTstack are single shot,
 * the periodic counter is implemented by re-registering the timer source in the
 * heartbeat handler callback function. Listing LEDToggler shows heartbeat handler
 * adapted to periodically toggle an LED and print number of toggles.  
 */ 

/* LISTING_START(LEDToggler): Periodic counter */  
static void heartbeat_handler(btstack_timer_source_t *ts){
    UNUSED(ts);

    // increment counter
    char lineBuffer[30];
    snprintf(lineBuffer, sizeof(lineBuffer), "BTstack counter %04u
\r", ++counter);
    puts(lineBuffer);
    
    // toggle LED
    hal_led_toggle();

    // re-register timer
    btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
    btstack_run_loop_add_timer(&heartbeat);
} 
/* LISTING_END */

      2.主要应用程序设置

        下面主要的应用程序代码。它配置了心跳层,并将其添加到运行循环中。

/* @section Main Application Setup
 *
 * @text Listing MainConfiguration shows main application code.
 * It configures the heartbeat tier and adds it to the run loop.
 */
 
/* LISTING_START(MainConfiguration): Setup heartbeat timer */
int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
    (void)argc;
    (void)argv;

    // set one-shot timer
    heartbeat.process = &heartbeat_handler;
    btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
    btstack_run_loop_add_timer(&heartbeat);

    printf("Running...
\r");
    return 0;
}
/* LISTING_END */

        3.运行log如下图

btstack协议栈实战篇–Hello World example插图

本站无任何商业行为
个人在线分享 » btstack协议栈实战篇–Hello World example
E-->