FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.

Dependencies:   mbed FXOS8700CQ MODSERIAL

Committer:
jwhammel
Date:
Wed Mar 06 21:11:49 2019 +0000
Revision:
84:fc8c9b39723a
Embedded Systems Midterm Project Spring 2019

Who changed what in which revision?

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