Skip to content

Commit 7f2f4f2

Browse files
committed
Add IRQ INPUTS examples
1 parent fd3fcd9 commit 7f2f4f2

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Testing strategy: alternatively create a short-time connection between
3+
WAKEUP 1-6 and any of the +BAT_ext pins (the row above the WAKUP ones).
4+
5+
Check IRQChannelMap for advanced C++ implementation.
6+
*/
7+
8+
#include <Arduino_EdgeControl.h>
9+
10+
volatile int irqCounts[6] { };
11+
12+
enum IRQChannelsIndex {
13+
irqChannel1 = 0,
14+
irqChannel2,
15+
irqChannel3,
16+
irqChannel4,
17+
irqChannel5,
18+
irqChannel6
19+
};
20+
21+
22+
void setup()
23+
{
24+
EdgeControl.begin();
25+
26+
Serial.begin(115200);
27+
28+
// Wait for Serial Monitor or start after 2.5s
29+
for (const auto timeout = millis() + 2500; millis() < timeout && !Serial; delay(250));
30+
31+
// Init IRQ INPUT pins
32+
for (auto pin = IRQ_CH1; pin <= IRQ_CH6; pin++)
33+
pinMode(pin, INPUT);
34+
35+
// Attach callbacks to IRQ pins
36+
attachInterrupt(digitalPinToInterrupt(IRQ_CH1), []{ irqCounts[irqChannel1]++; }, CHANGE);
37+
attachInterrupt(digitalPinToInterrupt(IRQ_CH2), []{ irqCounts[irqChannel2]++; }, CHANGE);
38+
attachInterrupt(digitalPinToInterrupt(IRQ_CH3), []{ irqCounts[irqChannel3]++; }, CHANGE);
39+
attachInterrupt(digitalPinToInterrupt(IRQ_CH4), []{ irqCounts[irqChannel4]++; }, CHANGE);
40+
attachInterrupt(digitalPinToInterrupt(IRQ_CH5), []{ irqCounts[irqChannel5]++; }, CHANGE);
41+
attachInterrupt(digitalPinToInterrupt(IRQ_CH6), []{ irqCounts[irqChannel6]++; }, CHANGE);
42+
43+
}
44+
45+
void loop()
46+
{
47+
// Check for received IRQ every second.
48+
Serial.println("--------");
49+
for (auto i = irqChannel1; i <= irqChannel6; i++) {
50+
Serial.print("IRQ Channel: ");
51+
Serial.print(i + 1);
52+
Serial.print(" - ");
53+
Serial.println(irqCounts[i]);
54+
}
55+
delay(1000);
56+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Testing strategy: alternatively create a short-time connection between
3+
WAKEUP 1-6 and any of the +BAT_ext pins (the row above the WAKUP ones).
4+
5+
Check IRQChannel for a simpler implementation.
6+
*/
7+
8+
#include <Arduino_EdgeControl.h>
9+
10+
#include <map>
11+
12+
// Use a map to collect IRQ counts
13+
std::map<const pin_size_t, volatile unsigned int> irqCounts {
14+
{ IRQ_CH1, 0 },
15+
{ IRQ_CH2, 0 },
16+
{ IRQ_CH3, 0 },
17+
{ IRQ_CH4, 0 },
18+
{ IRQ_CH5, 0 },
19+
{ IRQ_CH6, 0 }
20+
};
21+
22+
// Map pin numbers to pin names for pretty printing
23+
std::map<pin_size_t, const char*> irqNames {
24+
#define NE(IRQ) { IRQ, #IRQ }
25+
NE(IRQ_CH1),
26+
NE(IRQ_CH2),
27+
NE(IRQ_CH3),
28+
NE(IRQ_CH4),
29+
NE(IRQ_CH5),
30+
NE(IRQ_CH6),
31+
};
32+
33+
void setup()
34+
{
35+
EdgeControl.begin();
36+
37+
Serial.begin(115200);
38+
39+
// Wait for Serial Monitor or start after 2.5s
40+
for (const auto timeout = millis() + 2500; millis() < timeout && !Serial; delay(250))
41+
;
42+
43+
// Init IRQ pins and attach callbacks
44+
// NOTE: .first holds the channel pin and .second holds the counter
45+
for (const auto& irq : irqCounts) {
46+
// Init pins
47+
pinMode(irq.first, INPUT);
48+
49+
// Create a type alias helper
50+
using IrqCount = std::pair<const pin_size_t, volatile unsigned int>;
51+
52+
// Define the IRQ callback as lambda function
53+
// Will receive an entry from the irqCounts map:
54+
auto isr = [](void* arg) { IrqCount * ic = (IrqCount *)arg; (*ic).second++; };
55+
56+
// attach the callback passing the current map entry as parameter
57+
attachInterruptParam(
58+
digitalPinToInterrupt(irq.first), isr, RISING, (void*)&irq);
59+
}
60+
}
61+
62+
void loop()
63+
{
64+
// Print counters every second.
65+
Serial.println("--------");
66+
for (const auto& irq : irqCounts) {
67+
Serial.print("IRQ Channel: ");
68+
Serial.print(irqNames[irq.first]);
69+
Serial.print(" - Counts: ");
70+
Serial.println(irq.second);
71+
}
72+
delay(1000);
73+
}

0 commit comments

Comments
 (0)