-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblink.c
143 lines (118 loc) · 4.78 KB
/
blink.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
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************//**
* @file
* @brief Blink examples functions
*******************************************************************************
* # License
* <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "em_common.h"
#include "sl_status.h"
#include "sl_simple_led.h"
#include "sl_simple_led_instances.h"
#include "FreeRTOS.h"
#include "task.h"
#include "app_log.h"
#include "gatt_db.h"
#include "app_assert.h"
#include "sl_bluetooth.h"
/*******************************************************************************
******************************* DEFINES ***********************************
******************************************************************************/
#ifndef LED_INSTANCE
#define LED_INSTANCE sl_led_led0
#endif
#ifndef TOOGLE_DELAY_MS
#define TOOGLE_DELAY_MS 500
#endif
#ifndef BLINK_TASK_STACK_SIZE
#define BLINK_TASK_STACK_SIZE configMINIMAL_STACK_SIZE
#endif
#ifndef BLINK_TASK_PRIO
#define BLINK_TASK_PRIO 20
#endif
#ifndef EXAMPLE_USE_STATIC_ALLOCATION
#define EXAMPLE_USE_STATIC_ALLOCATION 0
#endif
/*******************************************************************************
*************************** LOCAL VARIABLES ********************************
******************************************************************************/
/*******************************************************************************
********************* LOCAL FUNCTION PROTOTYPES ***************************
******************************************************************************/
static void blink_task(void *arg);
/*******************************************************************************
************************** GLOBAL FUNCTIONS *******************************
******************************************************************************/
/***************************************************************************//**
* Initialize blink example.
******************************************************************************/
void blink_init(void)
{
TaskHandle_t xHandle = NULL;
#if (EXAMPLE_USE_STATIC_ALLOCATION == 1)
static StaticTask_t xTaskBuffer;
static StackType_t xStack[BLINK_TASK_STACK_SIZE];
app_log_info("start blink task\n\r");
// Create Blink Task without using any dynamic memory allocation
xHandle = xTaskCreateStatic(blink_task,
"blink task",
BLINK_TASK_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY + 1,
xStack,
&xTaskBuffer);
// Since puxStackBuffer and pxTaskBuffer parameters are not NULL,
// it is impossible for xHandle to be null. This check is for
// rigorous example demonstration.
EFM_ASSERT(xHandle != NULL);
#else
BaseType_t xReturned = pdFAIL;
// Create Blink Task using dynamic memory allocation
xReturned = xTaskCreate(blink_task,
"blink task",
BLINK_TASK_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY + 1,
&xHandle);
// Unlike task creation using static allocation, dynamic task creation can very likely
// fail due to lack of memory. Checking the return value is relevant.
EFM_ASSERT(xReturned == pdPASS);
#endif
}
/*******************************************************************************
* Blink task.
******************************************************************************/
static void blink_task(void *arg)
{
(void)&arg;
//Use the provided calculation macro to convert milliseconds to OS ticks
TickType_t xDelay = pdMS_TO_TICKS(TOOGLE_DELAY_MS);
sl_status_t sc;
uint8_t buf[1] = { 0 };
size_t readlen = 0;
while (1) {
//Wait for specified delay
vTaskDelay(xDelay);
// Toggle led
sc = sl_bt_gatt_server_read_attribute_value(gattdb_led_frequency, 0, sizeof(buf), &readlen, buf);
app_assert_status(sc);
if(buf[0]>0)
{
xDelay = pdMS_TO_TICKS(1000/buf[0]);
sl_led_toggle(&LED_INSTANCE);
}
else
{
sl_led_turn_off(&LED_INSTANCE);
}
}
}