• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <memory.h>
19 #include <hi_time.h>
20 #include <hi_watchdog.h>
21 #include "iot_errno.h"
22 #include "iot_gpio.h"
23 #include "ssd1306_oled.h"
24 #include "app_demo_i2c_oled.h"
25 #include "iot_gpio_ex.h"
26 #include "app_demo_gl5537_1.h"
27 #include "app_demo_multi_sample.h"
28 
29 #define DELAY_TIMES_100 100
30 #define DELAY_TIMES_5   5
31 
32 #define PWM_FRQ_50      50
33 #define PWM_FRQ_10      10
34 #define PWM_FRQ_100     100
35 #define PWM_FRQ_20      20
36 
37 #define TICK_COUNT_MAX  255
38 #define PAIR_2          2
39 
40 GlobalStatusType globalStaType = {0};
41 
42 /* Set key status */
SetKeyStatus(HiColorfulLightMode setSta)43 unsigned char SetKeyStatus(HiColorfulLightMode setSta)
44 {
45     globalStaType.g_currentMode = setSta;
46     return globalStaType.g_currentMode;
47 }
48 /* Set key status */
SetKeyType(HiColorfulLightMode setSta)49 unsigned char SetKeyType(HiColorfulLightMode setSta)
50 {
51     globalStaType.g_currentType = setSta;
52     return globalStaType.g_currentType;
53 }
54 /* GetKeyStatus */
GetKeyStatus(GlobalStatuDef staDef)55 unsigned char GetKeyStatus(GlobalStatuDef staDef)
56 {
57     unsigned char status = 0;
58     switch (staDef) {
59         case MENU_SELECT:
60             status = globalStaType.g_menuSelect;
61             break;
62         case MENU_MODE:
63             status = globalStaType.g_menuMode;
64             break;
65         case CURRENT_MODE:
66             status = globalStaType.g_currentMode;
67             break;
68         case CURRENT_TYPE:
69             status = globalStaType.g_currentType;
70             break;
71         case KEY_DOWN_FLAG:
72             status = globalStaType.g_keyDownFlag;
73             break;
74         case OC_BEEP_STATUS:
75             status = globalStaType.g_ocBeepStatus;
76             break;
77         case SOMEONE_WALKING:
78             status = globalStaType.g_someoneWalkingFlag;
79             break;
80         default:
81             break;
82     }
83     return status;
84 }
85 
86 /* factory test HiSpark board */
HisparkBoardTest(IotGpioValue value)87 void HisparkBoardTest(IotGpioValue value)
88 {
89     IoSetFunc(HI_GPIO_9, 0); // GPIO9
90     IoTGpioSetDir(HI_GPIO_9, IOT_GPIO_DIR_OUT); // GPIO9
91     IoTGpioSetOutputVal(HI_GPIO_9, value); // GPIO9
92 }
93 /* gpio init */
GpioControl(unsigned int gpio,unsigned int id,IotGpioDir dir,IotGpioValue gpioVal,unsigned char val)94 void GpioControl(unsigned int gpio, unsigned int id, IotGpioDir dir, IotGpioValue  gpioVal,
95                  unsigned char val)
96 {
97     IoSetFunc(gpio, val);
98     IoTGpioSetDir(id, dir);
99     IoTGpioSetOutputVal(id, gpioVal);
100 }
101 /* pwm init */
PwmInit(unsigned int id,unsigned char val,unsigned int port)102 void PwmInit(unsigned int id, unsigned char val, unsigned int port)
103 {
104     IoSetFunc(id, val); /* PWM0 OUT */
105     IoTPwmInit(port);
106 }
107 /* gpio key init */
HiSwitchInit(unsigned int id,unsigned char val,unsigned int idx,IotGpioDir dir,IotIoPull pval)108 void HiSwitchInit(unsigned int id, unsigned char val,
109                   unsigned int idx, IotGpioDir dir, IotIoPull pval)
110 {
111     IoSetFunc(id, val);
112     IoTGpioSetDir(idx, dir);
113     IoSetPull(id, pval);
114 }
115 
116 /*
117  * VCC:5V
118  * blue:  gpio12 yellow
119  * green: gpio11 green
120  * red: gpio10 red
121  * switch :
122  * gpio7 switch1
123  * gpio5 switch2
124  */
125 
TestGpioInit(void)126 void TestGpioInit(void)
127 {
128     // switch2,GPIO5,GPIO8,0
129     HiSwitchInit(HI_GPIO_5, 0, HI_GPIO_5, IOT_GPIO_DIR_IN, IOT_IO_PULL_UP);
130     // switch2,GPIO5,GPIO8,0
131     HiSwitchInit(HI_GPIO_8, 0, HI_GPIO_8, IOT_GPIO_DIR_IN, IOT_IO_PULL_UP);
132 
133     PwmInit(HI_GPIO_10, HI_PWM_OUT, HI_PWM1); // GPIO10 PWM port1,5
134     PwmInit(HI_GPIO_11, HI_PWM_OUT, HI_PWM2); // GPIO11 PWM port2,5
135     PwmInit(HI_GPIO_12, HI_PWM_OUT, HI_PWM3); // GPIO12 PWM port3,5
136 }
137 
138 /* 所有pwm的占空比为1,所有灯熄灭 */
AllLightOut(void)139 void AllLightOut(void)
140 {
141     IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
142     IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
143     IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
144 }
145 
146 /*
147  * 三色灯控制模式:每按一下类型S1按键,在绿灯亮、黄灯亮、红灯亮,三个状态之间切换
148  * mode:1.control mode
149  * type:1.red on,2.yellow on,3.green on,RED_ON = 1,YELLOW_ON,GREEN_ON,
150  */
ControlModeSample(void)151 void ControlModeSample(void)
152 {
153     unsigned char currentType = 0;
154     unsigned char currentMode = 0;
155 
156     currentMode = GetKeyStatus(CURRENT_MODE);
157     currentType = GetKeyStatus(CURRENT_TYPE);
158     while (1) {
159         switch (GetKeyStatus(CURRENT_TYPE)) {
160             case RED_ON:
161                 IoTPwmStart(HI_PWM1, PWM_DUTY, PWM_SMALL_DUTY); /* 1 */
162                 IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
163                 IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
164                 break;
165             case YELLOW_ON:
166                 IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
167                 IoTPwmStart(HI_PWM2, PWM_DUTY, PWM_SMALL_DUTY); /* 2 */
168                 IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
169                 break;
170             case GREEN_ON:
171                 IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
172                 IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
173                 IoTPwmStart(HI_PWM3, PWM_DUTY, PWM_SMALL_DUTY); /* 3 */
174                 break;
175             default:
176                 break;
177         }
178 
179         if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
180             AllLightOut();
181             break;
182         }
183         hi_sleep(SLEEP_1_MS);
184     }
185 }
186 
187 /* 中断检测函数,每10ms检测一次 */
DelayAndCheckKeyInterrupt(unsigned int delayTime)188 unsigned char DelayAndCheckKeyInterrupt(unsigned int delayTime)
189 {
190     unsigned int cycleCount = delayTime / DELAY_10_MS;
191     unsigned char currentType = 0;
192     unsigned char currentMode = 0;
193 
194     currentMode = GetKeyStatus(CURRENT_MODE);
195     currentType = GetKeyStatus(CURRENT_TYPE);
196 
197     for (unsigned int i = 0; i < cycleCount; i++) {
198         if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
199             return KEY_DOWN_INTERRUPT;
200         }
201         hi_udelay(DELAY_5_MS); // 10ms
202         hi_sleep(SLEEP_1_MS);
203     }
204     return HI_NULL;
205 }
206 
207 /* 红、黄、绿每1秒轮流亮 */
CycleForOneSecond(void)208 void CycleForOneSecond(void)
209 {
210     while (1) {
211         IoTPwmStart(HI_PWM1, PWM_DUTY, PWM_SMALL_DUTY); /* 1 */
212         if (DelayAndCheckKeyInterrupt(DELAY_1_S) == KEY_DOWN_INTERRUPT) {
213             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
214             break;
215         }
216 
217         IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
218         IoTPwmStart(HI_PWM2, PWM_DUTY, PWM_SMALL_DUTY); /* 2 */
219 
220         if (DelayAndCheckKeyInterrupt(DELAY_1_S) == KEY_DOWN_INTERRUPT) {
221             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
222             break;
223         }
224 
225         IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
226         IoTPwmStart(HI_PWM3, PWM_DUTY, PWM_SMALL_DUTY); /* 3 */
227 
228         if (DelayAndCheckKeyInterrupt(DELAY_1_S) == KEY_DOWN_INTERRUPT) {
229             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
230             break;
231         }
232         IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
233         hi_sleep(SLEEP_1_MS);
234     }
235 }
236 
237 /* 红、黄、绿每0.5秒轮流亮 */
CycleForHalfSecond(void)238 void CycleForHalfSecond(void)
239 {
240     while (1) {
241         IoTPwmStart(HI_PWM1, PWM_DUTY, PWM_SMALL_DUTY); /* 1 */
242         if (DelayAndCheckKeyInterrupt(DELAY_500_MS) == KEY_DOWN_INTERRUPT) {
243             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
244             break;
245         }
246         IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
247         IoTPwmStart(HI_PWM2, PWM_DUTY, PWM_SMALL_DUTY); /* 2 */
248 
249         if (DelayAndCheckKeyInterrupt(DELAY_500_MS) == KEY_DOWN_INTERRUPT) {
250             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
251             break;
252         }
253 
254         IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
255         IoTPwmStart(HI_PWM3, PWM_DUTY, PWM_SMALL_DUTY); /* 3 */
256 
257         if (DelayAndCheckKeyInterrupt(DELAY_500_MS) == KEY_DOWN_INTERRUPT) {
258             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
259             break;
260         }
261         IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
262         hi_sleep(SLEEP_1_MS);
263     }
264 }
265 
266 /* 红、黄、绿每0.25秒轮流亮 */
CycleForQuarterSecond(void)267 void CycleForQuarterSecond(void)
268 {
269     while (1) {
270         IoTPwmStart(HI_PWM1, PWM_DUTY, PWM_SMALL_DUTY); /* 1 */
271 
272         if (DelayAndCheckKeyInterrupt(DELAY_250_MS) == KEY_DOWN_INTERRUPT) {
273             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
274             break;
275         }
276 
277         IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1 */
278         IoTPwmStart(HI_PWM2, PWM_DUTY, PWM_SMALL_DUTY); /* 2 */
279 
280         if (DelayAndCheckKeyInterrupt(DELAY_250_MS) == KEY_DOWN_INTERRUPT) {
281             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
282             break;
283         }
284 
285         IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2 */
286         IoTPwmStart(HI_PWM3, PWM_DUTY, PWM_SMALL_DUTY); /* 3 */
287 
288         if (DelayAndCheckKeyInterrupt(DELAY_250_MS) == KEY_DOWN_INTERRUPT) {
289             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
290             break;
291         }
292         IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3 */
293         hi_sleep(SLEEP_1_MS);
294     }
295 }
296 
297 /*
298  * mode:2.Colorful light
299  * type:1.period by 1s,2.period by 0.5s,3.period by 0.25s
300  * 模式2:炫彩模式,每按一下S2,不同的炫彩类型,
301  * 类型1:红、黄、绿三色灯每隔1秒轮流亮
302  * 类型2:红、黄、绿每0.5秒轮流亮
303  * 类型3:红、黄、绿每0.25秒轮流亮。
304  */
305 
ColorfulLightSample(void)306 void ColorfulLightSample(void)
307 {
308     unsigned char currentType = 0;
309     unsigned char currentMode = 0;
310 
311     currentMode = GetKeyStatus(CURRENT_MODE);
312     currentType = GetKeyStatus(CURRENT_TYPE);
313 
314     while (1) {
315         switch (GetKeyStatus(CURRENT_TYPE)) {
316             case CYCLE_FOR_ONE_SECOND:
317                 CycleForOneSecond();
318                 break;
319             case CYCLE_FOR_HALF_SECOND:
320                 CycleForHalfSecond();
321                 break;
322             case CYCLE_FOR_QUARTER_SECOND:
323                 CycleForQuarterSecond();
324                 break;
325             default:
326                 break;
327         }
328 
329         if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
330             break;
331         }
332         hi_sleep(SLEEP_1_MS);
333     }
334 }
335 
336 /* 红色由暗到最亮 */
RedLightDarkToBright(void)337 void RedLightDarkToBright(void)
338 {
339     unsigned char currentType = 0;
340     unsigned char currentMode = 0;
341 
342     currentMode = GetKeyStatus(CURRENT_MODE);
343     currentType = GetKeyStatus(CURRENT_TYPE);
344     AllLightOut();
345     while (1) {
346         for (unsigned int i = 1; i < DELAY_TIMES_100; i++) { /* 计算延时100次 */
347             IoTPwmStart(HI_PWM1, i, PWM_SMALL_DUTY); /* 1 */
348             if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
349                 return NULL;
350             }
351             hi_sleep((SLEEP_6_S - i + 1) / SLEEP_30_MS); /* 1 */
352         }
353     }
354 }
355 
356 /* 绿灯由暗到最亮 */
GreenLightDarkToBright(void)357 void GreenLightDarkToBright(void)
358 {
359     unsigned char currentType = 0;
360     unsigned char currentMode = 0;
361 
362     currentMode = GetKeyStatus(CURRENT_MODE);
363     currentType = GetKeyStatus(CURRENT_TYPE);
364     AllLightOut();
365     while (1) {
366         for (hi_s32 i = 1; i < DELAY_TIMES_100; i++) { /* 计算延时100次 */
367             IoTPwmStart(HI_PWM2, i, PWM_SMALL_DUTY); /* PWM0 */
368             if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
369                 return NULL;
370             }
371             hi_sleep((SLEEP_6_S - i + 1) / SLEEP_30_MS); /* 1 */
372         }
373     }
374 }
375 
376 /* 蓝灯由暗到最亮 */
BlueLightDarkToBright(void)377 void BlueLightDarkToBright(void)
378 {
379     unsigned char currentType = 0;
380     unsigned char currentMode = 0;
381 
382     currentMode = GetKeyStatus(CURRENT_MODE);
383     currentType = GetKeyStatus(CURRENT_TYPE);
384     AllLightOut();
385     while (1) {
386         for (int i = 1; i < DELAY_TIMES_100; i++) { /* 计算延时100次 */
387             if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
388                 return NULL;
389             }
390             IoTPwmStart(HI_PWM3, i, PWM_SMALL_DUTY); /* PWM3 */
391 
392             hi_sleep((SLEEP_6_S - i + 1) / SLEEP_30_MS); /* 1 */
393         }
394     }
395 }
396 
397 /* 紫色灯由暗到最亮 */
PurpleLightDarkToBright(void)398 void PurpleLightDarkToBright(void)
399 {
400     unsigned char currentType = 0;
401     unsigned char currentMode = 0;
402 
403     currentMode = GetKeyStatus(CURRENT_MODE);
404     currentType = GetKeyStatus(CURRENT_TYPE);
405     AllLightOut();
406     while (1) {
407         for (int i = 0; i < DELAY_TIMES_5; i++) { /* 计算延时5次 */
408             IoTPwmStart(HI_PWM1, PWM_FRQ_50 + (i * PWM_FRQ_10), PWM_SMALL_DUTY); /* 1 */
409             IoTPwmStart(HI_PWM2, i, PWM_SMALL_DUTY); /* 2 */
410             IoTPwmStart(HI_PWM3, PWM_FRQ_50 + (i * PWM_FRQ_10), PWM_SMALL_DUTY); /* 3 */
411             if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
412                 return NULL;
413             }
414             hi_sleep(SLEEP_50_MS);
415         }
416     }
417 }
418 
419 /* 白灯由暗到全亮。 */
AllLightDarkToBright(void)420 void AllLightDarkToBright(void)
421 {
422     unsigned char currentType = 0;
423     unsigned char currentMode = 0;
424 
425     currentMode = GetKeyStatus(CURRENT_MODE);
426     currentType = GetKeyStatus(CURRENT_TYPE);
427     AllLightOut();
428     while (1) {
429         for (int i = 1; i < DELAY_TIMES_100; i++) { /* 计算延时100次 */
430             IoTPwmStart(HI_PWM1, i, PWM_SMALL_DUTY); /* 1 */
431             IoTPwmStart(HI_PWM2, i, PWM_SMALL_DUTY); /* 2 */
432             IoTPwmStart(HI_PWM3, i, PWM_SMALL_DUTY); /* 3 */
433             if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
434                 return NULL;
435             }
436             hi_sleep((SLEEP_6_S - i + 1) / SLEEP_30_MS); /* 1 */
437         }
438     }
439 }
440 
441 /*
442  * mode:3.pwm control
443  * type:1.red,2.yellow,3.green,4.purple,5.all
444  * 模式3:PWM无极调光模式,每按一下S2,不同的调光类型
445  * 类型1:红色由暗到最亮
446  * 类型2:黄灯由暗到最亮
447  * 类型3:绿灯由暗到最亮
448  * 类型4:紫色由暗到最亮
449  * 类型5:由暗到全亮。
450  */
451 
PwmControlSample(void)452 void PwmControlSample(void)
453 {
454     unsigned char currentType = 0;
455     unsigned char currentMode = 0;
456 
457     currentMode = GetKeyStatus(CURRENT_MODE);
458     currentType = GetKeyStatus(CURRENT_TYPE);
459     printf("PwmControlSample\n");
460     AllLightOut();
461     while (1) {
462         switch (GetKeyStatus(CURRENT_TYPE)) {
463             case PWM_CONTROL_RED:
464                 RedLightDarkToBright();
465                 break;
466             case PWM_CONTROL_YELLOW:
467                 RedLightDarkToBright();
468                 break;
469             case PWM_CONTROL_GREEN:
470                 RedLightDarkToBright();
471                 break;
472             case PWM_CONTROL_PURPLE:
473                 RedLightDarkToBright();
474                 break;
475             case PWM_CONTROL_ALL:
476                 AllLightDarkToBright();
477                 break;
478             default:
479                 break;
480         }
481 
482         if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
483             break;
484         }
485         hi_sleep(SLEEP_20_MS);
486     }
487 }
488 
489 /* 用按键控制白灯的亮度 */
BrightnessControlSample(void)490 void BrightnessControlSample(void)
491 {
492     unsigned char currentType = 0;
493     unsigned char currentMode = 0;
494 
495     currentMode = GetKeyStatus(CURRENT_MODE);
496     currentType = GetKeyStatus(CURRENT_TYPE);
497     AllLightOut();
498     while (1) {
499         switch (GetKeyStatus(CURRENT_TYPE)) {
500             case BRIGHTNESS_LOW:
501                 IoTPwmStart(HI_PWM1, PWM_FRQ_20, PWM_SMALL_DUTY); /* 1, 20 */
502                 IoTPwmStart(HI_PWM2, PWM_FRQ_20, PWM_SMALL_DUTY); /* 2, 20 */
503                 IoTPwmStart(HI_PWM3, PWM_FRQ_20, PWM_SMALL_DUTY); /* 3, 20 */
504                 break;
505             case BRIGHTNESS_MIDDLE:
506                 IoTPwmStart(HI_PWM1, PWM_FRQ_50, PWM_SMALL_DUTY); /* 1, 20 */
507                 IoTPwmStart(HI_PWM2, PWM_FRQ_50, PWM_SMALL_DUTY); /* 2, 20 */
508                 IoTPwmStart(HI_PWM3, PWM_FRQ_50, PWM_SMALL_DUTY); /* 3, 20 */
509                 break;
510             case BRIGHTNESS_HIGH:
511                 IoTPwmStart(HI_PWM1, PWM_FRQ_100, PWM_SMALL_DUTY); /* 1, 20 */
512                 IoTPwmStart(HI_PWM2, PWM_FRQ_100, PWM_SMALL_DUTY); /* 2, 20 */
513                 IoTPwmStart(HI_PWM3, PWM_FRQ_100, PWM_SMALL_DUTY); /* 3, 20 */
514                 break;
515             default:
516                 break;
517         }
518 
519         if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
520             break;
521         }
522         hi_sleep(SLEEP_10_MS);
523     }
524 }
525 
526 /*
527  * mode:5.Human detect
528  * 模式5:人体红外检测模式(设置一个好看的灯亮度),有人靠近灯亮,无人在检测区域灯灭
529  */
HumanDetectSample(void)530 void HumanDetectSample(void)
531 {
532     unsigned char currentMode = 0;
533     unsigned char someoneWalking = 0;
534 
535     currentMode = GetKeyStatus(CURRENT_MODE);
536     someoneWalking = GetKeyStatus(SOMEONE_WALKING);
537     printf("HumanDetectSample start\n");
538     IoSetFunc(HI_GPIO_7, 0); // gpio7
539     IoTGpioSetDir(HI_GPIO_7, IOT_GPIO_DIR_IN); // gpio7
540     IotGpioValue gpio_val = IOT_GPIO_VALUE1;
541 
542     while (1) {
543         /* use adc channel_0 */
544         /* use gpio7 */
545         IoTGpioGetInputVal(HI_GPIO_7, &gpio_val); // gpio7
546 
547         if (gpio_val == IOT_GPIO_VALUE1) {
548             someoneWalking = OLED_FLAG_ON;
549         } else {
550             someoneWalking = OLED_FLAG_OFF;
551         }
552         if (someoneWalking == OLED_FLAG_ON) {
553             IoTPwmStart(HI_PWM1, PWM_FRQ_50, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
554             IoTPwmStart(HI_PWM2, PWM_FRQ_50, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
555             IoTPwmStart(HI_PWM3, PWM_FRQ_50, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
556         } else if (someoneWalking == OLED_FLAG_OFF) {
557             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
558             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
559             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
560         }
561         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
562             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
563             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
564             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
565             break;
566         }
567         hi_sleep(SLEEP_1_MS);
568     }
569 }
570 
571 /*
572  * mode:6.Light detect
573  * 模式6:光敏检测模式,无光灯亮,有光灯灭
574  */
LightDetectSample(void)575 void LightDetectSample(void)
576 {
577     unsigned char currentMode = 0;
578     unsigned char withLigh = 0;
579 
580     currentMode = GetKeyStatus(CURRENT_MODE);
581     while (1) {
582         withLigh = GetLightStatus();
583         if (withLigh == OLED_FLAG_ON) {
584             IoTPwmStart(HI_PWM1, PWM_FRQ_50, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
585             IoTPwmStart(HI_PWM2, PWM_FRQ_50, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
586             IoTPwmStart(HI_PWM3, PWM_FRQ_50, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
587         } else if (withLigh == OLED_FLAG_OFF) {
588             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
589             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
590             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
591         }
592         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
593             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
594             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
595             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
596             break;
597         }
598         hi_sleep(SLEEP_1_MS);
599     }
600 }
601 
602 /*
603  * mode:7.Union detect
604  * 模式7:人体红外+光敏联合检测模式。
605  * 无人有光照 => 灯不亮
606  * 无人无光照 => 灯不亮
607  * 有人有光照 => 灯不亮
608  * 有人无光照 => 灯亮
609  * g_withLightFlag = OLED_FLAG_ON:无光照(黑夜),OLED_FLAG_OFF有光照(白天)
610  * g_someoneWalkingFlag = OLED_FLAG_ON(有人),OLED_FLAG_OFF(无人)
611  */
UnionDetectSample(void)612 void UnionDetectSample(void)
613 {
614     unsigned char currentMode = 0;
615     unsigned char withLigh = 0;
616     unsigned char someoneWalking = 0;
617 
618     currentMode = GetKeyStatus(CURRENT_MODE);
619     someoneWalking = GetKeyStatus(SOMEONE_WALKING);
620     IoSetFunc(HI_GPIO_7, 0); // gpio7
621     IoTGpioSetDir(HI_GPIO_7, IOT_GPIO_DIR_IN); // gpio7
622     IotGpioValue gpio_val = IOT_GPIO_VALUE1;
623 
624     while (1) {
625         IoTGpioGetInputVal(HI_GPIO_7, &gpio_val); // gpio7
626         if (gpio_val == IOT_GPIO_VALUE1) {
627             someoneWalking =  OLED_FLAG_ON;
628         } else {
629             someoneWalking =  OLED_FLAG_OFF;
630         }
631         withLigh = GetLightStatus();
632         if (((someoneWalking == OLED_FLAG_ON) && (withLigh == OLED_FLAG_OFF)) ||
633             (someoneWalking == OLED_FLAG_OFF)) {
634             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
635             IoTPwmStart(HI_PWM2, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
636             IoTPwmStart(HI_PWM3, PWM_LOW_DUTY, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
637         } else if ((withLigh == OLED_FLAG_ON) && (someoneWalking == OLED_FLAG_ON)) {
638             IoTPwmStart(HI_PWM1, PWM_FRQ_50, PWM_SMALL_DUTY); /* 1,占空比百分之50 */
639             IoTPwmStart(HI_PWM2, PWM_FRQ_50, PWM_SMALL_DUTY); /* 2,占空比百分之50 */
640             IoTPwmStart(HI_PWM3, PWM_FRQ_50, PWM_SMALL_DUTY); /* 3,占空比百分之50 */
641         }
642         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
643             IoTPwmStart(HI_PWM1, PWM_LOW_DUTY, PWM_SMALL_DUTY);
644             break;
645         }
646         hi_sleep(SLEEP_1_MS);
647     }
648 }
649 
650 /*
651  * mode:8.Return
652  * 模式8:返回主界面
653  */
ReturnMainEnumSample(void)654 void ReturnMainEnumSample(void)
655 {
656     unsigned char currentType = 0;
657     unsigned char currentMode = 0;
658 
659     currentMode = GetKeyStatus(CURRENT_MODE);
660     currentType = GetKeyStatus(CURRENT_TYPE);
661 
662     while (1) {
663         switch (GetKeyStatus(CURRENT_TYPE)) {
664             case KEY_UP:
665                 break;
666             case KEY_DOWN:
667                 return;
668             default:
669                 break;
670         }
671 
672         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
673             break;
674         }
675         task_msleep(SLEEP_1_MS);
676     }
677 }
Gpio9LedLightFunc(void)678 void Gpio9LedLightFunc(void)
679 {
680     globalStaType.g_gpio9Tick++;
681     if (globalStaType.g_gpio9Tick % PAIR_2 == 0) { /* tick 时间 对2 求余, 余数为0则打开LED灯,否则关闭LED灯,即不断闪烁 */
682         printf("led off\r\n");
683         HisparkBoardTest(IOT_GPIO_VALUE1);
684     } else {
685         printf("led on\r\n");
686         HisparkBoardTest(IOT_GPIO_VALUE1);
687     }
688 }
689 
OledShowMenuSelect(void)690 void OledShowMenuSelect(void)
691 {
692     unsigned char currentMode = 0;
693 
694     switch (GetKeyStatus(MENU_SELECT)) {
695         case COLORFUL_LIGHT_MENU:
696             if (GetKeyStatus(CURRENT_MODE) >= MAX_COLORFUL_LIGHT_MODE) {
697                 currentMode = SetKeyStatus(CONTROL_MODE);
698             }
699             break;
700         case TRAFFIC_LIGHT_MENU:
701             if (GetKeyStatus(CURRENT_MODE) >= MAX_TRAFFIC_LIGHT_MODE) {
702                 currentMode = SetKeyStatus(CONTROL_MODE);
703             }
704             break;
705         case ENVIRONMENT_MENU:
706             if (GetKeyStatus(CURRENT_MODE) >= MAX_ENVIRONMENT_MODE) {
707                 currentMode = SetKeyStatus(CONTROL_MODE);
708             }
709             break;
710         case NFC_TEST_MENU:
711             if (GetKeyStatus(CURRENT_MODE) >= MAX_NFC_TAG_MODE) {
712                 currentMode = SetKeyStatus(CONTROL_MODE);
713             }
714             break;
715         default:
716             break;
717     }
718 }
719 
720 /*
721  * 按键7每按下一次,就会产生中断并调用该函数
722  * 用来调整类型
723  */
GpioKey1IsrFuncMode(void)724 void GpioKey1IsrFuncMode(void)
725 {
726     unsigned char currentType = 0;
727     unsigned int currentGpio5Tick = hi_get_tick();
728     unsigned int tickInterval = currentGpio5Tick - globalStaType.g_gpio5Tick;
729     if (tickInterval < KEY_INTERRUPT_PROTECT_TIME) {
730         return HI_NULL;
731     }
732     globalStaType.g_gpio5Tick = currentGpio5Tick;
733     globalStaType.g_keyDownFlag = KEY_GPIO_5;
734     printf("gpio key 1 down\r\n");
735     if (GetKeyStatus(MENU_MODE) == MAIN_FUNCTION_SELECT_MODE) {
736         printf("gpio key 1 menu select\r\n");
737         globalStaType.g_menuSelect++;
738         if (globalStaType.g_menuSelect >= MAX_FUNCTION_DEMO_NUM) {
739             globalStaType.g_menuSelect = COLORFUL_LIGHT_MENU;
740         }
741         return HI_ERR_SUCCESS;
742     }
743 
744     if (GetKeyStatus(MENU_MODE) == SUB_MODE_SELECT_MODE) {
745         printf("gpio key 1 current mode\r\n");
746         globalStaType.g_currentMode++;
747         if (globalStaType.g_currentMode > TRAFFIC_RETURN_MODE) {
748             globalStaType.g_currentMode = 0;
749         }
750         currentType = SetKeyType(CONTROL_MODE);
751         OledShowTrafficLightMenuSelect();
752     }
753 }
754 
OledShowColorfulLightMenuSelect(void)755 void OledShowColorfulLightMenuSelect(void)
756 {
757     unsigned char currentType = 0;
758     switch (GetKeyStatus(CURRENT_MODE)) {
759         case CONTROL_MODE:
760             if (GetKeyStatus(CURRENT_TYPE) > MAX_CONTROL_MODE_TYPE) {
761                 currentType = SetKeyType(RED_ON);
762             }
763             break;
764         case COLORFUL_LIGHT_MODE:
765             if (GetKeyStatus(CURRENT_TYPE) > MAX_COLORFUL_LIGHT_TYPE) {
766                 currentType = SetKeyType(CYCLE_FOR_ONE_SECOND);
767             }
768             break;
769         case PWM_CONTROL_MODE:
770             if (GetKeyStatus(CURRENT_TYPE) > MAX_PWM_CONTROL_TYPE) {
771                 currentType = SetKeyType(PWM_CONTROL_RED);
772             }
773             break;
774         case BRIGHTNESS_MODE:
775             if (GetKeyStatus(CURRENT_TYPE) > MAX_BRIGHTNESS_TYPE) {
776                 currentType = SetKeyType(BRIGHTNESS_LOW);
777             }
778             break;
779         case HUMAN_DETECT_MODE:
780         case LIGHT_DETECT_MODE:
781         case UNION_DETECT_MODE:
782         case RETURN_MODE:
783             if (GetKeyStatus(CURRENT_TYPE) > RETURN_TYPE_MODE) {
784                 currentType = SetKeyType(KEY_UP);
785             }
786             break;
787         default:
788             break;
789     }
790 }
OledShowTrafficLightMenuSelect(void)791 void OledShowTrafficLightMenuSelect(void)
792 {
793     unsigned char currentType = 0;
794     switch (GetKeyStatus(CURRENT_MODE)) {
795         case TRAFFIC_CONTROL_MODE:
796             if (GetKeyStatus(CURRENT_TYPE) >= MAX_TRAFFIC_CONTROL_TYPE) {
797                 currentType = SetKeyType(TRAFFIC_NORMAL_TYPE);
798             }
799             break;
800         case TRAFFIC_AUTO_MODE:
801             if (GetKeyStatus(CURRENT_TYPE) > MAX_TRAFFIC_AUTO_TYPE) {
802                 currentType = SetKeyType(TRAFFIC_NORMAL_TYPE);
803             }
804             break;
805         case TRAFFIC_HUMAN_MODE:
806             if (GetKeyStatus(CURRENT_TYPE) > MAX_TRAFFIC_HUMAN_TYPE) {
807                 currentType = SetKeyType(TRAFFIC_NORMAL_TYPE);
808             }
809             break;
810         case TRAFFIC_RETURN_MODE:
811             if (GetKeyStatus(CURRENT_TYPE) > RETURN_TYPE_MODE) {
812                 currentType = SetKeyType(KEY_UP);
813             }
814             break;
815         default:
816             break;
817     }
818 }
819 /*
820  * 按键7每按下一次,就会产生中断并调用该函数
821  * 用来调整类型
822  */
GpioKey2IsrFuncType(void)823 void GpioKey2IsrFuncType(void)
824 {
825     /* 按键防抖 */
826     unsigned int currentGpio7Tick = hi_get_tick();
827     unsigned int tickInterval = currentGpio7Tick - globalStaType.g_gpio7Tick;
828 
829     if (tickInterval < KEY_INTERRUPT_PROTECT_TIME) {
830         return HI_NULL;
831     }
832     globalStaType.g_gpio7Tick = currentGpio7Tick;
833     globalStaType.g_keyDownFlag = KEY_GPIO_7;
834     printf("gpio key 2 down %d\r\n", globalStaType.g_currentType);
835     globalStaType.g_currentType++;
836     OledShowTrafficLightMenuSelect();
837 }
838 
Gpio8Interrupt(const char * param)839 void Gpio8Interrupt(const char *param)
840 {
841     hi_unref_param(param);
842     unsigned int currentGpio8Tick = hi_get_tick();
843     unsigned int tickInterval = currentGpio8Tick - globalStaType.g_gpio8Tick;
844     printf("gpio8 interrupt \r\n");
845 
846     if (tickInterval < KEY_INTERRUPT_PROTECT_TIME) {
847         printf("gpio8 interrupt return\r\n");
848         return HI_NULL;
849     }
850     globalStaType.g_gpio8CurrentType++;
851     if (globalStaType.g_gpio8CurrentType % PAIR_2 == 0) {  /* tick 时间 对2 求余, 余数为0则打开蜂鸣器响,否则关闭L蜂鸣器 */
852         globalStaType.g_ocBeepStatus = BEEP_OFF;
853     } else {
854         globalStaType.g_ocBeepStatus = BEEP_ON;
855     }
856     if (globalStaType.g_gpio8CurrentType >= TICK_COUNT_MAX) { /* 避免越界,清零 */
857         globalStaType.g_gpio8CurrentType = 0;
858     }
859 }
860 
861 /*
862  * S2(gpio5)是模式按键,S1(gpio7)是类型按键。
863  * 1、三色灯控制模式:每按一下类型S1按键,在绿灯亮、黄灯亮、红灯亮,三个状态之间切换
864  * 2、炫彩模式,每按一下S2,不同的炫彩类型
865  * 3、PWM无极调光模式,每按一下S2,不同的调光类型
866  * 4、人体红外检测模式(设置一个好看的灯亮度)
867  * 5、光敏检测模式
868  * 6、人体红外+光敏联合检测模式。
869  * 7、返回模式
870  * 创建两个按键中断,按键响应后会调用对应的回调函数
871  */
AppMultiSampleDemo(void)872 void AppMultiSampleDemo(void)
873 {
874     unsigned int ret = 0;
875     hi_gpio_init();
876     globalStaType.g_gpio5Tick = hi_get_tick();
877 
878     globalStaType.g_menuSelect = TRAFFIC_LIGHT_MENU;
879     globalStaType.g_menuMode = SUB_MODE_SELECT_MODE;
880 
881     ret = IoTGpioRegisterIsrFunc(HI_GPIO_5, IOT_INT_TYPE_EDGE,
882         IOT_GPIO_EDGE_FALL_LEVEL_LOW, GetGpio5Voltage, NULL); /* 5 */
883     if (ret == IOT_SUCCESS) {
884         printf(" register gpio 5\r\n");
885     }
886     globalStaType.g_gpio8Tick = hi_get_tick();
887     ret = IoTGpioRegisterIsrFunc(HI_GPIO_8, IOT_INT_TYPE_EDGE,
888         IOT_GPIO_EDGE_FALL_LEVEL_LOW, Gpio8Interrupt, NULL); /* 8 */
889     if (ret == IOT_SUCCESS) {
890         printf(" register gpio8\r\n");
891     }
892 }