CITY3032-wifi-mqtt

Committer:
reedas
Date:
Sat Nov 13 12:02:49 2021 +0000
Revision:
5:f62a9e4a499a
Parent:
3:62825c5f3cd7
trying to include mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
reedas 3:62825c5f3cd7 1 /*******************************************************************************
reedas 3:62825c5f3cd7 2 * Copyright (c) 2013, 2014
reedas 3:62825c5f3cd7 3 *
reedas 3:62825c5f3cd7 4 * All rights reserved. This program and the accompanying materials
reedas 3:62825c5f3cd7 5 * are made available under the terms of the Eclipse Public License v1.0
reedas 3:62825c5f3cd7 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
reedas 3:62825c5f3cd7 7 *
reedas 3:62825c5f3cd7 8 * The Eclipse Public License is available at
reedas 3:62825c5f3cd7 9 * http://www.eclipse.org/legal/epl-v10.html
reedas 3:62825c5f3cd7 10 * and the Eclipse Distribution License is available at
reedas 3:62825c5f3cd7 11 * http://www.eclipse.org/org/documents/edl-v10.php.
reedas 3:62825c5f3cd7 12 *
reedas 3:62825c5f3cd7 13 * Contributors:
reedas 3:62825c5f3cd7 14 * Sam Grove - initial API and implementation and/or initial documentation
reedas 3:62825c5f3cd7 15 * Ian Craggs - added attached and detached member functions
reedas 3:62825c5f3cd7 16 * Sam Grove - removed need for FP.cpp
reedas 3:62825c5f3cd7 17 *******************************************************************************/
reedas 3:62825c5f3cd7 18
reedas 3:62825c5f3cd7 19 #ifndef FP_H
reedas 3:62825c5f3cd7 20 #define FP_H
reedas 3:62825c5f3cd7 21
reedas 3:62825c5f3cd7 22 /** Example using the FP Class with global functions
reedas 3:62825c5f3cd7 23 * @code
reedas 3:62825c5f3cd7 24 * #include "mbed.h"
reedas 3:62825c5f3cd7 25 * #include "FP.h"
reedas 3:62825c5f3cd7 26 *
reedas 3:62825c5f3cd7 27 * FP<void,bool>fp;
reedas 3:62825c5f3cd7 28 * DigitalOut myled(LED1);
reedas 3:62825c5f3cd7 29 *
reedas 3:62825c5f3cd7 30 * void handler(bool value)
reedas 3:62825c5f3cd7 31 * {
reedas 3:62825c5f3cd7 32 * myled = value;
reedas 3:62825c5f3cd7 33 * return;
reedas 3:62825c5f3cd7 34 * }
reedas 3:62825c5f3cd7 35 *
reedas 3:62825c5f3cd7 36 * int main()
reedas 3:62825c5f3cd7 37 * {
reedas 3:62825c5f3cd7 38 * fp.attach(&handler);
reedas 3:62825c5f3cd7 39 *
reedas 3:62825c5f3cd7 40 * while(1)
reedas 3:62825c5f3cd7 41 * {
reedas 3:62825c5f3cd7 42 * fp(1);
reedas 3:62825c5f3cd7 43 * wait(0.2);
reedas 3:62825c5f3cd7 44 * fp(0);
reedas 3:62825c5f3cd7 45 * wait(0.2);
reedas 3:62825c5f3cd7 46 * }
reedas 3:62825c5f3cd7 47 * }
reedas 3:62825c5f3cd7 48 * @endcode
reedas 3:62825c5f3cd7 49 */
reedas 3:62825c5f3cd7 50
reedas 3:62825c5f3cd7 51 /** Example using the FP Class with different class member functions
reedas 3:62825c5f3cd7 52 * @code
reedas 3:62825c5f3cd7 53 * #include "mbed.h"
reedas 3:62825c5f3cd7 54 * #include "FP.h"
reedas 3:62825c5f3cd7 55 *
reedas 3:62825c5f3cd7 56 * FP<void,bool>fp;
reedas 3:62825c5f3cd7 57 * DigitalOut myled(LED4);
reedas 3:62825c5f3cd7 58 *
reedas 3:62825c5f3cd7 59 * class Wrapper
reedas 3:62825c5f3cd7 60 * {
reedas 3:62825c5f3cd7 61 * public:
reedas 3:62825c5f3cd7 62 * Wrapper(){}
reedas 3:62825c5f3cd7 63 *
reedas 3:62825c5f3cd7 64 * void handler(bool value)
reedas 3:62825c5f3cd7 65 * {
reedas 3:62825c5f3cd7 66 * myled = value;
reedas 3:62825c5f3cd7 67 * return;
reedas 3:62825c5f3cd7 68 * }
reedas 3:62825c5f3cd7 69 * };
reedas 3:62825c5f3cd7 70 *
reedas 3:62825c5f3cd7 71 * int main()
reedas 3:62825c5f3cd7 72 * {
reedas 3:62825c5f3cd7 73 * Wrapper wrapped;
reedas 3:62825c5f3cd7 74 * fp.attach(&wrapped, &Wrapper::handler);
reedas 3:62825c5f3cd7 75 *
reedas 3:62825c5f3cd7 76 * while(1)
reedas 3:62825c5f3cd7 77 * {
reedas 3:62825c5f3cd7 78 * fp(1);
reedas 3:62825c5f3cd7 79 * wait(0.2);
reedas 3:62825c5f3cd7 80 * fp(0);
reedas 3:62825c5f3cd7 81 * wait(0.2);
reedas 3:62825c5f3cd7 82 * }
reedas 3:62825c5f3cd7 83 * }
reedas 3:62825c5f3cd7 84 * @endcode
reedas 3:62825c5f3cd7 85 */
reedas 3:62825c5f3cd7 86
reedas 3:62825c5f3cd7 87 /** Example using the FP Class with member FP and member function
reedas 3:62825c5f3cd7 88 * @code
reedas 3:62825c5f3cd7 89 * #include "mbed.h"
reedas 3:62825c5f3cd7 90 * #include "FP.h"
reedas 3:62825c5f3cd7 91 *
reedas 3:62825c5f3cd7 92 * DigitalOut myled(LED2);
reedas 3:62825c5f3cd7 93 *
reedas 3:62825c5f3cd7 94 * class Wrapper
reedas 3:62825c5f3cd7 95 * {
reedas 3:62825c5f3cd7 96 * public:
reedas 3:62825c5f3cd7 97 * Wrapper()
reedas 3:62825c5f3cd7 98 * {
reedas 3:62825c5f3cd7 99 * fp.attach(this, &Wrapper::handler);
reedas 3:62825c5f3cd7 100 * }
reedas 3:62825c5f3cd7 101 *
reedas 3:62825c5f3cd7 102 * void handler(bool value)
reedas 3:62825c5f3cd7 103 * {
reedas 3:62825c5f3cd7 104 * myled = value;
reedas 3:62825c5f3cd7 105 * return;
reedas 3:62825c5f3cd7 106 * }
reedas 3:62825c5f3cd7 107 *
reedas 3:62825c5f3cd7 108 * FP<void,bool>fp;
reedas 3:62825c5f3cd7 109 * };
reedas 3:62825c5f3cd7 110 *
reedas 3:62825c5f3cd7 111 * int main()
reedas 3:62825c5f3cd7 112 * {
reedas 3:62825c5f3cd7 113 * Wrapper wrapped;
reedas 3:62825c5f3cd7 114 *
reedas 3:62825c5f3cd7 115 * while(1)
reedas 3:62825c5f3cd7 116 * {
reedas 3:62825c5f3cd7 117 * wrapped.fp(1);
reedas 3:62825c5f3cd7 118 * wait(0.2);
reedas 3:62825c5f3cd7 119 * wrapped.fp(0);
reedas 3:62825c5f3cd7 120 * wait(0.2);
reedas 3:62825c5f3cd7 121 * }
reedas 3:62825c5f3cd7 122 * }
reedas 3:62825c5f3cd7 123 * @endcode
reedas 3:62825c5f3cd7 124 */
reedas 3:62825c5f3cd7 125
reedas 3:62825c5f3cd7 126 /**
reedas 3:62825c5f3cd7 127 * @class FP
reedas 3:62825c5f3cd7 128 * @brief API for managing Function Pointers
reedas 3:62825c5f3cd7 129 */
reedas 3:62825c5f3cd7 130 template<class retT, class argT>
reedas 3:62825c5f3cd7 131 class FP
reedas 3:62825c5f3cd7 132 {
reedas 3:62825c5f3cd7 133 public:
reedas 3:62825c5f3cd7 134 /** Create the FP object - only one callback can be attached to the object, that is
reedas 3:62825c5f3cd7 135 * a member function or a global function, not both at the same time
reedas 3:62825c5f3cd7 136 */
reedas 3:62825c5f3cd7 137 FP()
reedas 3:62825c5f3cd7 138 {
reedas 3:62825c5f3cd7 139 obj_callback = 0;
reedas 3:62825c5f3cd7 140 c_callback = 0;
reedas 3:62825c5f3cd7 141 }
reedas 3:62825c5f3cd7 142
reedas 3:62825c5f3cd7 143 /** Add a callback function to the object
reedas 3:62825c5f3cd7 144 * @param item - Address of the initialized object
reedas 3:62825c5f3cd7 145 * @param member - Address of the member function (dont forget the scope that the function is defined in)
reedas 3:62825c5f3cd7 146 */
reedas 3:62825c5f3cd7 147 template<class T>
reedas 3:62825c5f3cd7 148 void attach(T *item, retT (T::*method)(argT))
reedas 3:62825c5f3cd7 149 {
reedas 3:62825c5f3cd7 150 obj_callback = (FPtrDummy *)(item);
reedas 3:62825c5f3cd7 151 method_callback = (retT (FPtrDummy::*)(argT))(method);
reedas 3:62825c5f3cd7 152 return;
reedas 3:62825c5f3cd7 153 }
reedas 3:62825c5f3cd7 154
reedas 3:62825c5f3cd7 155 /** Add a callback function to the object
reedas 3:62825c5f3cd7 156 * @param function - The address of a globally defined function
reedas 3:62825c5f3cd7 157 */
reedas 3:62825c5f3cd7 158 void attach(retT (*function)(argT))
reedas 3:62825c5f3cd7 159 {
reedas 3:62825c5f3cd7 160 c_callback = function;
reedas 3:62825c5f3cd7 161 }
reedas 3:62825c5f3cd7 162
reedas 3:62825c5f3cd7 163 /** Invoke the function attached to the class
reedas 3:62825c5f3cd7 164 * @param arg - An argument that is passed into the function handler that is called
reedas 3:62825c5f3cd7 165 * @return The return from the function hanlder called by this class
reedas 3:62825c5f3cd7 166 */
reedas 3:62825c5f3cd7 167 retT operator()(argT arg) const
reedas 3:62825c5f3cd7 168 {
reedas 3:62825c5f3cd7 169 if( 0 != c_callback ) {
reedas 3:62825c5f3cd7 170 return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg);
reedas 3:62825c5f3cd7 171 }
reedas 3:62825c5f3cd7 172 return (retT)0;
reedas 3:62825c5f3cd7 173 }
reedas 3:62825c5f3cd7 174
reedas 3:62825c5f3cd7 175 /** Determine if an callback is currently hooked
reedas 3:62825c5f3cd7 176 * @return 1 if a method is hooked, 0 otherwise
reedas 3:62825c5f3cd7 177 */
reedas 3:62825c5f3cd7 178 bool attached()
reedas 3:62825c5f3cd7 179 {
reedas 3:62825c5f3cd7 180 return obj_callback || c_callback;
reedas 3:62825c5f3cd7 181 }
reedas 3:62825c5f3cd7 182
reedas 3:62825c5f3cd7 183 /** Release a function from the callback hook
reedas 3:62825c5f3cd7 184 */
reedas 3:62825c5f3cd7 185 void detach()
reedas 3:62825c5f3cd7 186 {
reedas 3:62825c5f3cd7 187 obj_callback = 0;
reedas 3:62825c5f3cd7 188 c_callback = 0;
reedas 3:62825c5f3cd7 189 }
reedas 3:62825c5f3cd7 190
reedas 3:62825c5f3cd7 191 private:
reedas 3:62825c5f3cd7 192
reedas 3:62825c5f3cd7 193 // empty type used for casting
reedas 3:62825c5f3cd7 194 class FPtrDummy;
reedas 3:62825c5f3cd7 195
reedas 3:62825c5f3cd7 196 FPtrDummy *obj_callback;
reedas 3:62825c5f3cd7 197
reedas 3:62825c5f3cd7 198 /**
reedas 3:62825c5f3cd7 199 * @union Funciton
reedas 3:62825c5f3cd7 200 * @brief Member or global callback function
reedas 3:62825c5f3cd7 201 */
reedas 3:62825c5f3cd7 202 union {
reedas 3:62825c5f3cd7 203 retT (*c_callback)(argT); /*!< Footprint for a global function */
reedas 3:62825c5f3cd7 204 retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */
reedas 3:62825c5f3cd7 205 };
reedas 3:62825c5f3cd7 206 };
reedas 3:62825c5f3cd7 207
reedas 3:62825c5f3cd7 208 #endif