|
| 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