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 <unistd.h>
18 
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21 #include "iot_gpio.h"
22 
23 #define LED_INTERVAL_TIME_US 300000
24 #define LED_TASK_STACK_SIZE 512
25 #define LED_TASK_PRIO 25
26 #define LED_TEST_GPIO 9 // for hispark_pegasus
27 
28 static long long g_iState = 0;
29 
30 enum LedState {
31     LED_ON = 0,
32     LED_OFF,
33     LED_SPARK,
34 };
35 
36 enum LedState g_ledState = LED_SPARK;
37 
LedTask(const char *arg)38 static void *LedTask(const char *arg)
39 {
40     (void)arg;
41     while (1) {
42         switch (g_ledState) {
43             case LED_ON:
44                 IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
45                 usleep(LED_INTERVAL_TIME_US);
46                 g_iState++;
47                 break;
48             case LED_OFF:
49                 IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
50                 usleep(LED_INTERVAL_TIME_US);
51                 g_iState++;
52                 break;
53             case LED_SPARK:
54                 IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
55                 usleep(LED_INTERVAL_TIME_US);
56                 IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
57                 usleep(LED_INTERVAL_TIME_US);
58                 g_iState++;
59                 break;
60             default:
61                 usleep(LED_INTERVAL_TIME_US);
62                 break;
63         }
64         if (g_iState == 0xffffffff) {
65             g_iState = 0;
66             break;
67         }
68     }
69     return NULL;
70 }
71 
LedExampleEntry(void)72 static void LedExampleEntry(void)
73 {
74     osThreadAttr_t attr;
75 
76     IoTGpioInit(LED_TEST_GPIO);
77     IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
78 
79     attr.name = "LedTask";
80     attr.attr_bits = 0U;
81     attr.cb_mem = NULL;
82     attr.cb_size = 0U;
83     attr.stack_mem = NULL;
84     attr.stack_size = LED_TASK_STACK_SIZE;
85     attr.priority = LED_TASK_PRIO;
86 
87     if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
88         printf("[LedExample] Failed to create LedTask!\n");
89     }
90 }
91 
92 SYS_RUN(LedExampleEntry);
93