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.
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: initialization enter deal with 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.
Previous article:MCU--Xintang N76E003--Create Project
Next article:MCU--Time Slice & Time-sharing Polling
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- Can the master device send data to the slave device?
- MOSFET tube "fully saturated conduction"?
- [Sipeed LicheeRV 86 Panel Review] 4-Building the Compilation Environment
- 【AT32F421 Review】+RTC Electronic Clock
- [Synopsys IP Resources] AI+ML brings the long-running battle of chip verification to an end ahead of schedule
- Calculation method of maximum power handling of LTCC low-pass filter
- Please guide me, what is = for? Why not just use =
- [Visual Home Environment Analyzer] + ADI smoke sensor is moving a bit and can read
- MSP430 DC Motor Control
- A small topic about power supply: How to select a power supply when designing it?