1 /*
2 * Copyright (C) 2022 HiHope Open Source Organization .
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 *
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18
19 #include <unistd.h>
20
21 #include "ohos_init.h"
22 #include "cmsis_os2.h"
23 #include "wifiiot_gpio.h"
24 #include "wifiiot_gpio_ex.h"
25
26 #define LED_INTERVAL_TIME 30
27 #define LED_TASK_STACK_SIZE 512
28 #define LED_TASK_PRIO 25
29 #define NUM 1
30
31 enum LedState {
32 LED_ON = 0,
33 LED_OFF,
34 LED_SPARK,
35 };
36
37 enum LedState g_ledState = LED_SPARK;
38
LedTask(const char * arg)39 static int *LedTask(const char *arg)
40 {
41 (void)arg;
42 while (NUM) {
43 switch (g_ledState) {
44 case LED_ON:
45 GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_VALUE0);
46 osDelay(LED_INTERVAL_TIME);
47 break;
48 case LED_OFF:
49 GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_VALUE1);
50 osDelay(LED_INTERVAL_TIME);
51 break;
52 case LED_SPARK:
53 GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_VALUE0);
54 osDelay(LED_INTERVAL_TIME);
55 GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_VALUE1);
56 osDelay(LED_INTERVAL_TIME);
57 break;
58 default:
59 osDelay(LED_INTERVAL_TIME);
60 break;
61 }
62 }
63
64 return NULL;
65 }
66
OnButtonPressed(char * arg)67 static void OnButtonPressed(char *arg)
68 {
69 (void) arg;
70
71 enum LedState nextState = LED_SPARK;
72 switch (g_ledState) {
73 case LED_ON:
74 nextState = LED_OFF;
75 break;
76 case LED_OFF:
77 nextState = LED_ON;
78 break;
79 case LED_SPARK:
80 nextState = LED_OFF;
81 break;
82 default:
83 break;
84 }
85
86 g_ledState = nextState;
87 }
88
LedExampleEntry(void)89 static void LedExampleEntry(void)
90 {
91 osThreadAttr_t attr;
92
93 GpioInit();
94 IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
95 GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
96
97 IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
98 GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
99 OnButtonPressed, NULL);
100
101 attr.name = "LedTask";
102 attr.attr_bits = 0U;
103 attr.cb_mem = NULL;
104 attr.cb_size = 0U;
105 attr.stack_mem = NULL;
106 attr.stack_size = LED_TASK_STACK_SIZE;
107 attr.priority = LED_TASK_PRIO;
108
109 if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
110 printf("[LedExample] Failed to create LedTask!\n");
111 }
112 }
113
114 SYS_RUN(LedExampleEntry);