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
Previous article:[cx32l003][nrf51822][nrf51422] BLE ANT+ bicycle light
Next article:[MCU Framework][APP_KEY] Using soft timer to implement key scanning
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Definition of priority order of MSP430 microcontroller interrupts
- Discuss power supply and design issues
- Introduction to the working principles of accelerometers and gyroscopes, and AlgoBuilder Tool
- Talking about Flyback Converter
- CST Simulation Tutorial Document Collection
- Phased Array Antenna Simulation Considering the Effect of TR Components
- Received a Children's Day gift from EEW
- STM32MP157A-DK1 review——by freebsder
- Difference between GaN and SiC
- Some experience in porting from μCOS-Ⅱ to the recently popular RT-Thread