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
27enum LedState {
28    LED_ON = 0,
29    LED_OFF,
30    LED_SPARK,
31};
32
33enum LedState g_ledState = LED_SPARK;
34
35static void *LedTask(const char *arg)
36{
37    (void)arg;
38    switch (g_ledState) {
39        case LED_ON:
40            IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
41            usleep(LED_INTERVAL_TIME_US);
42            break;
43        case LED_OFF:
44            IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
45            usleep(LED_INTERVAL_TIME_US);
46            break;
47        case LED_SPARK:
48            IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
49            usleep(LED_INTERVAL_TIME_US);
50            IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
51            usleep(LED_INTERVAL_TIME_US);
52            break;
53        default:
54            usleep(LED_INTERVAL_TIME_US);
55            break;
56    }
57
58    return NULL;
59}
60
61static void LedExampleEntry(void)
62{
63    osThreadAttr_t attr;
64
65    IoTGpioInit(LED_TEST_GPIO);
66    IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
67
68    attr.name = "LedTask";
69    attr.attr_bits = 0U;
70    attr.cb_mem = NULL;
71    attr.cb_size = 0U;
72    attr.stack_mem = NULL;
73    attr.stack_size = LED_TASK_STACK_SIZE;
74    attr.priority = LED_TASK_PRIO;
75
76    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
77        printf("[LedExample] Failed to create LedTask!\n");
78    }
79}
80
81SYS_RUN(LedExampleEntry);
82