Skip to content

add Ticker examples #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions APIs_Drivers/Ticker_Example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ticker example

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.
36 changes: 36 additions & 0 deletions APIs_Drivers/Ticker_Example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

// A class for flip()-ing a DigitalOut
class Flipper {
public:
Flipper(PinName pin) : _pin(pin)
{
_pin = 0;
}
void flip()
{
_pin = !_pin;
}
private:
DigitalOut _pin;
};

DigitalOut led1(LED1);
Flipper f(LED2);
Ticker t;

int main()
{
// the address of the object, member function, and interval
t.attach(callback(&f, &Flipper::flip), 2.0);

// spin in a main loop. flipper will interrupt it to call flip
while (1) {
led1 = !led1;
ThisThread::sleep_for(200);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/Ticker_HelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ticker example

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.
26 changes: 26 additions & 0 deletions APIs_Drivers/Ticker_HelloWorld/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);

void flip()
{
led2 = !led2;
}

int main()
{
led2 = 1;
flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)

// spin in a main loop. flipper will interrupt it to call flip
while (1) {
led1 = !led1;
ThisThread::sleep_for(200);
}
}
4 changes: 4 additions & 0 deletions APIs_Drivers/Timeout_Example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Timeout example

This example shows how to use Timeout with a callback to schedule delayed events.

36 changes: 36 additions & 0 deletions APIs_Drivers/Timeout_Example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

// A class for flip()-ing a DigitalOut
class Flipper {
public:
Flipper(PinName pin) : _pin(pin)
{
_pin = 0;
}
void flip()
{
_pin = !_pin;
}
private:
DigitalOut _pin;
};

DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;

int main()
{
// the address of the object, member function, and interval
t.attach(callback(&f, &Flipper::flip), 2.0);

// spin in a main loop. flipper will interrupt it to call flip
while (1) {
led1 = !led1;
ThisThread::sleep_for(200);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/Timeout_HelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Timeout example

This example shows how to use Timeout to schedule delayed events.
26 changes: 26 additions & 0 deletions APIs_Drivers/Timeout_HelloWorld/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);

void flip()
{
led2 = !led2;
}

int main()
{
led2 = 1;
flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds

// spin in a main loop. flipper will interrupt it to call flip
while (1) {
led1 = !led1;
ThisThread::sleep_for(200);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/Timer_HelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Timer example

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.
16 changes: 16 additions & 0 deletions APIs_Drivers/Timer_HelloWorld/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"


Timer t;

int main()
{
t.start();
printf("Hello World!\n");
t.stop();
printf("The time taken was %f seconds\n", t.read());
}