You've already forked ESP32-DAPLink
mirror of
https://github.com/m5stack/ESP32-DAPLink.git
synced 2026-05-20 11:37:17 -07:00
57 lines
2.1 KiB
C
57 lines
2.1 KiB
C
#include "buzz.h"
|
|
|
|
#include "driver/ledc.h"
|
|
#include "esp_err.h"
|
|
#include "esp_rom_sys.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/timers.h"
|
|
#include <stdio.h>
|
|
|
|
#define LEDC_TIMER LEDC_TIMER_0
|
|
#define LEDC_MODE LEDC_LOW_SPEED_MODE
|
|
#define LEDC_OUTPUT_IO (5) // Define the output GPIO
|
|
#define LEDC_CHANNEL LEDC_CHANNEL_0
|
|
#define LEDC_DUTY_RES LEDC_TIMER_10_BIT // Set duty resolution to 13 bits
|
|
#define LEDC_DUTY (511) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
|
|
#define LEDC_FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
|
|
|
|
static volatile TimerHandle_t buzz_timer = NULL;
|
|
static volatile bool buzz_on = false;
|
|
|
|
static void buzz_timer_callback(TimerHandle_t timer) {
|
|
ledc_stop(LEDC_MODE, LEDC_CHANNEL, 0);
|
|
buzz_on = false;
|
|
}
|
|
|
|
void buzz_init(void) {
|
|
// Prepare and then apply the LEDC PWM timer configuration
|
|
ledc_timer_config_t ledc_timer = {
|
|
.speed_mode = LEDC_MODE,
|
|
.timer_num = LEDC_TIMER,
|
|
.duty_resolution = LEDC_DUTY_RES,
|
|
.freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
|
|
.clk_cfg = LEDC_AUTO_CLK};
|
|
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
|
|
|
// Prepare and then apply the LEDC PWM channel configuration
|
|
ledc_channel_config_t ledc_channel = {.speed_mode = LEDC_MODE,
|
|
.channel = LEDC_CHANNEL,
|
|
.timer_sel = LEDC_TIMER,
|
|
.intr_type = LEDC_INTR_DISABLE,
|
|
.gpio_num = LEDC_OUTPUT_IO,
|
|
.duty = 0, // Set duty to 0%
|
|
.hpoint = 0};
|
|
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
|
buzz_timer = xTimerCreate("buzz_timer", pdMS_TO_TICKS(50), pdTRUE, (void *)0,
|
|
buzz_timer_callback);
|
|
}
|
|
|
|
void buzz_beep(uint32_t duration_ms) {
|
|
xTimerChangePeriodFromISR(buzz_timer, pdMS_TO_TICKS(duration_ms), 0);
|
|
if (!buzz_on) {
|
|
buzz_on = true;
|
|
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
|
|
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
|
|
xTimerStartFromISR(buzz_timer, 0);
|
|
}
|
|
} |