Skip to main content

Posts

Showing posts from October, 2025

Function pointer and Callback advantages

  Example: Button Interrupt with Dynamic Callbacks #include <stdio.h> #include <stdint.h> // Define a callback type typedef void (*ButtonCallback)(void); // Global variables volatile uint8_t button_pressed_flag = 0; ButtonCallback button_callback = NULL; // ISR: fast, only sets flag void BUTTON_IRQHandler(void) { button_pressed_flag = 1; } // Register a callback dynamically void register_button_callback(ButtonCallback cb) { button_callback = cb; } // Two possible actions void turn_on_led(void) { printf("LED turned ON!\n"); } void send_message(void) { printf("Message sent!\n"); } int main() { // At runtime, we can decide which action to use int user_choice; while (1) { printf("Choose action: 1=LED, 2=Message: "); scanf("%d", &user_choice); if (user_choice == 1) register_button_callback(turn_on_led); // set LED action else register_button...