FPointer - A callback system that allows for 32bit unsigned ints to be passed to and from the callback.

Dependents:   FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example3.h Source File

example3.h

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #ifdef AJK_COMPILE_EXAMPLE3
00024 
00025 #include "mbed.h"
00026 #include "FPointer.h"
00027 
00028 DigitalOut led1(LED1);
00029 DigitalOut led2(LED2);
00030 DigitalOut led3(LED3);
00031 DigitalOut led4(LED4);
00032 
00033 // Create a data structure that matches what you need.
00034 // In this example the structure just holds an int and
00035 // a float. Your own data structure can be constructed
00036 // however you need.
00037 typedef struct {
00038     int     i_value;
00039     float   f_value;
00040 } MYDATA;
00041 
00042 // The callback function. 
00043 uint32_t myCallback(uint32_t value) {
00044 
00045     // Get the value of the count in main
00046     // (de-reference it) so we know what it is.
00047     // Note, Mbed/LPC17xx pointers are 32bit
00048     // values. So we can "cast" a value back
00049     // into a pointer.
00050     MYDATA *q = (MYDATA *)value;
00051     
00052     // Then display the bottom four bits of
00053     // the i_value on the LEDs.
00054     led4 = (q->i_value & 1) ? 1 : 0;
00055     led3 = (q->i_value & 2) ? 1 : 0;
00056     led2 = (q->i_value & 4) ? 1 : 0;
00057     led1 = (q->i_value & 8) ? 1 : 0;
00058     
00059     // Note, f_value isn't used but we could access
00060     // it's value with q->f_value if we needed to.
00061     
00062     // What we return doesn't matter as it's
00063     // not used in this example but we return
00064     // "something" (zero in this case) to keep
00065     // the compiler happy as it expects us to
00066     // return something.
00067     return 0;
00068 }
00069 
00070 int main() {
00071     FPointer myPointer;
00072     MYDATA data;
00073     
00074     data.i_value = 0;
00075     data.f_value = 0.0;
00076     
00077     // Attach a C function pointer as the callback.
00078     myPointer.attach(&myCallback);
00079     
00080     while(1) {
00081         wait(0.5);
00082         
00083         // Make the callback passing a pointer (& means "address of")
00084         // to the data structure "data".
00085         myPointer.call((uint32_t)&data);
00086         
00087         // Increment the i_value by one.
00088         data.i_value++;
00089     }
00090 }
00091 
00092 #endif