#include "app_inc.h"

//===========================================================================
//函数名称：task_lwlog
//功能概要：该任务用轻量级日志记录task_lwlog函数开始执行的相关信息，及10次击键输入的字符的相关信息，
//       然后打印出日志。
//参数说明：未使用
//函数返回：无
//===========================================================================

void task_lwlog(uint_32 initial_data)
{
	_mqx_uint i;
	uchar c;
	//创建轻量级日志组件
	_lwlog_create_component();
	//创建日志,lwlog1为创建的日志号，30为条目数，1为写满时写入的新条目覆盖掉最旧的条目的标志位
	_lwlog_create(lwlog1, 30, 1);	
	//记录数据有7位可用，根据具体情况选用
	_lwlog_write(lwlog1, (_mqx_max_type)'l',(_mqx_max_type)'w',
			             (_mqx_max_type)'l',(_mqx_max_type)'o',
			             (_mqx_max_type)'g',(_mqx_max_type)(++k),  0);	
	//LOG_READ_OLDEST_AND_DELETE:读完后将旧条目删除掉
	_lwlog_read(lwlog1, LOG_READ_OLDEST_AND_DELETE,&entry);
	printf("Time: %ld.%03ld%03ld", entry.SECONDS, entry.MILLISECONDS,
														entry.MICROSECONDS);
	printf("        task_");
	printf("%c%c%c%c%c,    I=%d\n", (uchar)entry.DATA[0] & 0xff,
			  (uchar)entry.DATA[1] & 0xff,(uchar)entry.DATA[2] & 0xff,
			  (uchar)entry.DATA[3] & 0xff,(uchar)entry.DATA[4] & 0xff,
						                  (_mqx_uint)entry.DATA[5]);
    printf("\n");
    
    //向日志写入数据
	printf("Enter 10 characters:\n");
	for (i = 1; i <= 10; i++) 
	{
		c = getchar();
		_lwlog_write(lwlog1, (_mqx_max_type)c,
				(_mqx_max_type)(++k), 0, 0, 0, 0, 0);	
	}
	printf("\n");
	//从日志中读数据
	while (_lwlog_read(lwlog1, LOG_READ_OLDEST_AND_DELETE,&entry) == MQX_OK)
	{
		printf("Time: ");
		#if MQX_LWLOG_TIME_STAMP_IN_TICKS
			_psp_print_ticks((PSP_TICK_STRUCT_PTR)&entry.TIMESTAMP);
		#else
			printf("%ld.%03ld%03ld", entry.SECONDS, entry.MILLISECONDS,
					                                entry.MICROSECONDS);
		#endif
			printf("    c=%c, I=%d\n", (uchar)entry.DATA[0] & 0xff,
					(_mqx_uint)entry.DATA[1]);
	}
	printf("\n\n");
	_task_block(); // 执行完毕，进入阻塞态
	}
