use crate::sequence::{self, Write};
use crate::{
Config, DAC_VOLUME_MAX, DEFAULT_DEVICE_ADDRESS, ES8311_CHIP_ID, Error, RESET_DELAY_MS,
coefficients,
};
use embedded_hal_async::delay::DelayNs;
use embedded_hal_async::i2c::I2c;
use es8311_registers::prelude::*;
use es8311_registers::{ChipId1, ChipId2, ChipVersion, DacControl, DacVolume};
#[derive(Debug)]
pub struct Es8311<I2C> {
i2c: I2C,
address: u8,
max_volume: u8,
}
impl<I2C> Es8311<I2C>
where
I2C: I2c,
{
pub fn new(i2c: I2C) -> Self {
Self::with_address(i2c, DEFAULT_DEVICE_ADDRESS)
}
pub fn with_address(i2c: I2C, address: u8) -> Self {
Self {
i2c,
address,
max_volume: DAC_VOLUME_MAX,
}
}
pub fn release(self) -> I2C {
self.i2c
}
pub async fn chip_id(&mut self) -> Result<u16, I2C::Error> {
let high = self.read_register::<ChipId1>().await?.value();
let low = self.read_register::<ChipId2>().await?.value();
Ok(u16::from_be_bytes([high, low]))
}
pub async fn chip_version(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<ChipVersion>().await?.value())
}
pub async fn identify(&mut self) -> Result<bool, I2C::Error> {
Ok(self.chip_id().await? == ES8311_CHIP_ID)
}
pub async fn init<D: DelayNs>(
&mut self,
delay: &mut D,
config: &Config,
) -> Result<(), Error<I2C::Error>> {
let clock = coefficients(config)?;
let id = self.chip_id().await.map_err(Error::I2c)?;
if id != ES8311_CHIP_ID {
return Err(Error::UnknownChip(id));
}
self.write_all(&sequence::RESET).await.map_err(Error::I2c)?;
delay.delay_ms(RESET_DELAY_MS).await;
self.write_all(&sequence::configure(config, clock))
.await
.map_err(Error::I2c)
}
pub async fn dac_power_up(&mut self) -> Result<(), I2C::Error> {
self.write_all(&sequence::DAC_POWER_UP).await
}
pub async fn dac_power_down(&mut self) -> Result<(), I2C::Error> {
self.set_mute(true).await?;
self.write_all(&sequence::DAC_POWER_DOWN).await
}
pub async fn set_mute(&mut self, mute: bool) -> Result<(), I2C::Error> {
self.modify_register(|r: DacControl| r.with_dac_dsmmute(mute).with_dac_demmute(mute))
.await
}
pub fn max_volume(&self) -> u8 {
self.max_volume
}
pub async fn set_max_volume(&mut self, max: u8) -> Result<(), I2C::Error> {
self.max_volume = max;
if self.volume().await? > max {
self.write_register(DacVolume::new().with_value(max))
.await?;
}
Ok(())
}
pub async fn set_volume(&mut self, volume: u8) -> Result<(), I2C::Error> {
self.write_register(DacVolume::new().with_value(volume.min(self.max_volume)))
.await
}
pub async fn volume(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<DacVolume>().await?.value())
}
pub async fn standby(&mut self) -> Result<(), I2C::Error> {
self.set_mute(true).await?;
self.write_all(&sequence::STANDBY).await
}
pub async fn read_register<R>(&mut self) -> Result<R, I2C::Error>
where
R: ReadableRegister,
{
let mut buffer = [0];
self.i2c
.write_read(self.address, &[*R::REGISTER_ADDRESS], &mut buffer)
.await?;
Ok(R::from_bits(buffer[0]))
}
pub async fn write_register<R>(&mut self, register: R) -> Result<(), I2C::Error>
where
R: WritableRegister,
{
self.i2c
.write(self.address, &[*R::REGISTER_ADDRESS, register.to_bits()])
.await
}
pub async fn modify_register<R, F>(&mut self, f: F) -> Result<(), I2C::Error>
where
R: ReadableRegister + WritableRegister,
F: FnOnce(R) -> R,
{
let register = self.read_register::<R>().await?;
self.write_register(f(register)).await
}
async fn write_all(&mut self, writes: &[Write]) -> Result<(), I2C::Error> {
for &(reg, value) in writes {
self.i2c.write(self.address, &[reg.addr(), value]).await?;
}
Ok(())
}
}