-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.c
More file actions
351 lines (313 loc) · 13 KB
/
app.c
File metadata and controls
351 lines (313 loc) · 13 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/***************************************************************************//**
* @file
* @brief Core application logic.
*******************************************************************************
* # License
* <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
#include <stdbool.h>
#include "em_common.h"
#include "sl_status.h"
#include "sl_simple_button_instances.h"
#include "app_timer.h"
#include "app_log.h"
#include "app_assert.h"
#include "sl_bluetooth.h"
#ifdef SL_COMPONENT_CATALOG_PRESENT
#include "sl_component_catalog.h"
#endif // SL_COMPONENT_CATALOG_PRESENT
#include "sl_sensor_rht.h"
#include "sl_health_thermometer.h"
#include "app.h"
#include "memfault/components.h"
// Connection handle.
static uint8_t app_connection = 0;
// The advertising set handle allocated from Bluetooth stack.
static uint8_t advertising_set_handle = 0xff;
// Button state.
static volatile bool app_btn0_pressed = false;
// Periodic timer handle.
static app_timer_t app_periodic_timer;
// Periodic timer callback.
static void app_periodic_timer_cb(app_timer_t *timer, void *data);
/**************************************************************************//**
* Application Init.
*****************************************************************************/
#define APP_TASK_PRIO 21u
#define APP_TASK_STACK_SIZE 256u /* Stack size in CPU_STK. */
static OS_TCB s_app_task_tcb;
static CPU_STK s_app_task_stack[APP_TASK_STACK_SIZE];
static void app_task(void *p_arg)
{
(void)p_arg;
RTOS_ERR err;
memfault_platform_boot();
while (true)
{
uint32_t os_ticks = (OSCfg_TickRate_Hz * 60);
OSTimeDly(os_ticks, OS_OPT_TIME_DLY, &err);
EFM_ASSERT((RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE));
}
}
static void app_task_create(void)
{
RTOS_ERR err;
OSTaskCreate(&s_app_task_tcb, /* Pointer to the task's TCB. */
"app_task", /* Name to help debugging. */
app_task, /* Pointer to the task's code. */
DEF_NULL, /* Pointer to task's argument. */
APP_TASK_PRIO, /* Task's priority. */
s_app_task_stack, /* Pointer to base of stack. */
(APP_TASK_STACK_SIZE / 10u), /* Stack limit, from base. */
APP_TASK_STACK_SIZE, /* Stack size, in CPU_STK. */
0u, /* Messages in task queue. */
0u, /* Round-Robin time quanta. */
DEF_NULL, /* External TCB data. */
OS_OPT_TASK_STK_CHK, /* Task options. */
&err);
MEMFAULT_ASSERT(RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE);
}
SL_WEAK void app_init(void)
{
sl_status_t sc;
app_log_info("health thermometer initialised\n");
// Init temperature sensor.
sc = sl_sensor_rht_init();
if (sc != SL_STATUS_OK) {
app_log_warning("Relative Humidity and Temperature sensor initialization failed.");
app_log_nl();
}
app_task_create();
}
#ifndef SL_CATALOG_KERNEL_PRESENT
/**************************************************************************//**
* Application Process Action.
*****************************************************************************/
SL_WEAK void app_process_action(void)
{
/////////////////////////////////////////////////////////////////////////////
// Put your additional application code here! //
// This is called infinitely. //
// Do not call blocking functions from here! //
/////////////////////////////////////////////////////////////////////////////
}
#endif
/**************************************************************************//**
* Bluetooth stack event handler.
* This overrides the dummy weak implementation.
*
* @param[in] evt Event coming from the Bluetooth stack.
*****************************************************************************/
void sl_bt_on_event(sl_bt_msg_t *evt)
{
sl_status_t sc;
bd_addr address;
uint8_t address_type;
// Handle stack events
switch (SL_BT_MSG_ID(evt->header)) {
// -------------------------------
// This event indicates the device has started and the radio is ready.
// Do not call any stack command before receiving this boot event!
case sl_bt_evt_system_boot_id:
// Print boot message.
app_log_info("Bluetooth stack booted: v%d.%d.%d-b%d\n",
evt->data.evt_system_boot.major,
evt->data.evt_system_boot.minor,
evt->data.evt_system_boot.patch,
evt->data.evt_system_boot.build);
// Extract unique ID from BT Address.
sc = sl_bt_system_get_identity_address(&address, &address_type);
app_assert_status(sc);
app_log_info("Bluetooth %s address: %02X:%02X:%02X:%02X:%02X:%02X\n",
address_type ? "static random" : "public device",
address.addr[5],
address.addr[4],
address.addr[3],
address.addr[2],
address.addr[1],
address.addr[0]);
// Create an advertising set.
sc = sl_bt_advertiser_create_set(&advertising_set_handle);
app_assert_status(sc);
// Generate data for advertising
sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle,
sl_bt_advertiser_general_discoverable);
app_assert_status(sc);
// Set advertising interval to 100ms.
sc = sl_bt_advertiser_set_timing(
advertising_set_handle, // advertising set handle
160, // min. adv. interval (milliseconds * 1.6)
160, // max. adv. interval (milliseconds * 1.6)
0, // adv. duration
0); // max. num. adv. events
app_assert_status(sc);
// Start advertising and enable connections.
sc = sl_bt_legacy_advertiser_start(advertising_set_handle,
sl_bt_advertiser_connectable_scannable);
app_assert_status(sc);
app_log_info("Started advertising\n");
break;
// -------------------------------
// This event indicates that a new connection was opened.
case sl_bt_evt_connection_opened_id:
app_log_info("Connection opened\n");
#ifdef SL_CATALOG_BLUETOOTH_FEATURE_POWER_CONTROL_PRESENT
// Set remote connection power reporting - needed for Power Control
sc = sl_bt_connection_set_remote_power_reporting(
evt->data.evt_connection_opened.connection,
sl_bt_connection_power_reporting_enable);
app_assert_status(sc);
#endif // SL_CATALOG_BLUETOOTH_FEATURE_POWER_CONTROL_PRESENT
break;
// -------------------------------
// This event indicates that a connection was closed.
case sl_bt_evt_connection_closed_id:
app_log_info("Connection closed\n");
// Generate data for advertising
sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle,
sl_bt_advertiser_general_discoverable);
app_assert_status(sc);
// Restart advertising after client has disconnected.
sc = sl_bt_legacy_advertiser_start(advertising_set_handle,
sl_bt_advertiser_connectable_scannable);
app_assert_status(sc);
app_log_info("Started advertising\n");
break;
///////////////////////////////////////////////////////////////////////////
// Add additional event handlers here as your application requires! //
///////////////////////////////////////////////////////////////////////////
// -------------------------------
// Default event handler.
default:
break;
}
}
/**************************************************************************//**
* Callback function of connection close event.
*
* @param[in] reason Unused parameter required by the health_thermometer component
* @param[in] connection Unused parameter required by the health_thermometer component
*****************************************************************************/
void sl_bt_connection_closed_cb(uint16_t reason, uint8_t connection)
{
(void)reason;
(void)connection;
sl_status_t sc;
// Stop timer.
sc = app_timer_stop(&app_periodic_timer);
app_assert_status(sc);
}
/**************************************************************************//**
* Health Thermometer - Temperature Measurement
* Indication changed callback
*
* Called when indication of temperature measurement is enabled/disabled by
* the client.
*****************************************************************************/
void sl_bt_ht_temperature_measurement_indication_changed_cb(uint8_t connection,
sl_bt_gatt_client_config_flag_t client_config)
{
sl_status_t sc;
app_connection = connection;
// Indication or notification enabled.
if (sl_bt_gatt_disable != client_config) {
// Start timer used for periodic indications.
sc = app_timer_start(&app_periodic_timer,
SL_BT_HT_MEASUREMENT_INTERVAL_SEC * 1000,
app_periodic_timer_cb,
NULL,
true);
app_assert_status(sc);
// Send first indication.
app_periodic_timer_cb(&app_periodic_timer, NULL);
}
// Indications disabled.
else {
// Stop timer used for periodic indications.
(void)app_timer_stop(&app_periodic_timer);
}
}
/**************************************************************************//**
* Simple Button
* Button state changed callback
* @param[in] handle Button event handle
*****************************************************************************/
void sl_button_on_change(const sl_button_t *handle)
{
// Button pressed.
if (sl_button_get_state(handle) == SL_SIMPLE_BUTTON_PRESSED) {
if (&sl_button_btn0 == handle) {
app_btn0_pressed = true;
}
}
// Button released.
else if (sl_button_get_state(handle) == SL_SIMPLE_BUTTON_RELEASED) {
if (&sl_button_btn0 == handle) {
app_btn0_pressed = false;
}
}
}
/**************************************************************************//**
* Timer callback
* Called periodically to time periodic temperature measurements and indications.
*****************************************************************************/
static void app_periodic_timer_cb(app_timer_t *timer, void *data)
{
(void)data;
(void)timer;
sl_status_t sc;
int32_t temperature = 0;
uint32_t humidity = 0;
float tmp_c = 0.0;
// float tmp_f = 0.0;
// Measure temperature; units are % and milli-Celsius.
sc = sl_sensor_rht_get(&humidity, &temperature);
if (SL_STATUS_NOT_INITIALIZED == sc) {
app_log_info("Relative Humidity and Temperature sensor is not initialized.");
app_log_nl();
} else if (sc != SL_STATUS_OK) {
app_log_warning("Invalid RHT reading: %lu %ld\n", humidity, temperature);
}
// button 0 pressed: overwrite temperature with -20C.
if (app_btn0_pressed) {
temperature = -20 * 1000;
}
tmp_c = (float)temperature / 1000;
app_log_info("Temperature: %5.2f C\n", tmp_c);
// Send temperature measurement indication to connected client.
sc = sl_bt_ht_temperature_measurement_indicate(app_connection,
temperature,
false);
// Conversion to Fahrenheit: F = C * 1.8 + 32
// tmp_f = (float)(temperature*18+320000)/10000;
// app_log_info("Temperature: %5.2f F\n", tmp_f);
// Send temperature measurement indication to connected client.
// sc = sl_bt_ht_temperature_measurement_indicate(app_connection,
// (temperature*18+320000)/10,
// true);
if (sc) {
app_log_warning("Failed to send temperature measurement indication\n");
}
}