Mbed SDK for XRange SX1272 LoRa module
Dependents: XRangePingPong XRange-LoRaWAN-lmic-app lora-transceiver
SX1272 LoRa RF module
https://www.netblocks.eu/xrange-sx1272-lora-datasheet/
api/FunctionPointer.h@339:ac6f3fd999f3, 2016-01-07 (annotated)
- Committer:
- netblocks
- Date:
- Thu Jan 07 13:01:25 2016 +0000
- Revision:
- 339:ac6f3fd999f3
- Parent:
- 336:1e18a06a987b
HSE_VALUE set for XTAL 16Mhz
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 336:1e18a06a987b | 1 | /* mbed Microcontroller Library |
dudmuck | 336:1e18a06a987b | 2 | * Copyright (c) 2006-2013 ARM Limited |
dudmuck | 336:1e18a06a987b | 3 | * |
dudmuck | 336:1e18a06a987b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dudmuck | 336:1e18a06a987b | 5 | * you may not use this file except in compliance with the License. |
dudmuck | 336:1e18a06a987b | 6 | * You may obtain a copy of the License at |
dudmuck | 336:1e18a06a987b | 7 | * |
dudmuck | 336:1e18a06a987b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dudmuck | 336:1e18a06a987b | 9 | * |
dudmuck | 336:1e18a06a987b | 10 | * Unless required by applicable law or agreed to in writing, software |
dudmuck | 336:1e18a06a987b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dudmuck | 336:1e18a06a987b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dudmuck | 336:1e18a06a987b | 13 | * See the License for the specific language governing permissions and |
dudmuck | 336:1e18a06a987b | 14 | * limitations under the License. |
dudmuck | 336:1e18a06a987b | 15 | */ |
dudmuck | 336:1e18a06a987b | 16 | #ifndef MBED_FUNCTIONPOINTER_H |
dudmuck | 336:1e18a06a987b | 17 | #define MBED_FUNCTIONPOINTER_H |
dudmuck | 336:1e18a06a987b | 18 | |
dudmuck | 336:1e18a06a987b | 19 | #include <string.h> |
dudmuck | 336:1e18a06a987b | 20 | |
dudmuck | 336:1e18a06a987b | 21 | namespace mbed { |
dudmuck | 336:1e18a06a987b | 22 | |
dudmuck | 336:1e18a06a987b | 23 | typedef void (*pvoidf_t)(void); |
dudmuck | 336:1e18a06a987b | 24 | |
dudmuck | 336:1e18a06a987b | 25 | /** A class for storing and calling a pointer to a static or member void function |
dudmuck | 336:1e18a06a987b | 26 | */ |
dudmuck | 336:1e18a06a987b | 27 | class FunctionPointer { |
dudmuck | 336:1e18a06a987b | 28 | public: |
dudmuck | 336:1e18a06a987b | 29 | |
dudmuck | 336:1e18a06a987b | 30 | /** Create a FunctionPointer, attaching a static function |
dudmuck | 336:1e18a06a987b | 31 | * |
dudmuck | 336:1e18a06a987b | 32 | * @param function The void static function to attach (default is none) |
dudmuck | 336:1e18a06a987b | 33 | */ |
dudmuck | 336:1e18a06a987b | 34 | FunctionPointer(void (*function)(void) = 0); |
dudmuck | 336:1e18a06a987b | 35 | |
dudmuck | 336:1e18a06a987b | 36 | /** Create a FunctionPointer, attaching a member function |
dudmuck | 336:1e18a06a987b | 37 | * |
dudmuck | 336:1e18a06a987b | 38 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
dudmuck | 336:1e18a06a987b | 39 | * @param function The address of the void member function to attach |
dudmuck | 336:1e18a06a987b | 40 | */ |
dudmuck | 336:1e18a06a987b | 41 | template<typename T> |
dudmuck | 336:1e18a06a987b | 42 | FunctionPointer(T *object, void (T::*member)(void)) { |
dudmuck | 336:1e18a06a987b | 43 | attach(object, member); |
dudmuck | 336:1e18a06a987b | 44 | } |
dudmuck | 336:1e18a06a987b | 45 | |
dudmuck | 336:1e18a06a987b | 46 | /** Attach a static function |
dudmuck | 336:1e18a06a987b | 47 | * |
dudmuck | 336:1e18a06a987b | 48 | * @param function The void static function to attach (default is none) |
dudmuck | 336:1e18a06a987b | 49 | */ |
dudmuck | 336:1e18a06a987b | 50 | void attach(void (*function)(void) = 0); |
dudmuck | 336:1e18a06a987b | 51 | |
dudmuck | 336:1e18a06a987b | 52 | /** Attach a member function |
dudmuck | 336:1e18a06a987b | 53 | * |
dudmuck | 336:1e18a06a987b | 54 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
dudmuck | 336:1e18a06a987b | 55 | * @param function The address of the void member function to attach |
dudmuck | 336:1e18a06a987b | 56 | */ |
dudmuck | 336:1e18a06a987b | 57 | template<typename T> |
dudmuck | 336:1e18a06a987b | 58 | void attach(T *object, void (T::*member)(void)) { |
dudmuck | 336:1e18a06a987b | 59 | _object = static_cast<void*>(object); |
dudmuck | 336:1e18a06a987b | 60 | memcpy(_member, (char*)&member, sizeof(member)); |
dudmuck | 336:1e18a06a987b | 61 | _membercaller = &FunctionPointer::membercaller<T>; |
dudmuck | 336:1e18a06a987b | 62 | _function = 0; |
dudmuck | 336:1e18a06a987b | 63 | } |
dudmuck | 336:1e18a06a987b | 64 | |
dudmuck | 336:1e18a06a987b | 65 | /** Call the attached static or member function |
dudmuck | 336:1e18a06a987b | 66 | */ |
dudmuck | 336:1e18a06a987b | 67 | void call(); |
dudmuck | 336:1e18a06a987b | 68 | |
dudmuck | 336:1e18a06a987b | 69 | pvoidf_t get_function() const { |
dudmuck | 336:1e18a06a987b | 70 | return (pvoidf_t)_function; |
dudmuck | 336:1e18a06a987b | 71 | } |
dudmuck | 336:1e18a06a987b | 72 | |
dudmuck | 336:1e18a06a987b | 73 | #ifdef MBED_OPERATORS |
dudmuck | 336:1e18a06a987b | 74 | void operator ()(void); |
dudmuck | 336:1e18a06a987b | 75 | #endif |
dudmuck | 336:1e18a06a987b | 76 | |
dudmuck | 336:1e18a06a987b | 77 | private: |
dudmuck | 336:1e18a06a987b | 78 | template<typename T> |
dudmuck | 336:1e18a06a987b | 79 | static void membercaller(void *object, char *member) { |
dudmuck | 336:1e18a06a987b | 80 | T* o = static_cast<T*>(object); |
dudmuck | 336:1e18a06a987b | 81 | void (T::*m)(void); |
dudmuck | 336:1e18a06a987b | 82 | memcpy((char*)&m, member, sizeof(m)); |
dudmuck | 336:1e18a06a987b | 83 | (o->*m)(); |
dudmuck | 336:1e18a06a987b | 84 | } |
dudmuck | 336:1e18a06a987b | 85 | |
dudmuck | 336:1e18a06a987b | 86 | void (*_function)(void); // static function pointer - 0 if none attached |
dudmuck | 336:1e18a06a987b | 87 | void *_object; // object this pointer - 0 if none attached |
dudmuck | 336:1e18a06a987b | 88 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
dudmuck | 336:1e18a06a987b | 89 | void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
dudmuck | 336:1e18a06a987b | 90 | }; |
dudmuck | 336:1e18a06a987b | 91 | |
dudmuck | 336:1e18a06a987b | 92 | } // namespace mbed |
dudmuck | 336:1e18a06a987b | 93 | |
dudmuck | 336:1e18a06a987b | 94 | #endif |