Skip to content

Commit fee70f9

Browse files
stracciofacchinm
authored andcommitted
attachInterrupt with std::function
A way to add member function for an interrupt callback. std::function<void()> int_func =std::bind(&button::press,this); attachInterrupt(digitalPinToInterrupt(pin),int_func,FALLING);
1 parent f5cbedd commit fee70f9

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

cores/arduino/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ extern const PinDescription g_APinDescription[] ;
204204
#include "wiring_digital.h"
205205
#include "wiring_analog.h"
206206
#include "wiring_shift.h"
207+
#ifdef __cplusplus
207208
#include "WInterrupts.h"
209+
#endif // __cplusplus
208210

209211
#include "watchdog.h"
210212

cores/arduino/USB/USBCore.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "PluggableUSB.h"
2222
#include <stdint.h>
2323

24+
#define _min(a, b) (((a) < (b)) ? (a) : (b))
25+
2426
//#define TRACE_CORE(x) x
2527
#define TRACE_CORE(x)
2628

@@ -144,7 +146,7 @@ uint32_t USBD_Recv(uint32_t ep, void* d, uint32_t len)
144146

145147
LockEP lock(ep);
146148
uint32_t n = UDD_FifoByteCount(ep & 0xF);
147-
len = min(n,len);
149+
len = _min(n,len);
148150
n = len;
149151
uint8_t* dst = (uint8_t*)d;
150152
while (n--)

cores/arduino/WInterrupts.c renamed to cores/arduino/WInterrupts.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@
88
99
This library is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
See the GNU Lesser General Public License for more details.
1313
1414
You should have received a copy of the GNU Lesser General Public
1515
License along with this library; if not, write to the Free Software
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
18-
18+
namespace std
19+
{
20+
void __throw_bad_function_call()
21+
{
22+
//Log.Error(F("STL ERROR - HALT NOW"));
23+
}
24+
}
1925
#include "WInterrupts.h"
2026

21-
typedef void (*interruptCB)(void);
27+
//typedef void (*interruptCB)(void);
28+
typedef std::function<void(void)> interruptCB;
2229

2330
static interruptCB callbacksPioA[32];
2431
static interruptCB callbacksPioB[32];
@@ -60,8 +67,17 @@ static void __initialize() {
6067
NVIC_EnableIRQ(PIOD_IRQn);
6168
}
6269

70+
// void attachInterrupt(uint32_t pin, std::function<void(void)> callback, uint32_t mode){
71+
//
72+
// }
73+
74+
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode){
75+
std::function<void(void)> _callback =callback;
76+
attachInterrupt(pin,_callback,mode);
77+
}
6378

64-
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
79+
// void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
80+
void attachInterrupt(uint32_t pin, std::function<void(void)> callback, uint32_t mode)
6581
{
6682
static int enabled = 0;
6783
if (!enabled) {

cores/arduino/WInterrupts.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
This library is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
See the GNU Lesser General Public License for more details.
1313
1414
You should have received a copy of the GNU Lesser General Public
@@ -21,16 +21,19 @@
2121

2222
#include "Arduino.h"
2323

24-
#ifdef __cplusplus
25-
extern "C" {
26-
#endif
24+
#include <functional>
2725

26+
// #ifdef __cplusplus
27+
// extern "C" {
28+
// #endif
29+
30+
void attachInterrupt(uint32_t pin, std::function<void(void)>, uint32_t mode);
2831
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode);
2932

3033
void detachInterrupt(uint32_t pin);
3134

32-
#ifdef __cplusplus
33-
}
34-
#endif
35+
// #ifdef __cplusplus
36+
// }
37+
// #endif
3538

3639
#endif /* _WIRING_INTERRUPTS_ */

0 commit comments

Comments
 (0)