Go back to the examples list

Reacting to a button by interrupt

This example builds on the previous one Reading a button by moving the button handling code from a polling logic to an interrupt logic (the difference between the two is explained in the GPIO module reference). Using this method, we define a function (called a handler) which will be called immediately whenever the button is pressed. This allows the main loop to do other things in the same time, while making sure the event will not be missed.

Two versions of the same program are presented : a version using the helper module specifically for the Carbide board, and a generic version using only the main library functions which can be used on any board.

Carbide

button.cpp [download]
wget https://libtungsten.io/static/code/examples/button_interrupt_carbide/button_interrupt.cpp

#include <carbide.h>
#include <core.h>

// Keep a trace of the current LED state
bool ledState = false;

// Handler function to be called by interrupt when the
// button is pressed
void onButtonPressedHandler() {
    ledState = !ledState;
    Carbide::setLedR(ledState);
}

int main() {
    // Init the board, including defining LED and button pins
    Carbide::init(true);

    // Register the button handler
    Carbide::onButtonPressed(onButtonPressedHandler);

    while true) {
        // Blink the blue LED, independently of the button logic
        Carbide::setLedB(true);
        Core::sleep(500);
        Carbide::setLedB(false);
        Core::sleep(500);
    }
}

Makefile [download]
wget https://libtungsten.io/static/code/examples/button_interrupt_carbide/Makefile

NAME=button_interrupt

BOOTLOADER=true
CARBIDE=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile

Generic

button.cpp [download]
wget https://libtungsten.io/static/code/examples/button_interrupt/button_interrupt.cpp

#include <core.h>
#include <gpio.h>

// Define the pins
const GPIO::Pin PIN_LED_RED = GPIO::PA00;
const GPIO::Pin PIN_LED_BLUE = GPIO::PA02;
const GPIO::Pin PIN_BUTTON = GPIO::PA04;

// Keep a trace of the current LED state
bool ledState = false;

// Handler function to be called by interrupt when the
// button is pressed
void onButtonPressedHandler() {
    ledState = !ledState;
    GPIO::set(PIN_LED_RED, !ledState);
}

int main() {
    // Init the microcontroller
    Core::init();

    // Enable the button
    GPIO::enableInput(PIN_BUTTON, GPIO::Pulling::PULLUP);

    // Enable the LEDs
    GPIO::enableOutput(PIN_LED_RED, GPIO::HIGH);
    GPIO::enableOutput(PIN_LED_BLUE, GPIO::HIGH);

    // Register the button handler
    GPIO::enableInterrupt(PIN_BUTTON, onButtonPressedHandler, GPIO::Trigger::FALLING);

    while (true) {
        // Blink the blue LED, independently of the button logic
        GPIO::setLow(PIN_LED_BLUE);
        Core::sleep(500);
        GPIO::setHigh(PIN_LED_BLUE);
        Core::sleep(500);
    }
}

Makefile [download]
wget https://libtungsten.io/static/code/examples/button_interrupt/Makefile

NAME=button_interrupt

BOOTLOADER=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile