#include <csi_config.h>
#include "app_buffer.h"
#include "app_queue.h"
#ifdef CONFIG_XIP
#define ATTRIBUTE_DATA __attribute__((section(".ram.code")))
#else
#define ATTRIBUTE_DATA
#endif
volatile uint32_t  DAcHpOutQueueBuf[DAC_HPOUT_QUEUE_BUF_LENGTH];
volatile QueueStruct DAcHpOutQueue;
volatile QueueStateCtrlStruce  DAcHpOutQueueCtrl;


volatile uint32_t  AdcMicInQueueBuf[ADC_MICIN_QUEUE_BUF_LENGTH];
volatile QueueStruct AdcMicInQueue;
volatile QueueStateCtrlStruce  AdcMicInQueueCtrl;

//==================================================================================================
//=============================================================================== HP OUT (DAC)
// 向队列中写入数据
ATTRIBUTE_DATA bool DAcHpOutQueueWrite(uint32_t *p_buf , int len)
{
	return QueueWriteVal((QueueStruct *)&DAcHpOutQueue,p_buf, len);
}

// 从队列中读出数据
ATTRIBUTE_DATA int DAcHpOutQueueRead(uint32_t *p_buf , int len)
{
	int out_len;
	out_len = QueueReadVal((QueueStruct *)&DAcHpOutQueue, p_buf ,  len);
	return out_len;
}

// 在队列接近满时，调节数据长度
ATTRIBUTE_DATA bool DAcHpOutQueueFullAdjust(void)
{
	return QueueFullAdjust(&DAcHpOutQueueCtrl);
}

// 在队列接近空时，调节数据长度
ATTRIBUTE_DATA bool DAcHpOutQueueEmptyAdjust(void)
{
	return QueueEmptyAdjust(&DAcHpOutQueueCtrl);
}

// 获取队列中数据长度
int DAcHpOutQueueDataLength(void)
{
	return QueueDataLength(&DAcHpOutQueue);
}

// 复位调节记录值
void DAcHpOutQueueResetAdjRecord(void)
{
	QueueResetAdjRecord(&DAcHpOutQueueCtrl);
}

//==================================================================================================
//===============================================================================Mic In (ADC)
// 向队列中写入数据
bool AdcMicInQueueWrite(uint32_t *p_buf , int len)
{
	return QueueWriteVal((QueueStruct *)&AdcMicInQueue,p_buf, len);
}

// 从队列中读出数据
int AdcMicInQueueRead(uint32_t *p_buf , int len)
{
	int out_len;
	out_len = QueueReadVal((QueueStruct *)&AdcMicInQueue, p_buf ,  len);
	return out_len;
}

// 在队列接近满时，调节数据长度
bool AdcMicInQueueFullAdjust(void)
{
	return QueueFullAdjust(&AdcMicInQueueCtrl);
}

// 在队列接近空时，调节数据长度
bool AdcMicInQueueEmptyAdjust(void)
{
	return QueueEmptyAdjust(&AdcMicInQueueCtrl);
}

// 获取队列中数据长度
int AdcMicInQueueDataLength(void)
{
	return QueueDataLength(&AdcMicInQueue);
}

// 获取队列中剩余空间
int AdcMicQueueResidualSpace(void)
{
	return QueueResidualSpace(&AdcMicInQueue);
}

// 清空队列中所有数据
bool AdcMicQueueReset()
{
	return QueueReset(&AdcMicInQueue);
}

// 初始化所有队列
void app_buffer_power_on_init(void)
{
	//------------------------------------------------------------------------
	// 1. WN8032从i2S获取麦克风数据，写入队列
	// 2. 主程序从队列中取出数据，进行算法处理
	QueueCreate((QueueStruct *)&AdcMicInQueue,(uint32_t *)&AdcMicInQueueBuf[0],ADC_MICIN_QUEUE_BUF_LENGTH);
	AdcMicInQueueCtrl.pQueue = (QueueStruct *)&AdcMicInQueue;
	AdcMicInQueueCtrl.NeedFillNum = 0;
	AdcMicInQueueCtrl.NeedLostNum = 0;
	AdcMicInQueueCtrl.MinIdleNum = 0;
	AdcMicInQueueCtrl.Tick = 0;
	AdcMicInQueueCtrl.SetTick = ADC_MICIN_QUEUE_ADJUST_TICK;
	AdcMicInQueueCtrl.SetMinIdleLength = ADC_MICIN_QUEUE_MIN_IDLE_LENGTH;
	AdcMicInQueueCtrl.SetLowThreshold = ADC_MICIN_QUEUE_LOW_THRESHOLD;
	AdcMicInQueueCtrl.SetHighThreshold = ADC_MICIN_QUEUE_HIGH_THRESHOLD;
	AdcMicInQueueCtrl.SetAdjustLength = ADC_MICIN_QUEUE_ADJUST_LENGTH;	
	AdcMicInQueueCtrl.AdjAddTotalNum = 0x00;	// 总共添加的采样点数
	AdcMicInQueueCtrl.AdjSubTotalNum = 0x00;	// 总共减少的采样点数
	AdcMicInQueueCtrl.BackAdjAddTotalNum = 0x00;	// 备份
	AdcMicInQueueCtrl.BackAdjSubTotalNum = 0x00;	// 备份
	//------------------------------------------------------------------------
	// 1. WN8032 DMA从i2S获取麦克风数据，进行处理后写入队列
	// 2. 主程序定时从队列中取出数据，写入IIS TX DMA队列
	QueueCreate((QueueStruct *)&DAcHpOutQueue,(uint32_t *)&DAcHpOutQueueBuf[0],DAC_HPOUT_QUEUE_BUF_LENGTH);
	DAcHpOutQueueCtrl.pQueue = (QueueStruct *)&DAcHpOutQueue;
	DAcHpOutQueueCtrl.NeedFillNum = 0;
	DAcHpOutQueueCtrl.NeedLostNum = 0;
	DAcHpOutQueueCtrl.MinIdleNum = 0;
	DAcHpOutQueueCtrl.Tick = 0;
	DAcHpOutQueueCtrl.SetTick = DAC_HPOUT_QUEUE_ADJUST_TICK;
	DAcHpOutQueueCtrl.SetMinIdleLength = DAC_HPOUT_QUEUE_MIN_IDLE_LENGTH;
	DAcHpOutQueueCtrl.SetLowThreshold = DAC_HPOUT_QUEUE_LOW_THRESHOLD;
	DAcHpOutQueueCtrl.SetHighThreshold = DAC_HPOUT_QUEUE_HIGH_THRESHOLD;
	DAcHpOutQueueCtrl.SetAdjustLength = DAC_HPOUT_QUEUE_ADJUST_LENGTH;
	DAcHpOutQueueCtrl.AdjAddTotalNum = 0x00;	// 总共添加的采样点数
	DAcHpOutQueueCtrl.AdjSubTotalNum = 0x00;	// 总共减少的采样点数
	DAcHpOutQueueCtrl.BackAdjAddTotalNum = 0x00;	// 备份
	DAcHpOutQueueCtrl.BackAdjSubTotalNum = 0x00;	// 备份
	//------------------------------------------------------------------------
}