当前位置:网站首页>Programming mode - table driven programming

Programming mode - table driven programming

2022-07-07 21:46:00 Big orange madness

Preface

        At the beginning of programming , There are often situations where different functions are performed according to different conditions , It's usually done with if-else perhaps switch-case The way , If there are many situations to distinguish , There will be many else if perhaps case Code processing , After the whole function is realized , At first glance, the code may have a lot of else if perhaps case, To avoid that , This part introduces a development method -- Table drive method .

         The table driven method is a way for you to find information in a table , Instead of using logical statements (if or case) The way to find them out . in fact , Any information can be picked from the table . In the simple case , Logical statements tend to be simpler and more direct . But with the complexity of the logical chain , The watch becomes more and more attractive . The point of table driven programming is The separation of logic and data .

It can be simply understood that different data are processed in the same way , The table driven method has the following characteristics :

  • High readability , The data processing flow is clear at a glance .
  • Easy to maintain , Just add 、 Delete the data index and method to realize the function .
  • To streamline the code , Reduce cycle complexity . Reduce if-else、switch-case Use .
  • To some extent, it can improve the running speed of the program .

Realization

First , Take a simple example , No table driven code , Get the string according to the input number .

void GetTimeString(int weak, char *pszTime)
{
    if (weak == 1)
    {
        sprintf(pszTime, " Monday ");
    }
    else if (weak == 2)
    {
        sprintf(pszTime, " Tuesday ");
    }
    else if (weak == 3)
    {
        sprintf(pszTime, " Wednesday ");
    }
    else if (weak == 4)
    {
        sprintf(pszTime, " Thursday ");
    }
    else if (weak == 5)
    {
        sprintf(pszTime, " Friday ");
    }
    else if (weak == 6)
    {
        sprintf(pszTime, " Saturday ");
    }
    else if (weak == 7)
    {
        sprintf(pszTime, " Sunday ");
    }
    else
    {
        sprintf(pszTime, " Unknown ");
    }
}

After using the table driven method , The code is streamlined , Readability enhancements (if-else Too much will cause the screen not to see at a glance ), And to a certain extent, it improves the running speed of the program ( if 7 You don't need to do it many times if Judge )

void GetTimeString(int weak, char *pszTime)
{
    const char *arrpszTime[7] = {" Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday ", " Sunday "};

    if (weak >= 1 && weak <= 7)
    {
        sprintf(pszTime, "%s", arrpszTime[weak - 1]);
    }
    else
    {
        sprintf(pszTime, " Unknown ");
    }
}

Applicable scenario

After learning simple table driven programming , But in the actual development, I still don't know under what circumstances , So when is it suitable for table driven development ?

1、 Common are driver development , For example, multiple identical driver chips are used , But the pins are different , Maybe some people debug one of the drivers , Will copy a copy and then modify the pin, etc , This is undoubtedly a waste Flash Space , At this time, the table drive mode is used to take the pins as data , Drive as logic , Use only one driver code , It can infinitely expand multiple driver chips .

2、EEPROM Data processing , Usually include address 、 Data values 、 Maximum 、 Minimum and default values, etc , The way they handle it is the same , The data to be saved can be made into an array table , Process this data through the same function .

3、 Key 、 menu 、LED And other modules can be processed in a table driven way , For example, the key will pin 、 Press level 、 Current status, etc. as data , The menu takes each option, function pointer, etc. as data .

4、 Other situations that can be converted into data after thinking .

Code reference :

Key :https://gitee.com/const-zpc/FML_KEY.git( Key management table , Contains pins 、 Press level 、 Current status, etc )

menu :https://gitee.com/const-zpc/menu.git( Menu options table , Contains the name of the menu option 、 Sub menu 、 A function pointer 【 Subsequent actions 】 etc. )

OLED:https://gitee.com/const-zpc/STM32_OLED.git ( Font data sheet , Include font index 、 Font data, etc )

ESP8266:https://gitee.com/const-zpc/esp8266.git (AT Instruction data sheet , Contains instructions 、 Expected response 、 Time and function pointers 【 Subsequent actions 】 etc. )

 

原网站

版权声明
本文为[Big orange madness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071448068341.html

随机推荐