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 23:09:32 2011 +0000
Revision:
2:56e309e76c19
Parent:
1:d7803001a259
1.2 See ChangeLog.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:fcfb13f40846 1 /*
AjK 1:d7803001a259 2 Copyright (c) 2011 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_EXAMPLE3
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 // Create a data structure that matches what you need.
AjK 0:fcfb13f40846 34 // In this example the structure just holds an int and
AjK 0:fcfb13f40846 35 // a float. Your own data structure can be constructed
AjK 0:fcfb13f40846 36 // however you need.
AjK 0:fcfb13f40846 37 typedef struct {
AjK 0:fcfb13f40846 38 int i_value;
AjK 0:fcfb13f40846 39 float f_value;
AjK 0:fcfb13f40846 40 } MYDATA;
AjK 0:fcfb13f40846 41
AjK 1:d7803001a259 42 // The callback function.
AjK 0:fcfb13f40846 43 uint32_t myCallback(uint32_t value) {
AjK 0:fcfb13f40846 44
AjK 0:fcfb13f40846 45 // Get the value of the count in main
AjK 1:d7803001a259 46 // (de-reference it) so we know what it is.
AjK 0:fcfb13f40846 47 // Note, Mbed/LPC17xx pointers are 32bit
AjK 0:fcfb13f40846 48 // values. So we can "cast" a value back
AjK 0:fcfb13f40846 49 // into a pointer.
AjK 0:fcfb13f40846 50 MYDATA *q = (MYDATA *)value;
AjK 0:fcfb13f40846 51
AjK 0:fcfb13f40846 52 // Then display the bottom four bits of
AjK 0:fcfb13f40846 53 // the i_value on the LEDs.
AjK 0:fcfb13f40846 54 led4 = (q->i_value & 1) ? 1 : 0;
AjK 0:fcfb13f40846 55 led3 = (q->i_value & 2) ? 1 : 0;
AjK 0:fcfb13f40846 56 led2 = (q->i_value & 4) ? 1 : 0;
AjK 0:fcfb13f40846 57 led1 = (q->i_value & 8) ? 1 : 0;
AjK 0:fcfb13f40846 58
AjK 0:fcfb13f40846 59 // Note, f_value isn't used but we could access
AjK 0:fcfb13f40846 60 // it's value with q->f_value if we needed to.
AjK 0:fcfb13f40846 61
AjK 0:fcfb13f40846 62 // What we return doesn't matter as it's
AjK 0:fcfb13f40846 63 // not used in this example but we return
AjK 0:fcfb13f40846 64 // "something" (zero in this case) to keep
AjK 1:d7803001a259 65 // the compiler happy as it expects us to
AjK 0:fcfb13f40846 66 // return something.
AjK 0:fcfb13f40846 67 return 0;
AjK 0:fcfb13f40846 68 }
AjK 0:fcfb13f40846 69
AjK 0:fcfb13f40846 70 int main() {
AjK 0:fcfb13f40846 71 FPointer myPointer;
AjK 0:fcfb13f40846 72 MYDATA data;
AjK 0:fcfb13f40846 73
AjK 0:fcfb13f40846 74 data.i_value = 0;
AjK 0:fcfb13f40846 75 data.f_value = 0.0;
AjK 0:fcfb13f40846 76
AjK 0:fcfb13f40846 77 // Attach a C function pointer as the callback.
AjK 0:fcfb13f40846 78 myPointer.attach(&myCallback);
AjK 0:fcfb13f40846 79
AjK 0:fcfb13f40846 80 while(1) {
AjK 0:fcfb13f40846 81 wait(0.5);
AjK 0:fcfb13f40846 82
AjK 1:d7803001a259 83 // Make the callback passing a pointer (& means "address of")
AjK 0:fcfb13f40846 84 // to the data structure "data".
AjK 0:fcfb13f40846 85 myPointer.call((uint32_t)&data);
AjK 0:fcfb13f40846 86
AjK 0:fcfb13f40846 87 // Increment the i_value by one.
AjK 0:fcfb13f40846 88 data.i_value++;
AjK 0:fcfb13f40846 89 }
AjK 0:fcfb13f40846 90 }
AjK 0:fcfb13f40846 91
AjK 0:fcfb13f40846 92 #endif