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