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 TWENTY 20
29 #define FORTY 40
30 #define ONE_THOUSAND 1000
31
32 static int g_beepState = 0;
33
34 // PWM
PWMBeerTask(const char * arg)35 static void *PWMBeerTask(const char *arg)
36 {
37 (void)arg;
38
39 printf("PWMBeerTask start!\r\n");
40
41 // 主循环
42 while (1) {
43 if (g_beepState) {
44 PwmStart(WIFI_IOT_PWM_PORT_PWM0, TWENTY*ONE_THOUSAND, FORTY*ONE_THOUSAND);
45 } else {
46 PwmStop(WIFI_IOT_PWM_PORT_PWM0);
47 }
48 }
49
50 return NULL;
51 }
52
53 // 按钮按压事件处理
OnButtonPressed(char * arg)54 static void OnButtonPressed(char *arg)
55 {
56 (void) arg;
57 g_beepState = !g_beepState;
58 }
59
StartPWMBeerTask(void)60 static void StartPWMBeerTask(void)
61 {
62 osThreadAttr_t attr;
63
64 GpioInit();
65
66 IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
67 GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
68 IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
69 GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
70 OnButtonPressed, NULL);
71
72 IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
73 GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
74 PwmInit(WIFI_IOT_PWM_PORT_PWM0);
75
76 WatchDogDisable();
77
78 attr.name = "PWMBeerTask";
79 attr.attr_bits = 0U;
80 attr.cb_mem = NULL;
81 attr.cb_size = 0U;
82 attr.stack_mem = NULL;
83 attr.stack_size = ATTR.STACK_SIZE;
84 attr.priority = osPriorityNormal;
85
86 if (osThreadNew((osThreadFunc_t)PWMBeerTask, NULL, &attr) == NULL) {
87 printf("[StartPWMBeerTask] Failed to create PWMBeerTask!\n");
88 }
89 }
90
91 APP_FEATURE_INIT(StartPWMBeerTask);