xref: /btstack/example/led_counter.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH 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 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "led_counter.c"
39ab2c6ae4SMatthias Ringwald 
40bcf00d8fSMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(led_counter): Hello World - Blinking an LED without Bluetooth
42bcf00d8fSMatthias Ringwald  *
43bcf00d8fSMatthias Ringwald  * @text The example demonstrates how to provide a periodic timer to toggle an
44bcf00d8fSMatthias Ringwald  * LED and send debug messages to the console as a minimal BTstack test.
45bcf00d8fSMatthias Ringwald  */
46bcf00d8fSMatthias Ringwald // *****************************************************************************
47bcf00d8fSMatthias Ringwald 
48bcf00d8fSMatthias Ringwald 
49bcf00d8fSMatthias Ringwald 
50bcf00d8fSMatthias Ringwald #include <stdint.h>
51bcf00d8fSMatthias Ringwald #include <stdio.h>
52bcf00d8fSMatthias Ringwald 
53bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
549ec2630cSMatthias Ringwald #include "btstack_defines.h"
55bcf00d8fSMatthias Ringwald #include "hal_led.h"
56bcf00d8fSMatthias Ringwald 
57bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
58bcf00d8fSMatthias Ringwald 
59bcf00d8fSMatthias Ringwald static int counter = 0;
60bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat;
61bcf00d8fSMatthias Ringwald 
62bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup
63bcf00d8fSMatthias Ringwald  *
64bcf00d8fSMatthias Ringwald  * @text As timers in BTstack are single shot,
65bcf00d8fSMatthias Ringwald  * the periodic counter is implemented by re-registering the timer source in the
66bcf00d8fSMatthias Ringwald  * heartbeat handler callback function. Listing LEDToggler shows heartbeat handler
67bcf00d8fSMatthias Ringwald  * adapted to periodically toggle an LED and print number of toggles.
68bcf00d8fSMatthias Ringwald  */
69bcf00d8fSMatthias Ringwald 
70bcf00d8fSMatthias Ringwald /* LISTING_START(LEDToggler): Periodic counter */
71bcf00d8fSMatthias Ringwald static void heartbeat_handler(btstack_timer_source_t *ts){
729ec2630cSMatthias Ringwald     UNUSED(ts);
739ec2630cSMatthias Ringwald 
74bcf00d8fSMatthias Ringwald     // increment counter
75bcf00d8fSMatthias Ringwald     char lineBuffer[30];
76bcf00d8fSMatthias Ringwald     sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
77bcf00d8fSMatthias Ringwald     puts(lineBuffer);
78bcf00d8fSMatthias Ringwald 
79bcf00d8fSMatthias Ringwald     // toggle LED
80bcf00d8fSMatthias Ringwald     hal_led_toggle();
81bcf00d8fSMatthias Ringwald 
82bcf00d8fSMatthias Ringwald     // re-register timer
83bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
84bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
85bcf00d8fSMatthias Ringwald }
86bcf00d8fSMatthias Ringwald /* LISTING_END */
87bcf00d8fSMatthias Ringwald 
88bcf00d8fSMatthias Ringwald /* @section Main Application Setup
89bcf00d8fSMatthias Ringwald  *
90bcf00d8fSMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
91bcf00d8fSMatthias Ringwald  * It configures the heartbeat tier and adds it to the run loop.
92bcf00d8fSMatthias Ringwald  */
93bcf00d8fSMatthias Ringwald 
94bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup heartbeat timer */
95bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
96bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
979ec2630cSMatthias Ringwald     (void)argc;
989ec2630cSMatthias Ringwald     (void)argv;
99bcf00d8fSMatthias Ringwald 
100bcf00d8fSMatthias Ringwald     // set one-shot timer
101bcf00d8fSMatthias Ringwald     heartbeat.process = &heartbeat_handler;
102bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
103bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
104bcf00d8fSMatthias Ringwald 
105bcf00d8fSMatthias Ringwald     printf("Running...\n\r");
106bcf00d8fSMatthias Ringwald     return 0;
107bcf00d8fSMatthias Ringwald }
108bcf00d8fSMatthias Ringwald /* LISTING_END */
109bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
110