mirror of
https://github.com/furyhawk/ESP32-X-Desktop.git
synced 2026-07-21 17:05:35 +00:00
- Introduced my_codec.h and my_codec.c for a new codec interface with control, data, and volume functionalities. - Implemented test_app_main.c to set up Unity test framework for memory leak checks. - Created test_board.h and test_board.c to define board-specific configurations and I2C/I2S initialization functions. - Developed test_my_codec.c to validate the functionality of the new codec interface, including volume control, mute settings, and data read/write operations. - Ensured compatibility with ESP-IDF versioning for I2C and I2S drivers.
62 lines
2.0 KiB
C
62 lines
2.0 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
#include <math.h>
|
|
#include "esp_codec_dev_vol.h"
|
|
|
|
int esp_codec_dev_vol_calc_reg(const esp_codec_dev_vol_range_t *vol_range, float db)
|
|
{
|
|
if (vol_range->max_vol.db_value == vol_range->min_vol.db_value) {
|
|
return vol_range->max_vol.vol;
|
|
}
|
|
if (db >= vol_range->max_vol.db_value) {
|
|
return vol_range->max_vol.vol;
|
|
}
|
|
if (db <= vol_range->min_vol.db_value) {
|
|
return vol_range->min_vol.vol;
|
|
}
|
|
float ratio =
|
|
(vol_range->max_vol.vol - vol_range->min_vol.vol) / (vol_range->max_vol.db_value - vol_range->min_vol.db_value);
|
|
return (int) ((db - vol_range->min_vol.db_value) * ratio + vol_range->min_vol.vol);
|
|
}
|
|
|
|
float esp_codec_dev_vol_calc_db(const esp_codec_dev_vol_range_t *vol_range, int vol)
|
|
{
|
|
if (vol_range->max_vol.vol == vol_range->min_vol.vol) {
|
|
return vol_range->max_vol.db_value;
|
|
}
|
|
if (vol_range->max_vol.vol > vol_range->min_vol.vol) {
|
|
if (vol >= vol_range->max_vol.vol) {
|
|
return vol_range->max_vol.db_value;
|
|
}
|
|
if (vol <= vol_range->min_vol.vol) {
|
|
return vol_range->min_vol.db_value;
|
|
}
|
|
} else {
|
|
if (vol <= vol_range->max_vol.vol) {
|
|
return vol_range->max_vol.db_value;
|
|
}
|
|
if (vol >= vol_range->min_vol.vol) {
|
|
return vol_range->min_vol.db_value;
|
|
}
|
|
}
|
|
float ratio =
|
|
(vol_range->max_vol.db_value - vol_range->min_vol.db_value) / (vol_range->max_vol.vol - vol_range->min_vol.vol);
|
|
return ((vol - vol_range->min_vol.vol) * ratio + vol_range->min_vol.db_value);
|
|
}
|
|
|
|
float esp_codec_dev_col_calc_hw_gain(esp_codec_dev_hw_gain_t *hw_gain)
|
|
{
|
|
float pa_voltage = hw_gain->pa_voltage;
|
|
float dac_voltage = hw_gain->codec_dac_voltage;
|
|
if (pa_voltage == 0.0) {
|
|
pa_voltage = 5.0;
|
|
}
|
|
if (dac_voltage == 0.0) {
|
|
dac_voltage = 3.3;
|
|
}
|
|
return 20 * log10(dac_voltage / pa_voltage) + hw_gain->pa_gain;
|
|
}
|