Skip to content

Commit 3190112

Browse files
author
Qinghao Shi
authored
Merge pull request #78 from maciejbocianski/move_ticker_examples
add Ticker examples
2 parents 7dab47b + 57187ae commit 3190112

File tree

10 files changed

+156
-0
lines changed

10 files changed

+156
-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 Ticker with a callback. The example uses Ticker to cause periodic events, such as blinking an LED at a certain rate. In this example, the main while loop controls LED1, and the Ticker callback function controls LED2.

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 Ticker to cause periodic events, such as blinking an LED at a certain rate. In this example, the main while loop controls LED1, and the Ticker callback function controls LED2.
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+
# Timeout example
2+
3+
This example shows how to use Timeout with a 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 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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Timer example
2+
3+
This example shows how to use the Timer class to measure the time between when the `start()` and `stop()` functions are run. The Timer class counts from 0 up.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
8+
Timer t;
9+
10+
int main()
11+
{
12+
t.start();
13+
printf("Hello World!\n");
14+
t.stop();
15+
printf("The time taken was %f seconds\n", t.read());
16+
}

0 commit comments

Comments
 (0)