/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */

/*
 * ----------------------------------------------------------------------------------
 * The code in this file is copied (with some modifications) from:
 *    core_esp8266_i2s.cpp -  I2S library for esp8266
 * ----------------------------------------------------------------------------------
 * Code taken and reworked from Espressif's I2S example
 *
 * Copyright (C) 2015 Hristo Gochkov. All rights reserved.
 *
 * This file is part of the esp8266 core for Arduino environment.
 *
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 * ----------------------------------------------------------------------------------
 */


#ifndef ECXX_PLATFORM_NODEMCU_I2S_OUT_DRIVER_H
#define ECXX_PLATFORM_NODEMCU_I2S_OUT_DRIVER_H


namespace eCxx {

namespace NodeMCU {


//
// I2S callback
//
using I2SCallbackISR = eCxx::VoidCallback_VoidPtr;


//
// I2S (output only) driver
//
extern bool i2sSetBufferLength(uint16_t bufferLength32); // Use zero to select the default length
extern bool i2sSetResetFinishedBuffer(bool enabled, uint32_t value); // The default is 'true' and 0x00000000
extern void i2sSetDividers(uint8_t div1, uint8_t div2);
extern void i2sSetRate(uint32_t rateHz);
extern float i2sGetActualRate();

extern bool i2sBegin(I2SCallbackISR callback, void* callbackArg, bool enableBitClock, bool enableWordSelect);
extern void i2sEnd();

extern void i2sPause();
extern void i2sResume();

extern bool i2sIsFull();
extern bool i2sIsEmpty();
extern uint16_t i2sAvailable();

extern bool i2sWriteSample(uint32_t sample, bool nonBlocking = false);
extern bool i2sWriteSampleLR(int16_t left, int16_t right);

extern uint16_t i2sWriteBufferMono(int16_t* frames, uint16_t frameCount, bool nonBlocking = false);
extern uint16_t i2sWriteBufferStereo(int16_t* frames, uint16_t frameCount, bool nonBlocking = false);


} // namespace NodeMCU

} // namespace eCxx


#endif
