MCU - Interface adapter mode commonly used in single-chip microcomputers implemented in C language

Publisher:cwm6269310Latest update time:2022-07-13 Source: csdnKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

In layman's terms, the adapter pattern converts the interface of a class into another interface that the customer wants. It is often used when we write programs, especially when we use microcontrollers to do projects.

picture

However, when we are writing programs for projects, we often don’t think so much. If we want the entire framework to be easy to port and understand without an operating system, then we really need to think carefully about how to write this design pattern.


Next, I will talk about the implementation of the interface adapter mode suitable for microcontrollers based on my own project experience. Please don't disturb me, please give me more advice.


General Implementation

When we do a project, in general, we may write code like this

// FileName: test.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

#include

#include "ExternModule.h"

 

int main(void)

{

 

  /*initialization*/

  vAllInit();

  

  while(1)

  {

  

    /*Project logic*/

    vLogicModule1();

    

    vLogicModule2();

    

  }

}


In its external file, the corresponding initialization function and logic function are called. However, when our project is very complex and the logical relationships are overlapped and alternated layer by layer, this way of writing is not very nice.


Interface adapter

First of all, we still need to define the data structure. Generally, such a project is divided into several steps:

  1. initialization

  2. enter

  3. deal with

  4. Output

We encapsulate these four steps and define the data structure as follows:


// FileName: test1.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/* Adapter type definition */

struct _ADAPTER 

{

    void (*Init )( void ); //initialization function

    void (*Input )( void ); //Input conversion function

    void (*Process )( void ); //processing function

    void (*Output )( void ); //Output conversion function

};


So let's define the initialization function like this

// FileName: test1.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/* Module initialization */

void moduleInit( ADAPTER *module )

{

    if( module->Init != NULL )

    {

        module->Init();

    }

}


The logic of the module runs, we can use it like this

// FileName: test1.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/* Module logic operation*/

void moduleRun( ADAPTER *module )

{

    // If the module input adapter interface is not empty, perform input adaptation operation

    if( module->Input != NULL )

    {

        module->Input();

    }

 

    // If the module processing interface is not empty, execute the processing operation   

    

    // If the module output adapter interface is not empty, then perform the output adapter operation

}


After defining these data structures and encapsulation, we only need to call this mode in each submodule. For example, if there is a requirement to turn on a light, we create an independent file and declare it in the file.

// FileName: led.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/*LED light running*/

ADAPTER LedModule = { vLedInit, NULL, vLedRunModule, NULL };


Then we just need to describe the initialization function and the logic operation function. Similarly, we need a button function, which is applied in another independent file.

// FileName: key.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/*button to run*/

ADAPTER KeyModule = { vKeyInit, NULL, vKeyRunModule, NULL };


This will make it easier for us to split requirements and port them, and the program will be modularized. Finally, all we need to do in the main file is to call these functions. We need to do this.


// FileName: main.c

// Source: WeChat Official Account [Technology Makes Dreams Greater]

/*Main function*/

void main( void )

{

 

    moduleInit( &LedModule );

    moduleInit( &keyModule );

    

    while( 1 )

    {

        moduleRun( &LedModule );

        moduleRun( &keyModule );

    }

    

}



 

at last

The main function is so simple, and the entire structure is also very clear, reflecting the beauty of programming.

Keywords:MCU Reference address:MCU - Interface adapter mode commonly used in single-chip microcomputers implemented in C language

Previous article:MCU--Xintang N76E003--Create Project
Next article:MCU--Time Slice & Time-sharing Polling

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号