0

当我尝试执行预处理器逻辑来识别定义的单词 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。

  1. WIFI_STA 和 WIFI_AP 分别评估为 1 和 2。不知道种类。

  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

谁能告诉我

  1. 问题的根源
  2. 如何解决它
  3. 其中 WIFI_STA 被定义。

我使用的是 ESP8266,但这个问题出在编译器而不是设备上。

4

2 回答 2

2

如果您查看 ESP8266WiFi 库 -
MacOSX For built in packages 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}
于 2020-05-12T21:26:39.953 回答
0

显然 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");
    }


作品。

但是,我仍然不知道定义这些的代码在哪里。

于 2020-05-12T21:10:28.703 回答