Content
In simple terms, the adapter pattern converts the interface of one class into another interface that the client expects. This pattern is often used when we are programming, especially in projects involving microcontrollers.

However, when we are working on projects and writing code, we often do not consider many factors. If we want the entire framework to be easy to port and understand without an operating system, we really need to think carefully about how to implement this design pattern.
Below, I will share my project experience regarding the implementation of the adapter pattern suitable for microcontrollers. Experts, please do not disturb, and I welcome your guidance.
General Implementation
In our projects, a typical implementation might look like this:
// FileName: test.c// Source: Public Account [Technology Makes Dreams Greater]#include <stdio.h>#include "ExternModule.h"
int main(void) {
/* Initialization */ vAllInit();
while(1) {
/* Project Logic */ vLogicModule1();
vLogicModule2();
}
}
In the external file, we call the corresponding initialization and logic functions. However, when our project becomes complex, with logic relationships overlapping and alternating, this style of writing can become less readable.
Adapter Interface
First, we need to define the data structure. Generally, such projects can be divided into the following steps:
- Initialization
- Input
- Processing
- Output
We encapsulate these four steps and define the data structure as follows:
// FileName: test1.c// Source: Public 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
};
typedef struct _ADAPTER ADAPTER;
Now, let’s define the initialization function:
// FileName: test1.c// Source: Public Account [Technology Makes Dreams Greater]/* Module Initialization */
void moduleInit(ADAPTER *module) {
if (module->Init != NULL) {
module->Init();
}
}
For the logical operation of the module, we can use it like this:
// FileName: test1.c// Source: Public Account [Technology Makes Dreams Greater]/* Module Logic Execution */
void moduleRun(ADAPTER *module) {
// If the module input adapter interface is not null, execute input adaptation
if (module->Input != NULL) {
module->Input();
}
// If the module processing interface is not null, execute processing
// If the module output adapter interface is not null, execute output adaptation
}
After defining these data structures and encapsulations, we only need to call this pattern in each submodule. For example, if there is a requirement to turn on a light, we create an independent file and declare it as follows:
// FileName: led.c// Source: Public Account [Technology Makes Dreams Greater]/* LED Operation */
ADAPTER LedModule = { vLedInit, NULL, vLedRunModule, NULL };
Next, we only need to describe the initialization function and the logical operation function. Similarly, if we need a button function, we declare it in another independent file:
// FileName: key.c// Source: Public Account [Technology Makes Dreams Greater]/* Button Operation */
ADAPTER KeyModule = { vKeyInit, NULL, vKeyRunModule, NULL };
This way, it is convenient for us to split requirements and facilitate portability, while the program becomes modular. Finally, in the main file, we just need to call these functions. We need to do this:
// FileName: main.c// Source: Public Account [Technology Makes Dreams Greater]/* Main Function */
void main(void) {
moduleInit(&LedModule);
moduleInit(&KeyModule);
while(1) {
moduleRun(&LedModule);
moduleRun(&KeyModule);
}
}
Conclusion
The main function is that simple, and the entire architecture is very clear, reflecting the beauty of programming.
In conclusion
That’s all for today. If you found this helpful, please remember to give alike~
Unique, permanent, and free sharing platform for embedded technology knowledge~
Recommended Albums Click the blue text to jump
☞ MCU Advanced Album 
☞ Embedded C Language Advanced Album 
☞ “Bug Talk” Album 
☞ Album | Complete Guide to Linux Application Programming
☞ Album | Learn Some Networking Knowledge
☞ Album | Handwritten C Language
☞ Album | Handwritten C++ Language
☞ Album | Experience Sharing
☞ Album | Power Control Technology
☞ Album | From Microcontroller to Linux
