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

Dependents:   FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more

Committer:
AjK
Date:
Tue Jan 18 17:31:19 2011 +0000
Revision:
0:fcfb13f40846
Child:
1:d7803001a259
1.0 Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:fcfb13f40846 1 /*
AjK 0:fcfb13f40846 2 Copyright (c) 2010 Andy Kirkham
AjK 0:fcfb13f40846 3
AjK 0:fcfb13f40846 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:fcfb13f40846 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:fcfb13f40846 6 in the Software without restriction, including without limitation the rights
AjK 0:fcfb13f40846 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:fcfb13f40846 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:fcfb13f40846 9 furnished to do so, subject to the following conditions:
AjK 0:fcfb13f40846 10
AjK 0:fcfb13f40846 11 The above copyright notice and this permission notice shall be included in
AjK 0:fcfb13f40846 12 all copies or substantial portions of the Software.
AjK 0:fcfb13f40846 13
AjK 0:fcfb13f40846 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:fcfb13f40846 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:fcfb13f40846 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:fcfb13f40846 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:fcfb13f40846 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:fcfb13f40846 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:fcfb13f40846 20 THE SOFTWARE.
AjK 0:fcfb13f40846 21 */
AjK 0:fcfb13f40846 22
AjK 0:fcfb13f40846 23 #ifdef AJK_COMPILE_EXAMPLE1
AjK 0:fcfb13f40846 24
AjK 0:fcfb13f40846 25 #include "mbed.h"
AjK 0:fcfb13f40846 26 #include "FPointer.h"
AjK 0:fcfb13f40846 27
AjK 0:fcfb13f40846 28 DigitalOut led1(LED1);
AjK 0:fcfb13f40846 29 DigitalOut led2(LED2);
AjK 0:fcfb13f40846 30 DigitalOut led3(LED3);
AjK 0:fcfb13f40846 31 DigitalOut led4(LED4);
AjK 0:fcfb13f40846 32
AjK 0:fcfb13f40846 33 uint32_t myCallback(uint32_t value) {
AjK 0:fcfb13f40846 34 // Get the value of the count in main
AjK 0:fcfb13f40846 35 // (deference it) so we know what it is.
AjK 0:fcfb13f40846 36 int i = *((int *)value);
AjK 0:fcfb13f40846 37
AjK 0:fcfb13f40846 38 // Then display the bottom four bits of
AjK 0:fcfb13f40846 39 // the count value on the LEDs.
AjK 0:fcfb13f40846 40 led4 = (i & 1) ? 1 : 0;
AjK 0:fcfb13f40846 41 led3 = (i & 2) ? 1 : 0;
AjK 0:fcfb13f40846 42 led2 = (i & 4) ? 1 : 0;
AjK 0:fcfb13f40846 43 led1 = (i & 8) ? 1 : 0;
AjK 0:fcfb13f40846 44
AjK 0:fcfb13f40846 45 // What we return doesn't matter as it's
AjK 0:fcfb13f40846 46 // not used in this example but we return
AjK 0:fcfb13f40846 47 // "something" (zero in this case) to keep
AjK 0:fcfb13f40846 48 // teh compiler happy as it expects us to
AjK 0:fcfb13f40846 49 // return something.
AjK 0:fcfb13f40846 50 return 0;
AjK 0:fcfb13f40846 51 }
AjK 0:fcfb13f40846 52
AjK 0:fcfb13f40846 53 int main() {
AjK 0:fcfb13f40846 54 FPointer myPointer;
AjK 0:fcfb13f40846 55 int count = 0;
AjK 0:fcfb13f40846 56
AjK 0:fcfb13f40846 57 // Attach a C function pointer as the callback.
AjK 0:fcfb13f40846 58 myPointer.attach(&myCallback);
AjK 0:fcfb13f40846 59
AjK 0:fcfb13f40846 60 while(1) {
AjK 0:fcfb13f40846 61 wait(0.5);
AjK 0:fcfb13f40846 62
AjK 0:fcfb13f40846 63 // Make the callback passing a pointer
AjK 0:fcfb13f40846 64 // to the int count variable.
AjK 0:fcfb13f40846 65 myPointer.call((uint32_t)&count);
AjK 0:fcfb13f40846 66
AjK 0:fcfb13f40846 67 count++;
AjK 0:fcfb13f40846 68 }
AjK 0:fcfb13f40846 69 }
AjK 0:fcfb13f40846 70
AjK 0:fcfb13f40846 71 #endif