当我尝试执行预处理器逻辑来识别定义的单词 WIFI_AP 或 WIFI_STA 时,Arduino.cc 预处理器(截至 2020 年 5 月 1 日的最新版本)出现意外行为。
Example:
#define FOO WIFI_STA
#if (FOO) == (WIFI_AP)
Serial.print ("if this the this is zero: ");
Serial.print( ((FOO) == (WIFI_AP))); // should eval to 0
Serial.println( " then there is no way to reach here!!!");
#endif
结果是 “如果这是零:0,那么就没有办法到达这里!!!”
预期行为: 预处理器应在 print 语句中评估相同的逻辑并在编译前删除所有打印!
回归: 1. 并非所有可能定义的常量都发生,只有 WIFI_STA 和 WIFI_AP。
WIFI_STA 和 WIFI_AP 分别评估为 1 和 2。不知道种类。
grep 在定义 WIFI_STA 和 WIFI_AP 的包含或 arduino 应用程序资源中找不到任何位置!
我真的很困惑。我在想也许 WIFI_STA 是在预处理器运行时未定义的常量。但我不知道如何以不同的方式做到这一点/
将运行并显示预期结果和意外结果的完整代码
#include <Arduino.h>
#include <ESP8266WiFi.h>
#define ONE 1
#define TWO 2
#define THREE 3
#define FOO ONE
#define WIFIACCESSMODE WIFI_STA
void setup_wifiActivate() {
Serial.println(WIFIACCESSMODE);
Serial.println(WIFI_STA);
Serial.println(WIFI_AP);
Serial.println(WIFI_AP_STA);
// select mode for connection
WiFi.mode(WIFIACCESSMODE);
// WiFi.mode(WIFI_AP); //Only Access point
// WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi network
// WiFi.mode(WIFI_AP_STA); //Both AP hotspot and router client are enabled
#if (WIFIACCESSMODE) == (WIFI_AP)
Serial.println("Wifi access mode is WIFI_AP");
Serial.println( (WIFIACCESSMODE) );
Serial.println((WIFI_AP) );
Serial.println( (WIFIACCESSMODE) == (WIFI_AP) );
//setup_WiFiAccessPoint();
#elif (WIFIACCESSMODE) == (WIFI_AP_STA)
Serial.println("Wifi access mode is WIFI_AP_STA");
// setup_WiFiAccessPoint();
// setup_WiFiRouter();
#elif (WIFIACCESSMODE) == (WIFI_STA)
Serial.println("Wifi access mode is WIFI_STA");
// setup_WiFiRouter();
#else
Serial.println("Wifi Access mode is Unknown");
#endif
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Serialdone");
#if FOO == TWO
Serial.println(FOO);
Serial.println(ONE);
Serial.println(TWO);
Serial.println(THREE);
Serial.println(FOO == TWO);
Serial.println("It's two");
#elif FOO == ONE
Serial.println(FOO);
Serial.println(ONE);
Serial.println(TWO);
Serial.println(THREE);
Serial.println(FOO == TWO);
Serial.println("it's ONE");
#elif FOO == THREEE
Serial.println(FOO);
Serial.println(ONE);
Serial.println(TWO);
Serial.println(THREE);
Serial.println(FOO == TWO);
Serial.println("it's THREE");
#else
Serial.println(FOO);
Serial.println(ONE);
Serial.println(TWO);
Serial.println(THREE);
Serial.println(FOO == TWO);
Serial.println("it's Unknown");
#endif
setup_wifiActivate();
}
void loop() {
// put your main code here, to run repeatedly:
}
我想做什么?
我试图让用户写这样的东西:
// edit to set desired access mode WIFI_AP WIFI_STA WIFI_AP_STA
#define WIFIACCESSMODE WIFI_STA
...
then later:
WiFi.mode( WIFIACCESSMODE);
#if WIFIACCESSMODE == WIFI_STA <<<--- this fails to work
// do some code
#endif
谁能告诉我
- 问题的根源
- 如何解决它
- 其中 WIFI_STA 被定义。
我使用的是 ESP8266,但这个问题出在编译器而不是设备上。