【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/
Dependents: mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn
MQTT/FP/FP.h@11:278ba4de2e99, 2017-09-07 (annotated)
- Committer:
- jksoft
- Date:
- Thu Sep 07 01:26:48 2017 +0000
- Revision:
- 11:278ba4de2e99
- Parent:
- 0:0a2f634d3324
???????(FP->_FP)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:0a2f634d3324 | 1 | /** |
jksoft | 0:0a2f634d3324 | 2 | * @file FP.h |
jksoft | 0:0a2f634d3324 | 3 | * @brief Core Utility - Templated Function Pointer Class |
jksoft | 0:0a2f634d3324 | 4 | * @author sam grove |
jksoft | 0:0a2f634d3324 | 5 | * @version 1.1 |
jksoft | 0:0a2f634d3324 | 6 | * @see http://mbed.org/users/sam_grove/code/FP/ |
jksoft | 0:0a2f634d3324 | 7 | * |
jksoft | 0:0a2f634d3324 | 8 | * Copyright (c) 2013 |
jksoft | 0:0a2f634d3324 | 9 | * |
jksoft | 0:0a2f634d3324 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 0:0a2f634d3324 | 11 | * you may not use this file except in compliance with the License. |
jksoft | 0:0a2f634d3324 | 12 | * You may obtain a copy of the License at |
jksoft | 0:0a2f634d3324 | 13 | * |
jksoft | 0:0a2f634d3324 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 0:0a2f634d3324 | 15 | * |
jksoft | 0:0a2f634d3324 | 16 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 0:0a2f634d3324 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 0:0a2f634d3324 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 0:0a2f634d3324 | 19 | * See the License for the specific language governing permissions and |
jksoft | 0:0a2f634d3324 | 20 | * limitations under the License. |
jksoft | 0:0a2f634d3324 | 21 | */ |
jksoft | 0:0a2f634d3324 | 22 | |
jksoft | 0:0a2f634d3324 | 23 | #ifndef FP_H |
jksoft | 0:0a2f634d3324 | 24 | #define FP_H |
jksoft | 0:0a2f634d3324 | 25 | |
jksoft | 0:0a2f634d3324 | 26 | /** Example using the FP Class with global functions |
jksoft | 0:0a2f634d3324 | 27 | * @code |
jksoft | 0:0a2f634d3324 | 28 | * #include "mbed.h" |
jksoft | 0:0a2f634d3324 | 29 | * #include "FP.h" |
jksoft | 0:0a2f634d3324 | 30 | * |
jksoft | 0:0a2f634d3324 | 31 | * FP<void,bool>fp; |
jksoft | 0:0a2f634d3324 | 32 | * DigitalOut myled(LED1); |
jksoft | 0:0a2f634d3324 | 33 | * |
jksoft | 0:0a2f634d3324 | 34 | * void handler(bool value) |
jksoft | 0:0a2f634d3324 | 35 | * { |
jksoft | 0:0a2f634d3324 | 36 | * myled = value; |
jksoft | 0:0a2f634d3324 | 37 | * return; |
jksoft | 0:0a2f634d3324 | 38 | * } |
jksoft | 0:0a2f634d3324 | 39 | * |
jksoft | 0:0a2f634d3324 | 40 | * int main() |
jksoft | 0:0a2f634d3324 | 41 | * { |
jksoft | 0:0a2f634d3324 | 42 | * fp.attach(&handler); |
jksoft | 0:0a2f634d3324 | 43 | * |
jksoft | 0:0a2f634d3324 | 44 | * while(1) |
jksoft | 0:0a2f634d3324 | 45 | * { |
jksoft | 0:0a2f634d3324 | 46 | * fp(1); |
jksoft | 0:0a2f634d3324 | 47 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 48 | * fp(0); |
jksoft | 0:0a2f634d3324 | 49 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 50 | * } |
jksoft | 0:0a2f634d3324 | 51 | * } |
jksoft | 0:0a2f634d3324 | 52 | * @endcode |
jksoft | 0:0a2f634d3324 | 53 | */ |
jksoft | 0:0a2f634d3324 | 54 | |
jksoft | 0:0a2f634d3324 | 55 | /** Example using the FP Class with different class member functions |
jksoft | 0:0a2f634d3324 | 56 | * @code |
jksoft | 0:0a2f634d3324 | 57 | * #include "mbed.h" |
jksoft | 0:0a2f634d3324 | 58 | * #include "FP.h" |
jksoft | 0:0a2f634d3324 | 59 | * |
jksoft | 0:0a2f634d3324 | 60 | * FP<void,bool>fp; |
jksoft | 0:0a2f634d3324 | 61 | * DigitalOut myled(LED4); |
jksoft | 0:0a2f634d3324 | 62 | * |
jksoft | 0:0a2f634d3324 | 63 | * class Wrapper |
jksoft | 0:0a2f634d3324 | 64 | * { |
jksoft | 0:0a2f634d3324 | 65 | * public: |
jksoft | 0:0a2f634d3324 | 66 | * Wrapper(){} |
jksoft | 0:0a2f634d3324 | 67 | * |
jksoft | 0:0a2f634d3324 | 68 | * void handler(bool value) |
jksoft | 0:0a2f634d3324 | 69 | * { |
jksoft | 0:0a2f634d3324 | 70 | * myled = value; |
jksoft | 0:0a2f634d3324 | 71 | * return; |
jksoft | 0:0a2f634d3324 | 72 | * } |
jksoft | 0:0a2f634d3324 | 73 | * }; |
jksoft | 0:0a2f634d3324 | 74 | * |
jksoft | 0:0a2f634d3324 | 75 | * int main() |
jksoft | 0:0a2f634d3324 | 76 | * { |
jksoft | 0:0a2f634d3324 | 77 | * Wrapper wrapped; |
jksoft | 0:0a2f634d3324 | 78 | * fp.attach(&wrapped, &Wrapper::handler); |
jksoft | 0:0a2f634d3324 | 79 | * |
jksoft | 0:0a2f634d3324 | 80 | * while(1) |
jksoft | 0:0a2f634d3324 | 81 | * { |
jksoft | 0:0a2f634d3324 | 82 | * fp(1); |
jksoft | 0:0a2f634d3324 | 83 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 84 | * fp(0); |
jksoft | 0:0a2f634d3324 | 85 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 86 | * } |
jksoft | 0:0a2f634d3324 | 87 | * } |
jksoft | 0:0a2f634d3324 | 88 | * @endcode |
jksoft | 0:0a2f634d3324 | 89 | */ |
jksoft | 0:0a2f634d3324 | 90 | |
jksoft | 0:0a2f634d3324 | 91 | /** Example using the FP Class with member FP and member function |
jksoft | 0:0a2f634d3324 | 92 | * @code |
jksoft | 0:0a2f634d3324 | 93 | * #include "mbed.h" |
jksoft | 0:0a2f634d3324 | 94 | * #include "FP.h" |
jksoft | 0:0a2f634d3324 | 95 | * |
jksoft | 0:0a2f634d3324 | 96 | * DigitalOut myled(LED2); |
jksoft | 0:0a2f634d3324 | 97 | * |
jksoft | 0:0a2f634d3324 | 98 | * class Wrapper |
jksoft | 0:0a2f634d3324 | 99 | * { |
jksoft | 0:0a2f634d3324 | 100 | * public: |
jksoft | 0:0a2f634d3324 | 101 | * Wrapper() |
jksoft | 0:0a2f634d3324 | 102 | * { |
jksoft | 0:0a2f634d3324 | 103 | * fp.attach(this, &Wrapper::handler); |
jksoft | 0:0a2f634d3324 | 104 | * } |
jksoft | 0:0a2f634d3324 | 105 | * |
jksoft | 0:0a2f634d3324 | 106 | * void handler(bool value) |
jksoft | 0:0a2f634d3324 | 107 | * { |
jksoft | 0:0a2f634d3324 | 108 | * myled = value; |
jksoft | 0:0a2f634d3324 | 109 | * return; |
jksoft | 0:0a2f634d3324 | 110 | * } |
jksoft | 0:0a2f634d3324 | 111 | * |
jksoft | 0:0a2f634d3324 | 112 | * FP<void,bool>fp; |
jksoft | 0:0a2f634d3324 | 113 | * }; |
jksoft | 0:0a2f634d3324 | 114 | * |
jksoft | 0:0a2f634d3324 | 115 | * int main() |
jksoft | 0:0a2f634d3324 | 116 | * { |
jksoft | 0:0a2f634d3324 | 117 | * Wrapper wrapped; |
jksoft | 0:0a2f634d3324 | 118 | * |
jksoft | 0:0a2f634d3324 | 119 | * while(1) |
jksoft | 0:0a2f634d3324 | 120 | * { |
jksoft | 0:0a2f634d3324 | 121 | * wrapped.fp(1); |
jksoft | 0:0a2f634d3324 | 122 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 123 | * wrapped.fp(0); |
jksoft | 0:0a2f634d3324 | 124 | * wait(0.2); |
jksoft | 0:0a2f634d3324 | 125 | * } |
jksoft | 0:0a2f634d3324 | 126 | * } |
jksoft | 0:0a2f634d3324 | 127 | * @endcode |
jksoft | 0:0a2f634d3324 | 128 | */ |
jksoft | 0:0a2f634d3324 | 129 | |
jksoft | 0:0a2f634d3324 | 130 | /** |
jksoft | 0:0a2f634d3324 | 131 | * @class FP |
jksoft | 0:0a2f634d3324 | 132 | * @brief API for managing Function Pointers |
jksoft | 0:0a2f634d3324 | 133 | */ |
jksoft | 0:0a2f634d3324 | 134 | template<class retT, class argT> |
jksoft | 11:278ba4de2e99 | 135 | class _FP |
jksoft | 0:0a2f634d3324 | 136 | { |
jksoft | 0:0a2f634d3324 | 137 | public: |
jksoft | 0:0a2f634d3324 | 138 | /** Create the FP object - only one callback can be attached to the object, that is |
jksoft | 0:0a2f634d3324 | 139 | * a member function or a global function, not both at the same time |
jksoft | 0:0a2f634d3324 | 140 | */ |
jksoft | 11:278ba4de2e99 | 141 | _FP() |
jksoft | 0:0a2f634d3324 | 142 | { |
jksoft | 0:0a2f634d3324 | 143 | obj_callback = 0; |
jksoft | 0:0a2f634d3324 | 144 | c_callback = 0; |
jksoft | 0:0a2f634d3324 | 145 | } |
jksoft | 0:0a2f634d3324 | 146 | |
jksoft | 0:0a2f634d3324 | 147 | /** Add a callback function to the object |
jksoft | 0:0a2f634d3324 | 148 | * @param item - Address of the initialized object |
jksoft | 0:0a2f634d3324 | 149 | * @param member - Address of the member function (dont forget the scope that the function is defined in) |
jksoft | 0:0a2f634d3324 | 150 | */ |
jksoft | 0:0a2f634d3324 | 151 | template<class T> |
jksoft | 0:0a2f634d3324 | 152 | void attach(T *item, retT (T::*method)(argT)) |
jksoft | 0:0a2f634d3324 | 153 | { |
jksoft | 0:0a2f634d3324 | 154 | obj_callback = (FPtrDummy *)(item); |
jksoft | 0:0a2f634d3324 | 155 | method_callback = (retT (FPtrDummy::*)(argT))(method); |
jksoft | 0:0a2f634d3324 | 156 | return; |
jksoft | 0:0a2f634d3324 | 157 | } |
jksoft | 0:0a2f634d3324 | 158 | |
jksoft | 0:0a2f634d3324 | 159 | /** Add a callback function to the object |
jksoft | 0:0a2f634d3324 | 160 | * @param function - The address of a globally defined function |
jksoft | 0:0a2f634d3324 | 161 | */ |
jksoft | 0:0a2f634d3324 | 162 | void attach(retT (*function)(argT)) |
jksoft | 0:0a2f634d3324 | 163 | { |
jksoft | 0:0a2f634d3324 | 164 | c_callback = function; |
jksoft | 0:0a2f634d3324 | 165 | } |
jksoft | 0:0a2f634d3324 | 166 | |
jksoft | 0:0a2f634d3324 | 167 | /** Invoke the function attached to the class |
jksoft | 0:0a2f634d3324 | 168 | * @param arg - An argument that is passed into the function handler that is called |
jksoft | 0:0a2f634d3324 | 169 | * @return The return from the function hanlder called by this class |
jksoft | 0:0a2f634d3324 | 170 | */ |
jksoft | 0:0a2f634d3324 | 171 | retT operator()(argT arg) const |
jksoft | 0:0a2f634d3324 | 172 | { |
jksoft | 0:0a2f634d3324 | 173 | if( 0 != c_callback ) { |
jksoft | 0:0a2f634d3324 | 174 | return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg); |
jksoft | 0:0a2f634d3324 | 175 | } |
jksoft | 0:0a2f634d3324 | 176 | return (retT)0; |
jksoft | 0:0a2f634d3324 | 177 | } |
jksoft | 0:0a2f634d3324 | 178 | |
jksoft | 0:0a2f634d3324 | 179 | /** Determine if an callback is currently hooked |
jksoft | 0:0a2f634d3324 | 180 | * @return 1 if a method is hooked, 0 otherwise |
jksoft | 0:0a2f634d3324 | 181 | */ |
jksoft | 0:0a2f634d3324 | 182 | bool attached() |
jksoft | 0:0a2f634d3324 | 183 | { |
jksoft | 0:0a2f634d3324 | 184 | return obj_callback || c_callback; |
jksoft | 0:0a2f634d3324 | 185 | } |
jksoft | 0:0a2f634d3324 | 186 | |
jksoft | 0:0a2f634d3324 | 187 | /** Release a function from the callback hook |
jksoft | 0:0a2f634d3324 | 188 | */ |
jksoft | 0:0a2f634d3324 | 189 | void detach() |
jksoft | 0:0a2f634d3324 | 190 | { |
jksoft | 0:0a2f634d3324 | 191 | obj_callback = 0; |
jksoft | 0:0a2f634d3324 | 192 | c_callback = 0; |
jksoft | 0:0a2f634d3324 | 193 | } |
jksoft | 0:0a2f634d3324 | 194 | |
jksoft | 0:0a2f634d3324 | 195 | private: |
jksoft | 0:0a2f634d3324 | 196 | |
jksoft | 0:0a2f634d3324 | 197 | // empty type used for casting |
jksoft | 0:0a2f634d3324 | 198 | class FPtrDummy; |
jksoft | 0:0a2f634d3324 | 199 | |
jksoft | 0:0a2f634d3324 | 200 | FPtrDummy *obj_callback; |
jksoft | 0:0a2f634d3324 | 201 | |
jksoft | 0:0a2f634d3324 | 202 | /** |
jksoft | 0:0a2f634d3324 | 203 | * @union Funciton |
jksoft | 0:0a2f634d3324 | 204 | * @brief Member or global callback function |
jksoft | 0:0a2f634d3324 | 205 | */ |
jksoft | 0:0a2f634d3324 | 206 | union { |
jksoft | 0:0a2f634d3324 | 207 | retT (*c_callback)(argT); /*!< Footprint for a global function */ |
jksoft | 0:0a2f634d3324 | 208 | retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */ |
jksoft | 0:0a2f634d3324 | 209 | }; |
jksoft | 0:0a2f634d3324 | 210 | }; |
jksoft | 0:0a2f634d3324 | 211 | |
jksoft | 0:0a2f634d3324 | 212 | #endif |