-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[audio_adc] Add new
audio_adc
component (#8094)
- Loading branch information
Showing
10 changed files
with
200 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from esphome import automation | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_ID, CONF_MIC_GAIN | ||
from esphome.core import coroutine_with_priority | ||
|
||
CODEOWNERS = ["@kbx81"] | ||
IS_PLATFORM_COMPONENT = True | ||
|
||
audio_adc_ns = cg.esphome_ns.namespace("audio_adc") | ||
AudioAdc = audio_adc_ns.class_("AudioAdc") | ||
|
||
SetMicGainAction = audio_adc_ns.class_("SetMicGainAction", automation.Action) | ||
|
||
|
||
SET_MIC_GAIN_ACTION_SCHEMA = cv.maybe_simple_value( | ||
{ | ||
cv.GenerateID(): cv.use_id(AudioAdc), | ||
cv.Required(CONF_MIC_GAIN): cv.templatable(cv.decibel), | ||
}, | ||
key=CONF_MIC_GAIN, | ||
) | ||
|
||
|
||
@automation.register_action( | ||
"audio_adc.set_mic_gain", SetMicGainAction, SET_MIC_GAIN_ACTION_SCHEMA | ||
) | ||
async def audio_adc_set_mic_gain_to_code(config, action_id, template_arg, args): | ||
paren = await cg.get_variable(config[CONF_ID]) | ||
var = cg.new_Pvariable(action_id, template_arg, paren) | ||
|
||
template_ = await cg.templatable(config.get(CONF_MIC_GAIN), args, float) | ||
cg.add(var.set_mic_gain(template_)) | ||
|
||
return var | ||
|
||
|
||
@coroutine_with_priority(100.0) | ||
async def to_code(config): | ||
cg.add_define("USE_AUDIO_ADC") | ||
cg.add_global(audio_adc_ns.using) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "esphome/core/defines.h" | ||
#include "esphome/core/hal.h" | ||
|
||
namespace esphome { | ||
namespace audio_adc { | ||
|
||
class AudioAdc { | ||
public: | ||
virtual bool set_mic_gain(float mic_gain) = 0; | ||
|
||
virtual float mic_gain() = 0; | ||
}; | ||
|
||
} // namespace audio_adc | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include "esphome/core/automation.h" | ||
#include "esphome/core/component.h" | ||
#include "audio_adc.h" | ||
|
||
namespace esphome { | ||
namespace audio_adc { | ||
|
||
template<typename... Ts> class SetMicGainAction : public Action<Ts...> { | ||
public: | ||
explicit SetMicGainAction(AudioAdc *audio_adc) : audio_adc_(audio_adc) {} | ||
|
||
TEMPLATABLE_VALUE(float, mic_gain) | ||
|
||
void play(Ts... x) override { this->audio_adc_->set_mic_gain(this->mic_gain_.value(x...)); } | ||
|
||
protected: | ||
AudioAdc *audio_adc_; | ||
}; | ||
|
||
} // namespace audio_adc | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +0,0 @@ | ||
import esphome.codegen as cg | ||
from esphome.components import i2c | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_BITS_PER_SAMPLE, CONF_ID, CONF_MIC_GAIN, CONF_SAMPLE_RATE | ||
|
||
CODEOWNERS = ["@kahrendt"] | ||
DEPENDENCIES = ["i2c"] | ||
|
||
es7210_ns = cg.esphome_ns.namespace("es7210") | ||
ES7210 = es7210_ns.class_("ES7210", cg.Component, i2c.I2CDevice) | ||
|
||
|
||
es7210_bits_per_sample = es7210_ns.enum("ES7210BitsPerSample") | ||
ES7210_BITS_PER_SAMPLE_ENUM = { | ||
16: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_16, | ||
24: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_24, | ||
32: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_32, | ||
} | ||
|
||
|
||
es7210_mic_gain = es7210_ns.enum("ES7210MicGain") | ||
ES7210_MIC_GAIN_ENUM = { | ||
"0DB": es7210_mic_gain.ES7210_MIC_GAIN_0DB, | ||
"3DB": es7210_mic_gain.ES7210_MIC_GAIN_3DB, | ||
"6DB": es7210_mic_gain.ES7210_MIC_GAIN_6DB, | ||
"9DB": es7210_mic_gain.ES7210_MIC_GAIN_9DB, | ||
"12DB": es7210_mic_gain.ES7210_MIC_GAIN_12DB, | ||
"15DB": es7210_mic_gain.ES7210_MIC_GAIN_15DB, | ||
"18DB": es7210_mic_gain.ES7210_MIC_GAIN_18DB, | ||
"21DB": es7210_mic_gain.ES7210_MIC_GAIN_21DB, | ||
"24DB": es7210_mic_gain.ES7210_MIC_GAIN_24DB, | ||
"27DB": es7210_mic_gain.ES7210_MIC_GAIN_27DB, | ||
"30DB": es7210_mic_gain.ES7210_MIC_GAIN_30DB, | ||
"33DB": es7210_mic_gain.ES7210_MIC_GAIN_33DB, | ||
"34.5DB": es7210_mic_gain.ES7210_MIC_GAIN_34_5DB, | ||
"36DB": es7210_mic_gain.ES7210_MIC_GAIN_36DB, | ||
"37.5DB": es7210_mic_gain.ES7210_MIC_GAIN_37_5DB, | ||
} | ||
|
||
_validate_bits = cv.float_with_unit("bits", "bit") | ||
|
||
CONFIG_SCHEMA = ( | ||
cv.Schema( | ||
{ | ||
cv.GenerateID(): cv.declare_id(ES7210), | ||
cv.Optional(CONF_BITS_PER_SAMPLE, default="16bit"): cv.All( | ||
_validate_bits, cv.enum(ES7210_BITS_PER_SAMPLE_ENUM) | ||
), | ||
cv.Optional(CONF_MIC_GAIN, default="24DB"): cv.enum( | ||
ES7210_MIC_GAIN_ENUM, upper=True | ||
), | ||
cv.Optional(CONF_SAMPLE_RATE, default=16000): cv.int_range(min=1), | ||
} | ||
) | ||
.extend(cv.COMPONENT_SCHEMA) | ||
.extend(i2c.i2c_device_schema(0x40)) | ||
) | ||
|
||
|
||
async def to_code(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
await cg.register_component(var, config) | ||
await i2c.register_i2c_device(var, config) | ||
|
||
cg.add(var.set_bits_per_sample(config[CONF_BITS_PER_SAMPLE])) | ||
cg.add(var.set_mic_gain(config[CONF_MIC_GAIN])) | ||
cg.add(var.set_sample_rate(config[CONF_SAMPLE_RATE])) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import esphome.codegen as cg | ||
from esphome.components import i2c | ||
from esphome.components.audio_adc import AudioAdc | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_BITS_PER_SAMPLE, CONF_ID, CONF_MIC_GAIN, CONF_SAMPLE_RATE | ||
|
||
CODEOWNERS = ["@kahrendt"] | ||
DEPENDENCIES = ["i2c"] | ||
|
||
es7210_ns = cg.esphome_ns.namespace("es7210") | ||
ES7210 = es7210_ns.class_("ES7210", AudioAdc, cg.Component, i2c.I2CDevice) | ||
|
||
|
||
es7210_bits_per_sample = es7210_ns.enum("ES7210BitsPerSample") | ||
ES7210_BITS_PER_SAMPLE_ENUM = { | ||
16: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_16, | ||
24: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_24, | ||
32: es7210_bits_per_sample.ES7210_BITS_PER_SAMPLE_32, | ||
} | ||
|
||
|
||
ES7210_MIC_GAINS = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 34.5, 36, 37.5] | ||
|
||
_validate_bits = cv.float_with_unit("bits", "bit") | ||
|
||
CONFIG_SCHEMA = ( | ||
cv.Schema( | ||
{ | ||
cv.GenerateID(): cv.declare_id(ES7210), | ||
cv.Optional(CONF_BITS_PER_SAMPLE, default="16bit"): cv.All( | ||
_validate_bits, cv.enum(ES7210_BITS_PER_SAMPLE_ENUM) | ||
), | ||
cv.Optional(CONF_MIC_GAIN, default="24db"): cv.All( | ||
cv.decibel, cv.one_of(*ES7210_MIC_GAINS) | ||
), | ||
cv.Optional(CONF_SAMPLE_RATE, default=16000): cv.int_range(min=1), | ||
} | ||
) | ||
.extend(cv.COMPONENT_SCHEMA) | ||
.extend(i2c.i2c_device_schema(0x40)) | ||
) | ||
|
||
|
||
async def to_code(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
await cg.register_component(var, config) | ||
await i2c.register_i2c_device(var, config) | ||
|
||
cg.add(var.set_bits_per_sample(config[CONF_BITS_PER_SAMPLE])) | ||
cg.add(var.set_mic_gain(config[CONF_MIC_GAIN])) | ||
cg.add(var.set_sample_rate(config[CONF_SAMPLE_RATE])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.