1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 21:21:24 +01:00

Reduce duplicated F() macros

Compiler isn't as clever as one might expect
This commit is contained in:
Asbelos 2021-02-14 20:20:36 +00:00
parent 919b975adc
commit 5a9d5d1f96
2 changed files with 28 additions and 22 deletions

View File

@ -26,7 +26,8 @@
#include "WifiInboundHandler.h" #include "WifiInboundHandler.h"
#define OK_SEARCH F("\r\nOK\r\n")
const unsigned long LOOP_TIMEOUT = 2000; const unsigned long LOOP_TIMEOUT = 2000;
bool WifiInterface::connected = false; bool WifiInterface::connected = false;
@ -125,7 +126,7 @@ wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, con
if (wifiState == WIFI_CONNECTED) { if (wifiState == WIFI_CONNECTED) {
StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo
checkForOK(200, OK_SEARCH, true); checkForOK(200, true);
} }
@ -154,25 +155,25 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
} }
StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT? StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT?
if(!checkForOK(200, OK_SEARCH, true)) if(!checkForOK(200, true))
return WIFI_NOAT; // No AT compatible WiFi module here return WIFI_NOAT; // No AT compatible WiFi module here
StringFormatter::send(wifiStream, F("ATE1\r\n")); // Turn on the echo, se we can see what's happening StringFormatter::send(wifiStream, F("ATE1\r\n")); // Turn on the echo, se we can see what's happening
checkForOK(2000, OK_SEARCH, true); // Makes this visible on the console checkForOK(2000, true); // Makes this visible on the console
// Display the AT version information // Display the AT version information
StringFormatter::send(wifiStream, F("AT+GMR\r\n")); StringFormatter::send(wifiStream, F("AT+GMR\r\n"));
checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console checkForOK(2000, true, false); // Makes this visible on the console
#ifdef DONT_TOUCH_WIFI_CONF #ifdef DONT_TOUCH_WIFI_CONF
DIAG(F("\nDONT_TOUCH_WIFI_CONF was set: Using existing config\n")); DIAG(F("\nDONT_TOUCH_WIFI_CONF was set: Using existing config\n"));
#else #else
StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as "station" = WiFi client StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as "station" = WiFi client
checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" checkForOK(1000, true); // Not always OK, sometimes "no change"
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME // Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n")); StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
if (checkForOK(2000, OK_SEARCH, true)) { if (checkForOK(2000, true)) {
oldCmd=true; oldCmd=true;
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
} }
@ -197,14 +198,14 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
if (oldCmd) { if (oldCmd) {
// AT command early version supports CWJAP/CWSAP // AT command early version supports CWJAP/CWSAP
StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password); StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password);
ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true); ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, true);
} else { } else {
// later version supports CWJAP_CUR // later version supports CWJAP_CUR
StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client
checkForOK(2000, OK_SEARCH, true); // dont care if not supported checkForOK(2000, true); // dont care if not supported
StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password); StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password);
ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true); ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, true);
} }
if (ipOK) { if (ipOK) {
@ -222,14 +223,14 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
// If we have not managed to get this going in station mode, go for AP mode // If we have not managed to get this going in station mode, go for AP mode
// StringFormatter::send(wifiStream, F("AT+RST\r\n")); // StringFormatter::send(wifiStream, F("AT+RST\r\n"));
// checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" // checkForOK(1000, true); // Not always OK, sometimes "no change"
int i=0; int i=0;
do { do {
// configure as AccessPoint. Try really hard as this is the // configure as AccessPoint. Try really hard as this is the
// last way out to get any Wifi connectivity. // last way out to get any Wifi connectivity.
StringFormatter::send(wifiStream, F("AT+CWMODE=2\r\n")); StringFormatter::send(wifiStream, F("AT+CWMODE=2\r\n"));
} while (!checkForOK(1000+i*500, OK_SEARCH, true) && i++<10); } while (!checkForOK(1000+i*500, true) && i++<10);
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
@ -248,7 +249,7 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
} }
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'}; char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
checkForOK(1000, OK_SEARCH, true, false); // suck up remainder of AT+CIFSR checkForOK(1000, true, false); // suck up remainder of AT+CIFSR
i=0; i=0;
do { do {
@ -261,24 +262,24 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",%d,4\r\n"), oldCmd ? "" : "_CUR", StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",%d,4\r\n"), oldCmd ? "" : "_CUR",
macTail, password, channel); macTail, password, channel);
} }
} while (!checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true) && i++<2); // do twice if necessary but ignore failure as AP mode may still be ok } while (!checkForOK(WIFI_CONNECT_TIMEOUT, true) && i++<2); // do twice if necessary but ignore failure as AP mode may still be ok
if (i >= 2) if (i >= 2)
DIAG(F("\nWarning: Setting AP SSID and password failed\n")); // but issue warning DIAG(F("\nWarning: Setting AP SSID and password failed\n")); // but issue warning
if (!oldCmd) { if (!oldCmd) {
StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct
checkForOK(2000, OK_SEARCH, true); checkForOK(2000, true);
} }
} }
StringFormatter::send(wifiStream, F("AT+CIPSERVER=0\r\n")); // turn off tcp server (to clean connections before CIPMUX=1) StringFormatter::send(wifiStream, F("AT+CIPSERVER=0\r\n")); // turn off tcp server (to clean connections before CIPMUX=1)
checkForOK(1000, OK_SEARCH, true); // ignore result in case it already was off checkForOK(1000, true); // ignore result in case it already was off
StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections
if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED; if (!checkForOK(1000, true)) return WIFI_DISCONNECTED;
StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port
if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED; if (!checkForOK(1000, true)) return WIFI_DISCONNECTED;
#endif //DONT_TOUCH_WIFI_CONF #endif //DONT_TOUCH_WIFI_CONF
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // Display ip addresses to the DIAG StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // Display ip addresses to the DIAG
@ -301,7 +302,7 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
LCD(4,F("%s"),ipString); // There is not enough room on some LCDs to put a title to this LCD(4,F("%s"),ipString); // There is not enough room on some LCDs to put a title to this
} }
// suck up anything after the IP. // suck up anything after the IP.
if (!checkForOK(1000, OK_SEARCH, true, false)) return WIFI_DISCONNECTED; if (!checkForOK(1000, true, false)) return WIFI_DISCONNECTED;
LCD(5,F("PORT=%d\n"),port); LCD(5,F("PORT=%d\n"),port);
return WIFI_CONNECTED; return WIFI_CONNECTED;
@ -324,12 +325,16 @@ void WifiInterface::ATCommand(const byte * command) {
} }
else { else {
StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command); StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command);
checkForOK(10000, OK_SEARCH, true); checkForOK(10000, true);
} }
} }
bool WifiInterface::checkForOK( const unsigned int timeout, bool echo, bool escapeEcho) {
return checkForOK(timeout,F("\r\nOK\r\n"),echo,escapeEcho);
}
bool WifiInterface::checkForOK( const unsigned int timeout, const FSH * waitfor, bool echo, bool escapeEcho) { bool WifiInterface::checkForOK( const unsigned int timeout, const FSH * waitfor, bool echo, bool escapeEcho) {
unsigned long startTime = millis(); unsigned long startTime = millis();
char *locator = (char *)waitfor; char *locator = (char *)waitfor;

View File

@ -46,6 +46,7 @@ private:
static DCCEXParser parser; static DCCEXParser parser;
static wifiSerialState setup2(const FSH *SSSid, const FSH *password, static wifiSerialState setup2(const FSH *SSSid, const FSH *password,
const FSH *hostname, int port, byte channel); const FSH *hostname, int port, byte channel);
static bool checkForOK(const unsigned int timeout, bool echo, bool escapeEcho = true);
static bool checkForOK(const unsigned int timeout, const FSH *waitfor, bool echo, bool escapeEcho = true); static bool checkForOK(const unsigned int timeout, const FSH *waitfor, bool echo, bool escapeEcho = true);
static bool connected; static bool connected;
}; };