mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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