/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Audio Logger
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-09-12 05:02:04

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Digitally input stereo audio from i2sadc and */
	/* display it on the serial monitor. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* in stereo display */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <tinyI2S.h>	// https://github.com/chrmlinux/tinyI2S
#include <EasyButton.h>	// https://github.com/evert-arias/EasyButton

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D4 = 4;

/***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
tinyI2S tI2S; // Instance of tinyI2S for handling I2S functionality
EasyButton button(button_PushButton_PIN_D4); // Instance of EasyButton for button handling

void setup(void)
{
    // Initialize Serial for debugging purposes
    Serial.begin(115200); // Set baud rate for serial communication

    // Initialize button with pull-up resistor enabled
    pinMode(button_PushButton_PIN_D4, INPUT_PULLUP); // Set the button pin as input with pull-up
    button.begin(); // Call begin to set up the button

    // Initialize tinyI2S with buffer size and sampling rate
    tI2S.begin(RECORD_BUFSIZE_80K, SAMPLING_RATE_16K); 
}

void loop(void)
{
    // Update button state
    button.update(); // Update the button state for debouncing

    // Check if the button is pressed
    if (button.read()) {
        // Perform actions when the button is pressed
        tI2S.record(); // Start recording
        
        // Display audio data on the serial monitor
        int16_t audioData[2]; // Array to hold stereo audio data
        while (tI2S.isRecording()) { // Continue while recording
            tI2S.read(audioData, sizeof(audioData)); // Read stereo audio data
            Serial.print("Left Channel: "); // Print left channel data
            Serial.print(audioData[0]); // Print left channel audio data
            Serial.print(" | Right Channel: "); // Print right channel data
            Serial.println(audioData[1]); // Print right channel audio data
        }
    }
    
    tI2S.update(); // Update tinyI2S for continuous operation
}

/* END CODE */