[MCU Framework] [app_led] Using soft timer to implement flashing and breathing lighting modes

Publisher:rnm888Latest update time:2022-09-22 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Example:

Initialize at any location: app_led_init();


app_led_indicate(light number, light type, cycle time, reload value);


Note: You need to implement the corresponding PWM function first


The file code is as follows

app_led.c


/********************************************************************************

* @file    app_led.c

* @author jianqiang.xue

* @Version V1.0.0

* @Date    2021-04-20

* @brief LED lighting effects

********************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include

#include

#include


#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


// Define a thread for receiving email messages

os_thread_id task_led_id;

os_thread_def(app_led, OS_PRIORITY_NORMAL, 1, 136);


// Define mailbox for sending light commands

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);

/****************************************[Lighting operation] External interface function****************************************/


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 Thread task: LED operation

  */

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 Lighting function initialization

  */

bool app_led_init(void)

{

#if BS_APP_LED_DRIVEN_MODE == 0

    // Initialize the peripheral tim

    bsp_pwm_init();

    // [app] Soft timer creation and parameter filling

    led_pwm_init();

    // Initialize 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 initialization

    bsp_led_init();

    // [app] Soft timer creation and parameter filling

    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 lighting effects

********************************************************************************/


#ifndef __APP_LED_H

#define __APP_LED_H


#include

#include


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

Reference address:[MCU Framework] [app_led] Using soft timer to implement flashing and breathing lighting modes

Previous article:[cx32l003][nrf51822][nrf51422] BLE ANT+ bicycle light
Next article:[MCU Framework][APP_KEY] Using soft timer to implement key scanning

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号