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