"""Audio output handling using I2S and MAX98357A"""
from machine import I2S, Pin

class AudioOutput:
    def __init__(self, bclk_pin, ws_pin, data_pin, sample_rate, bits):
        """Initialize I2S output for MAX98357A"""
        self.audio_out = I2S(
            1,                          # I2S ID (0 or 1)
            sck=Pin(bclk_pin),         # Bit clock
            ws=Pin(ws_pin),            # Word select
            sd=Pin(data_pin),          # Data
            mode=I2S.TX,               # Transmit mode
            bits=bits,                 # Sample size
            format=I2S.MONO,           # Mono format
            rate=sample_rate,          # Sample rate
            ibuf=10000                 # Internal buffer size
        )
    
    def write(self, audio_data):
        """Write audio data to I2S DAC"""
        self.audio_out.write(audio_data)
    
    def deinit(self):
        """Clean up I2S resources"""
        self.audio_out.deinit()