aboutsummaryrefslogtreecommitdiffstats
path: root/examples/exti-interrupt.cpp
blob: 89382d7524f1340187e68773d24cde46ce1c2ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Toggles the built-in LED when the built in button
// on the Maple is pushed in. This uses the attachInterrupt function to
// setup the interrupt handler for the button being pressed.
//
// More about attachInterrupt:
// http://leaflabs.com/docs/lang/api/attachinterrupt.html
//

#include <wirish/wirish.h>

// LED is off by default
bool isLEDOn = false;

// Interrupt handler takes in nothing and returns nothing.
void interruptHandler() {
    // Set LED
    isLEDOn = !isLEDOn;
    digitalWrite(BOARD_LED_PIN, isLEDOn);

    // Delay slightly for switch debouncing.
    delay(20);
}

// Setup pin modes and the interrupt handler
void setup() {
    pinMode(BOARD_BUTTON_PIN, INPUT);
    pinMode(BOARD_LED_PIN, OUTPUT);

    attachInterrupt(BOARD_BUTTON_PIN, interruptHandler, RISING);
}

// Loop. Does nothing in this example.
void loop() {

}

// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain() {
    init();
}

int main(void) {
    setup();

    while (true) {
        loop();
    }
    return 0;
}