xref: /btstack/example/led_counter.c (revision 9ec2630ce4bfd36adcb438d2e8ba6fc26890f857)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38bcf00d8fSMatthias Ringwald // *****************************************************************************
39bcf00d8fSMatthias Ringwald /* EXAMPLE_START(led_counter): Hello World: blinking LED without Bluetooth
40bcf00d8fSMatthias Ringwald  *
41bcf00d8fSMatthias Ringwald  * @text The example demonstrates how to provide a periodic timer to toggle an
42bcf00d8fSMatthias Ringwald  * LED and send debug messages to the console as a minimal BTstack test.
43bcf00d8fSMatthias Ringwald  */
44bcf00d8fSMatthias Ringwald // *****************************************************************************
45bcf00d8fSMatthias Ringwald 
46bcf00d8fSMatthias Ringwald 
47bcf00d8fSMatthias Ringwald 
48bcf00d8fSMatthias Ringwald #include <stdint.h>
49bcf00d8fSMatthias Ringwald #include <stdio.h>
50bcf00d8fSMatthias Ringwald 
51bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
52*9ec2630cSMatthias Ringwald #include "btstack_defines.h"
53bcf00d8fSMatthias Ringwald #include "hal_led.h"
54bcf00d8fSMatthias Ringwald 
55bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
56bcf00d8fSMatthias Ringwald 
57bcf00d8fSMatthias Ringwald static int counter = 0;
58bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat;
59bcf00d8fSMatthias Ringwald 
60bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup
61bcf00d8fSMatthias Ringwald  *
62bcf00d8fSMatthias Ringwald  * @text As timers in BTstack are single shot,
63bcf00d8fSMatthias Ringwald  * the periodic counter is implemented by re-registering the timer source in the
64bcf00d8fSMatthias Ringwald  * heartbeat handler callback function. Listing LEDToggler shows heartbeat handler
65bcf00d8fSMatthias Ringwald  * adapted to periodically toggle an LED and print number of toggles.
66bcf00d8fSMatthias Ringwald  */
67bcf00d8fSMatthias Ringwald 
68bcf00d8fSMatthias Ringwald /* LISTING_START(LEDToggler): Periodic counter */
69bcf00d8fSMatthias Ringwald static void heartbeat_handler(btstack_timer_source_t *ts){
70*9ec2630cSMatthias Ringwald     UNUSED(ts);
71*9ec2630cSMatthias Ringwald 
72bcf00d8fSMatthias Ringwald     // increment counter
73bcf00d8fSMatthias Ringwald     char lineBuffer[30];
74bcf00d8fSMatthias Ringwald     sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
75bcf00d8fSMatthias Ringwald     puts(lineBuffer);
76bcf00d8fSMatthias Ringwald 
77bcf00d8fSMatthias Ringwald     // toggle LED
78bcf00d8fSMatthias Ringwald     hal_led_toggle();
79bcf00d8fSMatthias Ringwald 
80bcf00d8fSMatthias Ringwald     // re-register timer
81bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
82bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
83bcf00d8fSMatthias Ringwald }
84bcf00d8fSMatthias Ringwald /* LISTING_END */
85bcf00d8fSMatthias Ringwald 
86bcf00d8fSMatthias Ringwald /* @section Main Application Setup
87bcf00d8fSMatthias Ringwald  *
88bcf00d8fSMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
89bcf00d8fSMatthias Ringwald  * It configures the heartbeat tier and adds it to the run loop.
90bcf00d8fSMatthias Ringwald  */
91bcf00d8fSMatthias Ringwald 
92bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup heartbeat timer */
93bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
94bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
95*9ec2630cSMatthias Ringwald     (void)argc;
96*9ec2630cSMatthias Ringwald     (void)argv;
97bcf00d8fSMatthias Ringwald 
98bcf00d8fSMatthias Ringwald     // set one-shot timer
99bcf00d8fSMatthias Ringwald     heartbeat.process = &heartbeat_handler;
100bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
101bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
102bcf00d8fSMatthias Ringwald 
103bcf00d8fSMatthias Ringwald     printf("Running...\n\r");
104bcf00d8fSMatthias Ringwald     return 0;
105bcf00d8fSMatthias Ringwald }
106bcf00d8fSMatthias Ringwald /* LISTING_END */
107bcf00d8fSMatthias Ringwald /* EXAMPLE_END */