首页 > 其他分享 >[单片机框架] [app_led] 利用软定时器实现闪烁和呼吸等灯光模式

[单片机框架] [app_led] 利用软定时器实现闪烁和呼吸等灯光模式

时间:2022-10-31 23:32:56浏览次数:56  
标签:led app pmsg 单片机 LED os id


使用例子:

任意地点初始化:app_led_init();

app_led_indicate(灯号,灯光类型,周期时间,重装载值);

注:需要先实现对应PWM函数

文件代码如下
app_led.c

/********************************************************************************
* @file app_led.c
* @author jianqiang.xue
* @Version V1.0.0
* @Date 2021-04-20
* @brief LED灯光效果
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include "os_api.h"
#include "app_led.h"
#include "log.h"
/* Private includes ----------------------------------------------------------*/
#include "business_gpio.h"
#include "business_function.h"
#if BS_APP_LED_DRIVEN_MODE == 0
#include "bsp_pwm.h"
#elif BS_APP_LED_DRIVEN_MODE == 1
#include "bsp_led.h"
#endif
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
void app_led(void const *argument);

/* Private typedef -----------------------------------------------------------*/
#if BS_APP_LED_DRIVEN_MODE == 0
extern void led_pwm_init(void);
extern bool app_set_pwm_parameter(app_led_id_t led_id, uint8_t level_logic, uint16_t period_max);
extern bool app_led_ioctl(app_led_id_t led_id, app_led_type_t type, uint16_t cycle_time, uint16_t period);
#elif BS_APP_LED_DRIVEN_MODE == 1
extern void led_gpio_init(void);
extern bool app_led_ioctl(app_led_id_t led_id, app_led_type_t type, uint16_t cycle_time, uint16_t period);
#elif BS_APP_LED_DRIVEN_MODE == 2
extern void led_ws2812_init(void);
extern bool app_led_ioctl(app_led_id_t led_id, app_led_type_t type, uint16_t cycle_time, uint16_t period);
#endif

// 定义线程,用于接收邮件消息
os_thread_id task_led_id;
os_thread_def(app_led, OS_PRIORITY_NORMAL, 1, 136);

// 定义邮箱,用于发送灯光命令
typedef struct
{
app_led_id_t led_id;
app_led_type_t type;
uint16_t cycle_time;
uint16_t period;
} led_msgqueue_t;

static os_pool_id pool_led_id = NULL;
os_pool_def(pool_led, 5, led_msgqueue_t);

static os_message_qid msg_led_id = NULL;
os_message_qdef(msg_led, 5, led_msgqueue_t);
/************************************[灯光操作]对外接口函数************************************/

bool app_led_indicate(app_led_id_t led_id, app_led_type_t type, uint16_t cycle_time, uint16_t period)
{
if (pool_led_id == NULL || msg_led_id == NULL)
{
return false;
}
led_msgqueue_t *pmsg = NULL;
pmsg = os_pool_calloc(pool_led_id);
if (pmsg)
{
pmsg->led_id = led_id;
pmsg->type = type;
pmsg->cycle_time = cycle_time;
pmsg->period = period;
if (os_message_put(msg_led_id, (uint32_t)pmsg, 0) != OS_OK)
{
os_pool_free(pool_led_id, pmsg);
return false;
}
}
return true;
}

/**
* @brief 线程任务:LED操作
*/
void app_led(void const *argument)
{
os_event evt;
led_msgqueue_t *pmsg = NULL;
while (1)
{
evt = os_message_get(msg_led_id, OS_WAIT_FOREVER);
if (evt.status == OS_EVENT_MESSAGE)
{
pmsg = evt.value.p;
if (pmsg)
{
app_led_ioctl(pmsg->led_id, pmsg->type, pmsg->cycle_time, pmsg->period);
os_pool_free(pool_led_id, pmsg);
}
}
}
}

/**
* @brief 灯光功能初始化
*/
bool app_led_init(void)
{
#if BS_APP_LED_DRIVEN_MODE == 0
// 外设tim初始化
bsp_pwm_init();
// [app] 软定时器创建和参数填充
led_pwm_init();
// 初始化pwm
app_set_pwm_parameter(BS_LIGHT_LAMP_CHANNEL, BS_TIM1_LEVEL_LOGIC, BS_TIM1_PERIOD);
app_set_pwm_parameter(BS_BATTERY_LAMP_CHANNEL, BS_TIM1_LEVEL_LOGIC, BS_TIM1_PERIOD);
#elif BS_APP_LED_DRIVEN_MODE == 1
// gpio初始化
bsp_led_init();
// [app] 软定时器创建和参数填充
led_gpio_init();
#elif BS_APP_LED_DRIVEN_MODE == 2
#endif

pool_led_id = os_pool_create(os_pool(pool_led));
if (!pool_led_id)
return false;
msg_led_id = os_message_create(os_messageq(msg_led), NULL);
if (!msg_led_id)
return false;
task_led_id = os_thread_create(os_thread(app_led), "led");
if (!task_led_id)
return false;
return true;
}

app_led.h

/********************************************************************************
* @file app_led.h
* @author jianqiang.xue
* @Version V1.0.0
* @Date 2021-04-20
* @brief LED灯光效果
********************************************************************************/

#ifndef __APP_LED_H
#define __APP_LED_H

#include <stdint.h>
#include <stdbool.h>

typedef enum
{
LED_TYPE_OFF = 0,
LED_TYPE_LIGHT,
LED_TYPE_BREATH,
LED_TYPE_BREATH_LAMP,
LED_TYPE_TWINKLE,
LED_TYPE_SOS,
LED_TYPE_RISE_SLOWLY,
LED_TYPE_FALL_SLOWLY,
LED_TYPE_MAX,
} app_led_type_t;

typedef enum
{
APP_LED_ID_0 = 0,
APP_LED_ID_1,
APP_LED_ID_2,
APP_LED_ID_3
} app_led_id_t;

bool app_led_init(void);
uint16_t app_led_get_current_period(app_led_id_t led_id);
bool app_led_indicate(app_led_id_t led_id, app_led_type_t type, uint16_t cycle_time, uint16_t period);
#endif


标签:led,app,pmsg,单片机,LED,os,id
From: https://blog.51cto.com/xuejianqiang/5811396

相关文章