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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka 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 <stdio.h>
51*e4cf526aSMatthias Ringwald #include "btstack.h"
52bcf00d8fSMatthias Ringwald #include "hal_led.h"
53bcf00d8fSMatthias Ringwald
54bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
55bcf00d8fSMatthias Ringwald
56bcf00d8fSMatthias Ringwald static int counter = 0;
57bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat;
58bcf00d8fSMatthias Ringwald
59bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup
60bcf00d8fSMatthias Ringwald *
61bcf00d8fSMatthias Ringwald * @text As timers in BTstack are single shot,
62bcf00d8fSMatthias Ringwald * the periodic counter is implemented by re-registering the timer source in the
63bcf00d8fSMatthias Ringwald * heartbeat handler callback function. Listing LEDToggler shows heartbeat handler
64bcf00d8fSMatthias Ringwald * adapted to periodically toggle an LED and print number of toggles.
65bcf00d8fSMatthias Ringwald */
66bcf00d8fSMatthias Ringwald
67bcf00d8fSMatthias Ringwald /* LISTING_START(LEDToggler): Periodic counter */
heartbeat_handler(btstack_timer_source_t * ts)68bcf00d8fSMatthias Ringwald static void heartbeat_handler(btstack_timer_source_t *ts){
699ec2630cSMatthias Ringwald UNUSED(ts);
709ec2630cSMatthias Ringwald
71bcf00d8fSMatthias Ringwald // increment counter
72*e4cf526aSMatthias Ringwald counter++;
73*e4cf526aSMatthias Ringwald
74*e4cf526aSMatthias Ringwald // print and log
75*e4cf526aSMatthias Ringwald log_info("BTstack Counter %u", counter);
76*e4cf526aSMatthias Ringwald printf("BTstack counter %04u\n\r", counter);
77bcf00d8fSMatthias Ringwald
78bcf00d8fSMatthias Ringwald // toggle LED
79bcf00d8fSMatthias Ringwald hal_led_toggle();
80bcf00d8fSMatthias Ringwald
81bcf00d8fSMatthias Ringwald // re-register timer
82bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
83bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
84bcf00d8fSMatthias Ringwald }
85bcf00d8fSMatthias Ringwald /* LISTING_END */
86bcf00d8fSMatthias Ringwald
87bcf00d8fSMatthias Ringwald /* @section Main Application Setup
88bcf00d8fSMatthias Ringwald *
89bcf00d8fSMatthias Ringwald * @text Listing MainConfiguration shows main application code.
90bcf00d8fSMatthias Ringwald * It configures the heartbeat tier and adds it to the run loop.
91bcf00d8fSMatthias Ringwald */
92bcf00d8fSMatthias Ringwald
93bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup heartbeat timer */
94bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])95bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
969ec2630cSMatthias Ringwald (void)argc;
979ec2630cSMatthias Ringwald (void)argv;
98bcf00d8fSMatthias Ringwald
99bcf00d8fSMatthias Ringwald // set one-shot timer
100bcf00d8fSMatthias Ringwald heartbeat.process = &heartbeat_handler;
101bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
102bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
103bcf00d8fSMatthias Ringwald
104bcf00d8fSMatthias Ringwald printf("Running...\n\r");
105bcf00d8fSMatthias Ringwald return 0;
106bcf00d8fSMatthias Ringwald }
107bcf00d8fSMatthias Ringwald /* LISTING_END */
108bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
109