• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unistd.h>
19 
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 #include "wifiiot_gpio.h"
23 #include "wifiiot_gpio_ex.h"
24 #include "wifiiot_watchdog.h"
25 #include "wifiiot_pwm.h"
26 
27 #define ATTR.STACK_SIZE 1024
28 #define FOUR 4
29 #define THREE 3
30 #define TWENTY 20
31 #define FORTY 40
32 #define ONE_HUNDRED 100
33 #define TWO_HUNDRED 200
34 #define ONE_THOUSAND 1000
35 
36 static int g_ledStates[3] = {0, 0, 0};
37 static int g_currentBright = 0;
38 static int g_beepState = 0;
39 
TrafficLightTask(const char * arg)40 static void *TrafficLightTask(const char *arg)
41 {
42     (void)arg;
43 
44     printf("TrafficLightTask start!\r\n");
45     WifiIotGpioIdx pins[] = {WIFI_IOT_GPIO_IDX_10, WIFI_IOT_GPIO_IDX_11, WIFI_IOT_GPIO_IDX_12};
46     for (int i = 0; i < FOUR; i++) {
47         for (unsigned int j = 0; j < THREE; j++) {
48             GpioSetOutputVal(pins[j], WIFI_IOT_GPIO_VALUE1);
49             usleep(TWO_HUNDRED*ONE_THOUSAND);
50 
51             GpioSetOutputVal(pins[j], WIFI_IOT_GPIO_VALUE0);
52             usleep(ONE_HUNDRED*ONE_THOUSAND);
53         }
54     }
55 
56     while (1) {
57         for (unsigned int j = 0; j < THREE; j++) {
58             GpioSetOutputVal(pins[j], g_ledStates[j]);
59         }
60         if (g_beepState) {
61             PwmStart(WIFI_IOT_PWM_PORT_PWM0, TWENTY*ONE_THOUSAND, FORTY*ONE_THOUSAND);
62         } else {
63             PwmStop(WIFI_IOT_PWM_PORT_PWM0);
64         }
65     }
66 
67     return NULL;
68 }
69 
OnButtonPressed(char * arg)70 static void OnButtonPressed(char *arg)
71 {
72     (void) arg;
73     for (int i = 0; i < THREE; i++) {
74         if (i == g_currentBright) {
75             g_ledStates[i] = 1;
76         } else {
77             g_ledStates[i] = 0;
78         }
79     }
80     g_currentBright++;
81     if (g_currentBright == THREE) {
82         g_currentBright = 0;
83 }
84 
85     g_beepState = !g_beepState;
86 }
87 
StartTrafficLightTask(void)88 static void StartTrafficLightTask(void)
89 {
90     osThreadAttr_t attr;
91 
92     GpioInit();
93     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
94     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
95 
96     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
97     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
98 
99     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
100     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
101 
102     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
103     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
104     IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
105     GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
106         OnButtonPressed, NULL);
107 
108     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
109     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
110     PwmInit(WIFI_IOT_PWM_PORT_PWM0);
111 
112     WatchDogDisable();
113 
114     attr.name = "TrafficLightTask";
115     attr.attr_bits = 0U;
116     attr.cb_mem = NULL;
117     attr.cb_size = 0U;
118     attr.stack_mem = NULL;
119     attr.stack_size = ATTR.STACK_SIZE;
120     attr.priority = osPriorityNormal;
121 
122     if (osThreadNew((osThreadFunc_t)TrafficLightTask, NULL, &attr) == NULL) {
123         printf("[LedExample] Failed to create TrafficLightTask!\n");
124     }
125 }
126 
127 APP_FEATURE_INIT(StartTrafficLightTask);