-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrv_led.c
359 lines (321 loc) · 6.63 KB
/
Drv_led.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
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
352
353
354
355
356
357
358
359
/******************** (C) COPYRIGHT 2017 ANO Tech ********************************
* 作者 :匿名科创
* 官网 :www.anotc.com
* 淘宝 :anotc.taobao.com
* 技术Q群 :190169595
* 描述 :LED驱动
**********************************************************************************/
#include "Drv_led.h"
#include "Ano_Math.h"
#include "Drv_time.h"
void Drv_LED_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(ANO_RCC_LED,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = ANO_Pin_LED2| ANO_Pin_LED3| ANO_Pin_LED4;
GPIO_Init(ANO_GPIO_LED, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = ANO_Pin_LED1;
GPIO_Init(ANO_GPIO_LED, &GPIO_InitStructure);
GPIO_ResetBits(ANO_GPIO_LED, ANO_Pin_LED1);
GPIO_SetBits(ANO_GPIO_LED, ANO_Pin_LED2);
GPIO_ResetBits(ANO_GPIO_LED, ANO_Pin_LED3);
GPIO_ResetBits(ANO_GPIO_LED, ANO_Pin_LED4);
}
u16 led_accuracy = 20; //该时间应与LED_Duty()调用周期相同
float LED_Brightness[4] = {0,20,0,0}; //TO 20 //XBRG
//LED的1ms驱动,在1ms定时中断里调用。
void LED_1ms_DRV( ) //0~20
{
static u16 led_cnt[4];
u8 i;
for(i=0;i<4;i++)
{
if( led_cnt[i] < LED_Brightness[i] )
{
switch(i)
{
case 0:
LED1_ON;
break;
case 1:
LED2_ON;
break;
case 2:
LED3_ON;
break;
case 3:
LED4_ON;
break;
}
}
else
{
switch(i)
{
case 0:
LED1_OFF;
break;
case 1:
LED2_OFF;
break;
case 2:
LED3_OFF;
break;
case 3:
LED4_OFF;
break;
}
}
if(++led_cnt[i]>=led_accuracy)
{
led_cnt[i] = 0;
}
}
}
static void ledOnOff(u8 led)
{
for(u8 i=0; i<LED_NUM; i++)
{
if(led & (1<<i))
LED_Brightness[i] = 20;
else
LED_Brightness[i] = 0;
}
}
static void ledBreath(u8 dT_ms,u8 led,u16 T)
{
static u8 dir[LED_NUM];
for(u8 i=0; i<LED_NUM; i++)
{
if(led & (1<<i))
{
switch(dir[i])
{
case 0:
LED_Brightness[i] += safe_div(led_accuracy,((float)T/(dT_ms)),0);
if(LED_Brightness[i]>20)
{
dir[i] = 1;
}
break;
case 1:
LED_Brightness[i] -= safe_div(led_accuracy,((float)T/(dT_ms)),0);
if(LED_Brightness[i]<0)
{
dir[i] = 0;
}
break;
default:
dir[i] = 0;
break;
}
}
else
LED_Brightness[i] = 0;
}
}
// 调用周期 LED 亮时间 灭时间
static void ledFlash(u8 dT_ms,u8 led, u16 on_ms,u16 off_ms)
{
static u16 tim_tmp;
if(tim_tmp < on_ms)
ledOnOff(led);
else
ledOnOff(0);
tim_tmp += dT_ms;
if(tim_tmp >= (on_ms + off_ms))
tim_tmp = 0;
}
_led_sta LED_STA;
#include "Ano_FcData.h"
#include "Ano_OPMV_CBTracking_Ctrl.h"
#include "Ano_OPMV_LineTracking_Ctrl.h"
void LED_Task2(u8 dT_ms)
{
static u16 timtmp = 0;
if(LED_STA.errOneTime)
{
ledOnOff(BIT_RLED);
timtmp += dT_ms;
if(timtmp > 3000)
{
timtmp = 0;
LED_STA.errOneTime = 0;
}
}
else if(LED_STA.errMpu>0 || LED_STA.errMag>0 || LED_STA.errBaro>0)
{
u8 flashtims;
static u8 cnttmp = 0;
if(LED_STA.errMpu>0)
flashtims = 2;
else if(LED_STA.errMag>0)
flashtims = 3;
else
flashtims = 4;
if(cnttmp < flashtims)
{
if(timtmp < 60)
ledOnOff(BIT_RLED);
else
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 200)
{
timtmp = 0;
cnttmp++;
}
}
else
{
timtmp += dT_ms;
if(timtmp > 1000)
{
timtmp = 0;
cnttmp = 0;
}
}
}
else if(LED_STA.saving)
{
LED_Brightness[G_led] = 20;
LED_Brightness[R_led] = 0;
LED_Brightness[B_led] = 0;
}
else if(LED_STA.calAcc || LED_STA.calGyr || LED_STA.rst_imu)
{
ledFlash(dT_ms,BIT_WLED,40,40);
}
else if(LED_STA.calMag)
{
if(LED_STA.calMag == 1)
ledBreath(dT_ms,BIT_GLED,300);
else if(LED_STA.calMag == 2)
ledFlash(dT_ms,BIT_PLED,40,40);
else if(LED_STA.calMag == 100)
ledFlash(dT_ms,BIT_RLED,40,40);
else
ledBreath(dT_ms,BIT_BLED,300);
}
else if(LED_STA.noRc)
{
ledBreath(dT_ms,BIT_RLED,500);
}
else if(LED_STA.lowVt)
{
ledFlash(dT_ms,BIT_RLED,60,60);
}
else //无其他提示,正常显示模式档位及外置光流、Gps等状态
{
static u8 statmp = 0;
static u8 modtmp = 0;
if(statmp == 0) //显示飞行模式1、2、3,白色未解锁,绿色解锁,模式几就闪几次
{
if(modtmp <= flag.flight_mode)
{
if(timtmp < 60)
{
if(flag.unlock_sta)
ledOnOff(BIT_GLED);
else
ledOnOff(BIT_WLED);
}
else
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 200)
{
timtmp = 0;
modtmp++;
}
}
else
{
modtmp = 0;
statmp = 1;
}
}
else if(statmp == 1) //显示光流、GPS等状态
{
if(modtmp == 0) //判断GPS是否正常
{
if(switchs.gps_on)
{
if(timtmp < 60)
ledOnOff(BIT_BLED);
else
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 200)
{
timtmp = 0;
modtmp++;
}
}
else
modtmp = 1;
}
else if(modtmp == 1) //判断光流是否正常
{
if(switchs.of_flow_on>0 && switchs.of_tof_on>0)
{
if(timtmp < 60)
ledOnOff(BIT_PLED);
else
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 200)
{
timtmp = 0;
modtmp++;
}
}
else
modtmp = 2;
}
else if(modtmp == 2) //判断飞控外接ANO_OPMV是否正常
{
if(switchs.opmv_on && (ano_opmv_cbt_ctrl.target_loss==0 || ano_opmv_lt_ctrl.target_loss==0) )
{
if(timtmp < 60)
ledOnOff(BIT_YLED);
else
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 200)
{
timtmp = 0;
modtmp++;
}
}
else
modtmp = 3;
}
if(modtmp == 3)
{
statmp = 2;
modtmp = 0;
}
}
else //每一轮循环后,加一个灭灯长延时
{
ledOnOff(0);
timtmp += dT_ms;
if(timtmp > 1000)
{
timtmp = 0;
statmp = 0;
modtmp = 0;
}
}
}
}
/******************* (C) COPYRIGHT 2016 ANO TECH *****END OF FILE************/