Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ticker.h Source File

Ticker.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_TICKER_H
00017 #define MBED_TICKER_H
00018 
00019 #include "drivers/TimerEvent.h"
00020 #include "platform/Callback.h"
00021 #include "platform/mbed_toolchain.h"
00022 #include "platform/NonCopyable.h"
00023 #include "platform/mbed_power_mgmt.h"
00024 #include "hal/lp_ticker_api.h"
00025 #include "platform/mbed_critical.h"
00026 
00027 namespace mbed {
00028 /** \addtogroup drivers */
00029 
00030 /** A Ticker is used to call a function at a recurring interval
00031  *
00032  *  You can use as many separate Ticker objects as you require.
00033  *
00034  * @note Synchronization level: Interrupt safe
00035  *
00036  * Example:
00037  * @code
00038  * // Toggle the blinking led after 5 seconds
00039  *
00040  * #include "mbed.h"
00041  *
00042  * Ticker timer;
00043  * DigitalOut led1(LED1);
00044  * DigitalOut led2(LED2);
00045  *
00046  * int flip = 0;
00047  *
00048  * void attime() {
00049  *     flip = !flip;
00050  * }
00051  *
00052  * int main() {
00053  *     timer.attach(&attime, 5);
00054  *     while(1) {
00055  *         if(flip == 0) {
00056  *             led1 = !led1;
00057  *         } else {
00058  *             led2 = !led2;
00059  *         }
00060  *         wait(0.2);
00061  *     }
00062  * }
00063  * @endcode
00064  * @ingroup drivers
00065  */
00066 class Ticker : public TimerEvent, private NonCopyable<Ticker> {
00067 
00068 public:
00069     Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) {
00070     }
00071 
00072     // When low power ticker is in use, then do not disable deep-sleep.
00073     Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true)  {
00074 #if DEVICE_LOWPOWERTIMER
00075         _lock_deepsleep = (data != get_lp_ticker_data());
00076 #endif
00077     }
00078 
00079     /** Attach a function to be called by the Ticker, specifying the interval in seconds
00080      *
00081      *  @param func pointer to the function to be called
00082      *  @param t the time between calls in seconds
00083      */
00084     void attach(Callback<void()> func, float t) {
00085         attach_us(func, t * 1000000.0f);
00086     }
00087 
00088     /** Attach a member function to be called by the Ticker, specifying the interval in seconds
00089      *
00090      *  @param obj pointer to the object to call the member function on
00091      *  @param method pointer to the member function to be called
00092      *  @param t the time between calls in seconds
00093      *  @deprecated
00094      *      The attach function does not support cv-qualifiers. Replaced by
00095      *      attach(callback(obj, method), t).
00096      */
00097     template<typename T, typename M>
00098     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00099         "The attach function does not support cv-qualifiers. Replaced by "
00100         "attach(callback(obj, method), t).")
00101     void attach(T *obj, M method, float t) {
00102         attach(callback(obj, method), t);
00103     }
00104 
00105     /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
00106      *
00107      *  @param func pointer to the function to be called
00108      *  @param t the time between calls in micro-seconds
00109      *
00110      *  @note setting @a t to a value shorter that it takes to process the ticker callback
00111      *  will cause the system to hang. Ticker callback will be called constantly with no time
00112      *  for threads scheduling.
00113      *
00114      */
00115     void attach_us(Callback<void()> func, us_timestamp_t t) {
00116         core_util_critical_section_enter();
00117         // lock only for the initial callback setup and this is not low power ticker
00118         if(!_function && _lock_deepsleep) {
00119             sleep_manager_lock_deep_sleep();
00120         }
00121         _function = func;
00122         setup(t);
00123         core_util_critical_section_exit();
00124     }
00125 
00126     /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
00127      *
00128      *  @param obj pointer to the object to call the member function on
00129      *  @param method pointer to the member function to be called
00130      *  @param t the time between calls in micro-seconds
00131      *  @deprecated
00132      *      The attach_us function does not support cv-qualifiers. Replaced by
00133      *      attach_us(callback(obj, method), t).
00134      */
00135     template<typename T, typename M>
00136     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00137         "The attach_us function does not support cv-qualifiers. Replaced by "
00138         "attach_us(callback(obj, method), t).")
00139     void attach_us(T *obj, M method, us_timestamp_t t) {
00140         attach_us(Callback<void()>(obj, method), t);
00141     }
00142 
00143     virtual ~Ticker() {
00144         detach();
00145     }
00146 
00147     /** Detach the function
00148      */
00149     void detach();
00150 
00151 protected:
00152     void setup(us_timestamp_t t);
00153     virtual void handler();
00154 
00155 protected:
00156     us_timestamp_t         _delay;  /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
00157     Callback<void()>    _function;  /**< Callback. */
00158     bool          _lock_deepsleep;  /**< Flag which indicates if deep-sleep should be disabled. */
00159 };
00160 
00161 } // namespace mbed
00162 
00163 #endif