1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 08:06:13 +01:00

Change Stream refernces to pointers

I misunderstood that references can never be changed.
This commit is contained in:
Asbelos 2020-08-01 16:32:16 +01:00
parent a14b9ef8bf
commit 9044cce2a3
13 changed files with 123 additions and 97 deletions

View File

@ -35,7 +35,7 @@
// //
// The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup(). // The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup().
void myFilter(Print & stream, byte & opcode, byte & paramCount, int p[]) { void myFilter(Print * stream, byte & opcode, byte & paramCount, int p[]) {
(void)stream; // avoid compiler warning if we don't access this parameter (void)stream; // avoid compiler warning if we don't access this parameter
switch (opcode) { switch (opcode) {
case '!': // Create a bespoke new command to clear all loco reminders <!> or specific locos e.g <! 3 4 99> case '!': // Create a bespoke new command to clear all loco reminders <!> or specific locos e.g <! 3 4 99>
@ -116,7 +116,7 @@ void loop() {
// Responsibility 3: Optionally handle any incoming WiFi traffic // Responsibility 3: Optionally handle any incoming WiFi traffic
#ifdef WIFI #ifdef WIFI
WifiInterface::loop(Serial1); WifiInterface::loop();
#endif #endif
// Your additional code // Your additional code

View File

@ -20,6 +20,7 @@
#include "DCCEXParser.h" #include "DCCEXParser.h"
#include "DCC.h" #include "DCC.h"
#include "DCCWaveform.h" #include "DCCWaveform.h"
#include "WifiInterface.h"
#include "Turnouts.h" #include "Turnouts.h"
#include "Outputs.h" #include "Outputs.h"
#include "Sensors.h" #include "Sensors.h"
@ -39,7 +40,7 @@ const int HASH_KEYWORD_JOIN=-30750;
int DCCEXParser::stashP[MAX_PARAMS]; int DCCEXParser::stashP[MAX_PARAMS];
bool DCCEXParser::stashBusy; bool DCCEXParser::stashBusy;
Print & DCCEXParser::stashStream=DIAGSERIAL; // keep compiler happy but ovevride in constructor Print * DCCEXParser::stashStream=NULL;
// This is a JMRI command parser, one instance per incoming stream // This is a JMRI command parser, one instance per incoming stream
// It doesnt know how the string got here, nor how it gets back. // It doesnt know how the string got here, nor how it gets back.
@ -67,7 +68,7 @@ void DCCEXParser::loop(Stream & stream) {
} }
else if (ch == '>') { else if (ch == '>') {
buffer[bufferLength]='\0'; buffer[bufferLength]='\0';
parse( stream, buffer, false); // Parse this allowing async responses parse( & stream, buffer, false); // Parse this allowing async responses
inCommandPayload = false; inCommandPayload = false;
break; break;
} else if(inCommandPayload) { } else if(inCommandPayload) {
@ -131,7 +132,7 @@ void DCCEXParser::setFilter(FILTER_CALLBACK filter) {
} }
// See documentation on DCC class for info on this section // See documentation on DCC class for info on this section
void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) { void DCCEXParser::parse(Print * stream, byte *com, bool blocking) {
DIAG(F("\nPARSING:%s\n"),com); DIAG(F("\nPARSING:%s\n"),com);
(void) EEPROM; // tell compiler not to warn thi is unused (void) EEPROM; // tell compiler not to warn thi is unused
int p[MAX_PARAMS]; int p[MAX_PARAMS];
@ -282,6 +283,9 @@ void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) {
DCC::setFn(p[0],p[1],p[2]==1); DCC::setFn(p[0],p[1],p[2]==1);
return; return;
case '+' : // Complex Wifi interface command (not usual parse)
WifiInterface::ATCommand(com);
break;
default: //anything else will diagnose and drop out to <X> default: //anything else will diagnose and drop out to <X>
DIAG(F("\nOpcode=%c params=%d\n"),opcode,params); DIAG(F("\nOpcode=%c params=%d\n"),opcode,params);
@ -294,7 +298,7 @@ void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) {
StringFormatter::send(stream, F("<X>")); StringFormatter::send(stream, F("<X>"));
} }
bool DCCEXParser::parseZ( Print & stream,int params, int p[]){ bool DCCEXParser::parseZ( Print * stream,int params, int p[]){
switch (params) { switch (params) {
@ -324,7 +328,7 @@ bool DCCEXParser::parseZ( Print & stream,int params, int p[]){
} }
//=================================== //===================================
bool DCCEXParser::parsef(Print & stream, int params, int p[]) { bool DCCEXParser::parsef(Print * stream, int params, int p[]) {
// JMRI sends this info in DCC message format but it's not exactly // JMRI sends this info in DCC message format but it's not exactly
// convenient for other processing // convenient for other processing
if (params==2) { if (params==2) {
@ -356,7 +360,7 @@ void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop) {
} }
//=================================== //===================================
bool DCCEXParser::parseT(Print & stream, int params, int p[]) { bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
switch(params){ switch(params){
case 0: // <T> case 0: // <T>
return (Turnout::showAll(stream)); break; return (Turnout::showAll(stream)); break;
@ -381,7 +385,7 @@ bool DCCEXParser::parseT(Print & stream, int params, int p[]) {
} }
} }
bool DCCEXParser::parseS( Print & stream,int params, int p[]) { bool DCCEXParser::parseS( Print * stream,int params, int p[]) {
switch(params){ switch(params){
@ -405,7 +409,7 @@ bool DCCEXParser::parseS( Print & stream,int params, int p[]) {
// CALLBACKS must be static // CALLBACKS must be static
bool DCCEXParser::stashCallback(Print & stream,int p[MAX_PARAMS]) { bool DCCEXParser::stashCallback(Print * stream,int p[MAX_PARAMS]) {
if (stashBusy || asyncBanned) return false; if (stashBusy || asyncBanned) return false;
stashBusy=true; stashBusy=true;
stashStream=stream; stashStream=stream;

View File

@ -20,13 +20,13 @@
#define DCCEXParser_h #define DCCEXParser_h
#include <Arduino.h> #include <Arduino.h>
typedef void (*FILTER_CALLBACK)(Print & stream, byte & opcode, byte & paramCount, int p[]); typedef void (*FILTER_CALLBACK)(Print * stream, byte & opcode, byte & paramCount, int p[]);
struct DCCEXParser struct DCCEXParser
{ {
DCCEXParser(); DCCEXParser();
void loop(Stream & pstream); void loop(Stream & stream);
void parse(Print & stream, const byte * command, bool blocking); void parse(Print * stream, byte * command, bool blocking);
void flush(); void flush();
static void setFilter(FILTER_CALLBACK filter); static void setFilter(FILTER_CALLBACK filter);
static const int MAX_PARAMS=10; // Must not exceed this static const int MAX_PARAMS=10; // Must not exceed this
@ -40,17 +40,17 @@ struct DCCEXParser
byte buffer[MAX_BUFFER+2]; byte buffer[MAX_BUFFER+2];
int splitValues( int result[MAX_PARAMS], const byte * command); int splitValues( int result[MAX_PARAMS], const byte * command);
bool parseT(Print & stream, int params, int p[]); bool parseT(Print * stream, int params, int p[]);
bool parseZ(Print & stream, int params, int p[]); bool parseZ(Print * stream, int params, int p[]);
bool parseS(Print & stream, int params, int p[]); bool parseS(Print * stream, int params, int p[]);
bool parsef(Print & stream, int params, int p[]); bool parsef(Print * stream, int params, int p[]);
static bool stashBusy; static bool stashBusy;
static Print & stashStream; static Print * stashStream;
static int stashP[MAX_PARAMS]; static int stashP[MAX_PARAMS];
bool stashCallback(Print & stream, int p[MAX_PARAMS]); bool stashCallback(Print * stream, int p[MAX_PARAMS]);
static void callback_W(int result); static void callback_W(int result);
static void callback_B(int result); static void callback_B(int result);
static void callback_R(int result); static void callback_R(int result);

View File

@ -121,7 +121,7 @@ bool Output::remove(int n){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool Output::showAll(Print & stream){ bool Output::showAll(Print * stream){
bool gotone=false; bool gotone=false;
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){ for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
gotone=true; gotone=true;
@ -130,7 +130,7 @@ bool Output::showAll(Print & stream){
return gotone; return gotone;
} }
void Output::show(Print & stream){ void Output::show(Print * stream){
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){ for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
StringFormatter::send(stream,F("<Y %d %d>"), tt->data.id, tt->data.oStatus); StringFormatter::send(stream,F("<Y %d %d>"), tt->data.id, tt->data.oStatus);
} }

View File

@ -36,8 +36,8 @@ class Output{
static void load(); static void load();
static void store(); static void store();
static Output *create(int, int, int, int=0); static Output *create(int, int, int, int=0);
static void show(Print & stream); static void show(Print * stream);
static bool showAll(Print & stream); static bool showAll(Print * stream);
private: private:
static Output *firstOutput; static Output *firstOutput;

View File

@ -73,7 +73,7 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Sensor::check(Print & stream){ void Sensor::check(Print * stream){
Sensor *tt; Sensor *tt;
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
@ -148,7 +148,7 @@ bool Sensor::remove(int n){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Sensor::show(Print & stream){ void Sensor::show(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<Q %d %d %d>"), tt->data.snum, tt->data.pin, tt->data.pullUp); StringFormatter::send(stream, F("<Q %d %d %d>"), tt->data.snum, tt->data.pin, tt->data.pullUp);
} }
@ -156,7 +156,7 @@ void Sensor::show(Print & stream){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Sensor::status(Print & stream){ void Sensor::status(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream,F("<%c %d>"), tt->active?'Q':'q', tt->data.snum); StringFormatter::send(stream,F("<%c %d>"), tt->active?'Q':'q', tt->data.snum);
} }

View File

@ -40,9 +40,9 @@ struct Sensor{
static Sensor *create(int, int, int); static Sensor *create(int, int, int);
static Sensor* get(int); static Sensor* get(int);
static bool remove(int); static bool remove(int);
static void show(Print & stream); static void show(Print * stream);
static void status(Print & stream); static void status(Print * stream);
static void check(Print & stream); static void check(Print * stream);
}; // Sensor }; // Sensor
#endif #endif

View File

@ -22,16 +22,22 @@
void StringFormatter::print( const __FlashStringHelper* input...) { void StringFormatter::print( const __FlashStringHelper* input...) {
va_list args; va_list args;
va_start(args, input); va_start(args, input);
send2(DIAGSERIAL,input,args); send(& DIAGSERIAL,input,args);
} }
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) { void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
va_list args; va_list args;
va_start(args, input); va_start(args, input);
send2(stream,input,args); send(& stream,input,args);
}
void StringFormatter::send(Print * stream, const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send(stream,input,args);
} }
void StringFormatter::send2(Print & stream,const __FlashStringHelper* format, va_list args) {
void StringFormatter::send(Print * stream,const __FlashStringHelper* format, va_list args) {
// thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output // thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output
@ -39,35 +45,35 @@ void StringFormatter::send2(Print & stream,const __FlashStringHelper* format, va
for(int i=0; ; ++i) { for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i); char c=pgm_read_byte_near(flash+i);
if (c=='\0') return; if (c=='\0') return;
if(c!='%') { stream.print(c); continue; } if(c!='%') { stream->print(c); continue; }
i++; i++;
c=pgm_read_byte_near(flash+i); c=pgm_read_byte_near(flash+i);
switch(c) { switch(c) {
case '%': stream.print('%'); break; case '%': stream->print('%'); break;
case 'c': stream.print((char) va_arg(args, int)); break; case 'c': stream->print((char) va_arg(args, int)); break;
case 's': stream.print(va_arg(args, char*)); break; case 's': stream->print(va_arg(args, char*)); break;
case 'e': printEscapes(stream,va_arg(args, char*)); break; case 'e': printEscapes(stream,va_arg(args, char*)); break;
case 'E': printEscapes(stream,(const __FlashStringHelper*)va_arg(args, char*)); break; case 'E': printEscapes(stream,(const __FlashStringHelper*)va_arg(args, char*)); break;
case 'S': stream.print((const __FlashStringHelper*)va_arg(args, char*)); break; case 'S': stream->print((const __FlashStringHelper*)va_arg(args, char*)); break;
case 'd': stream.print(va_arg(args, int), DEC); break; case 'd': stream->print(va_arg(args, int), DEC); break;
case 'l': stream.print(va_arg(args, long), DEC); break; case 'l': stream->print(va_arg(args, long), DEC); break;
case 'b': stream.print(va_arg(args, int), BIN); break; case 'b': stream->print(va_arg(args, int), BIN); break;
case 'o': stream.print(va_arg(args, int), OCT); break; case 'o': stream->print(va_arg(args, int), OCT); break;
case 'x': stream.print(va_arg(args, int), HEX); break; case 'x': stream->print(va_arg(args, int), HEX); break;
case 'f': stream.print(va_arg(args, double), 2); break; case 'f': stream->print(va_arg(args, double), 2); break;
} }
} }
va_end(args); va_end(args);
} }
void StringFormatter::printEscapes(Print & stream, char * input) { void StringFormatter::printEscapes(Print * stream, char * input) {
for(int i=0; ; ++i) { for(int i=0; ; ++i) {
char c=input[i]; char c=input[i];
printEscape(stream,c); printEscape(stream,c);
if (c=='\0') return; if (c=='\0') return;
} }
} }
void StringFormatter::printEscapes(Print & stream, const __FlashStringHelper* input) { void StringFormatter::printEscapes(Print * stream, const __FlashStringHelper* input) {
char* flash=(char*)input; char* flash=(char*)input;
for(int i=0; ; ++i) { for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i); char c=pgm_read_byte_near(flash+i);
@ -75,14 +81,14 @@ void StringFormatter::printEscapes(Print & stream, const __FlashStringHelper* in
if (c=='\0') return; if (c=='\0') return;
} }
} }
void StringFormatter::printEscape(Print & stream, char c) { void StringFormatter::printEscape(Print * stream, char c) {
switch(c) { switch(c) {
case '\n': stream.print(F("\\n")); break; case '\n': stream->print(F("\\n")); break;
case '\r': stream.print(F("\\r")); break; case '\r': stream->print(F("\\r")); break;
case '\0': stream.print(F("\\0")); return; case '\0': stream->print(F("\\0")); return;
case '\t': stream.print(F("\\t")); break; case '\t': stream->print(F("\\t")); break;
case '\\': stream.print(F("\\")); break; case '\\': stream->print(F("\\")); break;
default: stream.print(c); default: stream->print(c);
} }
} }

View File

@ -33,11 +33,11 @@ class StringFormatter
public: public:
static void print( const __FlashStringHelper* input...); static void print( const __FlashStringHelper* input...);
static void send(Print & serial, const __FlashStringHelper* input...); static void send(Print & serial, const __FlashStringHelper* input...);
static void printEscapes(Print & stream, char * input); static void send(Print * serial, const __FlashStringHelper* input...);
static void printEscapes(Print & stream, const __FlashStringHelper* input); static void printEscapes(Print * stream, char * input);
static void printEscape(Print & stream, char c); static void printEscapes(Print * stream, const __FlashStringHelper* input);
private: static void printEscape(Print * stream, char c);
static void send2(Print & serial, const __FlashStringHelper* input,va_list args); static void send(Print * serial, const __FlashStringHelper* input,va_list args);
}; };
#endif #endif

View File

@ -73,7 +73,7 @@ bool Turnout::remove(int n){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Turnout::show(Print & stream, int n){ void Turnout::show(Print * stream, int n){
for(Turnout *tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){ for(Turnout *tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
if (tt->data.id==n) { if (tt->data.id==n) {
StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE); StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE);
@ -82,7 +82,7 @@ void Turnout::show(Print & stream, int n){
} }
} }
bool Turnout::showAll(Print & stream){ bool Turnout::showAll(Print * stream){
bool gotOne=false; bool gotOne=false;
for(Turnout * tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){ for(Turnout * tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
StringFormatter::send(stream,F("<H %d %d %d %d>"), tt->data.id, tt->data.address, tt->data.subAddress, (tt->data.tStatus & STATUS_ACTIVE)!=0); StringFormatter::send(stream,F("<H %d %d %d %d>"), tt->data.id, tt->data.address, tt->data.subAddress, (tt->data.tStatus & STATUS_ACTIVE)!=0);

View File

@ -47,8 +47,8 @@ class Turnout {
static Turnout *create(int id , int address , int subAddress); static Turnout *create(int id , int address , int subAddress);
static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle); static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle);
static Turnout *create(int id); static Turnout *create(int id);
static void show(Print & stream, int n); static void show(Print * stream, int n);
static bool showAll(Print & stream); static bool showAll(Print * stream);
void activate(bool state); void activate(bool state);
}; // Turnout }; // Turnout

View File

@ -37,75 +37,89 @@ int WifiInterface::datalength=0;
int WifiInterface::connectionId; int WifiInterface::connectionId;
byte WifiInterface::buffer[MAX_WIFI_BUFFER]; byte WifiInterface::buffer[MAX_WIFI_BUFFER];
MemStream WifiInterface::streamer(buffer,sizeof(buffer)); MemStream WifiInterface::streamer(buffer,sizeof(buffer));
Stream * WifiInterface::wifiStream=NULL;
void WifiInterface::setup(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, void WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) { const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
wifiStream=&setupStream;
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n")); DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
connected=setup2(wifiStream, SSid, password,hostname, servername,port); connected=setup2( SSid, password,hostname, servername,port);
// TODO calloc the buffer and streamer and parser etc // TODO calloc the buffer and streamer and parser etc
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED")); DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED"));
} }
bool WifiInterface::setup2(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) { const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
delay(1000); delay(1000);
StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
checkForOK(wifiStream,5000,READY_SEARCH,false); // generally not interesting to DCC checkForOK(5000,READY_SEARCH,false); // generally not interesting to DCC
StringFormatter::send(wifiStream,F("AT+GMR\r\n")); // request AT version StringFormatter::send(wifiStream,F("AT+GMR\r\n")); // request AT version
checkForOK(wifiStream,2000,OK_SEARCH,true); // Makes this visible on the console checkForOK(2000,OK_SEARCH,true); // Makes this visible on the console
StringFormatter::send(wifiStream,F("AT+CWMODE=3\r\n")); // configure as server or access point StringFormatter::send(wifiStream,F("AT+CWMODE=3\r\n")); // configure as server or access point
checkForOK(wifiStream,1000,OK_SEARCH,true); // Not always OK, sometimes "no change" checkForOK(1000,OK_SEARCH,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(wifiStream,2000,OK_SEARCH,true)) { if (checkForOK(2000,OK_SEARCH,true)) {
// early version supports CWJAP // early version supports CWJAP
if (SSid!=NULL && password!=NULL) {
// Only connect to router if we are given a SSId and password,
// otherwise assume that the ES8266 has already got these from a previous call
// USER can pass these in with a <W "ssid" "password"> command which will re-call this setup
StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password); StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password);
checkForOK(wifiStream,20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok checkForOK(20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
}
} }
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(wifiStream,2000, OK_SEARCH, true); // dont care if not supported checkForOK(2000, OK_SEARCH, true); // dont care if not supported
if (SSid!=NULL && password!=NULL) {
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);
checkForOK(wifiStream,20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok checkForOK(20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
}
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(wifiStream,2000,OK_SEARCH,true); checkForOK(2000,OK_SEARCH,true);
// StringFormatter::send(wifiStream, F("AT+MDNS=1,\"%S.local\",\"%S.local\",%d\r\n"), hostname, servername, port); // Setup mDNS for Server // StringFormatter::send(wifiStream, F("AT+MDNS=1,\"%S.local\",\"%S.local\",%d\r\n"), hostname, servername, port); // Setup mDNS for Server
// if (!checkForOK(wifiStream,5000, OK_SEARCH, true)) return false; // if (!checkForOK(5000, OK_SEARCH, true)) return false;
(void)servername; // avoid compiler warning from commented out AT_MDNS above (void)servername; // avoid compiler warning from commented out AT_MDNS above
} }
StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1 StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false; if (!checkForOK(10000,OK_SEARCH,true)) return false;
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(wifiStream,10000,OK_SEARCH,true)) return false; if (!checkForOK(10000,OK_SEARCH,true)) return false;
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(wifiStream,10000,OK_SEARCH,true)) return false; if (!checkForOK(10000,OK_SEARCH,true)) return false;
return true; return true;
} }
void WifiInterface::ATCommand(const byte * command) {
StringFormatter:: send(wifiStream,F("AT+%s\r\n"), command+1);
checkForOK(10000,OK_SEARCH,true);
}
bool WifiInterface::checkForOK(Stream & wifiStream, const unsigned int timeout, const char * waitfor, bool echo) { bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo) {
unsigned long startTime = millis(); unsigned long startTime = millis();
char const *locator=waitfor; char const *locator=waitfor;
DIAG(F("\nWifi Check: [%E]"),waitfor); DIAG(F("\nWifi Check: [%E]"),waitfor);
while( millis()-startTime < timeout) { while( millis()-startTime < timeout) {
while(wifiStream.available()) { while(wifiStream->available()) {
int ch=wifiStream.read(); int ch=wifiStream->read();
if (echo) StringFormatter::printEscape(DIAGSERIAL,ch); /// THIS IS A DIAG IN DISGUISE if (echo) StringFormatter::printEscape(&DIAGSERIAL,ch); /// THIS IS A DIAG IN DISGUISE
if (ch!=pgm_read_byte_near(locator)) locator=waitfor; if (ch!=pgm_read_byte_near(locator)) locator=waitfor;
if (ch==pgm_read_byte_near(locator)) { if (ch==pgm_read_byte_near(locator)) {
locator++; locator++;
@ -141,17 +155,17 @@ bool WifiInterface::isHTML() {
} }
} }
void WifiInterface::loop(Stream & wifiStream) { void WifiInterface::loop() {
if (!connected) return; if (!connected) return;
WiThrottle::loop(); // check heartbeats WiThrottle::loop(); // check heartbeats
// read anything into a buffer, collecting info on the way // read anything into a buffer, collecting info on the way
while (loopstate!=99 && wifiStream.available()) { while (loopstate!=99 && wifiStream->available()) {
int ch=wifiStream.read(); int ch=wifiStream->read();
// echo the char to the diagnostic stream in escaped format // echo the char to the diagnostic stream in escaped format
StringFormatter::printEscape(DIAGSERIAL,ch); // DIAG in disguise StringFormatter::printEscape(&DIAGSERIAL,ch); // DIAG in disguise
switch (loopstate) { switch (loopstate) {
case 0: // looking for +IPD case 0: // looking for +IPD
@ -193,7 +207,7 @@ void WifiInterface::loop(Stream & wifiStream) {
} }
if (ch=='>'){ if (ch=='>'){
DIAG(F("\n> [%e]\n"),buffer); DIAG(F("\n> [%e]\n"),buffer);
wifiStream.print((char *) buffer); wifiStream->print((char *) buffer);
loopTimeoutStart=millis(); loopTimeoutStart=millis();
loopstate=closeAfter?11:0; loopstate=closeAfter?11:0;
} }
@ -228,7 +242,7 @@ void WifiInterface::loop(Stream & wifiStream) {
HTTPParser::parse(streamer,buffer); HTTPParser::parse(streamer,buffer);
closeAfter=true; closeAfter=true;
} }
else if (buffer[0]=='<') parser.parse(streamer,buffer, true); // tell JMRI parser that ACKS are blocking because we can't handle the async else if (buffer[0]=='<') parser.parse(&streamer,buffer, true); // tell JMRI parser that ACKS are blocking because we can't handle the async
else WiThrottle::getThrottle(connectionId)->parse(streamer, buffer); else WiThrottle::getThrottle(connectionId)->parse(streamer, buffer);

View File

@ -27,16 +27,18 @@
class WifiInterface { class WifiInterface {
public: public:
static void setup(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, static void setup(Stream & setupStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port); const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
static void loop(Stream & wifiStream); static void loop();
static void ATCommand(const byte * command);
private: private:
static Stream * wifiStream;
static DCCEXParser parser; static DCCEXParser parser;
static bool setup2(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, static bool setup2( const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port); const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
static bool checkForOK(Stream & wifiStream, const unsigned int timeout, const char* waitfor, bool echo); static bool checkForOK(const unsigned int timeout, const char* waitfor, bool echo);
static bool isHTML(); static bool isHTML();
static bool connected; static bool connected;
static bool closeAfter; static bool closeAfter;