aboutsummaryrefslogtreecommitdiffstats
path: root/examples/exti-interrupt.cpp
diff options
context:
space:
mode:
authorAditya Gaddam <adityagaddam@gmail.com>2012-09-03 17:58:04 -0400
committerAditya Gaddam <adityagaddam@gmail.com>2012-09-03 17:58:04 -0400
commitaec407773bbef80478941193f05fd67ce8ed49e0 (patch)
tree98e9dd1fa84eda2e9e03cf6e6255c9b454cde39d /examples/exti-interrupt.cpp
parent53b224544424f037c29617f066294058aa6572f5 (diff)
downloadlibrambutan-aec407773bbef80478941193f05fd67ce8ed49e0.tar.gz
librambutan-aec407773bbef80478941193f05fd67ce8ed49e0.zip
"Added two examples for using attachInterrupt. One shows the use of a global function. While the second shows the use of a static class method as the event handler. Both work on Maple REVC
Signed-off-by: Aditya Gaddam <adityagaddam@gmail.com>"
Diffstat (limited to 'examples/exti-interrupt.cpp')
-rw-r--r--examples/exti-interrupt.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/exti-interrupt.cpp b/examples/exti-interrupt.cpp
new file mode 100644
index 0000000..06d6b6f
--- /dev/null
+++ b/examples/exti-interrupt.cpp
@@ -0,0 +1,51 @@
+// 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;
+}