所以我有這樣的代碼:
void B();
void C();
void A() {
Serial.println("Looping");
B();
}
void B() {
C();
}
void C() {
A();
}
void setup() {
Serial.begin(115200);
A();
}
void loop() {
};
它作業得很好,但是當我想添加一個條件時,像這樣:
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() {
};
過了一會兒,我的 esp32 拋出回溯并重新啟動:
Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered
我需要某種解決方案來運行B()或C()“忘記”A()身體,因為我理解這個問題。
UDP 它只是一個真實代碼的骨架,我正在使用 FreeRTOS (xTaskCreate, ...) 來運行邏輯,我有一個想法:
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
我在一個專案中有這樣的邏輯:
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() {
};
uj5u.com熱心網友回復:
像這樣呼叫函式會導致無限遞回,耗盡堆疊并使整個事情崩潰。
您可以從內部呼叫所有函式,loop()而其他函式只回傳下一個應該呼叫的函式。
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424925.html
