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