mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
Config cleanup & http filter
Optional http filter Additional Firebox motordriver setups (untested) Config.h removed.
This commit is contained in:
parent
a180700f3a
commit
dd09342214
48
CVReader.ino
48
CVReader.ino
|
@ -1,20 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* © 2020, Chris Harlow. All rights reserved.
|
* © 2020, Chris Harlow. All rights reserved.
|
||||||
*
|
*
|
||||||
* This file is part of Asbelos DCC API
|
* This file is a demonstattion of xcalling the 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 <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,6 +11,7 @@
|
||||||
#include "DCCEXParser.h"
|
#include "DCCEXParser.h"
|
||||||
#include "WifiInterface.h"
|
#include "WifiInterface.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef ARDUINO_AVR_UNO
|
#ifdef ARDUINO_AVR_UNO
|
||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
SoftwareSerial Serial1(15,16); // YOU must get thee pins correct to use Wifi on a UNO
|
SoftwareSerial Serial1(15,16); // YOU must get thee pins correct to use Wifi on a UNO
|
||||||
|
@ -43,7 +31,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 myComandFilter(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>
|
||||||
|
@ -56,6 +44,24 @@ void myFilter(Print * stream, byte & opcode, byte & paramCount, int p[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// This is an OPTIONAL example of a HTTP filter...
|
||||||
|
// If you have configured wifi and an HTTP request is received on the Wifi connection
|
||||||
|
// it will normally be rejected 404 Not Found.
|
||||||
|
|
||||||
|
// If you wish to handle HTTP requests, you can create a filter and ask the WifiInterface to
|
||||||
|
// call your code for each detected http request.
|
||||||
|
|
||||||
|
void myHttpFilter(Print * stream, byte * cmd) {
|
||||||
|
(void)cmd; // Avoid compiler warning because this example doesnt use this parameter
|
||||||
|
|
||||||
|
// BEWARE - As soon as you start responding, the cmd buffer is trashed!
|
||||||
|
// You must get everything you need from it before using StringFormatter::send!
|
||||||
|
|
||||||
|
StringFormatter::send(stream,F("HTTP/1.1 200 OK\nContent-Type: text/html\nConnnection: close\n\n"));
|
||||||
|
StringFormatter::send(stream,F("<html><body>This is my HTTP filter responding.<br/></body></html>"));
|
||||||
|
}
|
||||||
|
|
||||||
// Callback functions are necessary if you call any API that must wait for a response from the
|
// Callback functions are necessary if you call any API that must wait for a response from the
|
||||||
// programming track. The API must return immediately otherwise other loop() functions would be blocked.
|
// programming track. The API must return immediately otherwise other loop() functions would be blocked.
|
||||||
// Your callback function will be invoked when the data arrives from the prog track.
|
// Your callback function will be invoked when the data arrives from the prog track.
|
||||||
|
@ -89,7 +95,9 @@ void setup() {
|
||||||
// Note: this provides DCC with two motor drivers, main and prog, which handle the motor shield(s)
|
// Note: this provides DCC with two motor drivers, main and prog, which handle the motor shield(s)
|
||||||
// Standard supported devices have pre-configured macros but custome hardware installations require
|
// Standard supported devices have pre-configured macros but custome hardware installations require
|
||||||
// detailed pin mappings and may also require modified subclasses of the MotorDriver to implement specialist logic.
|
// detailed pin mappings and may also require modified subclasses of the MotorDriver to implement specialist logic.
|
||||||
|
|
||||||
|
// STANDARD_MOTOR_SHIELD, POLOLU_MOTOR_SHIELD, FIREBOX_MK1, FIREBOX_MK1S are pre defined in MotorShields.h
|
||||||
|
|
||||||
DCC::begin(STANDARD_MOTOR_SHIELD);
|
DCC::begin(STANDARD_MOTOR_SHIELD);
|
||||||
|
|
||||||
// Responsibility 3: **Optionally** Start the WiFi interface if required.
|
// Responsibility 3: **Optionally** Start the WiFi interface if required.
|
||||||
|
@ -102,7 +110,10 @@ void setup() {
|
||||||
|
|
||||||
Serial1.begin(WIFI_BAUD);
|
Serial1.begin(WIFI_BAUD);
|
||||||
WifiInterface::setup(Serial1, F("BTHub5-M6PT"), F("49de8d4862"),F("DCCEX"),F("CVReader"),3532);
|
WifiInterface::setup(Serial1, F("BTHub5-M6PT"), F("49de8d4862"),F("DCCEX"),F("CVReader"),3532);
|
||||||
|
// Optionally tell the Wifi parser to use my http filter.
|
||||||
|
// This will intercept http commands from Wifi.
|
||||||
|
WifiInterface::setHTTPCallback(myHttpFilter);
|
||||||
|
|
||||||
// This is just for demonstration purposes
|
// This is just for demonstration purposes
|
||||||
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
|
||||||
|
@ -110,7 +121,8 @@ void setup() {
|
||||||
|
|
||||||
// Optionally tell the command parser to use my example filter.
|
// Optionally tell the command parser to use my example filter.
|
||||||
// This will intercept JMRI commands from both USB and Wifi
|
// This will intercept JMRI commands from both USB and Wifi
|
||||||
DCCEXParser::setFilter(myFilter);
|
DCCEXParser::setFilter(myComandFilter);
|
||||||
|
|
||||||
|
|
||||||
DIAG(F("\nReady for JMRI commands\n"));
|
DIAG(F("\nReady for JMRI commands\n"));
|
||||||
|
|
||||||
|
|
11
DCC.h
11
DCC.h
|
@ -19,8 +19,8 @@
|
||||||
#ifndef DCC_h
|
#ifndef DCC_h
|
||||||
#define DCC_h
|
#define DCC_h
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "Config.h"
|
|
||||||
#include "MotorDriver.h"
|
#include "MotorDriver.h"
|
||||||
|
#include "MotorDrivers.h"
|
||||||
|
|
||||||
typedef void (*ACK_CALLBACK)(int result);
|
typedef void (*ACK_CALLBACK)(int result);
|
||||||
|
|
||||||
|
@ -47,6 +47,15 @@ ITSKIP, // skip to SKIPTARGET if ack true
|
||||||
SKIPTARGET=0xFF // jump to target
|
SKIPTARGET=0xFF // jump to target
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Allocations with memory implications..!
|
||||||
|
// Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created
|
||||||
|
#ifdef ARDUINO_AVR_UNO
|
||||||
|
const byte MAX_LOCOS=20;
|
||||||
|
#else
|
||||||
|
const byte MAX_LOCOS=50;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
class DCC {
|
class DCC {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
*/
|
*/
|
||||||
#ifndef DCCWaveform_h
|
#ifndef DCCWaveform_h
|
||||||
#define DCCWaveform_h
|
#define DCCWaveform_h
|
||||||
#include "Config.h"
|
|
||||||
#include "MotorDriver.h"
|
#include "MotorDriver.h"
|
||||||
|
|
||||||
const int POWER_SAMPLE_ON_WAIT = 100;
|
const int POWER_SAMPLE_ON_WAIT = 100;
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
/*
|
|
||||||
* © 2020, Chris Harlow. All rights reserved.
|
|
||||||
*
|
|
||||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include "HTTPParser.h"
|
|
||||||
#include "StringFormatter.h"
|
|
||||||
|
|
||||||
void HTTPParser::parse(Print & stream, byte * cmd) {
|
|
||||||
(void)cmd; // Avoid compiler warning because this example doesnt use this parameter
|
|
||||||
|
|
||||||
// BEWARE - As soon as you start responding, the cmd buffer is trashed!
|
|
||||||
// You must get everything you need from it before using StringFormatter::send!
|
|
||||||
|
|
||||||
StringFormatter::send(stream,F("HTTP/1.1 200 OK\nContent-Type: text/html\nConnnection: close\n\n"));
|
|
||||||
StringFormatter::send(stream,F("<html><body>This is <b>not</b> a web server.<br/></body></html>"));
|
|
||||||
}
|
|
26
HTTPParser.h
26
HTTPParser.h
|
@ -1,26 +0,0 @@
|
||||||
/*
|
|
||||||
* © 2020, Chris Harlow. All rights reserved.
|
|
||||||
*
|
|
||||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef HTTPParser_h
|
|
||||||
#define HTTPParser_h
|
|
||||||
#include <Arduino.h>
|
|
||||||
class HTTPParser {
|
|
||||||
public:
|
|
||||||
static void parse(Print & stream, byte * cmd);
|
|
||||||
};
|
|
||||||
#endif
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef Config_h
|
#ifndef MotorDrivers_h
|
||||||
#define Config_h
|
#define MotorDrivers_h
|
||||||
|
|
||||||
// *** PLEASE NOTE *** THIS FILE IS **NOT** INTENDED TO BE EDITED WHEN CONFIGURING A SYSTEM.
|
// *** PLEASE NOTE *** THIS FILE IS **NOT** INTENDED TO BE EDITED WHEN CONFIGURING A SYSTEM.
|
||||||
// It will be overwritten if the library is updated.
|
// It will be overwritten if the library is updated.
|
||||||
|
@ -25,12 +25,16 @@ const byte UNUSED_PIN = 255;
|
||||||
new MotorDriver(4, 7, UNUSED_PIN, 9 , A0, 18, 2000, 12), \
|
new MotorDriver(4, 7, UNUSED_PIN, 9 , A0, 18, 2000, 12), \
|
||||||
new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, 250 , UNUSED_PIN)
|
new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, 250 , UNUSED_PIN)
|
||||||
|
|
||||||
// Allocations with memory implications..!
|
// Firebox Mk1
|
||||||
// Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created
|
#define FIREBOX_MK1 \
|
||||||
#ifdef ARDUINO_AVR_UNO
|
new MotorDriver(3, 6, 7, UNUSED_PIN, A5, ??, 5500, UNUSED_PIN), \
|
||||||
const byte MAX_LOCOS=20;
|
new MotorDriver(4, 8, 9, UNUSED_PIN, A1, ??, 250 , UNUSED_PIN)
|
||||||
#else
|
|
||||||
const byte MAX_LOCOS=50;
|
// Firebox Mk1S
|
||||||
#endif
|
#define FIREBOX_MK1S \
|
||||||
|
new MotorDriver(24, 21, 22, 25, 23, ??, 5500, UNUSED_PIN), \
|
||||||
|
new MotorDriver(30, 27, 28, 31, 29, ??, 250 , UNUSED_PIN)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -17,11 +17,9 @@
|
||||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "WifiInterface.h"
|
#include "WifiInterface.h"
|
||||||
#include "Config.h"
|
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
#include "WiThrottle.h"
|
#include "WiThrottle.h"
|
||||||
#include "HTTPParser.h"
|
|
||||||
const char PROGMEM READY_SEARCH[] ="\r\nready\r\n";
|
const char PROGMEM READY_SEARCH[] ="\r\nready\r\n";
|
||||||
const char PROGMEM OK_SEARCH[] ="\r\nOK\r\n";
|
const char PROGMEM OK_SEARCH[] ="\r\nOK\r\n";
|
||||||
const char PROGMEM END_DETAIL_SEARCH[] ="@ 1000";
|
const char PROGMEM END_DETAIL_SEARCH[] ="@ 1000";
|
||||||
|
@ -38,6 +36,8 @@ 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;
|
Stream * WifiInterface::wifiStream=NULL;
|
||||||
|
HTTP_CALLBACK WifiInterface::httpCallback=0;
|
||||||
|
|
||||||
|
|
||||||
void WifiInterface::setup(Stream & setupStream, 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) {
|
||||||
|
@ -114,6 +114,10 @@ void WifiInterface::ATCommand(const byte * command) {
|
||||||
checkForOK(10000,OK_SEARCH,true);
|
checkForOK(10000,OK_SEARCH,true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WifiInterface::setHTTPCallback(HTTP_CALLBACK callback) {
|
||||||
|
httpCallback=callback;
|
||||||
|
}
|
||||||
|
|
||||||
bool WifiInterface::checkForOK( 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;
|
||||||
|
@ -136,7 +140,7 @@ bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiInterface::isHTML() {
|
bool WifiInterface::isHTTP() {
|
||||||
|
|
||||||
// POST GET PUT PATCH DELETE
|
// POST GET PUT PATCH DELETE
|
||||||
// You may think a simple strstr() is better... but not when ram & time is in short supply
|
// You may think a simple strstr() is better... but not when ram & time is in short supply
|
||||||
|
@ -239,9 +243,14 @@ void WifiInterface::loop() {
|
||||||
// Otherwise we would have to copy the buffer elsewhere and RAM is in short supply.
|
// Otherwise we would have to copy the buffer elsewhere and RAM is in short supply.
|
||||||
|
|
||||||
closeAfter=false;
|
closeAfter=false;
|
||||||
|
|
||||||
// Intercept HTTP requests
|
// Intercept HTTP requests
|
||||||
if (isHTML()) {
|
if (isHTTP()) {
|
||||||
HTTPParser::parse(streamer,buffer);
|
if (httpCallback) httpCallback(&streamer,buffer);
|
||||||
|
else {
|
||||||
|
StringFormatter::send(streamer,F("HTTP/1.1 404 Not Found\nContent-Type: text/html\nConnnection: close\n\n"));
|
||||||
|
StringFormatter::send(streamer,F("<html><body>This is <b>not</b> a web server.<br/></body></html>"));
|
||||||
|
}
|
||||||
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
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
typedef void (*HTTP_CALLBACK)(Print * stream, byte * cmd);
|
||||||
|
|
||||||
class WifiInterface {
|
class WifiInterface {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -31,7 +33,7 @@ class WifiInterface {
|
||||||
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
|
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
|
||||||
static void loop();
|
static void loop();
|
||||||
static void ATCommand(const byte * command);
|
static void ATCommand(const byte * command);
|
||||||
|
static void setHTTPCallback(HTTP_CALLBACK callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Stream * wifiStream;
|
static Stream * wifiStream;
|
||||||
|
@ -39,7 +41,8 @@ class WifiInterface {
|
||||||
static bool setup2( 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(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 isHTTP();
|
||||||
|
static HTTP_CALLBACK httpCallback;
|
||||||
static bool connected;
|
static bool connected;
|
||||||
static bool closeAfter;
|
static bool closeAfter;
|
||||||
static byte loopstate;
|
static byte loopstate;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user