-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
131 lines (115 loc) · 2.87 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <logging/log.h>
#include <dk_buttons_and_leds.h>
#include "remote.h"
#define LOG_MODULE_NAME app
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define RUN_STATUS_LED DK_LED1
#define CONN_STATUS_LED DK_LED2
#define RUN_LED_BLINK_INTERVAL 1000
static struct bt_conn *current_conn;
/* Declarations */
void on_connected(struct bt_conn *conn, uint8_t err);
void on_disconnected(struct bt_conn *conn, uint8_t reason);
void on_notif_changed(enum bt_button_notifications_enabled status);
struct bt_conn_cb bluetooth_callbacks = {
.connected = on_connected,
.disconnected = on_disconnected,
};
struct bt_remote_service_cb remote_callbacks = {
.notif_changed = on_notif_changed,
};
void on_connected(struct bt_conn *conn, uint8_t err)
{
if(err) {
LOG_ERR("connection err: %d", err);
return;
}
LOG_INF("Connected.");
current_conn = bt_conn_ref(conn);
dk_set_led_on(CONN_STATUS_LED);
}
void on_disconnected(struct bt_conn *conn, uint8_t reason)
{
LOG_INF("Disconnected (reason: %d)", reason);
dk_set_led_off(CONN_STATUS_LED);
if(current_conn) {
bt_conn_unref(current_conn);
current_conn = NULL;
}
}
void on_notif_changed(enum bt_button_notifications_enabled status)
{
if (status == BT_BUTTON_NOTIFICATIONS_ENABLED) {
LOG_INF("Notifications enabled");
} else {
LOG_INF("Notifications disabled");
}
}
/* Callbacks */
void button_handler(uint32_t button_state, uint32_t has_changed)
{
int err;
int button_pressed = 0;
if (has_changed & button_state)
{
switch (has_changed)
{
case DK_BTN1_MSK:
button_pressed = 1;
break;
case DK_BTN2_MSK:
button_pressed = 2;
break;
case DK_BTN3_MSK:
button_pressed = 3;
break;
case DK_BTN4_MSK:
button_pressed = 4;
break;
default:
break;
}
LOG_INF("Button %d pressed.", button_pressed);
set_button_value(button_pressed);
err = send_button_notification(current_conn, button_pressed);
if (err) {
LOG_ERR("Couldn't send notificaton. (err: %d)", err);
}
}
}
/* Configurations */
static void configure_dk_buttons_leds(void)
{
int err;
err = dk_leds_init();
if (err) {
LOG_ERR("Couldn't init LEDS (err %d)", err);
}
err = dk_buttons_init(button_handler);
if (err) {
LOG_ERR("Couldn't init buttons (err %d)", err);
}
}
/* Main */
void main(void)
{
int err;
int blink_status = 0;
LOG_INF("Hello World! %s\n", CONFIG_BOARD);
configure_dk_buttons_leds();
err = bluetooth_init(&bluetooth_callbacks, &remote_callbacks);
if (err) {
LOG_INF("Couldn't initialize Bluetooth. err: %d", err);
}
LOG_INF("Running...");
for (;;) {
dk_set_led(RUN_STATUS_LED, (blink_status++)%2);
k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
}
}