Skip to content

add InterruptIn examples #93

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
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/InterruptIn_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# InterruptIn example

This example shows how to use InterruptIn to trigger a function call when a certain event happens. The example demonstrates calling a function on the rising edge of a button press. You can observe this through the counter value changing when the button is pressed.
38 changes: 38 additions & 0 deletions APIs_Drivers/InterruptIn_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

class Counter {
public:
Counter(PinName pin) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter
{
_interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
}

void increment()
{
_count++;
}

int read()
{
return _count;
}

private:
InterruptIn _interrupt;
volatile int _count;
};

Counter counter(SW2);

int main()
{
while (1) {
printf("Count so far: %d\n", counter.read());
ThisThread::sleep_for(2000);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/InterruptIn_ex_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# InterruptIn example

This example shows how to use InterruptIn to trigger a function call when a certain event happens. The example demonstrates calling a function on the rising edge of a button press. You can observe this through LED4 blinking as the program runs. LED1 only changes when the button is pressed.
24 changes: 24 additions & 0 deletions APIs_Drivers/InterruptIn_ex_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

InterruptIn button(SW2);
DigitalOut led(LED1);
DigitalOut flash(LED4);

void flip()
{
led = !led;
}

int main()
{
button.rise(&flip); // attach the address of the flip function to the rising edge
while (1) { // wait around, interrupts will interrupt this!
flash = !flash;
ThisThread::sleep_for(250);
}
}