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 example1.h Source File

example1.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_EXAMPLE1
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 uint32_t myCallback(uint32_t value) {
00034     // Get the value of the count in main
00035     // (de-reference it) so we know what it is.
00036     int i = *((int *)value);
00037     
00038     // Then display the bottom four bits of
00039     // the count value on the LEDs.
00040     led4 = (i & 1) ? 1 : 0;
00041     led3 = (i & 2) ? 1 : 0;
00042     led2 = (i & 4) ? 1 : 0;
00043     led1 = (i & 8) ? 1 : 0;
00044     
00045     // What we return doesn't matter as it's
00046     // not used in this example but we return
00047     // "something" (zero in this case) to keep
00048     // the compiler happy as it expects us to
00049     // return something.
00050     return 0;
00051 }
00052 
00053 int main() {
00054     FPointer myPointer;
00055     int count = 0;
00056     
00057     // Attach a C function pointer as the callback.
00058     myPointer.attach(&myCallback);
00059     
00060     while(1) {
00061         wait(0.5);
00062         
00063         // Make the callback passing a pointer
00064         // to the int count variable.
00065         myPointer.call((uint32_t)&count);
00066         
00067         count++;
00068     }
00069 }
00070 
00071 #endif