mirror of
https://github.com/espressif/esp-drone.git
synced 2026-07-21 10:15:36 +00:00
feature(buzzer):enable buzzer to play sound
This commit is contained in:
@@ -28,13 +28,14 @@
|
||||
|
||||
#include "buzzer.h"
|
||||
#include "config.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static struct buzzerControl *ctrl;
|
||||
extern void buzzDeckInit();
|
||||
|
||||
void buzzerInit()
|
||||
{
|
||||
#ifdef BUZZER_ON
|
||||
#ifdef CONFIG_BUZZER_ON
|
||||
buzzDeckInit();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -339,7 +339,7 @@ static void soundTimer(xTimerHandle timer)
|
||||
|
||||
void soundInit(void)
|
||||
{
|
||||
#ifdef BUZZER_ON
|
||||
#ifdef CONFIG_BUZZER_ON
|
||||
if (isInit) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -41,11 +41,10 @@
|
||||
#define BUZ_PWM_CH1 0
|
||||
#define BUZ_PWM_CH2 1
|
||||
|
||||
#define PIEZO_GPIO_POS_PIN CONFIG_BUZ1_PIN_POS
|
||||
#define PIEZO_GPIO_NEG_PIN CONFIG_BUZ2_PIN_NEG
|
||||
#define PIEZO_GPIO_POS_PIN CONFIG_BUZ1_PIN_POS // buzzer+ -> GPIO39 ; buzzer- -> GND:
|
||||
#define PIEZO_GPIO_NEG_PIN CONFIG_BUZ2_PIN_NEG // GND
|
||||
|
||||
|
||||
#define PIEZO_PWM_BITS (8)
|
||||
#define PIEZO_PWM_BITS (13)
|
||||
#define PIEZO_PWM_PERIOD ((1<<PIEZO_PWM_BITS) - 1)
|
||||
#define PIEZO_PWM_PRESCALE (0)
|
||||
|
||||
@@ -54,7 +53,7 @@
|
||||
|
||||
static bool isInit = false;
|
||||
|
||||
ledc_channel_config_t buzz_channel[2] = {
|
||||
ledc_channel_config_t buzz_channel[1] = {
|
||||
{
|
||||
.channel = BUZ_PWM_CH1,
|
||||
.duty = 0,
|
||||
@@ -63,14 +62,6 @@ ledc_channel_config_t buzz_channel[2] = {
|
||||
.timer_sel = LEDC_TIMER_1
|
||||
|
||||
},
|
||||
{
|
||||
.channel = BUZ_PWM_CH2,
|
||||
.duty = 0,
|
||||
.gpio_num = PIEZO_GPIO_NEG_PIN,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.timer_sel = LEDC_TIMER_1
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
/* Public functions */
|
||||
@@ -87,7 +78,7 @@ void piezoInit()
|
||||
.freq_hz = 4000, // frequency of PWM signal
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE, // timer mode
|
||||
.timer_num = LEDC_TIMER_1, // timer index
|
||||
// .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
||||
//.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
||||
};
|
||||
|
||||
// Set configuration of timer0 for high speed channels
|
||||
@@ -95,7 +86,7 @@ void piezoInit()
|
||||
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < 2; i++) {
|
||||
for (uint8_t i = 0; i < 1; i++) {
|
||||
ledc_channel_config(&buzz_channel[i]);
|
||||
}
|
||||
|
||||
@@ -109,16 +100,20 @@ bool piezoTest(void)
|
||||
|
||||
void piezoSetRatio(uint8_t ratio)
|
||||
{
|
||||
ledc_set_duty(buzz_channel[0].speed_mode, buzz_channel[0].channel, ratio);
|
||||
uint16_t ratio16 = 0;
|
||||
if (ratio > 0) {
|
||||
ratio16 = ratio << 5;
|
||||
}
|
||||
ledc_set_duty(buzz_channel[0].speed_mode, buzz_channel[0].channel, ratio16);
|
||||
ledc_update_duty(buzz_channel[0].speed_mode, buzz_channel[0].channel);
|
||||
|
||||
ledc_set_duty(buzz_channel[1].speed_mode, buzz_channel[1].channel, PIEZO_PWM_PERIOD - ratio);
|
||||
ledc_update_duty(buzz_channel[1].speed_mode, buzz_channel[1].channel);
|
||||
|
||||
}
|
||||
|
||||
void piezoSetFreq(uint16_t freq)
|
||||
{
|
||||
ledc_set_freq(buzz_channel[0].speed_mode, buzz_channel[0].timer_sel, freq);
|
||||
|
||||
if ( freq <= 0 ) {
|
||||
piezoSetRatio(0);
|
||||
} else {
|
||||
ledc_set_freq(buzz_channel[0].speed_mode, buzz_channel[0].timer_sel, freq);
|
||||
}
|
||||
}
|
||||
|
||||
+30
-22
@@ -76,6 +76,36 @@ menu "ESPDrone Config"
|
||||
|
||||
endmenu
|
||||
|
||||
menu "buzzer"
|
||||
|
||||
config BUZZER_ON
|
||||
bool "enable buzzer to play sound"
|
||||
default y
|
||||
|
||||
config BUZ1_PIN_POS
|
||||
int "BUZ1_PIN_POS GPIO number"
|
||||
range 0 34 if TARGET_ESPLANE_V1
|
||||
default 27 if TARGET_ESPLANE_V1
|
||||
range 0 43 if TARGET_ESPLANE_V2_S2
|
||||
default 39 if TARGET_ESPLANE_V2_S2
|
||||
range 0 43 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
default 39 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
help
|
||||
GPIO number (IOxx) BUZ1_PIN_POS
|
||||
|
||||
config BUZ2_PIN_NEG
|
||||
int "BUZ2_PIN_NEG GPIO number"
|
||||
range 0 34 if TARGET_ESPLANE_V1
|
||||
default 26 if TARGET_ESPLANE_V1
|
||||
range 0 43 if TARGET_ESPLANE_V2_S2
|
||||
default 38 if TARGET_ESPLANE_V2_S2
|
||||
range 0 43 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
default 38 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
help
|
||||
GPIO number (IOxx) BUZ2_PIN_NEG
|
||||
|
||||
endmenu
|
||||
|
||||
menu "sensors config"
|
||||
config I2C0_PIN_SDA
|
||||
int "I2C0_PIN_SDA GPIO number"
|
||||
@@ -165,28 +195,6 @@ menu "ESPDrone Config"
|
||||
help
|
||||
GPIO number (IOxx) SPI_PIN_CS0
|
||||
|
||||
config BUZ1_PIN_POS
|
||||
int "BUZ1_PIN_POS GPIO number"
|
||||
range 0 34 if TARGET_ESPLANE_V1
|
||||
default 27 if TARGET_ESPLANE_V1
|
||||
range 0 43 if TARGET_ESPLANE_V2_S2
|
||||
default 39 if TARGET_ESPLANE_V2_S2
|
||||
range 0 43 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
default 39 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
help
|
||||
GPIO number (IOxx) BUZ1_PIN_POS
|
||||
|
||||
config BUZ2_PIN_NEG
|
||||
int "BUZ2_PIN_NEG GPIO number"
|
||||
range 0 34 if TARGET_ESPLANE_V1
|
||||
default 26 if TARGET_ESPLANE_V1
|
||||
range 0 43 if TARGET_ESPLANE_V2_S2
|
||||
default 38 if TARGET_ESPLANE_V2_S2
|
||||
range 0 43 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
default 38 if TARGET_ESP32_S2_DRONE_V1_2
|
||||
help
|
||||
GPIO number (IOxx) BUZ2_PIN_NEG
|
||||
|
||||
config MPU_PIN_INT
|
||||
int "MPU_PIN_INT GPIO number"
|
||||
range 0 34 if TARGET_ESPLANE_V1
|
||||
|
||||
Reference in New Issue
Block a user