Lec17SkillTwo

Lec17SkillTwo - Function Pointers and Callback Functions...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE 471 17-Sk2-1 Lecture 17 Software Development Techniques II Outline Callback functions Conditional compilation File structures Summary
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 471 17-Sk2-2 Using an example of 16-bit counter building block to demonstrate the techniques Function pointer Callback function Function Pointers and Callback Functions
Background image of page 2
ECE 471 17-Sk2-3 Function Pointers Definition - pointer to functions (or procedures) Function pointer type declaration typedef void (*Counter_Callback_Routine_Type) (void); typedef U16 (*Counter_Callback_Routine_Type) (U16); Variable declaration Counter_Callback_Routine_Type function_pointer; Assign function to a function pointer void function1 (void); /*declare a function*/ function_pointer = &function1; Usage - the following instruction executes function1. function_pointer();
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 471 17-Sk2-4 Callback function Definition Callback functions is a function that is called by service routines to indicate the service is done. They are usually defined in the application and called by service routines. Callback function is defined the same as a regular function Why using callback functions When the collaboration needs the ability to dynamically change the service at run-time Prevent polling in asynchronous operations. Allow the service routine to interface with different applications.
Background image of page 4
ECE 471 17-Sk2-5 Implementation of Callback Functions Step 1 Define the interface of a callback function (function type, number of parameters, the type of each parameter, and the type of the return value) Step 2 Define a function pointer of the callback function type Step 3 Define an interface data structure that include the function pointer Step 4 Write the server program module that calls the callback function Step 5 Write the client program module (may be application module) that has the callback function. Associate the name of the callback function to the function pointer.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 471 17-Sk2-6 Example 1 Counter Building Block Many applications need counters Microcontroller has limited hardware counters The basic functionality of counters for all applications are similar, which include Initialize a counter ---BB_Initialize_Counter Reset a counter --- BB_Reset_Counter Service a counter --- BB_Service_Counter (BB_Service_Counter notifies the user when the counter expires) Read counter value - BB_Read_Counter
Background image of page 6
ECE 471 17-Sk2-7 To provide a convenient usage, the counter BB should support many independent counters simultaneously counter_1, counter_n use one function for each general operation BB_Initialize_Counter() BB_Service_Counter() allow each function to support all possible counters BB_Initialize_Counter(counter_number) BB_Service_Counter (counter_number) Example 1 (cont) Counter BB Requirement
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 471 17-Sk2-8 bb_cnt.h #ifndef _bb_cnt_h #define _bb_cnt_h 1 typedef unsigned int U16; typedef unsigned char U8; typedef void (*Counter_Callback_Routine_Type) (void); typedef const struct Counter_Block_Struct_Tag
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

Lec17SkillTwo - Function Pointers and Callback Functions...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online