Callback Method Design Patterns
The callback pattern in embedded systems is a design pattern where a function (callback function) is registered to be called later when a specific event or condition occurs.
This pattern is commonly used in event-driven programming, where the flow of the program is determined by events rather than a linear sequence of statements.
Problem Statement of Callback Method Design Pattern:
Let’s see an example of implementing a simple callback pattern in C for an embedded system. In this example, we’ll create a system that performs some actions when a button is pressed.
C
#include <stdio.h> // Callback function type definition typedef void (*CallbackFunction)( void ); // Function to register a callback void registerCallback(CallbackFunction callback) { // Store the callback function for later use // This could be an array of callbacks for multiple events CallbackFunction registeredCallback = callback; // Simulate registering the callback (e.g., in an interrupt setup) printf ("Callback registered successfully\n"); // Simulate an event triggering the callback (e.g., button press) printf ("Event occurred, calling the callback\n"); registeredCallback(); } // Callback function 1 void callbackFunction1( void ) { printf ("Callback Function 1 executed\n"); // Perform actions related to the event } // Callback function 2 void callbackFunction2( void ) { printf ("Callback Function 2 executed\n"); // Perform other actions related to the event } int main() { // Register callbackFunction1 for the button press event registerCallback(callbackFunction1); // Register callbackFunction2 for another event registerCallback(callbackFunction2); return 0; } |
Callback registered successfully Event occurred, calling the callback Callback Function 1 executed Callback registered successfully Event occurred, calling the callback Callback Function 2 executed
Explanation of the above Code:
- We define a callback function type (CallbackFunction) using a function pointer.
- The registerCallback function takes a callback function as an argument and stores it for later use.
- We simulate the occurrence of an event (e.g., button press) by calling the registerCallback function.
- The registered callback is then called, simulating the execution of actions associated with the event.
Design Patterns for Embedded Systems in C
When working with embedded systems in C, there are so many design patterns that are particularly very useful. Many design patterns can be applied to embedded systems development in C. In this article, we will discuss design patterns for Embedded Systems in C, let’s see all of them one by one with the help of examples.
Important Topics for Design Patterns for Embedded Systems in C
- What is a Design Pattern?
- Creational Design Patterns for Embedded Systems in C
- Factory Method Design Pattern
- Object Method Design Pattern
- Opaque Method Design Pattern
- Singleton Method Design Pattern
- Structural Design Patterns for Embedded Systems in C
- Callback Method Design Patterns
- Inheritance Method Design Pattern
- Virtual API Method Design Pattern
- Other Design Patterns for Embedded System in C
- Bridge Method Design Pattern
- Concurrency Method Design Pattern
- Spinlock Method Design Pattern
- Mutex Method Design Pattern
- Conditional Method Design Pattern
- Behavioral Method Design Pattern
Contact Us