FPointer - A callback system that allows for 32bit unsigned ints to be passed to and from the callback.
Dependents: FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more
example2.h@2:56e309e76c19, 2011-01-18 (annotated)
- 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?
User | Revision | Line number | New 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_EXAMPLE2 |
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 | class FOO { |
AjK | 0:fcfb13f40846 | 29 | protected: |
AjK | 0:fcfb13f40846 | 30 | DigitalOut *led1; |
AjK | 0:fcfb13f40846 | 31 | DigitalOut *led2; |
AjK | 0:fcfb13f40846 | 32 | DigitalOut *led3; |
AjK | 0:fcfb13f40846 | 33 | DigitalOut *led4; |
AjK | 0:fcfb13f40846 | 34 | |
AjK | 0:fcfb13f40846 | 35 | public: |
AjK | 0:fcfb13f40846 | 36 | FOO() { |
AjK | 0:fcfb13f40846 | 37 | led1 = new DigitalOut(LED1); |
AjK | 0:fcfb13f40846 | 38 | led2 = new DigitalOut(LED2); |
AjK | 0:fcfb13f40846 | 39 | led3 = new DigitalOut(LED3); |
AjK | 0:fcfb13f40846 | 40 | led4 = new DigitalOut(LED4); |
AjK | 0:fcfb13f40846 | 41 | } |
AjK | 0:fcfb13f40846 | 42 | |
AjK | 0:fcfb13f40846 | 43 | uint32_t myCallback(uint32_t value) { |
AjK | 0:fcfb13f40846 | 44 | int i = *((int *)value); |
AjK | 0:fcfb13f40846 | 45 | led4->write( (i & 1) ? 1 : 0 ); |
AjK | 0:fcfb13f40846 | 46 | led3->write( (i & 2) ? 1 : 0 ); |
AjK | 0:fcfb13f40846 | 47 | led2->write( (i & 4) ? 1 : 0 ); |
AjK | 0:fcfb13f40846 | 48 | led1->write( (i & 8) ? 1 : 0 ); |
AjK | 0:fcfb13f40846 | 49 | return 0; |
AjK | 0:fcfb13f40846 | 50 | } |
AjK | 0:fcfb13f40846 | 51 | }; |
AjK | 0:fcfb13f40846 | 52 | |
AjK | 0:fcfb13f40846 | 53 | // Create an instance of FOO called foo. |
AjK | 0:fcfb13f40846 | 54 | FOO foo; |
AjK | 0:fcfb13f40846 | 55 | |
AjK | 0:fcfb13f40846 | 56 | int main() { |
AjK | 0:fcfb13f40846 | 57 | FPointer myPointer; |
AjK | 0:fcfb13f40846 | 58 | int count = 0; |
AjK | 0:fcfb13f40846 | 59 | |
AjK | 0:fcfb13f40846 | 60 | // Attach a C++ object/method pointer as the callback. |
AjK | 0:fcfb13f40846 | 61 | myPointer.attach(&foo, &FOO::myCallback); |
AjK | 0:fcfb13f40846 | 62 | |
AjK | 0:fcfb13f40846 | 63 | while(1) { |
AjK | 0:fcfb13f40846 | 64 | wait(0.5); |
AjK | 0:fcfb13f40846 | 65 | |
AjK | 0:fcfb13f40846 | 66 | // Make the callback passing a pointer |
AjK | 0:fcfb13f40846 | 67 | // to the int count variable. |
AjK | 0:fcfb13f40846 | 68 | myPointer.call((uint32_t)&count); |
AjK | 0:fcfb13f40846 | 69 | |
AjK | 0:fcfb13f40846 | 70 | count++; |
AjK | 0:fcfb13f40846 | 71 | } |
AjK | 0:fcfb13f40846 | 72 | } |
AjK | 0:fcfb13f40846 | 73 | |
AjK | 0:fcfb13f40846 | 74 | #endif |