Rtos API example

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_sleep.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         data->interface->init();
00075 #if DEVICE_LOWPOWERTIMER
00076         _lock_deepsleep = (data != get_lp_ticker_data());
00077 #endif
00078     }
00079 
00080     /** Attach a function to be called by the Ticker, specifying the interval in seconds
00081      *
00082      *  @param func pointer to the function to be called
00083      *  @param t the time between calls in seconds
00084      */
00085     void attach(Callback<void()> func, float t) {
00086         attach_us(func, t * 1000000.0f);
00087     }
00088 
00089     /** Attach a member function to be called by the Ticker, specifying the interval in seconds
00090      *
00091      *  @param obj pointer to the object to call the member function on
00092      *  @param method pointer to the member function to be called
00093      *  @param t the time between calls in seconds
00094      *  @deprecated
00095      *      The attach function does not support cv-qualifiers. Replaced by
00096      *      attach(callback(obj, method), t).
00097      */
00098     template<typename T, typename M>
00099     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00100         "The attach function does not support cv-qualifiers. Replaced by "
00101         "attach(callback(obj, method), t).")
00102     void attach(T *obj, M method, float t) {
00103         attach(callback(obj, method), t);
00104     }
00105 
00106     /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
00107      *
00108      *  @param func pointer to the function to be called
00109      *  @param t the time between calls in micro-seconds
00110      *
00111      *  @note setting @a t to a value shorter that it takes to process the ticker callback
00112      *  will cause the system to hang. Ticker callback will be called constantly with no time
00113      *  for threads scheduling.
00114      *
00115      */
00116     void attach_us(Callback<void()> func, us_timestamp_t t) {
00117         core_util_critical_section_enter();
00118         // lock only for the initial callback setup and this is not low power ticker
00119         if(!_function && _lock_deepsleep) {
00120             sleep_manager_lock_deep_sleep();
00121         }
00122         _function = func;
00123         setup(t);
00124         core_util_critical_section_exit();
00125     }
00126 
00127     /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
00128      *
00129      *  @param obj pointer to the object to call the member function on
00130      *  @param method pointer to the member function to be called
00131      *  @param t the time between calls in micro-seconds
00132      *  @deprecated
00133      *      The attach_us function does not support cv-qualifiers. Replaced by
00134      *      attach_us(callback(obj, method), t).
00135      */
00136     template<typename T, typename M>
00137     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00138         "The attach_us function does not support cv-qualifiers. Replaced by "
00139         "attach_us(callback(obj, method), t).")
00140     void attach_us(T *obj, M method, us_timestamp_t t) {
00141         attach_us(Callback<void()>(obj, method), t);
00142     }
00143 
00144     virtual ~Ticker() {
00145         detach();
00146     }
00147 
00148     /** Detach the function
00149      */
00150     void detach();
00151 
00152 protected:
00153     void setup(us_timestamp_t t);
00154     virtual void handler();
00155 
00156 protected:
00157     us_timestamp_t         _delay;  /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
00158     Callback<void()>    _function;  /**< Callback. */
00159     bool          _lock_deepsleep;  /**< Flag which indicates if deep-sleep should be disabled. */
00160 };
00161 
00162 } // namespace mbed
00163 
00164 #endif