So I've such of a code:
void B();
void C();
void A() {
Serial.println("Looping");
B();
}
void B() {
C();
}
void C() {
A();
}
void setup() {
Serial.begin(115200);
A();
}
void loop() {
};
and it works just fine, but when I want to add a condition, like this:
bool flag = false;
void B();
void C();
void A() {
Serial.println("Looping");
if(flag)
B();
else
C();
}
void B() {
flag = false;
A();
}
void C() {
flag = true;
A();
}
void setup() {
Serial.begin(115200);
A();
}
void loop() {
};
After a while my esp32 throws a backtrace and reboots:
Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered
I need some kind of solution to run B()
or C()
and "forget" about A()
body, as I understood this problem.
UDP It's just a skelet of a real code, I'm using FreeRTOS (xTaskCreate, ...) to run the logic, I`ve an idea:
1) Create 2 Tasks (one of them will be running)
2) Later pass the function I want to call, delete running
3) Such way by switching tasks it should work
I've such logic in a project:
void B();
void C();
void A() {
if(server.connect())
while(server.isConnected()) {
//makeUpdate, receive new messages
}
// as it fails, make reconnect
//check wifi connection
if(wifi)
B();
else
C();
}
void B() {
// Update Connection With Wifi
// Go back to connect
A();
}
void C() {
// Update Connection With Gprs
// Go back to connect
A();
}
void setup() {
Serial.begin(115200);
A();
}
void loop() {
};
CodePudding user response:
Calling functions like that will cause an infinite recursion, exhaust the stack and make the whole thing crash.
You could call all the functions from inside loop()
and other functions just return which function should be called next.
char A() {
Serial.println("Looping");
if(flag)
return 'B';
else
return 'C';
}
char B() {
// Update Connection With Wifi
// Go back to connect
return 'A';
}
char C() {
// Update Connection With Gprs
// Go back to connect
return 'A';
}
void setup() {
Serial.begin(115200);
}
void loop() {
static char next_function = 'A';
switch(next_function) {
case 'A': next_function = A(); break;
case 'B': next_function = B(); break;
case 'C': next_function = C(); break;
}
}