Skip to content

Commit 297fe43

Browse files
add Ticker examples
1 parent 73f5e9f commit 297fe43

File tree

8 files changed

+137
-0
lines changed

8 files changed

+137
-0
lines changed

APIs_Drivers/Ticker_Example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ticker example
2+
3+
This example shows how to use a Ticker with callback. The example use the Ticker to cause periodic events, like blinking an LED on and Off at a certain rate. In this example LED1 is controlled by the main while loop, while LED2 is controlled by the Ticker callback function.

APIs_Drivers/Ticker_Example/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
// A class for flip()-ing a DigitalOut
8+
class Flipper {
9+
public:
10+
Flipper(PinName pin) : _pin(pin)
11+
{
12+
_pin = 0;
13+
}
14+
void flip()
15+
{
16+
_pin = !_pin;
17+
}
18+
private:
19+
DigitalOut _pin;
20+
};
21+
22+
DigitalOut led1(LED1);
23+
Flipper f(LED2);
24+
Ticker t;
25+
26+
int main()
27+
{
28+
// the address of the object, member function, and interval
29+
t.attach(callback(&f, &Flipper::flip), 2.0);
30+
31+
// spin in a main loop. flipper will interrupt it to call flip
32+
while (1) {
33+
led1 = !led1;
34+
ThisThread::sleep_for(200);
35+
}
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ticker example
2+
3+
This example shows how to use the Ticker to cause periodic events, like blinking an LED on and Off at a certain rate. In this example LED1 is controlled by the main while loop, while LED2 is controlled by the Ticker callback function.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Ticker flipper;
8+
DigitalOut led1(LED1);
9+
DigitalOut led2(LED2);
10+
11+
void flip()
12+
{
13+
led2 = !led2;
14+
}
15+
16+
int main()
17+
{
18+
led2 = 1;
19+
flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
20+
21+
// spin in a main loop. flipper will interrupt it to call flip
22+
while (1) {
23+
led1 = !led1;
24+
ThisThread::sleep_for(200);
25+
}
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Timout example
2+
3+
This example shows how to use a Timeout with callback to schedule delayed events.
4+

APIs_Drivers/Timeout_Example/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
// A class for flip()-ing a DigitalOut
8+
class Flipper {
9+
public:
10+
Flipper(PinName pin) : _pin(pin)
11+
{
12+
_pin = 0;
13+
}
14+
void flip()
15+
{
16+
_pin = !_pin;
17+
}
18+
private:
19+
DigitalOut _pin;
20+
};
21+
22+
DigitalOut led1(LED1);
23+
Flipper f(LED2);
24+
Timeout t;
25+
26+
int main()
27+
{
28+
// the address of the object, member function, and interval
29+
t.attach(callback(&f, &Flipper::flip), 2.0);
30+
31+
// spin in a main loop. flipper will interrupt it to call flip
32+
while (1) {
33+
led1 = !led1;
34+
ThisThread::sleep_for(200);
35+
}
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Timeout example
2+
3+
This example shows how to use a Timeout to schedule delayed events.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Timeout flipper;
8+
DigitalOut led1(LED1);
9+
DigitalOut led2(LED2);
10+
11+
void flip()
12+
{
13+
led2 = !led2;
14+
}
15+
16+
int main()
17+
{
18+
led2 = 1;
19+
flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
20+
21+
// spin in a main loop. flipper will interrupt it to call flip
22+
while (1) {
23+
led1 = !led1;
24+
ThisThread::sleep_for(200);
25+
}
26+
}

0 commit comments

Comments
 (0)