-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeChanger.ino
103 lines (74 loc) · 1.76 KB
/
modeChanger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
void f1 (void) {
Serial.println ("Mode: f1");
}
void f2 (void) {
Serial.println ("Mode: f2");
}
typedef void (*fPtr[])(void);
class modeChanger {
public:
modeChanger (fPtr *funcArray);
private:
fPtr *_funcArray;
};
modeChanger::modeChanger (fPtr *funcArray) {
_funcArray = funcArray;
}
static fPtr modeFuncArray = {f1, f2};
const unsigned int numModes = sizeof(modeFuncArray)/sizeof(modeFuncArray[0]);
modeChanger mode = new modeChanger (modeFuncArray);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
/* https://ideone.com
Works like a charm:
#include <iostream>
void f1(void) {
std::cout << "f1" << "\n";
}
void f2(void) {
std::cout << "f2" << "\n";
}
int main() {
typedef void (*fPtr[])(void);
static fPtr pt = {f1, f2, f2};
(*pt[2])();
(*pt[1])();
(*pt[0])();
return 0;
}
*/
/* Really works thanos to stackowerflow:
#include <iostream>
void f1 (void) {
std::cout << "f1" << "\n";
}
void f2 (void) {
std::cout << "f2" << "\n";
}
typedef void (*fPtr)(void); // simple "[]" gets the compile error
class modeChanger {
public:
modeChanger (fPtr *funcArray);
void op ();
private:
fPtr *_funcArray;
};
modeChanger::modeChanger (fPtr *funcArray) {
_funcArray = funcArray;
}
void modeChanger::op () {
(*_funcArray[0])();
(*_funcArray[1])(); // Line 27: this line generates a runtime error! Just comment it to get all work
}
void (*modeFuncArray[])(void) = {f1, f2, f2};
modeChanger *mode = new modeChanger (modeFuncArray);
int main() {
(*modeFuncArray[1])(); // Works fine
mode->op(); // generates a runtime error
return 0;
}
*/