函数指针模型与状态转换表

来源:互联网 发布:2017流行网络红歌 编辑:程序博客网 时间:2024/06/04 19:41

在工作中,经常使用一些列表操作,经过总结提炼,汇总了一下函数指针结合状态转换的通用编程模型。

单片机状态机编程思想。


在PLC固件开发中,通信协议,指令解析与处理,以及通用的AD处理程序大量使用了这种程序设计架构。

现总结如下:


1 查表 算法模型
enum
{
ADD=0,
SUB,
MUL,
DIV,
};
int FuncAdd(int i ,int j)
{
    return (i+j);
}
int FuncSub(int i ,int j)
{
    return (i-j);
}
int FuncMul(int i ,int j)
{
    return (i*j);
}
int FuncDiv(int i ,int j)
{
    return (i/j);
}




int (* (FuncSelect[]))(int i,int j)=
{
   FuncAdd,
   FuncSub,
   FuncMul,
   FuncDiv


};


void main(void)
{


        FuncSelect[ADD](2,4);
}


2 状态机模型 
  通过SWITCH CASE组
void add_new_trans(Node *list,Node **current,Transaction *trans);
void delete_trans(Node *list,Node **current,Transaction *trans);
void search(Node *list,Node **current,Transaction *trans);
void edit(Node *list,Node **current,Transaction *trans);
void forward(Node *list,Node **current,Transaction *trans)
{
    *current =(*current)->prev;
}
void (*function[])(Node *list,Node **current,Transaction *trans)
{
    add_new_trans,
    delete_trans,
    forward,
    backward,
    search,
    edit
}


函数功能实现
function[transaction->type](list,&current,transaction);


3 按键处理模型
将窗口元素、消息处理函数中封转在窗口中
struct windows
{
    BYTE currentFocus;
    ELEMENT element[ELEMENT_NUM];
    void (*messageFuc)(BYTE keyValue);
}
//消息处理函数
void messageFunction(BYTE keyValue)
{
    byte i=0;
   //获得焦点
   while( (element[i].ID!=currentFocus)&&(i<ELEMENT_NUM) )
   {
       i++;
   }
  // 消息映射
  if(i<ELEMENT_NUM)
  {
      case OK:
          element[i].OnOk();
          break;
  }
}


modbus 协议功能指令处理模型


static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = 
{
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
//TODO Add Master function define
    {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
#endif
#if MB_FUNC_READ_INPUT_ENABLED > 0
    {MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
#endif
#if MB_FUNC_READ_HOLDING_ENABLED > 0
    {MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
#endif
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
    {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
#endif
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
    {MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
#endif
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
    {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
#endif
#if MB_FUNC_READ_COILS_ENABLED > 0
    {MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
#endif
#if MB_FUNC_WRITE_COIL_ENABLED > 0
    {MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
#endif
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
    {MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
#endif
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
    {MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
#endif
};
//最大功能指令个数
#define MB_FUNC_HANDLERS_MAX                    ( 16 )


            ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];//取功能码
            eException = MB_EX_ILLEGAL_FUNCTION;


            for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
            {
                /* No more function handlers registered. Abort. */
                if( xMasterFuncHandlers[i].ucFunctionCode == 0 )
                {
                    break;
                }
                else if( xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode )
                {
                    vMBMasterSetCBRunInMasterMode(TRUE);
                    eException = xMasterFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
                    vMBMasterSetCBRunInMasterMode(FALSE);
                    break;
                }

            }

QQ   903825878

电话 15275452086

0 0
原创粉丝点击