1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18 #include "blt_led.h"
19 #include "drivers.h"
20 #include "tl_common.h"
21
22 device_led_t device_led;
23
24 /**
25 * @brief This function is used to control device led on or off
26 * @param[in] on - the status of led
27 * @return none
28 */
device_led_on_off(u8 on)29 void device_led_on_off(u8 on)
30 {
31 gpio_write(device_led.gpio_led, on ^ device_led.polar);
32 gpio_set_output_en(device_led.gpio_led, on);
33 device_led.isOn = on;
34 }
35
36 /**
37 * @brief This function is used to initialize device led setting
38 * @param[in] gpio - the GPIO corresponding to device led
39 * @param[in] polarity - 1 for high led on, 0 for low led on
40 * @return none
41 */
device_led_init(u32 gpio,u8 polarity)42 void device_led_init(u32 gpio, u8 polarity)
43 { // polarity: 1 for high led on, 0 for low led on
44 #if (BLT_APP_LED_ENABLE)
45 device_led.gpio_led = gpio;
46 device_led.polar = !polarity;
47 gpio_write(gpio, !polarity);
48 #endif
49 }
50
51 /**
52 * @brief This function is used to create new led task
53 * @param[in] led_cfg - Configure the parameters for led event
54 * @return 0 - new led event priority not higher than the not ongoing one
55 * 1 - new led event created successfully
56 */
device_led_setup(led_cfg_t led_cfg)57 int device_led_setup(led_cfg_t led_cfg)
58 {
59 #if (BLT_APP_LED_ENABLE)
60 if (device_led.repeatCount && device_led.priority >= led_cfg.priority) {
61 return 0; // new led event priority not higher than the not ongoing one
62 } else {
63 device_led.onTime_ms = led_cfg.onTime_ms;
64 device_led.offTime_ms = led_cfg.offTime_ms;
65 device_led.repeatCount = led_cfg.repeatCount;
66 device_led.priority = led_cfg.priority;
67
68 if (led_cfg.repeatCount == 0xff) { // for long on/long off
69 device_led.repeatCount = 0;
70 } else { // process one of on/off Time is zero situation
71 if (!device_led.onTime_ms) { // onTime is zero
72 device_led.offTime_ms *= device_led.repeatCount;
73 device_led.repeatCount = 1;
74 } else if (!device_led.offTime_ms) {
75 device_led.onTime_ms *= device_led.repeatCount;
76 device_led.repeatCount = 1;
77 }
78 }
79
80 device_led.startTick = clock_time();
81 device_led_on_off(device_led.onTime_ms ? 1 : 0);
82
83 return 1;
84 }
85 #else
86 return 0;
87 #endif
88 }
89
90 /**
91 * @brief This function is used to manage led tasks
92 * @param[in] none
93 * @return none
94 */
led_proc(void)95 void led_proc(void)
96 {
97 #if (BLT_APP_LED_ENABLE)
98 if (device_led.isOn) {
99 if (clock_time_exceed(device_led.startTick, device_led.onTime_ms * 1000)) {
100 device_led_on_off(0);
101 if (device_led.offTime_ms) { // offTime not zero
102 device_led.startTick += device_led.onTime_ms * SYSTEM_TIMER_TICK_1MS;
103 } else {
104 device_led.repeatCount = 0;
105 }
106 }
107 } else {
108 if (clock_time_exceed(device_led.startTick, device_led.offTime_ms * 1000)) {
109 if (--device_led.repeatCount) {
110 device_led_on_off(1);
111 device_led.startTick += device_led.offTime_ms * SYSTEM_TIMER_TICK_1MS;
112 }
113 }
114 }
115 #endif
116 }
117