Skip to content

Commit

Permalink
Add simple examples for interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtjd committed May 4, 2022
1 parent ce58358 commit 9cd7bb2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/Interrupts/Interrupt_Delayed/Interrupt_Delayed.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <Loom.h>

// Include configuration
const char* json_config =
#include "config.h"
;

// In Tools menu, set:
// Internet > Disabled
// Sensors > Enabled
// Radios > Disabled
// Actuators > Disabled
// Max > Disabled

using namespace Loom;

Loom::Manager Feather{};

// Can be whatever pin you want
const int PIN_NUM = 11;

// This is not actually called immediately upon an interrupt being generated
// It is called on next iteration of loop()
// Thus, this function can be as complicated as you wish since it is not called directly upon interrupt trigger
void handle_ISR() {
detachInterrupt(PIN_NUM);

LPrintln("Interrupt triggered!");
LPrintln("Waiting for interrupt to trigger...");

// If using a button/switch, simple solution to bounce (just pause a bit for switch to settle)
delay(5);

// Reconnect interrupt since it was previously detached
getInterruptManager(Feather).reconnect_interrupt(PIN_NUM);
}

void setup()
{
Feather.begin_serial(true);
Feather.parse_config(json_config);
Feather.print_config();

// Creates a delayed interrupt for specified pin number, sets it as an internal pullup, and generates an interrupt on a LOW signal
getInterruptManager(Feather).register_ISR(PIN_NUM, handle_ISR, LOW, ISR_Type::CHECK_FLAG);

LPrintln("\n ** Setup Complete ** ");
LPrintln("Waiting for interrupt to trigger...");
}

void loop()
{
// Runs the ISR for any interrupts that may have been triggered since last call
getInterruptManager(Feather).run_pending_ISRs();
}
15 changes: 15 additions & 0 deletions examples/Interrupts/Interrupt_Delayed/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"{\
'general':\
{\
'name':'Device',\
'instance':1,\
'interval':500,\
'print_verbosity':2\
},\
'components':[\
{\
'name':'InterruptManager',\
'params':[0]\
}\
]\
}"
56 changes: 56 additions & 0 deletions examples/Interrupts/Interrupt_Immediate/Interrupt_Immediate.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <Loom.h>

// Include configuration
const char* json_config =
#include "config.h"
;

// In Tools menu, set:
// Internet > Disabled
// Sensors > Enabled
// Radios > Disabled
// Actuators > Disabled
// Max > Disabled

using namespace Loom;

Loom::Manager Feather{};

// Can be whatever pin you want
const int PIN_NUM = 11;

// Typically a flag is set in an ISR since ISR's should be quick and concise
// Should be volatile since the variable can be changed outside normal program flow
volatile bool flag = false;

// Typical Interrupt Service Routine
void handle_ISR() {
detachInterrupt(PIN_NUM);
flag = true;
}

void setup()
{
Feather.begin_serial(true);
Feather.parse_config(json_config);
Feather.print_config();

// Creates an immediate interrupt for specified pin number, sets it as an internal pullup, and generates an interrupt on a LOW signal
getInterruptManager(Feather).register_ISR(PIN_NUM, handle_ISR, LOW, ISR_Type::IMMEDIATE);

LPrintln("\n ** Setup Complete ** ");
}

void loop() {
// Reconnect interrupt since it is disabled in ISR
getInterruptManager(Feather).reconnect_interrupt(PIN_NUM);
LPrintln("Waiting for interrupt to trigger...");

// Set flag to false and wait until the ISR sets it to true
flag = false;
while(!flag);
LPrintln("Interrupt triggered!");

// If using a button/switch, simple solution to bounce (just pause a bit for switch to settle)
delay(5);
}
15 changes: 15 additions & 0 deletions examples/Interrupts/Interrupt_Immediate/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"{\
'general':\
{\
'name':'Device',\
'instance':1,\
'interval':500,\
'print_verbosity':2\
},\
'components':[\
{\
'name':'InterruptManager',\
'params':[0]\
}\
]\
}"

0 comments on commit 9cd7bb2

Please sign in to comment.