// Teensy I2S Audio Input Implementation
// Compatible with: Teensy 3.x and 4.x
// Supported Microphones: INMP441, ICS43432, SPH0645LM4H-B, ICS43434

#pragma once

// Keep fbuild's library scanner aware of PJRC Audio sources for Teensy.
#include <Audio.h>

#include "fl/audio/audio_input.h"

// ============================================================================
// TEENSY CONFIGURATION
// ============================================================================
//
// IMPORTANT: Teensy uses FIXED hardware pins that cannot be changed.
// Pin assignments vary by board - see wiring diagrams below.
//
// ============================================================================
// WIRING FOR INMP441 MICROPHONE → Teensy
// ============================================================================
//
// --- Teensy 3.x (I2S1 only) ---
// INMP441 Pin  → Teensy Pin
// SCK (BCLK)   → Pin 9  (I2S1 Bit Clock)
// WS (LRCLK)   → Pin 23 (I2S1 Word Select)
// SD (Data)    → Pin 13 (I2S1 Data In)
// L/R Select   → GND (Left channel) or 3.3V (Right channel)
// VDD          → 3.3V
// GND          → GND
// Note: MCLK (Pin 11) is automatically generated by Teensy Audio Library
//
// --- Teensy 4.x (I2S1) ---
// INMP441 Pin  → Teensy Pin
// SCK (BCLK)   → Pin 21 (I2S1 Bit Clock)
// WS (LRCLK)   → Pin 20 (I2S1 Word Select)
// SD (Data)    → Pin 8  (I2S1 Data In)
// L/R Select   → GND (Left channel) or 3.3V (Right channel)
// VDD          → 3.3V
// GND          → GND
// Note: MCLK (Pin 23) is automatically generated by Teensy Audio Library
//
// --- Teensy 4.x (I2S2) ---
// INMP441 Pin  → Teensy Pin
// SCK (BCLK)   → Pin 4  (I2S2 Bit Clock)
// WS (LRCLK)   → Pin 3  (I2S2 Word Select)
// SD (Data)    → Pin 5  (I2S2 Data In)
// L/R Select   → GND (Left channel) or 3.3V (Right channel)
// VDD          → 3.3V
// GND          → GND
// Note: MCLK (Pin 33) is automatically generated by Teensy Audio Library
//
// ============================================================================
// MICROPHONE CONFIGURATION
// ============================================================================
// For the INMP441:
//   - Connect L/R pin to 3.3V to capture Right channel
//   - Connect L/R pin to GND to capture Left channel
//   - Single microphone: Connect L/R to 3.3V (Right is more common)
//
// ============================================================================
// REQUIREMENTS
// ============================================================================
// - Teensy Audio Library (install via Arduino Library Manager)
// - FastLED library
//
// ============================================================================

// Select I2S Port (I2S1 is available on all Teensy 3.x and 4.x)
// I2S2 is only available on Teensy 4.x
#define USE_I2S_PORT fl::audio::TeensyI2S::I2SPort::I2S1

// Select audio channel (Left or Right)
// Match this to your INMP441 L/R pin connection
#define AUDIO_CHANNEL fl::audio::AudioChannel::Right

// Sample rate (Teensy Audio Library uses 44100 Hz)
#define SAMPLE_RATE 44100

// Platform-specific initialization delay (Teensy needs less startup time)
#define PLATFORM_INIT_DELAY_MS 2000

// Platform name for serial output
#define PLATFORM_NAME "Teensy I2S Audio FastLED Example"

// Create platform-specific Config
inline fl::audio::Config createAudioConfig() {
    return fl::audio::Config::CreateTeensyI2S(USE_I2S_PORT, AUDIO_CHANNEL, SAMPLE_RATE);
}

// Print platform-specific setup information
inline void printPlatformInfo() {
#if defined(FL_IS_TEENSY_3X)
    Serial.println("Detected: Teensy 3.x");
    Serial.println("I2S1 pins: BCLK=9, MCLK=11, RX=13, LRCLK=23");
#elif defined(FL_IS_TEENSY_40)
    Serial.println("Detected: Teensy 4.0");
    Serial.println("I2S1 pins: BCLK=21, MCLK=23, RX=8, LRCLK=20");
    Serial.println("I2S2 pins: BCLK=4, MCLK=33, RX=5, LRCLK=3");
#elif defined(FL_IS_TEENSY_41)
    Serial.println("Detected: Teensy 4.1");
    Serial.println("I2S1 pins: BCLK=21, MCLK=23, RX=8, LRCLK=20");
    Serial.println("I2S2 pins: BCLK=4, MCLK=33, RX=5, LRCLK=3");
#else
    Serial.println("Detected: Teensy (model unknown)");
#endif
    Serial.print("Using I2S Port: ");
    Serial.println(USE_I2S_PORT == fl::audio::TeensyI2S::I2SPort::I2S1 ? "I2S1" : "I2S2");
    Serial.print("Channel: ");
    Serial.println(AUDIO_CHANNEL == fl::audio::AudioChannel::Right ? "Right" : "Left");
    Serial.print("Sample Rate: ");
    Serial.println(SAMPLE_RATE);

    Serial.println();
    Serial.println("Troubleshooting:");
    Serial.println("1. Install Teensy Audio Library from Arduino Library Manager");
    Serial.println("2. Verify microphone wiring matches pin assignments above");
    Serial.println("3. Check that microphone has 3.3V power and ground connections");
}
