Skip to content

add LowPowerTicker examples #79

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 1 commit into from
Mar 12, 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
4 changes: 4 additions & 0 deletions APIs_Drivers/lowpowerTicker_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# LowpowerTicker example

This example shows how to use the LowpowerTicker 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.

24 changes: 24 additions & 0 deletions APIs_Drivers/lowpowerTicker_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#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) {
ThisThread::sleep_for(200);
}
}
4 changes: 4 additions & 0 deletions APIs_Drivers/lowpowerTimeout_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# LowpowerTimout example

This example shows how to use a LowpowerTimout to schedule delayed events. The LowPowerTimeout API is a countdown timer that triggers a callback function when the timer runs out. The LowPowerTimeout interface can be though of as a one shot LowPowerTicker.

27 changes: 27 additions & 0 deletions APIs_Drivers/lowpowerTimeout_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

LowPowerTimeout 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);
}
}
4 changes: 4 additions & 0 deletions APIs_Drivers/lowpowerTimer_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# LowpowerTimer example

This example shows how to use a LowpowerTimer to measure time. The time will be the amount of 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/lowpowerTimer_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

LowPowerTimer t;

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