diff --git a/Display.cpp b/Display.cpp new file mode 100644 index 0000000..7ce1bed --- /dev/null +++ b/Display.cpp @@ -0,0 +1,40 @@ +/* + * © 2020, Harald Barth. + * + * This file is part of Asbelos DCC API + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ +#include +#include "Display.h" +#include "DIAG.h" + +void Display::displayIP(byte ip[4]) { + if (ip != NULL) + LCD(6,F("%d.%d.%d.%d"),ip[0],ip[1],ip[2],ip[3]); + else + LCD(6,F("NO IP")); +} +void Display::displayIP(char *ip) { + if (ip != NULL) + LCD(6,F("%s"),ip); + else + LCD(6,F("NO IP")); +} +void Display::displayPort(int i) { + if (i > 0) + LCD(7,F(":%d"),i); + else + LCD(7,F("none")); +} diff --git a/Display.h b/Display.h new file mode 100644 index 0000000..dde2e88 --- /dev/null +++ b/Display.h @@ -0,0 +1,29 @@ +/* + * © 2020, Harald Barth. + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ +#ifndef Display_h +#define Display_h + +class Display { + public: + void displayIP(byte[4]); + void displayIP(char *); + void displayPort(int); + + private: + +}; +#endif diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 459bd09..f8c504c 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -22,8 +22,8 @@ #include #include "DIAG.h" #include "StringFormatter.h" - #include "WifiInboundHandler.h" +#include "Display.h" const char PROGMEM READY_SEARCH[] = "\r\nready\r\n"; const char PROGMEM OK_SEARCH[] = "\r\nOK\r\n"; @@ -33,6 +33,10 @@ const char PROGMEM IPD_SEARCH[] = "+IPD"; const unsigned long LOOP_TIMEOUT = 2000; bool WifiInterface::connected = false; Stream * WifiInterface::wifiStream; +wifiESPMode WifiInterface::wifiMode = WIFIMODE_UNKNOWN; +char WifiInterface::ip[16] = "\0234567890123456"; +//char WifiInterface::ip[0] = 0; +int WifiInterface::wifiPort = 0; //////////////////////////////////////////////////////////////////////////////// @@ -121,6 +125,9 @@ wifiSerialState WifiInterface::setup(Stream & setupStream, const __FlashStringH } if (wifiState == WIFI_CONNECTED) { + /// test here + SetIP(); + DisplayIP(); StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo checkForOK(200, OK_SEARCH, true); } @@ -286,7 +293,49 @@ void WifiInterface::ATCommand(const byte * command) { } } +void WifiInterface::SetIP() { + StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); + if (!checkForOK(10000, (const char *)F("+CIFSR:"), true, false)) + return; + // now we should be at APIP," or STAIP," + char c = ':'; + char oldc = ':'; + while (c != '"') { + while (!wifiStream->available()); + switch(c = wifiStream->read()) { + case 'T': + if (oldc == 'S') wifiMode = WIFIMODE_STA; + break; + case 'P': + if (oldc == 'A') wifiMode = WIFIMODE_AP; + break; + default: + // ignore other chars + break; + } + oldc = c; + } + + byte i=0; + for (;;) { + while(!wifiStream->available()); + ip[i]=wifiStream->read(); + if (ip[i] == '"' || i >= 15) { + ip[i]='\0'; + break; + } + i++; + } +} + +void WifiInterface::DisplayIP() { + //XXXX here I want to something like Display::displayIP(ip); + if (wifiMode == WIFIMODE_STA) + LCD(3,F("WifiCLIENT")); + else + LCD(3,F("AP")); +} bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo, bool escapeEcho) { unsigned long startTime = millis(); diff --git a/WifiInterface.h b/WifiInterface.h index 390a85f..13a8b99 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -25,6 +25,7 @@ #include enum wifiSerialState { WIFI_NOAT, WIFI_DISCONNECTED, WIFI_CONNECTED }; +enum wifiESPMode { WIFIMODE_UNKNOWN = 0, WIFIMODE_STA = 1, WIFIMODE_AP = 2 }; class WifiInterface { @@ -37,7 +38,8 @@ public: const int port = 2560); static void loop(); static void ATCommand(const byte *command); - + static void SetIP(); + static void DisplayIP(); private: static wifiSerialState setup(Stream &setupStream, const __FlashStringHelper *SSSid, const __FlashStringHelper *password, const __FlashStringHelper *hostname, int port); @@ -52,5 +54,8 @@ private: static int datalength; static int connectionId; static unsigned long loopTimeoutStart; + static wifiESPMode wifiMode; + static char ip[16]; + static int wifiPort; }; #endif