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

example2.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_EXAMPLE2
00024 
00025 #include "mbed.h"
00026 #include "FPointer.h"
00027 
00028 class FOO {
00029 protected:
00030     DigitalOut *led1;
00031     DigitalOut *led2;
00032     DigitalOut *led3;
00033     DigitalOut *led4;
00034 
00035 public:
00036     FOO() {
00037         led1 = new DigitalOut(LED1);
00038         led2 = new DigitalOut(LED2);
00039         led3 = new DigitalOut(LED3);
00040         led4 = new DigitalOut(LED4);
00041     }
00042     
00043     uint32_t myCallback(uint32_t value) {
00044         int i = *((int *)value);
00045         led4->write( (i & 1) ? 1 : 0 );
00046         led3->write( (i & 2) ? 1 : 0 );
00047         led2->write( (i & 4) ? 1 : 0 );
00048         led1->write( (i & 8) ? 1 : 0 );    
00049         return 0;
00050     }
00051 };
00052 
00053 // Create an instance of FOO called foo.
00054 FOO foo;
00055 
00056 int main() {
00057     FPointer myPointer;
00058     int count = 0;
00059     
00060     // Attach a C++ object/method pointer as the callback.
00061     myPointer.attach(&foo, &FOO::myCallback);
00062     
00063     while(1) {
00064         wait(0.5);
00065         
00066         // Make the callback passing a pointer
00067         // to the int count variable.
00068         myPointer.call((uint32_t)&count);
00069         
00070         count++;
00071     }
00072 }
00073 
00074 #endif