![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a SLIP interface for the STM32F446RE Nucleo Board. It is designed to work specifically with the esp-link software for the ESP8266. The program is an example of a rest command.
FP.h@0:70a6082c1bf7, 2016-07-14 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Thu Jul 14 20:38:18 2016 +0000
- Revision:
- 0:70a6082c1bf7
first commit. Rest example working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ShaneKirkbride | 0:70a6082c1bf7 | 1 | /** |
ShaneKirkbride | 0:70a6082c1bf7 | 2 | * @file FP.h |
ShaneKirkbride | 0:70a6082c1bf7 | 3 | * @brief Core Utility - Templated Function Pointer Class |
ShaneKirkbride | 0:70a6082c1bf7 | 4 | * @author sam grove |
ShaneKirkbride | 0:70a6082c1bf7 | 5 | * @version 1.0 |
ShaneKirkbride | 0:70a6082c1bf7 | 6 | * @see |
ShaneKirkbride | 0:70a6082c1bf7 | 7 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 8 | * Copyright (c) 2013 |
ShaneKirkbride | 0:70a6082c1bf7 | 9 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ShaneKirkbride | 0:70a6082c1bf7 | 11 | * you may not use this file except in compliance with the License. |
ShaneKirkbride | 0:70a6082c1bf7 | 12 | * You may obtain a copy of the License at |
ShaneKirkbride | 0:70a6082c1bf7 | 13 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ShaneKirkbride | 0:70a6082c1bf7 | 15 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 16 | * Unless required by applicable law or agreed to in writing, software |
ShaneKirkbride | 0:70a6082c1bf7 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ShaneKirkbride | 0:70a6082c1bf7 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ShaneKirkbride | 0:70a6082c1bf7 | 19 | * See the License for the specific language governing permissions and |
ShaneKirkbride | 0:70a6082c1bf7 | 20 | * limitations under the License. |
ShaneKirkbride | 0:70a6082c1bf7 | 21 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | #ifndef FP_H |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | #define FP_H |
ShaneKirkbride | 0:70a6082c1bf7 | 25 | |
ShaneKirkbride | 0:70a6082c1bf7 | 26 | /** Example using the FP Class with global functions |
ShaneKirkbride | 0:70a6082c1bf7 | 27 | * @code |
ShaneKirkbride | 0:70a6082c1bf7 | 28 | * #include "mbed.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 29 | * #include "FP.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 30 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 31 | * FP<void,bool>fp; |
ShaneKirkbride | 0:70a6082c1bf7 | 32 | * DigitalOut myled(LED1); |
ShaneKirkbride | 0:70a6082c1bf7 | 33 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 34 | * void handler(bool value) |
ShaneKirkbride | 0:70a6082c1bf7 | 35 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 36 | * myled = value; |
ShaneKirkbride | 0:70a6082c1bf7 | 37 | * return; |
ShaneKirkbride | 0:70a6082c1bf7 | 38 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 39 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 40 | * int main() |
ShaneKirkbride | 0:70a6082c1bf7 | 41 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 42 | * fp.attach(&handler); |
ShaneKirkbride | 0:70a6082c1bf7 | 43 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 44 | * while(1) |
ShaneKirkbride | 0:70a6082c1bf7 | 45 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 46 | * fp(1); |
ShaneKirkbride | 0:70a6082c1bf7 | 47 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 48 | * fp(0); |
ShaneKirkbride | 0:70a6082c1bf7 | 49 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 50 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 51 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 52 | * @endcode |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 54 | |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | /** Example using the FP Class with different class member functions |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | * @code |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | * #include "mbed.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | * #include "FP.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | * FP<void,bool>fp; |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | * DigitalOut myled(LED4); |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | * class Wrapper |
ShaneKirkbride | 0:70a6082c1bf7 | 64 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | * public: |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | * Wrapper(){} |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | * void handler(bool value) |
ShaneKirkbride | 0:70a6082c1bf7 | 69 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 70 | * myled = value; |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | * return; |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 73 | * }; |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | * int main() |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | * Wrapper wrapped; |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | * fp.attach(&wrapped, &Wrapper::handler); |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | * while(1) |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | * fp(1); |
ShaneKirkbride | 0:70a6082c1bf7 | 83 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | * fp(0); |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 88 | * @endcode |
ShaneKirkbride | 0:70a6082c1bf7 | 89 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 90 | |
ShaneKirkbride | 0:70a6082c1bf7 | 91 | /** Example using the FP Class with member FP and member function |
ShaneKirkbride | 0:70a6082c1bf7 | 92 | * @code |
ShaneKirkbride | 0:70a6082c1bf7 | 93 | * #include "mbed.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 94 | * #include "FP.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 95 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 96 | * DigitalOut myled(LED2); |
ShaneKirkbride | 0:70a6082c1bf7 | 97 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 98 | * class Wrapper |
ShaneKirkbride | 0:70a6082c1bf7 | 99 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 100 | * public: |
ShaneKirkbride | 0:70a6082c1bf7 | 101 | * Wrapper() |
ShaneKirkbride | 0:70a6082c1bf7 | 102 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 103 | * fp.attach(this, &Wrapper::handler); |
ShaneKirkbride | 0:70a6082c1bf7 | 104 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 105 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 106 | * void handler(bool value) |
ShaneKirkbride | 0:70a6082c1bf7 | 107 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 108 | * myled = value; |
ShaneKirkbride | 0:70a6082c1bf7 | 109 | * return; |
ShaneKirkbride | 0:70a6082c1bf7 | 110 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 111 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 112 | * FP<void,bool>fp; |
ShaneKirkbride | 0:70a6082c1bf7 | 113 | * }; |
ShaneKirkbride | 0:70a6082c1bf7 | 114 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 115 | * int main() |
ShaneKirkbride | 0:70a6082c1bf7 | 116 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 117 | * Wrapper wrapped; |
ShaneKirkbride | 0:70a6082c1bf7 | 118 | * |
ShaneKirkbride | 0:70a6082c1bf7 | 119 | * while(1) |
ShaneKirkbride | 0:70a6082c1bf7 | 120 | * { |
ShaneKirkbride | 0:70a6082c1bf7 | 121 | * wrapped.fp(1); |
ShaneKirkbride | 0:70a6082c1bf7 | 122 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 123 | * wrapped.fp(0); |
ShaneKirkbride | 0:70a6082c1bf7 | 124 | * wait(0.2); |
ShaneKirkbride | 0:70a6082c1bf7 | 125 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 126 | * } |
ShaneKirkbride | 0:70a6082c1bf7 | 127 | * @endcode |
ShaneKirkbride | 0:70a6082c1bf7 | 128 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 129 | |
ShaneKirkbride | 0:70a6082c1bf7 | 130 | /** |
ShaneKirkbride | 0:70a6082c1bf7 | 131 | * @class FP |
ShaneKirkbride | 0:70a6082c1bf7 | 132 | * @brief API abstraction for a Function Pointers |
ShaneKirkbride | 0:70a6082c1bf7 | 133 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 134 | template<class retT, class argT> |
ShaneKirkbride | 0:70a6082c1bf7 | 135 | class FP |
ShaneKirkbride | 0:70a6082c1bf7 | 136 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 137 | public: |
ShaneKirkbride | 0:70a6082c1bf7 | 138 | /** Create the FP object |
ShaneKirkbride | 0:70a6082c1bf7 | 139 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 140 | FP(); |
ShaneKirkbride | 0:70a6082c1bf7 | 141 | |
ShaneKirkbride | 0:70a6082c1bf7 | 142 | /** Add a callback function to the class |
ShaneKirkbride | 0:70a6082c1bf7 | 143 | * @param item - Address of the initialized class |
ShaneKirkbride | 0:70a6082c1bf7 | 144 | * @param member - Address of the member function (dont forget the scope that the function is defined in) |
ShaneKirkbride | 0:70a6082c1bf7 | 145 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 146 | template<class T> |
ShaneKirkbride | 0:70a6082c1bf7 | 147 | void attach(T *item, retT (T::*method)(argT)) |
ShaneKirkbride | 0:70a6082c1bf7 | 148 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 149 | obj_callback = (FPtrDummy *)(item); |
ShaneKirkbride | 0:70a6082c1bf7 | 150 | method_callback = (retT (FPtrDummy::*)(argT))(method); |
ShaneKirkbride | 0:70a6082c1bf7 | 151 | return; |
ShaneKirkbride | 0:70a6082c1bf7 | 152 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 153 | |
ShaneKirkbride | 0:70a6082c1bf7 | 154 | /** Add a callback function to the class |
ShaneKirkbride | 0:70a6082c1bf7 | 155 | * @param function - The address of a globally defined function |
ShaneKirkbride | 0:70a6082c1bf7 | 156 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 157 | void attach(retT (*function)(argT)); |
ShaneKirkbride | 0:70a6082c1bf7 | 158 | |
ShaneKirkbride | 0:70a6082c1bf7 | 159 | /** Invoke the function attached to the class |
ShaneKirkbride | 0:70a6082c1bf7 | 160 | * @param arg - An argument that is passed into the function handler that is called |
ShaneKirkbride | 0:70a6082c1bf7 | 161 | * @return The return from the function hanlder called by this class |
ShaneKirkbride | 0:70a6082c1bf7 | 162 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 163 | retT operator()(argT arg) const; |
ShaneKirkbride | 0:70a6082c1bf7 | 164 | |
ShaneKirkbride | 0:70a6082c1bf7 | 165 | bool attached(); |
ShaneKirkbride | 0:70a6082c1bf7 | 166 | |
ShaneKirkbride | 0:70a6082c1bf7 | 167 | void detach(); |
ShaneKirkbride | 0:70a6082c1bf7 | 168 | |
ShaneKirkbride | 0:70a6082c1bf7 | 169 | private: |
ShaneKirkbride | 0:70a6082c1bf7 | 170 | |
ShaneKirkbride | 0:70a6082c1bf7 | 171 | // empty type used for casting |
ShaneKirkbride | 0:70a6082c1bf7 | 172 | class FPtrDummy; |
ShaneKirkbride | 0:70a6082c1bf7 | 173 | |
ShaneKirkbride | 0:70a6082c1bf7 | 174 | FPtrDummy *obj_callback; |
ShaneKirkbride | 0:70a6082c1bf7 | 175 | |
ShaneKirkbride | 0:70a6082c1bf7 | 176 | /** |
ShaneKirkbride | 0:70a6082c1bf7 | 177 | * @union Funciton |
ShaneKirkbride | 0:70a6082c1bf7 | 178 | * @brief Member or global callback function |
ShaneKirkbride | 0:70a6082c1bf7 | 179 | */ |
ShaneKirkbride | 0:70a6082c1bf7 | 180 | union |
ShaneKirkbride | 0:70a6082c1bf7 | 181 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 182 | retT (*c_callback)(argT); /*!< Footprint for a global function */ |
ShaneKirkbride | 0:70a6082c1bf7 | 183 | retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */ |
ShaneKirkbride | 0:70a6082c1bf7 | 184 | }; |
ShaneKirkbride | 0:70a6082c1bf7 | 185 | }; |
ShaneKirkbride | 0:70a6082c1bf7 | 186 | |
ShaneKirkbride | 0:70a6082c1bf7 | 187 | #endif |