1- #include <zephyr.h>
2- #include <device.h>
3- #include <pwm.h>
4- #include <sys_clock.h>
5- #include <misc/byteorder.h>
6- #include <adc.h>
1+ /* main.c - Application main entry point */
72
8- #if defined(CONFIG_STDOUT_CONSOLE )
9- #include <stdio.h>
10- #define PRINT printf
11- #else
3+ /*
4+ * Copyright (c) 2015 Intel Corporation
5+ *
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ #include <stdint.h>
20+ #include <stddef.h>
21+ #include <string.h>
22+ #include <errno.h>
1223#include <misc/printk.h>
13- #define PRINT printk
14- #endif
24+ #include <misc/byteorder.h>
25+ #include <zephyr.h>
1526
16- #define IO3_RED "PWM_0"
17- #define IO5_GREEN "PWM_1"
18- #define IO6_BLUE "PWM_2"
27+ #include <bluetooth/bluetooth.h>
28+ #include <bluetooth/hci.h>
29+ #include <bluetooth/conn.h>
30+ #include <bluetooth/uuid.h>
31+ #include <bluetooth/gatt.h>
1932
20- #define ADC_DEVICE_NAME "ADC_0 "
33+ #include "service.h "
2134
22- /*
23- * The analog input pin and channel number mapping
24- * for Arduino 101 board.
25- * A0 Channel 10
26- * A1 Channel 11
27- * A2 Channel 12
28- * A3 Channel 13
29- * A4 Channel 14
30- */
31- #define A0 10
32- #define BUFFER_SIZE 40
35+ #define DEVICE_NAME "Arduino101"
36+ #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
3337
34- static uint8_t buffer [ BUFFER_SIZE ] ;
38+ struct bt_conn * default_conn ;
3539
36- static struct adc_seq_entry sample = {
37- .sampling_delay = 12 ,
38- .channel_id = A0 ,
39- .buffer = buffer ,
40- .buffer_length = BUFFER_SIZE ,
40+ static const struct bt_data ad [] = {
41+ BT_DATA_BYTES (BT_DATA_FLAGS , (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR )),
42+ BT_DATA_BYTES (BT_DATA_UUID16_ALL , 0x00 , 0xfc ),
4143};
4244
43- static struct adc_seq_table table = {
44- .entries = & sample ,
45- .num_entries = 1 ,
45+ static const struct bt_data sd [] = {
46+ BT_DATA (BT_DATA_NAME_COMPLETE , DEVICE_NAME , DEVICE_NAME_LEN ),
4647};
4748
48- #define SLEEPTICKS SECONDS(1)
49-
50- void main (void )
49+ static void connected (struct bt_conn * conn , uint8_t err )
50+ {
51+ if (err ) {
52+ printk ("Connection failed (err %u)\n" , err );
53+ } else {
54+ default_conn = bt_conn_ref (conn );
55+ printk ("Connected\n" );
56+ }
57+ }
58+
59+ static void disconnected (struct bt_conn * conn , uint8_t reason )
5160{
52- struct nano_timer timer ;
53- uint32_t data [2 ] = {0 , 0 };
61+ printk ("Disconnected (reason %u)\n" , reason );
62+
63+ if (default_conn ) {
64+ bt_conn_unref (default_conn );
65+ default_conn = NULL ;
66+ }
67+ }
5468
55- struct device * red , * green , * blue , * tmp36 ;
69+ static struct bt_conn_cb conn_callbacks = {
70+ .connected = connected ,
71+ .disconnected = disconnected ,
72+ };
73+
74+ static void bt_ready (int err )
75+ {
76+ if (err ) {
77+ printk ("Bluetooth init failed (err %d)\n" , err );
78+ return ;
79+ }
5680
57- PRINT ( "Zephyr WebBluetooth demo \n" );
81+ printk ( "Bluetooth initialized \n" );
5882
59- red = device_get_binding (IO3_RED );
60- green = device_get_binding (IO5_GREEN );
61- blue = device_get_binding (IO6_BLUE );
83+ service_init ();
6284
63- if (!red || !green || !blue ) {
64- PRINT ("Cannot find LED connected to pin 3 (red), 5 (green) and 6 (blue).\n" );
65- }
85+ err = bt_le_adv_start (BT_LE_ADV (BT_LE_ADV_IND ), ad , ARRAY_SIZE (ad ),
86+ sd , ARRAY_SIZE (sd ));
87+ if (err ) {
88+ printk ("Advertising failed to start (err %d)\n" , err );
89+ return ;
90+ }
6691
67- tmp36 = device_get_binding (ADC_DEVICE_NAME );
68- if (!tmp36 ) {
69- PRINT ("Cannot find the TMP36 connected to pin A0.\n" );
70- }
92+ printk ("Advertising successfully started\n" );
93+ }
7194
72- nano_timer_init (& timer , data );
73- adc_enable (tmp36 );
95+ static void auth_cancel (struct bt_conn * conn )
96+ {
97+ char addr [BT_ADDR_LE_STR_LEN ];
7498
75- while (1 ) {
76- pwm_pin_set_values (red , 1024 , SLEEPTICKS , 0 );
99+ bt_addr_le_to_str (bt_conn_get_dst (conn ), addr , sizeof (addr ));
77100
78- if (adc_read (tmp36 , & table ) == 0 ) {
79- uint32_t length = BUFFER_SIZE ;
80- uint8_t * buf = buffer ;
81- for (; length > 0 ; length -= 4 , buf += 4 ) {
82- uint32_t rawValue = * ((uint32_t * ) buf );
83- float voltage = (rawValue / 1024.0 ) * 5.0 ;
84- float celsius = (voltage - 0.5 ) * 100 ;
85- PRINT ("Celsius %f\n" , celsius );
86- }
87- }
101+ printk ("Pairing cancelled: %s\n" , addr );
102+ }
88103
89- nano_timer_start ( & timer , SLEEPTICKS );
90- nano_timer_test ( & timer , TICKS_UNLIMITED );
91- }
104+ static struct bt_conn_auth_cb auth_cb_display = {
105+ . cancel = auth_cancel ,
106+ };
92107
93- adc_disable (tmp36 );
94- }
108+ #ifdef CONFIG_MICROKERNEL
109+ void mainloop (void )
110+ #else
111+ void main (void )
112+ #endif
113+ {
114+ int err ;
115+
116+ err = bt_enable (bt_ready );
117+ if (err ) {
118+ printk ("Bluetooth init failed (err %d)\n" , err );
119+ return ;
120+ }
121+
122+ bt_conn_cb_register (& conn_callbacks );
123+ bt_conn_auth_cb_register (& auth_cb_display );
124+
125+ /* Implement notification. At the moment there is no suitable way
126+ * of starting delayed work so we do it here
127+ */
128+ while (1 ) {
129+ task_sleep (sys_clock_ticks_per_sec );
130+
131+ /* Battery level simulation */
132+ service_notify ();
133+ }
134+ }
0 commit comments