/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cmsis_os.h"
#include "main.h"


#if USE_RAM_RECORD==1

#include "Data_deal.h"
/* Private includes ----------------------------------------------------------*/
#include "lib_ringbuf.h"
#include "func_ram_record.h"
/* Private typedef -----------------------------------------------------------*/


/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
uint8_t  			ram_record_buf[DATA_UNIT_NUM][DATA_UNIT_SIZE] = {0};
RingBufOBJ 		ram_record_obj 					= {0};
RingBuf 			ram_record_handle 			= {0}; 
/* Private function prototypes -----------------------------------------------*/


/* Private user code ---------------------------------------------------------*/



/**
  * @brief  指定长度len,buf内容写入文件
  * @retval int
  */
uint32_t func_ram_record_write(uint8_t *buf ,uint32_t len)
{
	uint32_t 		data_len = RingBuf_Push(ram_record_handle, (void *)buf, (int) len);
	return data_len;

}


/**
  * @brief  The application entry point.
  * @retval int
  */
uint32_t func_ram_record_read(uint8_t *buf, uint32_t len)
{
	uint32_t 		data_len = RingBuf_Peek(ram_record_handle, (void *)buf, (int) len);
	
	return data_len;
}


uint32_t func_ram_record_delete(uint8_t *buf,uint32_t len)
{
	uint32_t 		read_len = RingBuf_Pop(ram_record_handle, (void *)buf, (int) len);
	return read_len;
}

uint32_t func_ram_obj_num(uint32_t obj_size)
{
	uint32_t 		read_len = RingBuf_obj_num(ram_record_handle, (int) obj_size);
	return read_len;
}

void func_ram_record_init(void)
{	
	ram_record_handle = RingBuf_Init(&ram_record_obj, ram_record_buf, sizeof(ram_record_buf));
}


typedef struct record_test_object
{
	uint32_t 	cnts;
	uint8_t 	test_flag;
	char 			buf[32];
}record_ram_OBJ;
	
record_ram_OBJ record_ram_obj;

void func_ram_record_test(void)
{	
	
	record_ram_obj.cnts++;
	sprintf(record_ram_obj.buf,"XX%08xXX",record_ram_obj.cnts);
	switch(record_ram_obj.test_flag)
	{
		case 0:
		{
			func_ram_record_write((uint8_t *)record_ram_obj.buf ,strlen(record_ram_obj.buf));
		}break;
		case 1:
		{
			func_ram_record_read	((uint8_t *)record_ram_obj.buf ,sizeof(record_ram_obj.buf));
			func_ram_record_delete((uint8_t *)record_ram_obj.buf ,sizeof(record_ram_obj.buf));
			func_ram_record_read	((uint8_t *)record_ram_obj.buf ,sizeof(record_ram_obj.buf));
		}break;
		default:
		{
			func_ram_record_read	((uint8_t *)record_ram_obj.buf ,sizeof(record_ram_obj.buf));
		}break;
		
	}
	
}

void func_ram_record_main(void const *argument)
{
	osDelay(1000);//
	func_ram_record_init();
	while(1)
	{
		func_ram_record_test();
		osDelay(1000);
	}
}

#endif //------------------------USE_RAM_RECORD----------------------------
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
