1 /*
2 Copyright (C) 2024 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 limitations under the License.
14 */
15
16 #include <stdio.h>
17
18 #include <unistd.h>
19
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 #include "iot_gpio.h"
23 #include "iot_gpio_ex.h"
24
25 #define LED_TASK_GPIO 10
26 #define IOT_GPIO_KEY 13
27 #define LED_INTERVAL_TIME 30
28 #define LED_TASK_STACK_SIZE 0x1000
29 #define LED_TASK_PRIO 25
30
31 enum LedState{
32 LED_ON = 0,
33 LED_OFF,
34 LED_SPARK,
35 LED_EXIT,
36 };
37
38 enum LedState g_ledState = LED_SPARK;
39
LedTask(const char * arg)40 static void *LedTask(const char *arg)
41 {
42 (void) arg;
43 uint32_t running = 1;
44 while (running) {
45 switch (g_ledState) {
46 case LED_ON:
47 IoTGpioSetOutputVal(LED_TASK_GPIO, 0);
48 osDelay(LED_INTERVAL_TIME);
49 break;
50 case LED_OFF:
51 IoTGpioSetOutputVal(LED_TASK_GPIO, 1);
52 osDelay(LED_INTERVAL_TIME);
53 break;
54 case LED_SPARK:
55 IoTGpioSetOutputVal(LED_TASK_GPIO, 0);
56 osDelay(LED_INTERVAL_TIME);
57 IoTGpioSetOutputVal(LED_TASK_GPIO, 1);
58 osDelay(LED_INTERVAL_TIME);
59 break;
60 case LED_EXIT:
61 running = 0;
62 break;
63 default:
64 osDelay(LED_INTERVAL_TIME);
65 return ;
66 }
67 }
68
69 return NULL;
70 }
71
OnButtonPressed(char * arg)72 static void OnButtonPressed(char *arg)
73 {
74 (void) arg;
75
76 enum LedState nextState = LED_SPARK;
77 switch (g_ledState) {
78 case LED_ON:
79 nextState = LED_SPARK;
80 break;
81 case LED_OFF:
82 nextState = LED_ON;
83 break;
84 case LED_SPARK:
85 nextState = LED_OFF;
86 break;
87 default:
88 break;
89 }
90
91 g_ledState = nextState;
92 }
93
LedExampleEntry(void)94 static void LedExampleEntry(void)
95 {
96 osThreadAttr_t attr;
97
98 IoTGpioInit(LED_TASK_GPIO);
99 IoSetFunc(LED_TASK_GPIO, 0);
100 IoTGpioSetDir(LED_TASK_GPIO, IOT_GPIO_DIR_OUT);
101
102
103 IoTGpioInit(IOT_GPIO_KEY);
104 IoSetFunc(IOT_GPIO_KEY, 0);
105 IoTGpioSetDir(IOT_GPIO_KEY, IOT_GPIO_DIR_IN);
106 IoSetPull(IOT_GPIO_KEY, IOT_IO_PULL_UP);
107
108 IoTGpioRegisterIsrFunc(IOT_GPIO_KEY, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW,
109 OnButtonPressed, NULL);
110
111 attr.name = "LedTask";
112 attr.attr_bits = 0U;
113 attr.cb_mem = NULL;
114 attr.cb_size = 0U;
115 attr.stack_mem = NULL;
116 attr.stack_size = LED_TASK_STACK_SIZE;
117 attr.priority = LED_TASK_PRIO;
118
119 if (osThreadNew((osThreadFunc_t) LedTask, NULL, &attr) == NULL) {
120 printf("[LedExample] Falied to create LedTask!\n");
121 }
122 }
123
124 SYS_RUN(LedExampleEntry);