springderek
Member
So this runs fine on ccp.sh, but on arduino 1.05 teensy 3.1
1. i had to comment the "..SpecialFunction(Thingy....) i'm guessing just because of some compiler issue with inline class declaration
2. weird, i had to remove the ()'s when calling aButton.action(); to it's aButton.action; i guess Arduino is doing some type of assumption when compiling
More importantly...
3. once the code compiles, i can call the action functions, but nothing actually happens, i don't know if they're actually getting assigned or whats the deal
1. i had to comment the "..SpecialFunction(Thingy....) i'm guessing just because of some compiler issue with inline class declaration
2. weird, i had to remove the ()'s when calling aButton.action(); to it's aButton.action; i guess Arduino is doing some type of assumption when compiling
More importantly...
3. once the code compiles, i can call the action functions, but nothing actually happens, i don't know if they're actually getting assigned or whats the deal
Code:
#include <functional>
int global1 = 0;
int global2 = 5;
class aButton {
public:
int status, oldStatus;
aButton(int initStatus) { oldStatus = status = initStatus; }
std::function<int()> action;
};
class Thingy {
private:
int mode = 1;
int value = 0;
public:
void reset() { value = 0; }
void setMode(int newMode) { mode = newMode; }
void increment() { value = value + global2; }
//...
};
// void specialFunction(Thingy *thingyToWorkOn) {
// //do stuff...
// }
// void anotherSpecialFunction(Thingy *firstThingy, Thingy *secondThingy) {
// //more stuff...
// }
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) { }
Serial.println( "-- Start Test -- \n" );
Thingy one;
Thingy two;
aButton on(0);
aButton speedUp(0);
on.action = std::function<int()>([&]() -> int{
//some specific stuff....
global1 = 1;
if (global2 < 10) {
global2++;
}
one.reset();
two.reset();
// anotherSpecialFunction(&one, &two);
Serial.println( "on action \n" );
return 1;
});
speedUp.action = std::function<int()>([&]() -> int{
//some specific stuff....
if (global1) {
one.setMode(global2);
two.setMode(global2);
// specialFunction(&one);
// specialFunction(&two);
Serial.println( "speedUp action \n" );
return 1;
}
return 0;
});
for(int i=0; i<5; i++) {
//if on pushed
on.action;
//if speedUp pushed
speedUp.action;
}
Serial.println( "\n-- End Test --" );
}
void loop() {
}
Last edited: