[Arduino.cc预处理器(截至2020年5月1日的最新版本)在尝试执行预处理器逻辑以识别定义的单词WIFI_AP或WIFI_STA时具有意外行为。
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,那么就没有办法到达这里!!”
预期行为:预处理程序应该在打印语句中评估相同的逻辑,并在编译之前删除所有打印内容!
回归:1.不会针对所有可能定义的常数发生,仅发生在WIFI_STA和WIFI_AP。
WIFI_STA和WIFI_AP的计算结果分别为1和2。不知道类型。
grep在包含或arduino应用程序资源中找不到定义WIFI_STA和WIFI_AP的任何位置!
我真的很困惑。我想也许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
有人可以告诉我
我正在使用ESP8266,但此问题出在编译器而非设备中。
如果您查看ESP8266WiFi库-MacOSX对于内置软件包,按住Control键并单击/Applications/Arduino.app
,然后从弹出菜单中选择“显示软件包内容”。从那里您可以深入到;
/Applications/Arduino.app/Contents/Resources/Java/packages
ESP8266软件包(至少在我的OSX Sierra上)Users\YOUR_USERNAME\Library\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\ESP8266WiFi\src
Windows C:\Users\YOUR_USERNAME\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\ESP8266WiFi\src
-在ESP8266WiFiType.h
中找到以下定义
typedef enum WiFiMode
{
WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3,
/* these two pseudo modes are experimental: */ WIFI_SHUTDOWN = 4, WIFI_RESUME = 8
} WiFiMode_t;
因此,对于您的应用程序,您必须使用:
if (WIFIACCESSMODE == WIFI_STA ) {do STA things}
if (WIFIACCESSMODE == WIFI_AP) {do AP things}
显然WIFI_STA和WIFI_AP是代码定义的常数,而不是预处理器定义。因此,选择这些值的唯一方法是使用显式代码,而不是通过C ++ if或case语句进行预处理器优化。
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();
}
else if((WIFIACCESSMODE) == (WIFI_AP_STA)){
Serial.println("Wifi access mode is WIFI_AP_STA");
// setup_WiFiAccessPoint();
// setup_WiFiRouter();
}
else if ((WIFIACCESSMODE) == (WIFI_STA)) {
Serial.println("Wifi access mode is WIFI_STA");
// setup_WiFiRouter();
}
else {
Serial.println("Wifi Access mode is Unknown");
}
作品。
无论如何我仍然不知道这些代码在何处定义。