Compare commits

...

2 Commits

Author SHA1 Message Date
EN
4c9cae72e0 完善LED模块 2024-07-19 22:48:49 +08:00
EN
775d8ff037 完善蜂鸣器模块 2024-07-19 22:48:14 +08:00
4 changed files with 118 additions and 22 deletions

View File

@ -7,7 +7,13 @@ void BUZZER_Start();
void BUZZER_Stop();
/// @brief 蜂鸣器定时鸣叫
/// @param time 鸣叫时间,单位100毫秒
/// @param time 鸣叫时间,单位毫秒
void BUZZER_StartTimed(uint8_t time);
/// @brief 蜂鸣器鸣叫数次
/// @param loop 鸣叫次数
/// @param time 鸣叫时间,单位毫秒
/// @param interval 间隔时间,单位毫秒
void BUZZER_StartNTimes(uint8_t loop, uint16_t interval, uint16_t time);
#endif

View File

@ -14,4 +14,13 @@ void LED_Stop(uint8_t led);
// duty in [0, 255]
void LED_SetDuty(uint8_t r, uint8_t g, uint8_t b);
/// @brief LED闪烁
/// @param loop 循环次数
/// @param interval 点亮间隔,单位毫秒
/// @param time 点亮时间,单位毫秒
/// @param r 通道R
/// @param g 通道G
/// @param b 通道B
void LED_StartNTimes(uint8_t loop, uint16_t interval, uint16_t time, uint8_t r, uint8_t g, uint8_t b);
#endif

View File

@ -1,26 +1,61 @@
#include "buzzer.h"
#include "timer.h"
#define OFF 0
#define ON 1
#define ONCE 2
static uint8_t state;
static uint8_t loop_times;
static uint16_t loop_interval;
static uint16_t beep_time;
inline void BUZZER_Start()
{
state = ON;
HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET);
}
inline void BUZZER_Stop()
{
HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET);
if (state == 1)
{
state = 0;
if (state == ONCE)
TIMER_DelLoopEvent(EVENT_BUZZER);
}
state = OFF;
}
void BUZZER_StartTimed(uint8_t time)
{
state = 1;
state = ONCE;
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Stop, time * 100);
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Stop, time);
}
void BUZZER_Callback();
void BUZZER_StartNTimes(uint8_t loop, uint16_t interval, uint16_t time)
{
loop_times = loop;
loop_interval = interval;
beep_time = time;
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, time);
}
void BUZZER_Callback()
{
switch (state)
{
case ON:
BUZZER_Stop();
if (--loop_times)
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, loop_interval);
else
TIMER_DelLoopEvent(EVENT_BUZZER);
break;
case OFF:
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, beep_time);
break;
}
}

View File

@ -1,11 +1,21 @@
#include "led.h"
#include "tim.h"
#include "timer.h"
#define LED_TIM (&htim3)
#define LED_R_CHAN TIM_CHANNEL_4
#define LED_G_CHAN TIM_CHANNEL_3
#define LED_B_CHAN TIM_CHANNEL_2
#define OFF 0
#define ON 1
static uint8_t state;
static uint8_t loop_times;
static uint16_t light_time;
static uint16_t loop_interval;
static uint8_t led_r, led_g, led_b;
void LED_Start(uint8_t led)
{
if (led & LED_R)
@ -32,3 +42,39 @@ void LED_SetDuty(uint8_t r, uint8_t g, uint8_t b)
__HAL_TIM_SetCompare(LED_TIM, LED_G_CHAN, g);
__HAL_TIM_SetCompare(LED_TIM, LED_B_CHAN, b);
}
void LED_Callback();
void LED_StartNTimes(uint8_t loop, uint16_t interval, uint16_t time,uint8_t r, uint8_t g, uint8_t b)
{
loop_times = loop;
loop_interval = interval;
light_time = time;
led_r = r;
led_g = g;
led_b = b;
state = ON;
LED_SetDuty(r, g, b);
LED_Start(LED_ALL);
TIMER_AddLoopEvent(EVENT_LED, LED_Callback, time);
}
void LED_Callback()
{
switch (state)
{
case ON:
state = OFF;
LED_SetDuty(0, 0, 0);
if (--loop_times)
TIMER_AddLoopEvent(EVENT_LED, LED_Callback, loop_interval);
else
TIMER_DelLoopEvent(EVENT_LED);
break;
case OFF:
state = ON;
LED_SetDuty(led_r, led_g, led_b);
TIMER_AddLoopEvent(EVENT_LED, LED_Callback, light_time);
break;
}
}