Realtime spectrogram for DISCO-F746NG. On-board MEMS microphone is used for input sound signal. リアルタイムスペクトログラム.入力:MEMSマイク
Dependencies: F746_GUI F746_SAI_IO UIT_FFT_Real
mbed_src_STM32F7/api/FunctionPointer.h@4:c1beacfc42c7, 2016-10-02 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Oct 02 10:51:10 2016 +0000
- Revision:
- 4:c1beacfc42c7
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 4:c1beacfc42c7 | 1 | /* mbed Microcontroller Library |
MikamiUitOpen | 4:c1beacfc42c7 | 2 | * Copyright (c) 2006-2015 ARM Limited |
MikamiUitOpen | 4:c1beacfc42c7 | 3 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MikamiUitOpen | 4:c1beacfc42c7 | 5 | * you may not use this file except in compliance with the License. |
MikamiUitOpen | 4:c1beacfc42c7 | 6 | * You may obtain a copy of the License at |
MikamiUitOpen | 4:c1beacfc42c7 | 7 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MikamiUitOpen | 4:c1beacfc42c7 | 9 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 10 | * Unless required by applicable law or agreed to in writing, software |
MikamiUitOpen | 4:c1beacfc42c7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MikamiUitOpen | 4:c1beacfc42c7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MikamiUitOpen | 4:c1beacfc42c7 | 13 | * See the License for the specific language governing permissions and |
MikamiUitOpen | 4:c1beacfc42c7 | 14 | * limitations under the License. |
MikamiUitOpen | 4:c1beacfc42c7 | 15 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 16 | #ifndef MBED_FUNCTIONPOINTER_H |
MikamiUitOpen | 4:c1beacfc42c7 | 17 | #define MBED_FUNCTIONPOINTER_H |
MikamiUitOpen | 4:c1beacfc42c7 | 18 | |
MikamiUitOpen | 4:c1beacfc42c7 | 19 | #include <string.h> |
MikamiUitOpen | 4:c1beacfc42c7 | 20 | #include <stdint.h> |
MikamiUitOpen | 4:c1beacfc42c7 | 21 | |
MikamiUitOpen | 4:c1beacfc42c7 | 22 | namespace mbed { |
MikamiUitOpen | 4:c1beacfc42c7 | 23 | |
MikamiUitOpen | 4:c1beacfc42c7 | 24 | /* If we had variaditic templates, this wouldn't be a problem, but until C++11 is enabled, we are stuck with multiple classes... */ |
MikamiUitOpen | 4:c1beacfc42c7 | 25 | |
MikamiUitOpen | 4:c1beacfc42c7 | 26 | /** A class for storing and calling a pointer to a static or member function |
MikamiUitOpen | 4:c1beacfc42c7 | 27 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 28 | template <typename R, typename A1> |
MikamiUitOpen | 4:c1beacfc42c7 | 29 | class FunctionPointerArg1{ |
MikamiUitOpen | 4:c1beacfc42c7 | 30 | public: |
MikamiUitOpen | 4:c1beacfc42c7 | 31 | /** Create a FunctionPointer, attaching a static function |
MikamiUitOpen | 4:c1beacfc42c7 | 32 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 33 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 4:c1beacfc42c7 | 34 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 35 | FunctionPointerArg1(R (*function)(A1) = 0) { |
MikamiUitOpen | 4:c1beacfc42c7 | 36 | attach(function); |
MikamiUitOpen | 4:c1beacfc42c7 | 37 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 38 | |
MikamiUitOpen | 4:c1beacfc42c7 | 39 | /** Create a FunctionPointer, attaching a member function |
MikamiUitOpen | 4:c1beacfc42c7 | 40 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 41 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 4:c1beacfc42c7 | 42 | * @param function The address of the member function to attach |
MikamiUitOpen | 4:c1beacfc42c7 | 43 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 44 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 45 | FunctionPointerArg1(T *object, R (T::*member)(A1)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 46 | attach(object, member); |
MikamiUitOpen | 4:c1beacfc42c7 | 47 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 48 | |
MikamiUitOpen | 4:c1beacfc42c7 | 49 | /** Attach a static function |
MikamiUitOpen | 4:c1beacfc42c7 | 50 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 51 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 4:c1beacfc42c7 | 52 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 53 | void attach(R (*function)(A1)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 54 | _p.function = function; |
MikamiUitOpen | 4:c1beacfc42c7 | 55 | _membercaller = 0; |
MikamiUitOpen | 4:c1beacfc42c7 | 56 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 57 | |
MikamiUitOpen | 4:c1beacfc42c7 | 58 | /** Attach a member function |
MikamiUitOpen | 4:c1beacfc42c7 | 59 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 60 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 4:c1beacfc42c7 | 61 | * @param function The address of the member function to attach |
MikamiUitOpen | 4:c1beacfc42c7 | 62 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 63 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 64 | void attach(T *object, R (T::*member)(A1)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 65 | _p.object = static_cast<void*>(object); |
MikamiUitOpen | 4:c1beacfc42c7 | 66 | *reinterpret_cast<R (T::**)(A1)>(_member) = member; |
MikamiUitOpen | 4:c1beacfc42c7 | 67 | _membercaller = &FunctionPointerArg1::membercaller<T>; |
MikamiUitOpen | 4:c1beacfc42c7 | 68 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 69 | |
MikamiUitOpen | 4:c1beacfc42c7 | 70 | /** Call the attached static or member function |
MikamiUitOpen | 4:c1beacfc42c7 | 71 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 72 | R call(A1 a) { |
MikamiUitOpen | 4:c1beacfc42c7 | 73 | if (_membercaller == 0 && _p.function) { |
MikamiUitOpen | 4:c1beacfc42c7 | 74 | return _p.function(a); |
MikamiUitOpen | 4:c1beacfc42c7 | 75 | } else if (_membercaller && _p.object) { |
MikamiUitOpen | 4:c1beacfc42c7 | 76 | return _membercaller(_p.object, _member, a); |
MikamiUitOpen | 4:c1beacfc42c7 | 77 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 78 | return (R)0; |
MikamiUitOpen | 4:c1beacfc42c7 | 79 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 80 | |
MikamiUitOpen | 4:c1beacfc42c7 | 81 | /** Get registered static function |
MikamiUitOpen | 4:c1beacfc42c7 | 82 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 83 | R(*get_function(A1))() { |
MikamiUitOpen | 4:c1beacfc42c7 | 84 | return _membercaller ? (R(*)(A1))0 : (R(*)(A1))_p.function; |
MikamiUitOpen | 4:c1beacfc42c7 | 85 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 86 | |
MikamiUitOpen | 4:c1beacfc42c7 | 87 | #ifdef MBED_OPERATORS |
MikamiUitOpen | 4:c1beacfc42c7 | 88 | R operator ()(A1 a) { |
MikamiUitOpen | 4:c1beacfc42c7 | 89 | return call(a); |
MikamiUitOpen | 4:c1beacfc42c7 | 90 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 91 | operator bool(void) const { |
MikamiUitOpen | 4:c1beacfc42c7 | 92 | return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; |
MikamiUitOpen | 4:c1beacfc42c7 | 93 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 94 | #endif |
MikamiUitOpen | 4:c1beacfc42c7 | 95 | private: |
MikamiUitOpen | 4:c1beacfc42c7 | 96 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 97 | static R membercaller(void *object, uintptr_t *member, A1 a) { |
MikamiUitOpen | 4:c1beacfc42c7 | 98 | T* o = static_cast<T*>(object); |
MikamiUitOpen | 4:c1beacfc42c7 | 99 | R (T::**m)(A1) = reinterpret_cast<R (T::**)(A1)>(member); |
MikamiUitOpen | 4:c1beacfc42c7 | 100 | return (o->**m)(a); |
MikamiUitOpen | 4:c1beacfc42c7 | 101 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 102 | |
MikamiUitOpen | 4:c1beacfc42c7 | 103 | union { |
MikamiUitOpen | 4:c1beacfc42c7 | 104 | R (*function)(A1); // static function pointer |
MikamiUitOpen | 4:c1beacfc42c7 | 105 | void *object; // object this pointer |
MikamiUitOpen | 4:c1beacfc42c7 | 106 | } _p; |
MikamiUitOpen | 4:c1beacfc42c7 | 107 | uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller |
MikamiUitOpen | 4:c1beacfc42c7 | 108 | R (*_membercaller)(void*, uintptr_t*, A1); // registered membercaller function to convert back and call _m.member on _object |
MikamiUitOpen | 4:c1beacfc42c7 | 109 | }; |
MikamiUitOpen | 4:c1beacfc42c7 | 110 | |
MikamiUitOpen | 4:c1beacfc42c7 | 111 | /** A class for storing and calling a pointer to a static or member function (R ()(void)) |
MikamiUitOpen | 4:c1beacfc42c7 | 112 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 113 | template <typename R> |
MikamiUitOpen | 4:c1beacfc42c7 | 114 | class FunctionPointerArg1<R, void>{ |
MikamiUitOpen | 4:c1beacfc42c7 | 115 | public: |
MikamiUitOpen | 4:c1beacfc42c7 | 116 | /** Create a FunctionPointer, attaching a static function |
MikamiUitOpen | 4:c1beacfc42c7 | 117 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 118 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 4:c1beacfc42c7 | 119 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 120 | FunctionPointerArg1(R (*function)(void) = 0) { |
MikamiUitOpen | 4:c1beacfc42c7 | 121 | attach(function); |
MikamiUitOpen | 4:c1beacfc42c7 | 122 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 123 | |
MikamiUitOpen | 4:c1beacfc42c7 | 124 | /** Create a FunctionPointer, attaching a member function |
MikamiUitOpen | 4:c1beacfc42c7 | 125 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 126 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 4:c1beacfc42c7 | 127 | * @param function The address of the void member function to attach |
MikamiUitOpen | 4:c1beacfc42c7 | 128 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 129 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 130 | FunctionPointerArg1(T *object, R (T::*member)(void)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 131 | attach(object, member); |
MikamiUitOpen | 4:c1beacfc42c7 | 132 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 133 | |
MikamiUitOpen | 4:c1beacfc42c7 | 134 | /** Attach a static function |
MikamiUitOpen | 4:c1beacfc42c7 | 135 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 136 | * @param function The void static function to attach (default is none) |
MikamiUitOpen | 4:c1beacfc42c7 | 137 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 138 | void attach(R (*function)(void)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 139 | _p.function = function; |
MikamiUitOpen | 4:c1beacfc42c7 | 140 | _membercaller = 0; |
MikamiUitOpen | 4:c1beacfc42c7 | 141 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 142 | |
MikamiUitOpen | 4:c1beacfc42c7 | 143 | /** Attach a member function |
MikamiUitOpen | 4:c1beacfc42c7 | 144 | * |
MikamiUitOpen | 4:c1beacfc42c7 | 145 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 4:c1beacfc42c7 | 146 | * @param function The address of the void member function to attach |
MikamiUitOpen | 4:c1beacfc42c7 | 147 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 148 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 149 | void attach(T *object, R (T::*member)(void)) { |
MikamiUitOpen | 4:c1beacfc42c7 | 150 | _p.object = static_cast<void*>(object); |
MikamiUitOpen | 4:c1beacfc42c7 | 151 | *reinterpret_cast<R (T::**)(void)>(_member) = member; |
MikamiUitOpen | 4:c1beacfc42c7 | 152 | _membercaller = &FunctionPointerArg1::membercaller<T>; |
MikamiUitOpen | 4:c1beacfc42c7 | 153 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 154 | |
MikamiUitOpen | 4:c1beacfc42c7 | 155 | /** Call the attached static or member function |
MikamiUitOpen | 4:c1beacfc42c7 | 156 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 157 | R call(){ |
MikamiUitOpen | 4:c1beacfc42c7 | 158 | if (_membercaller == 0 && _p.function) { |
MikamiUitOpen | 4:c1beacfc42c7 | 159 | return _p.function(); |
MikamiUitOpen | 4:c1beacfc42c7 | 160 | } else if (_membercaller && _p.object) { |
MikamiUitOpen | 4:c1beacfc42c7 | 161 | return _membercaller(_p.object, _member); |
MikamiUitOpen | 4:c1beacfc42c7 | 162 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 163 | return (R)0; |
MikamiUitOpen | 4:c1beacfc42c7 | 164 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 165 | |
MikamiUitOpen | 4:c1beacfc42c7 | 166 | /** Get registered static function |
MikamiUitOpen | 4:c1beacfc42c7 | 167 | */ |
MikamiUitOpen | 4:c1beacfc42c7 | 168 | R(*get_function())() { |
MikamiUitOpen | 4:c1beacfc42c7 | 169 | return _membercaller ? (R(*)())0 : (R(*)())_p.function; |
MikamiUitOpen | 4:c1beacfc42c7 | 170 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 171 | |
MikamiUitOpen | 4:c1beacfc42c7 | 172 | #ifdef MBED_OPERATORS |
MikamiUitOpen | 4:c1beacfc42c7 | 173 | R operator ()(void) { |
MikamiUitOpen | 4:c1beacfc42c7 | 174 | return call(); |
MikamiUitOpen | 4:c1beacfc42c7 | 175 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 176 | operator bool(void) const { |
MikamiUitOpen | 4:c1beacfc42c7 | 177 | return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; |
MikamiUitOpen | 4:c1beacfc42c7 | 178 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 179 | #endif |
MikamiUitOpen | 4:c1beacfc42c7 | 180 | |
MikamiUitOpen | 4:c1beacfc42c7 | 181 | private: |
MikamiUitOpen | 4:c1beacfc42c7 | 182 | template<typename T> |
MikamiUitOpen | 4:c1beacfc42c7 | 183 | static R membercaller(void *object, uintptr_t *member) { |
MikamiUitOpen | 4:c1beacfc42c7 | 184 | T* o = static_cast<T*>(object); |
MikamiUitOpen | 4:c1beacfc42c7 | 185 | R (T::**m)(void) = reinterpret_cast<R (T::**)(void)>(member); |
MikamiUitOpen | 4:c1beacfc42c7 | 186 | return (o->**m)(); |
MikamiUitOpen | 4:c1beacfc42c7 | 187 | } |
MikamiUitOpen | 4:c1beacfc42c7 | 188 | |
MikamiUitOpen | 4:c1beacfc42c7 | 189 | union { |
MikamiUitOpen | 4:c1beacfc42c7 | 190 | R (*function)(void); // static function pointer |
MikamiUitOpen | 4:c1beacfc42c7 | 191 | void *object; // object this pointer |
MikamiUitOpen | 4:c1beacfc42c7 | 192 | } _p; |
MikamiUitOpen | 4:c1beacfc42c7 | 193 | uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller |
MikamiUitOpen | 4:c1beacfc42c7 | 194 | R (*_membercaller)(void*, uintptr_t*); // registered membercaller function to convert back and call _m.member on _object |
MikamiUitOpen | 4:c1beacfc42c7 | 195 | }; |
MikamiUitOpen | 4:c1beacfc42c7 | 196 | |
MikamiUitOpen | 4:c1beacfc42c7 | 197 | typedef FunctionPointerArg1<void, void> FunctionPointer; |
MikamiUitOpen | 4:c1beacfc42c7 | 198 | typedef FunctionPointerArg1<void, int> event_callback_t; |
MikamiUitOpen | 4:c1beacfc42c7 | 199 | |
MikamiUitOpen | 4:c1beacfc42c7 | 200 | } // namespace mbed |
MikamiUitOpen | 4:c1beacfc42c7 | 201 | |
MikamiUitOpen | 4:c1beacfc42c7 | 202 | #endif |