LowPowerTicker
LowPowerTicker class hierarchy
The LowPowerTicker class has the same methods as the Ticker class but operates in deep sleep mode and has less resolution. Use the LowPowerTicker interface to set up a recurring interrupt when you only need millisecond accuracy; it calls a function repeatedly and at a specified rate. Because the LowPowerTicker class can operate in deep sleep mode, it does not block deep sleep when active.
You can create any number of LowPowerTicker objects, allowing multiple outstanding interrupts at the same time. The function can be a static function, a member function of a particular object or a Callback object.
Warnings and notes
-
No blocking code in ISR: avoid any calls to wait, infinite while loops or blocking calls in general.
-
No printf, malloc or new in ISR: avoid any calls to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.
LowPowerTicker class reference
Public Member Functions | |
template<typename F > | |
MBED_FORCEINLINE void | attach (F &&func, float t) |
Attach a function to be called by the Ticker, specifying the interval in seconds. More... | |
template<typename T , typename M > | |
void | attach (T *obj, M method, float t) |
Attach a member function to be called by the Ticker, specifying the interval in seconds. More... | |
void | attach_us (Callback< void()> func, us_timestamp_t t) |
Attach a function to be called by the Ticker, specifying the interval in microseconds. More... | |
template<typename T , typename M > | |
void | attach_us (T *obj, M method, us_timestamp_t t) |
Attach a member function to be called by the Ticker, specifying the interval in microseconds. More... | |
void | detach () |
Detach the function. More... |
Static Public Member Functions | |
static void | irq (uint32_t id) |
The handler registered with the underlying timer interrupt. More... |
LowPowerTicker example
Try this program to set up a LowPowerTicker to repeatedly invert an LED:
/* mbed Example Program
* Copyright (c) 2017-2017 ARM Limited
*
* 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.
*/
#include "mbed.h"
LowPowerTicker flipper;
DigitalOut led1(LED1);
void flip() {
led1 = !led1;
}
int main() {
led1 = 1;
flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
while(1) {
wait(0.2);
}
}