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

back working no wifi

This commit is contained in:
Asbelos 2020-06-11 13:35:16 +01:00
parent aebc35b183
commit e769361c26
7 changed files with 117 additions and 38 deletions

View File

@ -1,6 +1,7 @@
#include "DCC.h" #include "DCC.h"
#include "DIAG.h" #include "DIAG.h"
#include "DCCEXParser.h" #include "DCCEXParser.h"
#include "WifiInterface.h"
/* this code is here to test the waveform generator and reveal the issues involved in programming track operations. /* this code is here to test the waveform generator and reveal the issues involved in programming track operations.
@ -17,13 +18,13 @@ void myCallback(int result) {
DIAG(F("\n getting Loco Id callback result=%d"),result); DIAG(F("\n getting Loco Id callback result=%d"),result);
} }
DCCEXParser serialParser(Serial);; DCCEXParser serialParser;
DCCEXParser wifiParser(Serial1); DCCEXParser wifiParser;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
DCC::begin(); DCC::begin();
// if (WIFI_PORT>0) WifiInterface::setup();
DIAG(F("\n===== CVReader demonstrating DCC::getLocoId() call ==========\n")); DIAG(F("\n===== CVReader demonstrating DCC::getLocoId() call ==========\n"));
DCC::getLocoId(myCallback); // myCallback will be called with the result DCC::getLocoId(myCallback); // myCallback will be called with the result
DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n")); DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n"));
@ -34,6 +35,10 @@ void loop() {
DCC::loop(); // required to keep locos running and check powwer DCC::loop(); // required to keep locos running and check powwer
// This line passes input on Serial to the DCCEXParser // This line passes input on Serial to the DCCEXParser
serialParser.loop(); serialParser.loop(Serial);
wifiParser.loop();
if (WIFI_PORT>0) {
// wifiParser=WifiInterface::getSingleClient(wifiParser);
// if (wifiParser) wifiParser->loop();
}
} }

View File

@ -1,6 +1,13 @@
#ifndef Config_h #ifndef Config_h
#define Config_h #define Config_h
const int WIFI_PORT = 0; // OR set to zero for no wifi
const char WIFI_SSID[] = "BTHub5-M6PT"; // your network SSID (name)
const char WIFI_PASS[] = "49de8d4862"; // your network password
const long WIFI_BAUD_RATE=115200;
const long WIFI_CONNECT_RETRIES=10;
// This hardware configuration would normally be setup using a bunch of #ifdefs. // This hardware configuration would normally be setup using a bunch of #ifdefs.
const byte MAIN_POWER_PIN = 3; const byte MAIN_POWER_PIN = 3;

View File

@ -7,25 +7,25 @@
#include "Sensors.h" #include "Sensors.h"
#include "EEStore.h" #include "EEStore.h"
#include "DIAG.h"
const char VERSION[]="99.666"; const char VERSION[]="99.666";
int DCCEXParser::stashP[MAX_PARAMS]; int DCCEXParser::stashP[MAX_PARAMS];
bool DCCEXParser::stashBusy; bool DCCEXParser::stashBusy;
Stream & DCCEXParser::stashStream=Serial; // keep compiler happy but ovevride in constructor Stream & DCCEXParser::stashStream=Serial; // keep compiler happy but ovevride in constructor
DCCEXParser::DCCEXParser(Stream & myStream) {
stream=myStream;
}
// 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.
// It knows nothing about hardware or tracks... it just parses strings and // It knows nothing about hardware or tracks... it just parses strings and
// calls the corresponding DCC api. // calls the corresponding DCC api.
// Non-DCC things like turnouts, pins and sensors are handled in additional JMRI interface classes. // Non-DCC things like turnouts, pins and sensors are handled in additional JMRI interface classes.
void DCCEXParser::loop() { DCCEXParser::DCCEXParser() {}
void DCCEXParser::loop(Stream & stream) {
//DIAG(F("\nDCCEXParser Loop in %d "),stream.available());
while(stream.available()) { while(stream.available()) {
if (bufferLength==MAX_BUFFER) { if (bufferLength==MAX_BUFFER) {
bufferLength=0; bufferLength=0;
@ -39,13 +39,14 @@ void DCCEXParser::loop() {
} }
else if (ch == '>') { else if (ch == '>') {
buffer[bufferLength]='\0'; buffer[bufferLength]='\0';
parse(buffer); parse( stream, buffer);
inCommandPayload = false; inCommandPayload = false;
break; break;
} else if(inCommandPayload) { } else if(inCommandPayload) {
buffer[bufferLength++]= ch; buffer[bufferLength++]= ch;
} }
} }
//DIAG(F(" out\n"));
} }
int DCCEXParser::splitValues( int result[MAX_PARAMS]) { int DCCEXParser::splitValues( int result[MAX_PARAMS]) {
@ -92,7 +93,7 @@ void DCCEXParser::loop() {
} }
// See documentation on DCC class for info on this section // See documentation on DCC class for info on this section
void DCCEXParser::parse(const char *com) { void DCCEXParser::parse(Stream & stream, const char *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];
int params=splitValues(p); int params=splitValues(p);
@ -117,15 +118,15 @@ void DCCEXParser::parse(const char *com) {
return; return;
case 'T': // TURNOUT <T ...> case 'T': // TURNOUT <T ...>
if (parseT(params,p)) return; if (parseT(stream,params,p)) return;
break; break;
case 'Z': // OUTPUT <Z ...> case 'Z': // OUTPUT <Z ...>
if (parseZ(params,p)) return; if (parseZ(stream,params,p)) return;
break; break;
case 'S': // SENSOR <S ...> case 'S': // SENSOR <S ...>
if (parseS(params,p)) return; if (parseS(stream,params,p)) return;
break; break;
case 'w': // WRITE CV on MAIN <w CAB CV VALUE> case 'w': // WRITE CV on MAIN <w CAB CV VALUE>
@ -201,7 +202,7 @@ void DCCEXParser::parse(const char *com) {
StringFormatter::send(stream, F("<X>")); StringFormatter::send(stream, F("<X>"));
} }
bool DCCEXParser::parseZ( int params, int p[]){ bool DCCEXParser::parseZ( Stream & stream,int params, int p[]){
switch (params) { switch (params) {
@ -232,7 +233,7 @@ bool DCCEXParser::parseZ( int params, int p[]){
//=================================== //===================================
bool DCCEXParser::parseT( int params, int p[]) { bool DCCEXParser::parseT(Stream & 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;
@ -257,7 +258,7 @@ bool DCCEXParser::parseT( int params, int p[]) {
} }
} }
bool DCCEXParser::parseS( int params, int p[]) { bool DCCEXParser::parseS( Stream & stream,int params, int p[]) {
switch(params){ switch(params){
@ -281,7 +282,7 @@ bool DCCEXParser::parseS( int params, int p[]) {
// CALLBACKS must be static // CALLBACKS must be static
bool DCCEXParser::stashCallback(Stream & stream, int p[MAX_PARAMS]) { bool DCCEXParser::stashCallback(Stream & stream,int p[MAX_PARAMS]) {
if (stashBusy) return false; if (stashBusy) return false;
stashBusy=true; stashBusy=true;
stashStream=stream; stashStream=stream;

View File

@ -2,23 +2,22 @@
#define DCCEXParser_h #define DCCEXParser_h
struct DCCEXParser struct DCCEXParser
{ {
DCCEXParser(Stream & myStream); DCCEXParser();
void loop(); void loop(Stream & pstream);
private: private:
static const int MAX_PARAMS=10; // longest command sent in static const int MAX_PARAMS=10; // longest command sent in
static const int MAX_BUFFER=50; // longest command sent in static const int MAX_BUFFER=50; // longest command sent in
Stream & stream; Stream & stream;
byte bufferLength=0; byte bufferLength=0;
bool inCommandPayload=false; bool inCommandPayload=false;
char buffer[MAX_BUFFER]; char buffer[MAX_BUFFER+2];
void parse(const char * command);
int splitValues( int result[MAX_PARAMS]); int splitValues( int result[MAX_PARAMS]);
void parse(Stream & stream, const char * command);
bool parseT(int params, int p[]); bool parseT(Stream & stream, int params, int p[]);
bool parseZ(int params, int p[]); bool parseZ(Stream & stream, int params, int p[]);
bool parseS( int params, int p[]); bool parseS(Stream & stream, int params, int p[]);
static bool stashBusy; static bool stashBusy;

View File

@ -26,13 +26,12 @@ void StringFormatter::send(Stream & stream,const __FlashStringHelper* format, va
i++; i++;
c=pgm_read_byte_near(flash+i); c=pgm_read_byte_near(flash+i);
switch(c) { switch(c) {
case '%': stream.write('%'); break; case '%': stream.print('%'); break;
case 's': stream.print(va_arg(args, char*)); break; case 's': stream.print(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 '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 'c': stream.write(va_arg(args, int)); break;
case 'f': stream.print(va_arg(args, double), 2); break; case 'f': stream.print(va_arg(args, double), 2); break;
} }
} }

49
WifiInterface.cpp Normal file
View File

@ -0,0 +1,49 @@
//
//#include "WifiInterface.h"
//#include "Config.h"
//#include "DIAG.h"
//
//
//WiFiEspServer WifiInterface::server(WIFI_PORT);
//
//bool WifiInterface::connected=false;
//
//void WifiInterface::setup()
//{
// Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module
// WiFi.init(&Serial1); // initialize ESP module
//
// // check for the presence of the shield
// if (WiFi.status() == WL_NO_SHIELD) {
// Serial.println("WiFi shield not present");
// return;
// }
//
// // attempt to connect to WiFi network
// int status = WL_IDLE_STATUS;
// for (int retries=0;status != WL_CONNECTED && retries<WIFI_CONNECT_RETRIES; retries++) {
// DIAG(F("\nAttempting to connect to WPA SSID: %s\n"),WIFI_SSID);
// delay(100);
// status = WiFi.begin(WIFI_SSID, WIFI_PASS);
// }
//
// if (status==WL_CONNECTED) {
// connected=true;
//
//
// // start the web server on port WIFI_PORT
// server.begin();
//
// // print the SSID of the network you're attached to
// DIAG(F("SSID: %s IP=%s "), WiFi.SSID(), WiFi.localIP());
// }
//}
//
//
//void WifiInterface::loop(DCCEXParser * parser) {
// if (!connected) return existing;
// if (!client) return existing;
// DIAG(F("\nnew Wifi Client connected %s \n"),client.remoteIP());
// if (existing) delete existing;
// return new DCCEXParser(client);
//}

19
WifiInterface.h Normal file
View File

@ -0,0 +1,19 @@
//
//#ifndef WifiInterface_h
//#define WifiInterface_h
//#include <WiFiEsp.h>
//#include "DCCEXParser.h"
//
//class WifiInterface {
//
// public:
// static void setup();
// static DCCEXParser * getSingleClient(DCCEXParser * existing);
//
// private:
// static WiFiEspServer server;
// static bool connected;
//};
//
//#endif
//