#pragma once
/*
 * es8388.h — Minimal ES8388 codec driver for AI-Thinker ESP32-A1S (IDF 5.x).
 *
 * Controls the ES8388 via I2C (addr 0x10) for playback (DAC) and capture (ADC).
 * I2S is configured separately via the standard IDF driver/i2s_std.h API.
 *
 * Thread safety: all public functions are non-reentrant; call from a single task.
 */

#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Initialise the I2C master bus and configure the ES8388 registers for
 * stereo playback + mic capture at 16 kHz.  Must be called once before any
 * other es8388_* function and after driver_i2c is available.
 *
 * Returns ESP_OK on success.  On I2C error the register that failed is logged
 * and ESP_FAIL is returned — the codec is in an undefined state.
 */
esp_err_t es8388_init(void);

/* Set output volume.  vol: 0 (mute) … 100 (full).  Mapped to ES8388
 * OUT1 (headphone) + OUT2 (speaker) attenuation registers. */
esp_err_t es8388_set_volume(uint8_t vol);

/* Set the DIGITAL DAC volume (DACCONTROL4/5), applied before the analog output
 * stages. atten: 0 = 0 dB (full), each step -0.5 dB, 0xC0 = mute. Lowering it
 * gives analog headroom while keeping es8388_set_volume() high. */
esp_err_t es8388_set_dac_volume(uint8_t atten);

/* Mute / unmute DAC output. */
esp_err_t es8388_mute(bool mute);

/* Read a single ES8388 register for diagnostic purposes. */
esp_err_t es8388_read_reg(uint8_t reg, uint8_t *value);

/* Write a single ES8388 register for diagnostic purposes (bench input/gain
 * sweeps via /debug/es8388?reg=&val= — find which input the SLIC feeds). */
esp_err_t es8388_write_reg(uint8_t reg, uint8_t value);

#ifdef __cplusplus
}
#endif
