2013-08-08 09:58:34 +00:00
|
|
|
/* mbed Microcontroller Library
|
2019-07-11 15:23:39 +00:00
|
|
|
* Copyright (c) 2006-2019 ARM Limited
|
2018-11-09 11:22:52 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_TICKER_H
|
|
|
|
#define MBED_TICKER_H
|
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "drivers/TimerEvent.h"
|
|
|
|
#include "platform/Callback.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_toolchain.h"
|
drivers: Mark non identity types as non copyable with the NonCopyable traits.
Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial.
2017-06-20 13:26:29 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2017-09-20 09:07:42 +00:00
|
|
|
#include "hal/lp_ticker_api.h"
|
2013-08-08 09:58:34 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2019-07-11 15:23:39 +00:00
|
|
|
/** \ingroup drivers */
|
|
|
|
/** \addtogroup drivers-public-api */
|
|
|
|
/** @{*/
|
|
|
|
/**
|
|
|
|
* \defgroup drivers_Ticker Ticker class
|
|
|
|
* @{
|
|
|
|
*/
|
2013-08-08 09:58:34 +00:00
|
|
|
|
|
|
|
/** A Ticker is used to call a function at a recurring interval
|
|
|
|
*
|
2017-09-20 09:07:42 +00:00
|
|
|
* You can use as many separate Ticker objects as you require.
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @note Synchronization level: Interrupt safe
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2013-08-08 09:58:34 +00:00
|
|
|
* Example:
|
|
|
|
* @code
|
2018-10-12 16:16:32 +00:00
|
|
|
* // Toggle the blinking LED after 5 seconds
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
|
|
|
* #include "mbed.h"
|
|
|
|
*
|
|
|
|
* Ticker timer;
|
|
|
|
* DigitalOut led1(LED1);
|
|
|
|
* DigitalOut led2(LED2);
|
|
|
|
*
|
|
|
|
* int flip = 0;
|
|
|
|
*
|
|
|
|
* void attime() {
|
|
|
|
* flip = !flip;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* timer.attach(&attime, 5);
|
|
|
|
* while(1) {
|
|
|
|
* if(flip == 0) {
|
|
|
|
* led1 = !led1;
|
|
|
|
* } else {
|
|
|
|
* led2 = !led2;
|
|
|
|
* }
|
|
|
|
* wait(0.2);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*/
|
drivers: Mark non identity types as non copyable with the NonCopyable traits.
Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial.
2017-06-20 13:26:29 +00:00
|
|
|
class Ticker : public TimerEvent, private NonCopyable<Ticker> {
|
2013-08-08 09:58:34 +00:00
|
|
|
|
|
|
|
public:
|
2019-07-11 15:23:39 +00:00
|
|
|
Ticker();
|
2015-04-23 13:56:34 +00:00
|
|
|
|
2018-10-12 16:16:32 +00:00
|
|
|
// When low power ticker is in use, then do not disable deep sleep.
|
2019-07-11 15:23:39 +00:00
|
|
|
Ticker(const ticker_data_t *data);
|
2013-08-08 09:58:34 +00:00
|
|
|
|
2017-09-20 09:07:42 +00:00
|
|
|
/** Attach a function to be called by the Ticker, specifying the interval in seconds
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
2016-05-15 21:17:47 +00:00
|
|
|
* @param func pointer to the function to be called
|
2013-08-08 09:58:34 +00:00
|
|
|
* @param t the time between calls in seconds
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
void attach(Callback<void()> func, float t)
|
|
|
|
{
|
2016-05-15 21:17:47 +00:00
|
|
|
attach_us(func, t * 1000000.0f);
|
2013-08-08 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 09:07:42 +00:00
|
|
|
/** Attach a member function to be called by the Ticker, specifying the interval in seconds
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
2016-05-15 21:17:47 +00:00
|
|
|
* @param obj pointer to the object to call the member function on
|
|
|
|
* @param method pointer to the member function to be called
|
2013-08-08 09:58:34 +00:00
|
|
|
* @param t the time between calls in seconds
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
* @deprecated
|
|
|
|
* The attach function does not support cv-qualifiers. Replaced by
|
|
|
|
* attach(callback(obj, method), t).
|
2013-08-08 09:58:34 +00:00
|
|
|
*/
|
2016-05-15 21:17:47 +00:00
|
|
|
template<typename T, typename M>
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
2018-05-24 15:58:14 +00:00
|
|
|
"The attach function does not support cv-qualifiers. Replaced by "
|
|
|
|
"attach(callback(obj, method), t).")
|
|
|
|
void attach(T *obj, M method, float t)
|
|
|
|
{
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
attach(callback(obj, method), t);
|
2013-08-08 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:16:32 +00:00
|
|
|
/** Attach a function to be called by the Ticker, specifying the interval in microseconds
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @param func pointer to the function to be called
|
2013-08-08 09:58:34 +00:00
|
|
|
* @param t the time between calls in micro-seconds
|
2017-09-07 08:19:39 +00:00
|
|
|
*
|
2018-10-12 16:16:32 +00:00
|
|
|
* @note setting @a t to a value shorter than it takes to process the ticker callback
|
|
|
|
* causes the system to hang. Ticker callback is called constantly with no time
|
2017-09-07 08:19:39 +00:00
|
|
|
* for threads scheduling.
|
|
|
|
*
|
2013-08-08 09:58:34 +00:00
|
|
|
*/
|
2019-01-24 07:22:24 +00:00
|
|
|
void attach_us(Callback<void()> func, us_timestamp_t t);
|
2013-08-08 09:58:34 +00:00
|
|
|
|
2018-10-12 16:16:32 +00:00
|
|
|
/** Attach a member function to be called by the Ticker, specifying the interval in microseconds
|
2013-08-08 09:58:34 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @param obj pointer to the object to call the member function on
|
|
|
|
* @param method pointer to the member function to be called
|
2018-10-12 16:16:32 +00:00
|
|
|
* @param t the time between calls in microseconds
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
* @deprecated
|
|
|
|
* The attach_us function does not support cv-qualifiers. Replaced by
|
|
|
|
* attach_us(callback(obj, method), t).
|
2013-08-08 09:58:34 +00:00
|
|
|
*/
|
2016-05-15 21:17:47 +00:00
|
|
|
template<typename T, typename M>
|
Added support for cv-qualifiers in Callback class
Additionally, the following changes were don to avoid combinatorial
explosion in function overloads as a result of adding cv-qualifiers:
- Added convenience function for inferred type
- Deprecated callback overloads qhere cv-qualifiers are not scalable
Supported overloads:
callback(void (*f)(A...));
callback(const Callback<R(A...)> &);
callback(T *t, void (*f)(T*, A...));
callback(const T *t, void (*f)(const T*, A...));
callback(volatile T *t, void (*f)(volatile T*, A...));
callback(const volatile T *t, void (*f)(const volatile T*, A...));
callback(T *t, void (T::*f)(A...));
callback(const T *t, void (T::*f)(A...) const);
callback(volatile T *t, void (T::*f)(A...) volatile);
callback(const volatile T *t, void (T::*f)(A...) const volatile);
2016-08-18 18:28:57 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
2018-05-24 15:58:14 +00:00
|
|
|
"The attach_us function does not support cv-qualifiers. Replaced by "
|
|
|
|
"attach_us(callback(obj, method), t).")
|
|
|
|
void attach_us(T *obj, M method, us_timestamp_t t)
|
|
|
|
{
|
2016-05-15 21:17:47 +00:00
|
|
|
attach_us(Callback<void()>(obj, method), t);
|
2013-08-08 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 15:58:14 +00:00
|
|
|
virtual ~Ticker()
|
|
|
|
{
|
2014-08-27 08:30:35 +00:00
|
|
|
detach();
|
|
|
|
}
|
|
|
|
|
2013-08-08 09:58:34 +00:00
|
|
|
/** Detach the function
|
|
|
|
*/
|
|
|
|
void detach();
|
|
|
|
|
2018-10-11 22:05:35 +00:00
|
|
|
#if !defined(DOXYGEN_ONLY)
|
2013-08-08 09:58:34 +00:00
|
|
|
protected:
|
2017-03-31 13:36:55 +00:00
|
|
|
void setup(us_timestamp_t t);
|
2013-08-08 09:58:34 +00:00
|
|
|
virtual void handler();
|
|
|
|
|
2014-08-27 08:25:24 +00:00
|
|
|
protected:
|
2018-10-12 16:16:32 +00:00
|
|
|
us_timestamp_t _delay; /**< Time delay (in microseconds) for resetting the multishot callback. */
|
2017-03-31 13:36:55 +00:00
|
|
|
Callback<void()> _function; /**< Callback. */
|
2018-10-12 16:16:32 +00:00
|
|
|
bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */
|
2018-10-11 22:05:35 +00:00
|
|
|
#endif
|
2013-08-08 09:58:34 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 15:23:39 +00:00
|
|
|
/** @}*/
|
|
|
|
/** @}*/
|
|
|
|
|
2013-08-08 09:58:34 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|