mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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