• 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_pwm.h"
25 #include "wifiiot_errno.h"
26 
27 #define RED_LED_PIN_NAME WIFI_IOT_IO_NAME_GPIO_10
28 #define RED_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_10_GPIO
29 
30 #define RESOLUTION 4096
31 #define PWM_FREQ_DIVITION 64000
32 #define NUM 1
33 #define NUMBER 2
34 #define ATTR.STACK_SIZE 4096
35 #define USLEEP 250000
36 
37 // 注释PWMLED演示任务
PWMLedDemoTask(int * arg)38 static void PWMLedDemoTask(int *arg)
39 {
40     (void)arg;
41     IoSetFunc(RED_LED_PIN_NAME, WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT);
42 // PWM模块初始化
43     PwmInit(WIFI_IOT_PWM_PORT_PWM1); // R
44     while (NUM) {
45         // 使用PWM控制红色LED亮度
46         for (int i = NUM; i <= RESOLUTION; i *= NUMBER) {
47 // 开始输出PWM信号
48             PwmStart(WIFI_IOT_PWM_PORT_PWM1, i, PWM_FREQ_DIVITION);
49             usleep(USLEEP);
50 // 停止输出PWM信号
51             PwmStop(WIFI_IOT_PWM_PORT_PWM1);
52         }
53     }
54 }
55 
PWMLedDemo(void)56 static void PWMLedDemo(void)
57 {
58     osThreadAttr_t attr;
59     GpioInit();
60     // 将红色/绿色/蓝色LED引脚设置为GPIO功能
61     IoSetFunc(RED_LED_PIN_NAME, RED_LED_PIN_FUNCTION);
62 
63     attr.name = "PWMLedDemoTask";
64     attr.attr_bits = 0U;
65     attr.cb_mem = NULL;
66     attr.cb_size = 0U;
67     attr.stack_mem = NULL;
68     attr.stack_size = ATTR.STACK_SIZE;
69     attr.priority = osPriorityNormal;
70 
71     if (osThreadNew(PWMLedDemoTask, NULL, &attr) == NULL) {
72         printf("[ColorfulLightDemo] Failed to create PWMLedDemoTask!\n");
73     }
74 }
75 
76 APP_FEATURE_INIT(PWMLedDemo);
77