Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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