Add retry mechanism for volume write (#3121)

This commit is contained in:
qwer123
2026-04-10 14:33:48 +08:00
committed by GitHub
parent ef2ae9874b
commit fc18992442
6 changed files with 34 additions and 26 deletions
+9 -2
View File
@@ -225,8 +225,15 @@ volume_range_t volume_range() {
return audio_codec->headphone_gain_range();
}
void set_volume(const volume_t volume) {
audio_codec->set_headphone_volume(volume);
bool set_volume(const volume_t volume) {
// add retry method
for (int i = 0; i < 100; ++i) {
if (audio_codec->set_headphone_volume(volume)) {
return true;
}
chThdSleepMilliseconds(2);
}
return false;
}
} /* namespace headphone */
+2 -2
View File
@@ -49,7 +49,7 @@ class Codec {
virtual void headphone_enable() = 0;
virtual void headphone_disable() = 0;
virtual volume_range_t headphone_gain_range() const = 0;
virtual void set_headphone_volume(const volume_t volume) = 0;
virtual bool set_headphone_volume(const volume_t volume) = 0;
virtual void microphone_enable(int8_t alc_mode, bool mic_to_HP_enabled) = 0; // added user-GUI AK4951 ,selected ALC mode.
virtual void microphone_disable() = 0;
@@ -93,7 +93,7 @@ namespace headphone {
volume_range_t volume_range();
void set_volume(const volume_t volume);
bool set_volume(const volume_t volume);
} /* namespace headphone */
+8 -8
View File
@@ -130,15 +130,15 @@ bool AK4951::reset() {
return true;
}
void AK4951::set_digtal_volume_control(const reg_t value) {
bool AK4951::set_digtal_volume_control(const reg_t value) {
map.r.l_ch_digital_volume_control.DV = value;
update(Register::LchDigitalVolumeControl);
return update(Register::LchDigitalVolumeControl);
}
void AK4951::set_headphone_volume(const volume_t volume) {
bool AK4951::set_headphone_volume(const volume_t volume) {
const auto normalized = headphone_gain_range().normalize(volume);
auto n = normalized.centibel() / 5;
set_digtal_volume_control(0xcb - n);
return set_digtal_volume_control(0xcb - n);
}
void AK4951::headphone_mute() {
@@ -602,13 +602,13 @@ reg_t AK4951::read(const address_t reg_address) {
return rx[0];
}
void AK4951::update(const Register reg) {
write(toUType(reg), map.w[toUType(reg)]);
bool AK4951::update(const Register reg) {
return write(toUType(reg), map.w[toUType(reg)]);
}
void AK4951::write(const address_t reg_address, const reg_t value) {
bool AK4951::write(const address_t reg_address, const reg_t value) {
const std::array<uint8_t, 2> tx{reg_address, value};
bus.transmit(bus_address, tx.data(), tx.size());
return bus.transmit(bus_address, tx.data(), tx.size());
}
} /* namespace ak4951 */
+4 -4
View File
@@ -841,7 +841,7 @@ class AK4951 : public audio::Codec {
return true;
}
void set_headphone_volume(const volume_t volume) override;
bool set_headphone_volume(const volume_t volume) override;
void headphone_mute();
void microphone_enable(int8_t alc_mode, bool mic_to_HP_enabled); // added user GUI parameter , to set up AK4951 ALC mode, and mic_to_HP_enabled to control "Hear to Mic"
@@ -879,15 +879,15 @@ class AK4951 : public audio::Codec {
void configure_digital_interface_i2s();
void configure_digital_interface_external_slave();
void configure_digital_interface_external_master();
void set_digtal_volume_control(const reg_t value);
bool set_digtal_volume_control(const reg_t value);
void set_dac_power(const bool enable);
void set_headphone_power(const bool enable);
void set_speaker_power(const bool enable);
void select_line_out(const LineOutSelect value);
reg_t read(const address_t reg_address);
void update(const Register reg);
void write(const address_t reg_address, const reg_t value);
bool update(const Register reg);
bool write(const address_t reg_address, const reg_t value);
};
} /* namespace ak4951 */
+4 -4
View File
@@ -145,14 +145,14 @@ void WM8731::write(const RightLineIn value) {
write(Register::RightLineIn);
}
void WM8731::write(const LeftHeadphoneOut value) {
bool WM8731::write(const LeftHeadphoneOut value) {
map.r.left_headphone_out = value;
write(Register::LeftHeadphoneOut);
return write(Register::LeftHeadphoneOut);
}
void WM8731::write(const RightHeadphoneOut value) {
bool WM8731::write(const RightHeadphoneOut value) {
map.r.right_headphone_out = value;
write(Register::RightHeadphoneOut);
return write(Register::RightHeadphoneOut);
}
void WM8731::write(const AnalogAudioPathControl value) {
+7 -6
View File
@@ -306,21 +306,22 @@ class WM8731 : public audio::Codec {
});
}
void set_wm_headphone_volume(const volume_t volume) {
bool set_wm_headphone_volume(const volume_t volume) {
const auto normalized = headphone_gain_range().normalize(volume);
auto n = normalized.centibel() / 10;
write(LeftHeadphoneOut{
bool tag = write(LeftHeadphoneOut{
.lhpvol = static_cast<reg_t>(n),
.lzcen = 0,
.lrhpboth = 1,
.reserved0 = 0,
});
return tag;
}
void set_headphone_volume(const volume_t volume) override {
bool set_headphone_volume(const volume_t volume) override {
headphone_volume = volume;
set_wm_headphone_volume(volume);
return set_wm_headphone_volume(volume);
}
volume_range_t headphone_gain_range() const override {
@@ -422,8 +423,8 @@ class WM8731 : public audio::Codec {
void write(const LeftLineIn value);
void write(const RightLineIn value);
void write(const LeftHeadphoneOut value);
void write(const RightHeadphoneOut value);
bool write(const LeftHeadphoneOut value);
bool write(const RightHeadphoneOut value);
void write(const AnalogAudioPathControl value);
void write(const DigitalAudioPathControl value);
void write(const PowerDownControl value);