Skip to content

Commit b5faca5

Browse files
committed
Add attachInterrupt with parameters
1 parent 3dbd7d7 commit b5faca5

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Diff for: api/ArduinoAPI.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Client.h"
3030
#include "HardwareI2C.h"
3131
#include "HardwareSerial.h"
32+
#include "Interrupts.h"
3233
#include "IPAddress.h"
3334
#include "Print.h"
3435
#include "Printable.h"

Diff for: api/Common.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#include <stdint.h>
23

34
#ifdef __cplusplus
@@ -67,6 +68,7 @@ typedef enum {
6768
#endif
6869

6970
typedef void (*voidFuncPtr)(void);
71+
typedef void (*voidFuncPtrParam)(void*);
7072

7173
// interrupts() / noInterrupts() must be defined by the core
7274

@@ -118,6 +120,7 @@ void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_
118120
pin_size_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder);
119121

120122
void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode);
123+
void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param);
121124
void detachInterrupt(pin_size_t interruptNumber);
122125

123126
void setup(void);
@@ -147,4 +150,4 @@ long random(long, long);
147150
void randomSeed(unsigned long);
148151
long map(long, long, long, long, long);
149152

150-
#endif // __cplusplus
153+
#endif // __cplusplus

Diff for: api/Interrupts.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef W_INTERRUPTS_CPP
2+
#define W_INTERRUPTS_CPP
3+
#ifdef __cplusplus
4+
5+
#include <stdlib.h>
6+
#include <stdbool.h>
7+
#include <stdint.h>
8+
#include "Common.h"
9+
10+
template <typename T>
11+
using voidTemplateFuncPtrParam = void (*)(T param);
12+
13+
template<typename T> struct __container__ {
14+
void* param;
15+
voidTemplateFuncPtrParam<T> function;
16+
};
17+
18+
// C++ only overloaded version of attachInterrupt function
19+
template<typename T> void attachInterrupt(pin_size_t interruptNum, voidTemplateFuncPtrParam<T> userFunc, int mode, T& param) {
20+
21+
struct __container__<T> *cont = new __container__<T>();
22+
cont->param = &param;
23+
cont->function = userFunc;
24+
25+
// TODO: check lambda scope
26+
// TODO: add structure to delete(__container__) when detachInterrupt() is called
27+
auto f = [](void* a) -> void
28+
{
29+
T param = *(T*)((struct __container__<T>*)a)->param;
30+
(((struct __container__<T>*)a)->function)(param);
31+
};
32+
33+
attachInterruptParam(interruptNum, f, mode, cont);
34+
}
35+
36+
template<typename T> void attachInterrupt(uint8_t interruptNum, voidTemplateFuncPtrParam<T*> userFunc, int mode, T* param) {
37+
attachInterruptParam(interruptNum, (voidFuncPtrParam)userFunc, mode, (void*)param);
38+
}
39+
40+
#endif
41+
#endif

0 commit comments

Comments
 (0)