From aec407773bbef80478941193f05fd67ce8ed49e0 Mon Sep 17 00:00:00 2001 From: Aditya Gaddam Date: Mon, 3 Sep 2012 17:58:04 -0400 Subject: "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 " --- examples/exti-interrupt-callback.cpp | 90 ++++++++++++++++++++++++++++++++++++ examples/exti-interrupt.cpp | 51 ++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 examples/exti-interrupt-callback.cpp create mode 100644 examples/exti-interrupt.cpp (limited to 'examples') diff --git a/examples/exti-interrupt-callback.cpp b/examples/exti-interrupt-callback.cpp new file mode 100644 index 0000000..9f018cf --- /dev/null +++ b/examples/exti-interrupt-callback.cpp @@ -0,0 +1,90 @@ +// 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. +// +// This is similar to the exti-interrupt example, but shows the use of a class +// method to handle interrupts. +// +// More about attachInterrupt: +// http://leaflabs.com/docs/lang/api/attachinterrupt.html +// + + +#include + +class MyAwesomeClass +{ +public: + // Setup the interrupt handler + void initialize() + { + // LED is off by default + this->isLEDOn = false; + + // Attach interrupt to class method handler + attachInterrupt(BOARD_BUTTON_PIN, buttonInterruptHandler, this, RISING); + } + +private: + + bool isLEDOn; + + // Static event handler takes a void * argument that was originally + // passed to the attachInterrupt call. If the argument in question is an + // instance of the class (MyAwesomeClass in this case), the static function + // get access to that instance's data (even private data). + // + // In other words, this setup allows the Maple to have class method + // interrupt handlers (albeit with a work around). + // + // However, as you might imagine, this argument can be anything (if you + // don't need instance data access). + // + static void buttonInterruptHandler(void *arg) + { + // Cast the "generic" void argument to the class instance. + MyAwesomeClass *instance = (MyAwesomeClass *)arg; + + // Accessing private instance data + instance->isLEDOn = !(instance->isLEDOn); + + // Set LED + digitalWrite(BOARD_LED_PIN, instance->isLEDOn); + + // Delay slightly for switch de-bouncing + delay(20); + } +}; + +MyAwesomeClass myClass; + +// Setup pin modes and the interrupt handler class +void setup() { + pinMode(BOARD_BUTTON_PIN, INPUT); + pinMode(BOARD_LED_PIN, OUTPUT); + + // The initialize method sets up the event handler to the private method + // in MyAwesomeClass. There is however, nothing stopping you from setting + // up event handlers which are public methods in classes. + myClass.initialize(); +} + +// 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; +} 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 + +// 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; +} -- cgit v1.2.3 From 3c0af902520b67ba2a8eb50f96a392d8070e3614 Mon Sep 17 00:00:00 2001 From: Aditya Gaddam Date: Mon, 3 Sep 2012 18:05:57 -0400 Subject: "Changed tabs to spaces. Changed some braces placement to match existing examples Signed-off-by: Aditya Gaddam " --- examples/exti-interrupt-callback.cpp | 75 +++++++++++++++++------------------- examples/exti-interrupt.cpp | 17 ++++---- 2 files changed, 44 insertions(+), 48 deletions(-) (limited to 'examples') diff --git a/examples/exti-interrupt-callback.cpp b/examples/exti-interrupt-callback.cpp index 9f018cf..1053ab2 100644 --- a/examples/exti-interrupt-callback.cpp +++ b/examples/exti-interrupt-callback.cpp @@ -12,55 +12,52 @@ #include -class MyAwesomeClass -{ +class MyAwesomeClass { public: - // Setup the interrupt handler - void initialize() - { - // LED is off by default - this->isLEDOn = false; - - // Attach interrupt to class method handler - attachInterrupt(BOARD_BUTTON_PIN, buttonInterruptHandler, this, RISING); - } - + // Setup the interrupt handler + void initialize() { + // LED is off by default + this->isLEDOn = false; + + // Attach interrupt to class method handler + attachInterrupt(BOARD_BUTTON_PIN, buttonInterruptHandler, this, RISING); + } + private: - bool isLEDOn; + bool isLEDOn; - // Static event handler takes a void * argument that was originally - // passed to the attachInterrupt call. If the argument in question is an - // instance of the class (MyAwesomeClass in this case), the static function - // get access to that instance's data (even private data). - // - // In other words, this setup allows the Maple to have class method - // interrupt handlers (albeit with a work around). - // - // However, as you might imagine, this argument can be anything (if you - // don't need instance data access). - // - static void buttonInterruptHandler(void *arg) - { - // Cast the "generic" void argument to the class instance. - MyAwesomeClass *instance = (MyAwesomeClass *)arg; - - // Accessing private instance data - instance->isLEDOn = !(instance->isLEDOn); - - // Set LED - digitalWrite(BOARD_LED_PIN, instance->isLEDOn); - - // Delay slightly for switch de-bouncing - delay(20); - } + // Static event handler takes a void * argument that was originally + // passed to the attachInterrupt call. If the argument in question is an + // instance of the class (MyAwesomeClass in this case), the static function + // get access to that instance's data (even private data). + // + // In other words, this setup allows the Maple to have class method + // interrupt handlers (albeit with a work around). + // + // However, as you might imagine, this argument can be anything (if you + // don't need instance data access). + // + static void buttonInterruptHandler(void *arg) { + // Cast the "generic" void argument to the class instance. + MyAwesomeClass *instance = (MyAwesomeClass *)arg; + + // Accessing private instance data + instance->isLEDOn = !(instance->isLEDOn); + + // Set LED + digitalWrite(BOARD_LED_PIN, instance->isLEDOn); + + // Delay slightly for switch de-bouncing + delay(20); + } }; MyAwesomeClass myClass; // Setup pin modes and the interrupt handler class void setup() { - pinMode(BOARD_BUTTON_PIN, INPUT); + pinMode(BOARD_BUTTON_PIN, INPUT); pinMode(BOARD_LED_PIN, OUTPUT); // The initialize method sets up the event handler to the private method diff --git a/examples/exti-interrupt.cpp b/examples/exti-interrupt.cpp index 06d6b6f..bc02f30 100644 --- a/examples/exti-interrupt.cpp +++ b/examples/exti-interrupt.cpp @@ -12,19 +12,18 @@ 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); +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_BUTTON_PIN, INPUT); pinMode(BOARD_LED_PIN, OUTPUT); attachInterrupt(BOARD_BUTTON_PIN, interruptHandler, RISING); -- cgit v1.2.3 From 9b8d7c508f9ff0633b5ab8acf9e64bee891604dc Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 3 Sep 2012 18:28:48 -0400 Subject: EXTI examples: whitespace fixups. Signed-off-by: Marti Bolivar --- examples/exti-interrupt-callback.cpp | 32 ++++++++++++++++---------------- examples/exti-interrupt.cpp | 12 ++++++------ 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/exti-interrupt-callback.cpp b/examples/exti-interrupt-callback.cpp index 1053ab2..c87c064 100644 --- a/examples/exti-interrupt-callback.cpp +++ b/examples/exti-interrupt-callback.cpp @@ -4,10 +4,10 @@ // // This is similar to the exti-interrupt example, but shows the use of a class // method to handle interrupts. -// -// More about attachInterrupt: +// +// More about attachInterrupt: // http://leaflabs.com/docs/lang/api/attachinterrupt.html -// +// #include @@ -18,36 +18,36 @@ public: void initialize() { // LED is off by default this->isLEDOn = false; - + // Attach interrupt to class method handler attachInterrupt(BOARD_BUTTON_PIN, buttonInterruptHandler, this, RISING); } - + private: bool isLEDOn; - // Static event handler takes a void * argument that was originally - // passed to the attachInterrupt call. If the argument in question is an + // Static event handler takes a void * argument that was originally + // passed to the attachInterrupt call. If the argument in question is an // instance of the class (MyAwesomeClass in this case), the static function - // get access to that instance's data (even private data). + // get access to that instance's data (even private data). // - // In other words, this setup allows the Maple to have class method + // In other words, this setup allows the Maple to have class method // interrupt handlers (albeit with a work around). // - // However, as you might imagine, this argument can be anything (if you + // However, as you might imagine, this argument can be anything (if you // don't need instance data access). // static void buttonInterruptHandler(void *arg) { // Cast the "generic" void argument to the class instance. MyAwesomeClass *instance = (MyAwesomeClass *)arg; - + // Accessing private instance data instance->isLEDOn = !(instance->isLEDOn); - + // Set LED digitalWrite(BOARD_LED_PIN, instance->isLEDOn); - + // Delay slightly for switch de-bouncing delay(20); } @@ -59,16 +59,16 @@ MyAwesomeClass myClass; void setup() { pinMode(BOARD_BUTTON_PIN, INPUT); pinMode(BOARD_LED_PIN, OUTPUT); - + // The initialize method sets up the event handler to the private method - // in MyAwesomeClass. There is however, nothing stopping you from setting + // in MyAwesomeClass. There is however, nothing stopping you from setting // up event handlers which are public methods in classes. myClass.initialize(); } // Loop. Does nothing in this example. void loop() { - + } // Force init to be called *first*, i.e. before static object allocation. diff --git a/examples/exti-interrupt.cpp b/examples/exti-interrupt.cpp index bc02f30..89382d7 100644 --- a/examples/exti-interrupt.cpp +++ b/examples/exti-interrupt.cpp @@ -1,10 +1,10 @@ // 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: +// +// More about attachInterrupt: // http://leaflabs.com/docs/lang/api/attachinterrupt.html -// +// #include @@ -16,7 +16,7 @@ void interruptHandler() { // Set LED isLEDOn = !isLEDOn; digitalWrite(BOARD_LED_PIN, isLEDOn); - + // Delay slightly for switch debouncing. delay(20); } @@ -25,13 +25,13 @@ void interruptHandler() { 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. -- cgit v1.2.3