鸿蒙Hi3861:自家开发版上实现三色灯的变化

鸿蒙时代
发布于 2020-12-26 17:20
浏览
1收藏

这里使用的是hi3861的三色灯板块,实现的是三色灯的变换,可以设置三色灯变换的次数,灯光变换的频率。
代码如下:
#include<stdio.h>
#include<unistd.h>
 
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_pwm.h"
#include "wifiiot_adc.h"
#include "wifiiot_errno.h"
 
#define LED_DELAY_tiME_US 300000
#define LED_BRIGHT WIFI_IOT_GPIO_VALUE1
#define LED_DARK WIFI_IOT_GPIO_VALUE0
 
#define RED_LED_PIN_NAME WIFI_IOT_IO_NAME_GPIO_10
#define RED_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_10_GPIO
 
#define GREEN_LED_PIN_NAME WIFI_IOT_IO_NAME_GPIO_11
#define GREEN_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_11_GPIO
 
#define BLUE_LED_PIN_NAME WIFI_IOT_IO_NAME_GPIO_12
#define BLUE_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_12_GPIO
 
//设置灯光的初始值
#define NUM_BLINKS 2
 
#define PWM_FREQ_DIVITION 64000
 
static void jltfcloudcom(void *arg)
{
    (void)arg;
    IoSetFunc(RED_LED_PIN_NAME,WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT);
    IoSetFunc(GREEN_LED_PIN_NAME,WIFI_IOT_IO_FUNC_GPIO_11_PWM2_OUT);
    IoSetFunc(BLUE_LED_PIN_NAME,WIFI_IOT_IO_FUNC_GPIO_12_PWM3_OUT);
    PwmInit(WIFI_IOT_PWM_PORT_PWM1);
    PwmInit(WIFI_IOT_PWM_PORT_PWM2);
    PwmInit(WIFI_IOT_PWM_PORT_PWM3);
 
    static const WifiIotGpioIdx pins[] = {RED_LED_PIN_NAME,GREEN_LED_PIN_NAME,BLUE_LED_PIN_NAME};
    for(int i = 1 ; i <= NUM_BLINKS ; i++){
         for(int i = 1 ; i <= PWM_FREQ_DIVITION ; i *= 2){
           Pwmstart(WIFI_IOT_PWM_PORT_PWM1,i,PWM_FREQ_DIVITION);
           usleep(250000);
           PwmStop(WIFI_IOT_PWM_PORT_PWM1);
       }
        for(int i = 1 ; i <= PWM_FREQ_DIVITION ; i *= 2 ){
           PwmStart(WIFI_IOT_PWM_PORT_PWM2,i,PWM_FREQ_DIVITION);
            usleep(250000);
            PwmStop(WIFI_IOT_PWM_PORT_PWM2);
        }
        for(int i = 1 ; i <= PWM_FREQ_DIVITION ; i *= 2){
           PwmStart(WIFI_IOT_PWM_PORT_PWM3,i,PWM_FREQ_DIVITION);
           usleep(250000);
           PwmStop(WIFI_IOT_PWM_PORT_PWM3);
       }
        for(unsigned j = 0 ; j < sizeof(pins)/sizeof(pins[0]);j++){
            GpioSetOutputVal(pins[j],LED_BRIGHT);
            usleep(LED_DELAY_TIME_US);
            GpioSetOutputVal(pins[j],LED_DARK);
            usleep(LED_DARK);
        }
    }
   
}
static void jltfcloudcn(void)
{
    osThreadAttr_t attr;
    GpioInit();
    attr.name="jltfcloudcom";
    attr.attr_bits=0U;
    attr.cb_mem=NULL;
    attr.cb_size=0U;
    attr.stack_mem=NULL;
    attr.stack_size=4096;
    attr.priority=osPriorityNormal;
 
    if(osThreadNew(jltfcloudcom,NULL,&attr) == NULL){
        printf("[jltfcloudcn] Failed to create jltfcloudcom!\n");
    }
 
}
APP_FEATURE_INIT(jltfcloudcn);
以上功能实现,参考了网络上部分的公开代码。

分类
收藏 1
回复
举报
回复
    相关推荐