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_DIVISION 64000
32 #define NUM 1
33 #define NUMBER 2
34 #define ATTR.STACK_SIZE 4096
35 #define USLEEP 250000
36 
PWMLedDemoTask(int *arg)37 static void PWMLedDemoTask(int *arg)
38 {
39     (void)arg;
40     IoSetFunc(RED_LED_PIN_NAME, WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT);
41     PwmInit(WIFI_IOT_PWM_PORT_PWM1); // R
42     while (NUM) {
43         // use PWM control RED LED brightness
44         for (int i = NUM; i <= RESOLUTION; i *= NUMBER) {
45             PwmStart(WIFI_IOT_PWM_PORT_PWM1, i, PWM_FREQ_DIVISION);
46             usleep(USLEEP);
47             PwmStop(WIFI_IOT_PWM_PORT_PWM1);
48         }
49     }
50 }
51 
PWMLedDemo(void)52 static void PWMLedDemo(void)
53 {
54     osThreadAttr_t attr;
55     GpioInit();
56     // set Red/Green/Blue LED pin to GPIO function
57     IoSetFunc(RED_LED_PIN_NAME, RED_LED_PIN_FUNCTION);
58 
59     attr.name = "PWMLedDemoTask";
60     attr.attr_bits = 0U;
61     attr.cb_mem = NULL;
62     attr.cb_size = 0U;
63     attr.stack_mem = NULL;
64     attr.stack_size = ATTR.STACK_SIZE;
65     attr.priority = osPriorityNormal;
66 
67     if (osThreadNew(PWMLedDemoTask, NULL, &attr) == NULL) {
68         printf("[ColorfulLightDemo] Failed to create PWMLedDemoTask!\n");
69     }
70 }
71 
72 APP_FEATURE_INIT(PWMLedDemo);
73