消息响应:
消息有多种:
IR/KEY/系统及异常 消息
处理消息的最佳机制:队列
消息处理得好,直接关系到系统的耦合性及健壮性。
下面给出一个消息机制的框架:
数据结构:
typedef struct
{
BYTE encode_command_out;
BYTE encode_command_cnt;
UINT16 encode_command_FIFO[ENCODE_CMD_QUEUE_LEN];
}QUNE_DATA;
QUNE_DATA *pVMCDENC=NULL;
1)获得外界或系统触发,获得消息
//将消息发出
case ir_msg:
case key_msg:
case sys_msg:
vmcd_encode_post_command(VMCD_ENCODE_CMD_NEW_DISC| vmcd_ui_disc_index);
tRESULT vmcd_encode_post_command(UINT16 cmd)
{
/*
#define GET_VMCD_ENCODE_CMD_FUNC(x) (x & VMCD_ENCODE_CMD_MASK_FUNC)
#define GET_VMCD_ENCODE_CMD_PARA(x) (x & VMCD_ENCODE_CMD_MASK_PARA)
*/
UINT16 cmd_func = GET_VMCD_ENCODE_CMD_FUNC(cmd);
UINT16 cmd_para = GET_VMCD_ENCODE_CMD_PARA(cmd);
switch(cmd_func)
{
case VMCD_ENCODE_CMD_NEW_DISC:
if((cmd_para>0)&&(cmd_para<=max_vmcd_disc))
{
if(SUCCESS == push_vmcd_current_encode_cmd(cmd))
res = PENDING;
}
break;
......
}
2)将消息加入队列
tRESULT push_vmcd_current_encode_cmd(UINT16 cmd)
{
if(pVMCDENC->encode_command_cnt<ENCODE_CMD_QUEUE_LEN)
{
BYTE index_in=pVMCDENC->encode_command_out + pVMCDENC->encode_command_cnt; //队列循环的,关键判定条件
if(index_in>=ENCODE_CMD_QUEUE_LEN)
index_in -= ENCODE_CMD_QUEUE_LEN;
pVMCDENC->encode_command_FIFO[index_in]=cmd; //加入队列
pVMCDENC->encode_command_cnt ++;
return SUCCESS;
}
return FAILURE;
}
2)消息出队
UINT16 get_vmcd_current_encode_cmd(void)
{
UINT16 cmd=0;
if(pVMCDENC->encode_command_cnt>0)
{
cmd = pVMCDENC->encode_command_FIFO[pVMCDENC->encode_command_out];
}
return cmd;
}
void pop_vmcd_current_encode_cmd(void)
{
if(pVMCDENC->encode_command_cnt>0)
{
pVMCDENC->encode_command_cnt --;
pVMCDENC->encode_command_out ++;
if(pVMCDENC->encode_command_out>=ENCODE_CMD_QUEUE_LEN)
pVMCDENC->encode_command_out = 0;
}
}
3)队列命令相应
tRESULT vmcd_encode_parse_command(void)
{
UINT16 cmd = get_vmcd_current_encode_cmd();
UINT16 cmd_para = GET_VMCD_ENCODE_CMD_PARA(cmd);
UINT16 cmd_func = GET_VMCD_ENCODE_CMD_FUNC(cmd);
//根据命令执行相应的动作
switch(cmd_func)
{
case VMCD_ENCODE_CMD_NEW_DISC:
break;
......
}
3)队列清空及满空队列判断
void clear_vmcd_encode_cmd_queue(void)
{
ASSERT(pVMCDENC!=NULL,"pVMCDENC not NULL");
pVMCDENC->encode_command_cnt = 0;
pVMCDENC->encode_command_out = 0;
}
BYTE is_vmcd_encode_cmd_queue_full(void)
{
ASSERT(pVMCDENC!=NULL,"pVMCDENC not NULL");
return (pVMCDENC->encode_command_cnt == ENCODE_CMD_QUEUE_LEN);
}
BYTE is_vmcd_encode_cmd_queue_empty(void)
{
ASSERT(pVMCDENC!=NULL,"pVMCDENC not NULL");
return (pVMCDENC->encode_command_cnt == 0 );
}
消息机制,有其有点,但有点复杂;因此,对于流程简单,功能单一的系统,使用简单的接受,命令相应即可。
消息机制,便于系统结构化及系统稳定性