ESPHome  2024.12.2
es8311.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace es8311 {
9 
21 };
22 
23 enum ES8311Resolution : uint8_t {
29 };
30 
32  uint32_t mclk; // mclk frequency
33  uint32_t rate; // sample rate
34  uint8_t pre_div; // the pre divider with range from 1 to 8
35  uint8_t pre_mult; // the pre multiplier with x1, x2, x4 and x8 selection
36  uint8_t adc_div; // adcclk divider
37  uint8_t dac_div; // dacclk divider
38  uint8_t fs_mode; // single speed (0) or double speed (1)
39  uint8_t lrck_h; // adc lrck divider and dac lrck divider
40  uint8_t lrck_l; //
41  uint8_t bclk_div; // sclk divider
42  uint8_t adc_osr; // adc osr
43  uint8_t dac_osr; // dac osr
44 };
45 
46 class ES8311 : public audio_dac::AudioDac, public Component, public i2c::I2CDevice {
47  public:
49  // Component overrides //
51 
52  void setup() override;
53  float get_setup_priority() const override { return setup_priority::DATA; }
54  void dump_config() override;
55 
57  // AudioDac overrides //
59 
63  bool set_volume(float volume) override;
64 
67  float volume() override;
68 
71  bool set_mute_off() override { return this->set_mute_state_(false); }
72 
75  bool set_mute_on() override { return this->set_mute_state_(true); }
76 
77  bool is_muted() override { return this->is_muted_; }
78 
80  // ES8311 configuration setters //
82 
83  void set_use_mclk(bool use_mclk) { this->use_mclk_ = use_mclk; }
85  this->resolution_in_ = resolution;
86  this->resolution_out_ = resolution;
87  }
88  void set_sample_frequency(uint32_t sample_frequency) { this->sample_frequency_ = sample_frequency; }
89  void set_use_mic(bool use_mic) { this->use_mic_ = use_mic; }
90  void set_mic_gain(ES8311MicGain mic_gain) { this->mic_gain_ = mic_gain; }
91 
92  protected:
96  static uint8_t calculate_resolution_value(ES8311Resolution resolution);
97 
102  static const ES8311Coefficient *get_coefficient(uint32_t mclk, uint32_t rate);
103 
106  bool configure_clock_();
107 
110  bool configure_format_();
111 
114  bool configure_mic_();
115 
119  bool set_mute_state_(bool mute_state);
120 
121  bool use_mic_;
123 
124  bool use_mclk_; // true = use dedicated MCLK pin, false = use SCLK
125  bool sclk_inverted_{false}; // SCLK is inverted
126  bool mclk_inverted_{false}; // MCLK is inverted (ignored if use_mclk_ == false)
127  uint32_t mclk_multiple_{256}; // MCLK frequency is sample rate * mclk_multiple_ (ignored if use_mclk_ == false)
128 
129  uint32_t sample_frequency_; // in Hz
132 };
133 
134 } // namespace es8311
135 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
ES8311Resolution resolution_in_
Definition: es8311.h:130
void set_bits_per_sample(ES8311Resolution resolution)
Definition: es8311.h:84
bool is_muted() override
Definition: es8311.h:77
void set_use_mic(bool use_mic)
Definition: es8311.h:89
uint32_t sample_frequency_
Definition: es8311.h:129
ES8311MicGain mic_gain_
Definition: es8311.h:122
void set_sample_frequency(uint32_t sample_frequency)
Definition: es8311.h:88
ES8311Resolution resolution_out_
Definition: es8311.h:131
void set_use_mclk(bool use_mclk)
Definition: es8311.h:83
bool set_mute_on() override
Enables mute for audio out.
Definition: es8311.h:75
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool set_mute_off() override
Disables mute for audio out.
Definition: es8311.h:71
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
float get_setup_priority() const override
Definition: es8311.h:53
void set_mic_gain(ES8311MicGain mic_gain)
Definition: es8311.h:90