1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-07-29 02:13:45 +02:00

Compare commits

..

22 Commits

Author SHA1 Message Date
pmantoine
c99eac6ada STM32 platformio.ini PeripheralPins update 2025-02-11 11:47:58 +11:00
pmantoine
c93dd75323 Upgrade ststm32 to v18.0.0 for PeripheralPins issue 2024-12-12 15:18:37 +08:00
Harald Barth
519cabffb6 version tag 2024-12-05 22:10:55 +01:00
Harald Barth
aa4306123d more explaining 2024-12-05 22:10:29 +01:00
Harald Barth
ccbff56355 rename to STM32lwipopts.h.copyme because of #include hell 2024-12-05 22:04:44 +01:00
Harald Barth
e0aa16ff2c adjust max default TCP clients to 9 because of bug in LwIP 2024-12-05 22:03:55 +01:00
pmantoine
6b0dc272ea Fixes for Mega, HTONS/L macro 2024-11-27 19:59:15 +08:00
Harald Barth
9602c32ea7 mDNS malloc error fix 2024-11-16 20:50:25 +01:00
Harald Barth
1235c288dc make it compile on all default platforms 2024-11-10 21:11:40 +01:00
Harald Barth
001c4664c1 as MDNS_Generic does not work and we now have our own, remove it 2024-11-10 20:32:06 +01:00
Harald Barth
9786ea9b3a fix compiler warnings 2024-11-10 20:25:20 +01:00
Harald Barth
6a35daab6b remove dead code 2024-11-10 20:20:25 +01:00
Harald Barth
afff10df28 add copyright notices 2024-11-10 20:15:45 +01:00
Harald Barth
b9ce166028 remove everything that are not our changes 2024-11-10 20:13:40 +01:00
Harald Barth
0a96320fd0 tidy up values into name, serviceName, serviceProto and packet generation 2024-11-10 19:48:52 +01:00
Harald Barth
9a6e1707e7 add own mDNS files 2024-11-10 11:31:49 +01:00
Harald Barth
64a34b3a32 proto works 2024-11-10 11:31:14 +01:00
Harald Barth
19f4869401 multicast receive tests 2024-11-09 12:57:49 +01:00
Harald Barth
bcdf9cb1c5 Add STM32lwipopts.h 2024-11-04 17:35:34 +01:00
Harald Barth
701f4e852f Variable number of TCP clients 2024-11-04 17:28:51 +01:00
Harald Barth
657fb7009c tag 2024-11-04 17:27:34 +01:00
Harald Barth
e131a9cce8 Ethernet: Reject additonal connection instead of looping on OVERFLOW 2024-11-02 21:25:21 +01:00
51 changed files with 664 additions and 1215 deletions

View File

@@ -56,8 +56,7 @@ template<typename... Targs> void CommandDistributor::broadcastReply(clientType t
#ifdef CD_HANDLE_RING
// wifi or ethernet ring streams with multiple client types
RingStream * CommandDistributor::ring=0;
CommandDistributor::clientType CommandDistributor::clients[8]={
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE};
CommandDistributor::clientType CommandDistributor::clients[MAX_NUM_TCP_CLIENTS]={ NONE_TYPE }; // 0 is and must be NONE_TYPE
// Parse is called by Withrottle or Ethernet interface to determine which
// protocol the client is using and call the appropriate part of dcc++Ex
@@ -280,9 +279,6 @@ void CommandDistributor::broadcastPower() {
state = '1';
}
if (state != '2')
broadcastReply(COMMAND_TYPE, F("<p%c>\n"),state);
// additional info about MAIN, PROG and JOIN
bool main=TrackManager::getMainPower()==POWERMODE::ON;
bool prog=TrackManager::getProgPower()==POWERMODE::ON;
@@ -291,7 +287,7 @@ void CommandDistributor::broadcastPower() {
const FSH * reason=F("");
if (join) {
reason = F(" JOIN"); // with space at start so we can append without space
broadcastReply(COMMAND_TYPE, F("<p1%S>\n"),reason);
broadcastReply(COMMAND_TYPE, F("<p1 %S>\n"),reason);
} else {
if (main) {
//reason = F("MAIN");
@@ -302,6 +298,9 @@ void CommandDistributor::broadcastPower() {
broadcastReply(COMMAND_TYPE, F("<p1 PROG>\n"));
}
}
if (state != '2')
broadcastReply(COMMAND_TYPE, F("<p%c>\n"),state);
#ifdef CD_HANDLE_RING
// send '1' if all main are on, otherwise global state (which in that case is '0' or '2')
broadcastReply(WITHROTTLE_TYPE, F("PPA%c\n"), main?'1': state);
@@ -323,11 +322,11 @@ void CommandDistributor::broadcastTrackState(const FSH* format, byte trackLetter
broadcastReply(COMMAND_TYPE, format, trackLetter, modename, dcAddr);
}
void CommandDistributor::broadcastRouteState(int16_t routeId, byte state ) {
void CommandDistributor::broadcastRouteState(uint16_t routeId, byte state ) {
broadcastReply(COMMAND_TYPE, F("<jB %d %d>\n"),routeId,state);
}
void CommandDistributor::broadcastRouteCaption(int16_t routeId, const FSH* caption ) {
void CommandDistributor::broadcastRouteCaption(uint16_t routeId, const FSH* caption ) {
broadcastReply(COMMAND_TYPE, F("<jB %d \"%S\">\n"),routeId,caption);
}

View File

@@ -36,13 +36,13 @@
class CommandDistributor {
public:
enum clientType: byte {NONE_TYPE,COMMAND_TYPE,WITHROTTLE_TYPE};
enum clientType: byte {NONE_TYPE=0,COMMAND_TYPE,WITHROTTLE_TYPE};
private:
static void broadcastToClients(clientType type);
static StringBuffer * broadcastBufferWriter;
#ifdef CD_HANDLE_RING
static RingStream * ring;
static clientType clients[8];
static clientType clients[MAX_NUM_TCP_CLIENTS];
#endif
public :
static void parse(byte clientId,byte* buffer, RingStream * ring);
@@ -59,8 +59,8 @@ public :
static void broadcastTrackState(const FSH* format,byte trackLetter, const FSH* modename, int16_t dcAddr);
template<typename... Targs> static void broadcastReply(clientType type, Targs... msg);
static void forget(byte clientId);
static void broadcastRouteState(int16_t routeId,byte state);
static void broadcastRouteCaption(int16_t routeId,const FSH * caption);
static void broadcastRouteState(uint16_t routeId,byte state);
static void broadcastRouteCaption(uint16_t routeId,const FSH * caption);
static void broadcastMessage(char * message);
// Handling code for virtual LCD receiver.

View File

@@ -51,12 +51,6 @@
#include "DCCEX.h"
#include "Display_Implementation.h"
#ifdef ARDUINO_ARCH_ESP32
#include "Sniffer.h"
#include "DCCDecoder.h"
Sniffer *dccSniffer = NULL;
bool DCCDecoder::active = false;
#endif // ARDUINO_ARCH_ESP32
#ifdef CPU_TYPE_ERROR
#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH THE ARCHITECTURES LISTED IN defines.h
@@ -130,11 +124,6 @@ void setup()
// Start RMFT aka EX-RAIL (ignored if no automnation)
RMFT::begin();
#ifdef ARDUINO_ARCH_ESP32
#ifdef BOOSTER_INPUT
dccSniffer = new Sniffer(BOOSTER_INPUT);
#endif // BOOSTER_INPUT
#endif // ARDUINO_ARCH_ESP32
// Invoke any DCC++EX commands in the form "SETUP("xxxx");"" found in optional file mySetup.h.
// This can be used to create turnouts, outputs, sensors etc. through the normal text commands.
@@ -152,28 +141,25 @@ void setup()
CommandDistributor::broadcastPower();
}
void loop()
/**************** for future reference
void looptimer(unsigned long timeout, const FSH* message)
{
#ifdef ARDUINO_ARCH_ESP32
#ifdef BOOSTER_INPUT
static bool oldactive = false;
if (dccSniffer) {
bool newactive = dccSniffer->inputActive();
if (oldactive != newactive) {
RMFT2::railsyncEvent(newactive);
oldactive = newactive;
}
DCCPacket p = dccSniffer->fetchPacket();
if (p.len() != 0) {
if (DCCDecoder::parse(p)) {
if (Diag::SNIFFER)
p.print();
}
static unsigned long lasttimestamp = 0;
unsigned long now = micros();
if (timeout != 0) {
unsigned long diff = now - lasttimestamp;
if (diff > timeout) {
DIAG(message);
DIAG(F("DeltaT=%L"), diff);
lasttimestamp = micros();
return;
}
}
#endif // BOOSTER_INPUT
#endif // ARDUINO_ARCH_ESP32
lasttimestamp = now;
}
*********************************************/
void loop()
{
// The main sketch has responsibilities during loop()
// Responsibility 1: Handle DCC background processes

50
DCC.cpp
View File

@@ -229,9 +229,15 @@ bool DCC::setFn( int cab, int16_t functionNumber, bool on) {
// Flip function state (used from withrottle protocol)
void DCC::changeFn( int cab, int16_t functionNumber) {
auto currentValue=getFn(cab,functionNumber);
if (currentValue<0) return; // function not valid for change
setFn(cab,functionNumber, currentValue?false:true);
if (cab<=0 || functionNumber>31) return;
int reg = lookupSpeedTable(cab);
if (reg<0) return;
unsigned long funcmask = (1UL<<functionNumber);
speedTable[reg].functions ^= funcmask;
if (functionNumber <= 28) {
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
}
CommandDistributor::broadcastLoco(reg);
}
// Report function state (used from withrottle protocol)
@@ -289,7 +295,7 @@ void DCC::setAccessory(int address, byte port, bool gate, byte onoff /*= 2*/) {
// the initial decoders were orgnized and that influenced how the DCC
// standard was made.
#ifdef DIAG_IO
DIAG(F("DCC::setAccessory(%d,%d,%d,%d)"), address, port, gate, onoff);
DIAG(F("DCC::setAccessory(%d,%d,%d)"), address, port, gate);
#endif
// use masks to detect wrong values and do nothing
if(address != (address & 511))
@@ -523,7 +529,6 @@ const ackOp FLASH LOCO_ID_PROG[] = {
V0, WACK, MERGE,
V0, WACK, MERGE,
VB, WACK, NAKSKIP, // bad read of cv20, assume its 0
BAD20SKIP, // detect invalid cv20 value and ignore
STASHLOCOID, // keep cv 20 until we have cv19 as well.
SETCV, (ackOp)19,
STARTMERGE, // Setup to read cv 19
@@ -629,9 +634,7 @@ const ackOp FLASH CONSIST_ID_PROG[] = {
BASELINE,
SETCV,(ackOp)20,
SETBYTEH, // high byte to CV 20
WB,WACK,ITSKIP,
FAIL_IF_NONZERO_NAK, // fail if writing long address to decoder that cant support it
SKIPTARGET,
WB,WACK, // ignore dedcoder without cv20 support
SETCV,(ackOp)19,
SETBYTEL, // low byte of word
WB,WACK,ITC1, // If ACK, we are done - callback(1) means Ok
@@ -762,15 +765,7 @@ void DCC::issueReminders() {
if (!DCCWaveform::mainTrack.isReminderWindowOpen()) return;
// Move to next loco slot. If occupied, send a reminder.
int reg = lastLocoReminder+1;
if (reg > highestUsedReg) {
if (loopStatus == 0 /*only needed if numLocos == 1 but we do not have a counter*/) {
// insert idle packet in the speed packet loop to fullfill the *censored*
// >5ms between packets to same decoder rule
const byte idlepacket[] = {0xFF, 0x00};
DCCWaveform::mainTrack.schedulePacket(idlepacket, 2, 0);
}
reg = 0; // Go to start of table
}
if (reg > highestUsedReg) reg = 0; // Go to start of table
if (speedTable[reg].loco > 0) {
// have found loco to remind
if (issueReminder(reg))
@@ -791,23 +786,40 @@ bool DCC::issueReminder(int reg) {
break;
case 1: // remind function group 1 (F0-F4)
if (flags & FN_GROUP_1)
#ifndef DISABLE_FUNCTION_REMINDERS
setFunctionInternal(loco,0, 128 | ((functions>>1)& 0x0F) | ((functions & 0x01)<<4),0); // 100D DDDD
#else
setFunctionInternal(loco,0, 128 | ((functions>>1)& 0x0F) | ((functions & 0x01)<<4),2);
flags&= ~FN_GROUP_1; // dont send them again
#endif
break;
case 2: // remind function group 2 F5-F8
if (flags & FN_GROUP_2)
#ifndef DISABLE_FUNCTION_REMINDERS
setFunctionInternal(loco,0, 176 | ((functions>>5)& 0x0F),0); // 1011 DDDD
#else
setFunctionInternal(loco,0, 176 | ((functions>>5)& 0x0F),2);
flags&= ~FN_GROUP_2; // dont send them again
#endif
break;
case 3: // remind function group 3 F9-F12
if (flags & FN_GROUP_3)
#ifndef DISABLE_FUNCTION_REMINDERS
setFunctionInternal(loco,0, 160 | ((functions>>9)& 0x0F),0); // 1010 DDDD
#else
setFunctionInternal(loco,0, 160 | ((functions>>9)& 0x0F),2);
flags&= ~FN_GROUP_3; // dont send them again
#endif
break;
case 4: // remind function group 4 F13-F20
if (flags & FN_GROUP_4)
setFunctionInternal(loco,222, ((functions>>13)& 0xFF),0);
setFunctionInternal(loco,222, ((functions>>13)& 0xFF),2);
flags&= ~FN_GROUP_4; // dont send them again
break;
case 5: // remind function group 5 F21-F28
if (flags & FN_GROUP_5)
setFunctionInternal(loco,223, ((functions>>21)& 0xFF),0);
setFunctionInternal(loco,223, ((functions>>21)& 0xFF),2);
flags&= ~FN_GROUP_5; // dont send them again
break;
}
loopStatus++;

View File

@@ -67,24 +67,16 @@ CALLBACK_STATE DCCACK::callbackState=READY;
ACK_CALLBACK DCCACK::ackManagerCallback;
void DCCACK::Setup(int cv, byte byteValueOrBitnum, ackOp const program[], ACK_CALLBACK callback) {
// On ESP32 the joined track is hidden from sight (it has type MAIN)
// and because of that we need first check if track was joined and
// then unjoin if necessary. This requires that the joined flag is
// cleared when the prog track is removed.
ackManagerRejoin=TrackManager::isJoined();
//DIAG(F("Joined is %d"), ackManagerRejoin);
if (ackManagerRejoin) {
// Change from JOIN must zero resets packet.
TrackManager::setJoin(false);
DCCWaveform::progTrack.clearResets();
}
progDriver=TrackManager::getProgDriver();
//DIAG(F("Progdriver is %d"), progDriver);
if (progDriver==NULL) {
if (ackManagerRejoin) {
DIAG(F("Joined but no Prog track"));
TrackManager::setJoin(false);
}
TrackManager::setJoin(ackManagerRejoin);
callback(-3); // we dont have a prog track!
return;
}
@@ -347,20 +339,6 @@ void DCCACK::loop() {
opcode=GETFLASH(ackManagerProg);
}
break;
case BAD20SKIP:
if (ackManagerByte > 120) {
// skip to SKIPTARGET if cv20 is >120 (some decoders respond with 255)
if (Diag::ACK) DIAG(F("XX cv20=%d "),ackManagerByte);
while (opcode!=SKIPTARGET) {
ackManagerProg++;
opcode=GETFLASH(ackManagerProg);
}
}
break;
case FAIL_IF_NONZERO_NAK: // fail if writing long address to decoder that cant support it
if (ackManagerByte==0) break;
callback(-4);
return;
case SKIPTARGET:
break;
default:

View File

@@ -58,8 +58,6 @@ enum ackOp : byte
ITSKIP, // skip to SKIPTARGET if ack true
NAKSKIP, // skip to SKIPTARGET if ack false
COMBINE1920, // combine cvs 19 and 20 and callback
BAD20SKIP, // skip to SKIPTARGET if cv20 is >120 (some decoders respond with 255)
FAIL_IF_NONZERO_NAK, // fail if writing long address to decoder that cant support it
SKIPTARGET = 0xFF // jump to target
};

View File

@@ -1,178 +0,0 @@
/*
* © 2025 Harald Barth
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
#ifdef ARDUINO_ARCH_ESP32
#include "DCCDecoder.h"
#include "LocoTable.h"
#include "DCCEXParser.h"
#include "DIAG.h"
#include "DCC.h"
bool DCCDecoder::parse(DCCPacket &p) {
if (!active)
return false;
const byte DECODER_MOBILE = 1;
const byte DECODER_ACCESSORY = 2;
byte decoderType = 0; // use 0 as none
byte *d = p.data();
byte *instr = 0; // will be set to point to the instruction part of the DCC packet (instr[0] to instr[n])
uint16_t addr; // will be set to decoder addr (long/shor mobile or accessory)
bool locoInfoChanged = false;
if (d[0] == 0B11111111) { // Idle packet
return false;
}
// CRC verification here
byte checksum = 0;
for (byte n = 0; n < p.len(); n++)
checksum ^= d[n];
if (checksum) { // Result should be zero, if not it's an error!
if (Diag::SNIFFER) {
DIAG(F("Checksum error"));
p.print();
}
return false;
}
/*
Serial.print("< ");
for(int n=0; n<8; n++) {
Serial.print(d[0]&(1<<n)?"1":"0");
}
Serial.println(" >");
*/
if (bitRead(d[0],7) == 0) { // bit7 == 0 => loco short addr
decoderType = DECODER_MOBILE;
instr = d+1;
addr = d[0];
} else {
if (bitRead(d[0],6) == 1) { // bit7 == 1 and bit6 == 1 => loco long addr
decoderType = DECODER_MOBILE;
instr = d+2;
addr = 256 * (d[0] & 0B00111111) + d[1];
} else { // bit7 == 1 and bit 6 == 0
decoderType = DECODER_ACCESSORY;
instr = d+1;
addr = d[0] & 0B00111111;
}
}
if (decoderType == DECODER_MOBILE) {
switch (instr[0] & 0xE0) {
case 0x20: // 001x-xxxx Extended commands
if (instr[0] == 0B00111111) { // 128 speed steps
if ((locoInfoChanged = LocoTable::updateLoco(addr, instr[1])) == true) {
byte speed = instr[1] & 0B01111111;
byte direction = instr[1] & 0B10000000;
DCC::setThrottle(addr, speed, direction);
//DIAG(F("UPDATE"));
// send speed change to DCCEX here
}
}
break;
case 0x40: // 010x-xxxx 28 (or 14 step) speed we assume 28
case 0x60: // 011x-xxxx
if ((locoInfoChanged = LocoTable::updateLoco(addr, instr[0] & 0B00111111)) == true) {
byte speed = instr[0] & 0B00001111; // first only look at 4 bits
if (speed > 1) { // neither stop nor emergency stop, recalculate speed
speed = ((instr[0] & 0B00001111) << 1) + bitRead(instr[0], 4); // reshuffle bits
speed = (speed - 3) * 9/2;
}
byte direction = instr[0] & 0B00100000;
DCC::setThrottle(addr, speed, direction);
}
break;
case 0x80: // 100x-xxxx Function group 1
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[0], 1)) == true) {
byte normalized = (instr[0] << 1 & 0x1e) | (instr[0] >> 4 & 0x01);
DCCEXParser::funcmap(addr, normalized, 0, 4);
}
break;
case 0xA0: // 101x-xxxx Function group 3 and 2
{
byte low, high;
if (bitRead(instr[0], 4)) {
low = 5;
high = 8;
} else {
low = 9;
high = 12;
}
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[0], low)) == true) {
DCCEXParser::funcmap(addr, instr[0], low, high);
}
}
break;
case 0xC0: // 110x-xxxx Extended (here are functions F13 and up
switch (instr[0] & 0B00011111) {
case 0B00011110: // F13-F20 Function Control
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[0], 13)) == true) {
DCCEXParser::funcmap(addr, instr[1], 13, 20);
}
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[0], 17)) == true) {
DCCEXParser::funcmap(addr, instr[1], 13, 20);
}
break;
case 0B00011111: // F21-F28 Function Control
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[1], 21)) == true) {
DCCEXParser::funcmap(addr, instr[1], 21, 28);
} // updateFunc handles only the 4 low bits as that is the most common case
if ((locoInfoChanged = LocoTable::updateFunc(addr, instr[1]>>4, 25)) == true) {
DCCEXParser::funcmap(addr, instr[1], 21, 28);
}
break;
/* do that later
case 0B00011000: // F29-F36 Function Control
break;
case 0B00011001: // F37-F44 Function Control
break;
case 0B00011010: // F45-F52 Function Control
break;
case 0B00011011: // F53-F60 Function Control
break;
case 0B00011100: // F61-F68 Function Control
break;
*/
}
break;
case 0xE0: // 111x-xxxx Config vars
break;
}
return locoInfoChanged;
}
if (decoderType == DECODER_ACCESSORY) {
if (instr[0] & 0B10000000) { // Basic Accessory
addr = (((~instr[0]) & 0B01110000) << 2) + addr;
byte port = (instr[0] & 0B00000110) >> 1;
byte activate = (instr[0] & 0B00001000) >> 3;
byte coil = (instr[0] & 0B00000001);
locoInfoChanged = true;
//(void)addr; (void)port; (void)coil; (void)activate;
//DIAG(F("HL=%d LL=%d C=%d A=%d"), addr, port, coil, activate);
DCC::setAccessory(addr, port, coil, activate);
} else { // Accessory Extended NMRA spec, do we need to decode this?
/*
addr = (addr << 5) +
((instr[0] & 0B01110000) >> 2) +
((instr[0] & 0B00000110) >> 1);
*/
}
return locoInfoChanged;
}
return false;
}
#endif // ARDUINO_ARCH_ESP32

View File

@@ -1,30 +0,0 @@
/*
* © 2025 Harald Barth
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <Arduino.h>
#include "DCCPacket.h"
class DCCDecoder {
public:
static bool parse(DCCPacket &p);
static inline void onoff(bool on) {active = on;};
private:
static bool active;
};
#endif // ARDUINO_ARCH_ESP32

View File

@@ -3,7 +3,7 @@
* © 2021 Neil McKechnie
* © 2021 Mike S
* © 2021-2024 Herb Morton
* © 2020-2025 Harald Barth
* © 2020-2023 Harald Barth
* © 2020-2021 M Steve Todd
* © 2020-2021 Fred Decker
* © 2020-2021 Chris Harlow
@@ -120,7 +120,6 @@ Once a new OPCODE is decided upon, update this list.
#include "CamParser.h"
#ifdef ARDUINO_ARCH_ESP32
#include "WifiESP32.h"
#include "DCCDecoder.h"
#endif
// This macro can't be created easily as a portable function because the
@@ -168,10 +167,8 @@ int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], byte *cmd,
break;
if (hot == '\0')
return -1;
if (hot == '>') {
*remainingCmd = '\0'; // terminate the cmd string with 0 instead of '>'
if (hot == '>')
return parameterCount;
}
state = 2;
continue;
@@ -268,22 +265,17 @@ void DCCEXParser::parse(const FSH * cmd) {
// See documentation on DCC class for info on this section
void DCCEXParser::parse(Print *stream, byte *com, RingStream *ringStream) {
// This function can get stings of the form "<C OMM AND>" or "C OMM AND>"
// found is true first after the leading "<" has been passed which results
// in parseOne() getting c="C OMM AND>"
byte *cForLater = NULL;
// This function can get stings of the form "<C OMM AND>" or "C OMM AND"
// found is true first after the leading "<" has been passed
bool found = (com[0] != '<');
for (byte *c=com; c[0] != '\0'; c++) {
if (found) {
cForLater = c;
parseOne(stream, c, ringStream);
found=false;
}
if (c[0] == '<') {
if (cForLater) parseOne(stream, cForLater, ringStream);
if (c[0] == '<')
found = true;
}
}
if (cForLater) parseOne(stream, cForLater, ringStream);
}
void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
@@ -410,8 +402,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
|| (p[activep] > 1) || (p[activep] < 0) // invalid activate 0|1
) break;
// Honour the configuration option (config.h) which allows the <a> command to be reversed
// Because of earlier confusion we need to do the same thing under both defines
#if defined(DCC_ACCESSORY_COMMAND_REVERSE)
#ifdef DCC_ACCESSORY_COMMAND_REVERSE
DCC::setAccessory(address, subaddress,p[activep]==0,onoff);
#else
DCC::setAccessory(address, subaddress,p[activep]==1,onoff);
@@ -684,14 +675,6 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
case 'C': // CONFIG <C [params]>
#if defined(ARDUINO_ARCH_ESP32)
// currently this only works on ESP32
if (p[0] == "SNIFFER"_hk) { // <C SNIFFER ON|OFF>
bool on = false;
if (params>1 && p[1] == "ON"_hk) {
on = true;
}
DCCDecoder::onoff(on);
return;
}
#if defined(HAS_ENOUGH_MEMORY)
if (p[0] == "WIFI"_hk) { // <C WIFI SSID PASSWORD>
if (params != 5) // the 5 params 0 to 4 are (kinda): WIFI_hk 0x7777 &SSID 0x7777 &PASSWORD
@@ -1243,9 +1226,6 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
case "LCN"_hk: // <D LCN ON/OFF>
Diag::LCN = onOff;
return true;
case "SNIFFER"_hk: // <D SNIFFER ON/OFF>
Diag::SNIFFER = onOff;
return true;
#endif
#ifndef DISABLE_EEPROM
case "EEPROM"_hk: // <D EEPROM NumEntries>
@@ -1464,7 +1444,6 @@ void DCCEXParser::callback_Wloco(int16_t result)
void DCCEXParser::callback_Wconsist(int16_t result)
{
if (result==-4) DIAG(F("Long Consist %d not supported by decoder"),stashP[1]);
if (result==1) result=stashP[1]; // pick up original requested id from command
StringFormatter::send(getAsyncReplyStream(), F("<w CONSIST %d%S>\n"),
result, stashP[2]=="REVERSE"_hk ? F(" REVERSE") : F(""));

View File

@@ -39,7 +39,6 @@ struct DCCEXParser
static void setRMFTFilter(FILTER_CALLBACK filter);
static void setAtCommandCallback(AT_COMMAND_CALLBACK filter);
static const int MAX_COMMAND_PARAMS=10; // Must not exceed this
static bool funcmap(int16_t cab, byte value, byte fstart, byte fstop);
private:
@@ -78,6 +77,7 @@ struct DCCEXParser
static FILTER_CALLBACK filterCallback;
static FILTER_CALLBACK filterRMFTCallback;
static AT_COMMAND_CALLBACK atCommandCallback;
static bool funcmap(int16_t cab, byte value, byte fstart, byte fstop);
static void sendFlashList(Print * stream,const int16_t flashList[]);
};

View File

@@ -1,83 +0,0 @@
/*
* © 2025 Harald Barth
*
* This file is part of CommandStation-EX
*
* 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 <Arduino.h>
#ifndef DCCPacket_h
#define DCCPacket_h
#include <strings.h>
#include "defines.h"
class DCCPacket {
public:
DCCPacket() {
_len = 0;
_data = NULL;
};
DCCPacket(byte *d, byte l) {
_len = l;
_data = new byte[_len];
for (byte n = 0; n<_len; n++)
_data[n] = d[n];
};
DCCPacket(const DCCPacket &old) {
_len = old._len;
_data = new byte[_len];
for (byte n = 0; n<_len; n++)
_data[n] = old._data[n];
};
DCCPacket &operator=(const DCCPacket &rhs) {
if (this == &rhs)
return *this;
delete[]_data;
_len = rhs._len;
_data = new byte[_len];
for (byte n = 0; n<_len; n++)
_data[n] = rhs._data[n];
return *this;
};
~DCCPacket() {
if (_len) {
delete[]_data;
_len = 0;
_data = NULL;
}
};
inline bool operator==(const DCCPacket &right) {
if (_len != right._len)
return false;
if (_len == 0)
return true;
return (bcmp(_data, right._data, _len) == 0);
};
void print() {
static const char hexchars[]="0123456789ABCDEF";
USB_SERIAL.print(F("<* DCCPACKET "));
for (byte n = 0; n< _len; n++) {
USB_SERIAL.print(hexchars[_data[n]>>4]);
USB_SERIAL.print(hexchars[_data[n] & 0x0f]);
USB_SERIAL.print(' ');
}
USB_SERIAL.print(F("*>\n"));
};
inline byte len() {return _len;};
inline byte *data() {return _data;};
private:
byte _len = 0;
byte *_data = NULL;
};
#endif

View File

@@ -44,12 +44,6 @@ class RMTChannel {
return true;
return dataReady;
};
inline void waitForDataCopy() {
while(1) { // do nothing and wait for interrupt clearing dataReady to happen
if (dataReady == false)
break;
}
};
inline uint32_t packetCount() { return packetCounter; };
private:

View File

@@ -61,7 +61,6 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
void DCCTimer::startRailcomTimer(byte brakePin) {
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
/* The Railcom timer is started in such a way that it
- First triggers 28uS after the last TIMER1 tick.
This provides an accurate offset (in High Accuracy mode)
@@ -114,13 +113,10 @@ void DCCTimer::startRailcomTimer(byte brakePin) {
// it does seems to get a good answer.
TCNT2=193 + (ICR1 - TCNT1)/8;
#endif
}
void DCCTimer::ackRailcomTimer() {
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
OCR2B= 0x00; // brake pin pwm duty cycle 0 at next tick
#endif
}

View File

@@ -78,17 +78,11 @@ int DCCTimer::freeMemory() {
////////////////////////////////////////////////////////////////////////
#ifdef ARDUINO_ARCH_ESP32
#if __has_include("esp_idf_version.h")
#include "esp_idf_version.h"
#endif
#if ESP_IDF_VERSION_MAJOR == 4
// all well correct IDF version
#else
#error "DCC-EX does not support compiling with IDF version 5.0 or later. Downgrade your ESP32 library to a version that contains IDF version 4. Arduino ESP32 library 3.0.0 is too new. Downgrade to one of 2.0.9 to 2.0.17"
#if ESP_IDF_VERSION_MAJOR > 4
#error "DCC-EX does not support compiling with IDF version 5.0 or later. Downgrade your ESP32 library to a version that contains IDE version 4. Arduino ESP32 library 3.0.0 is too new. Downgrade to one of 2.0.9 to 2.0.17"
#endif
// protect all the rest of the code from IDF version 5
#if ESP_IDF_VERSION_MAJOR == 4
#include "DIAG.h"
#include <driver/adc.h>
#include <soc/sens_reg.h>
@@ -203,9 +197,7 @@ void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t frequency
}
void DCCTimer::DCCEXledcDetachPin(uint8_t pin) {
#ifdef DIAG_IO
DIAG(F("Clear pin %d from ledc channel"), pin);
#endif
DIAG(F("Clear pin %d channel"), pin);
pin_to_channel[pin] = 0;
pinMatrixOutDetach(pin, false, false);
}
@@ -330,5 +322,5 @@ void ADCee::scan() {
void ADCee::begin() {
}
#endif //IDF v4
#endif //ESP32

View File

@@ -145,7 +145,7 @@ void DCCWaveform::interrupt2() {
// As we get to the end of the preambles, open the reminder window.
// This delays any reminder insertion until the last moment so
// that the reminder doesn't block a more urgent packet.
reminderWindowOpen=transmitRepeats==0 && remainingPreambles<12 && remainingPreambles>1;
reminderWindowOpen=transmitRepeats==0 && remainingPreambles<4 && remainingPreambles>1;
if (remainingPreambles==1) promotePendingPacket();
else if (remainingPreambles==10 && isMainTrack && railcomActive) DCCTimer::ackRailcomTimer();
// Update free memory diagnostic as we don't have anything else to do this time.
@@ -278,11 +278,7 @@ void DCCWaveform::begin() {
void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repeats) {
if (byteCount > MAX_PACKET_SIZE) return; // allow for chksum
RMTChannel *rmtchannel = (isMainTrack ? rmtMainChannel : rmtProgChannel);
if (rmtchannel == NULL)
return; // no idea to prepare packet if we can not send it anyway
rmtchannel->waitForDataCopy(); // blocking wait so we can write into buffer
byte checksum = 0;
for (byte b = 0; b < byteCount; b++) {
checksum ^= buffer[b];
@@ -300,7 +296,13 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea
{
int ret = 0;
do {
ret = rmtchannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats);
if(isMainTrack) {
if (rmtMainChannel != NULL)
ret = rmtMainChannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats);
} else {
if (rmtProgChannel != NULL)
ret = rmtProgChannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats);
}
} while(ret > 0);
}
}

View File

@@ -188,7 +188,6 @@ Display *Display::loop2(bool force) {
#endif
noMoreRowsToDisplay = false;
slot = 0;
_deviceDriver->setRowNative(slot); // Set position for display
lastScrollTime = currentMillis;
return NULL;
}

View File

@@ -1,23 +1,3 @@
/*
* © 2021 Fred Decker
* All rights reserved.
*
* This file is part of CommandStation-EX
*
* 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 EXRAIL_H
#define EXRAIL_H

View File

@@ -4,7 +4,6 @@
* © 2021-2023 Harald Barth
* © 2020-2023 Chris Harlow
* © 2022-2023 Colin Murdoch
* © 2025 Morten Nielsen
* All rights reserved.
*
* This file is part of CommandStation-EX
@@ -88,10 +87,6 @@ LookList * RMFT2::onClockLookup=NULL;
LookList * RMFT2::onRotateLookup=NULL;
#endif
LookList * RMFT2::onOverloadLookup=NULL;
#ifdef BOOSTER_INPUT
LookList * RMFT2::onRailSyncOnLookup=NULL;
LookList * RMFT2::onRailSyncOffLookup=NULL;
#endif
byte * RMFT2::routeStateArray=nullptr;
const FSH * * RMFT2::routeCaptionArray=nullptr;
int16_t * RMFT2::stashArray=nullptr;
@@ -208,10 +203,6 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) {
onRotateLookup=LookListLoader(OPCODE_ONROTATE);
#endif
onOverloadLookup=LookListLoader(OPCODE_ONOVERLOAD);
#ifdef BOOSTER_INPUT
onRailSyncOnLookup=LookListLoader(OPCODE_ONRAILSYNCON);
onRailSyncOffLookup=LookListLoader(OPCODE_ONRAILSYNCOFF);
#endif
// onLCCLookup is not the same so not loaded here.
// Second pass startup, define any turnouts or servos, set signals red
@@ -321,7 +312,7 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) {
case OPCODE_EXTTTURNTABLE: {
VPIN id=operand;
VPIN pin=getOperand(progCounter,1);
int home=getOperand(progCounter,2);
int home=getOperand(progCounter,3);
setTurntableHiddenState(EXTTTurntable::create(id,pin));
Turntable *tto=Turntable::get(id);
tto->addPosition(0,0,home);
@@ -686,14 +677,13 @@ void RMFT2::loop2() {
break;
case OPCODE_SET:
killBlinkOnVpin(operand);
IODevice::write(operand,true);
break;
case OPCODE_RESET:
{
auto count=getOperand(1);
for (uint16_t i=0;i<count;i++) {
killBlinkOnVpin(operand+i);
IODevice::write(operand+i,opcode==OPCODE_SET);
}
}
killBlinkOnVpin(operand);
IODevice::write(operand,false);
break;
case OPCODE_BLINK:
@@ -880,14 +870,6 @@ void RMFT2::loop2() {
DCC::changeFn(operand,getOperand(1));
break;
case OPCODE_XFWD:
DCC::setThrottle(operand,getOperand(1), true);
break;
case OPCODE_XREV:
DCC::setThrottle(operand,getOperand(1), false);
break;
case OPCODE_DCCACTIVATE: {
// operand is address<<3 | subaddr<<1 | active
int16_t addr=operand>>3;
@@ -939,9 +921,8 @@ void RMFT2::loop2() {
#ifndef DISABLE_PROG
case OPCODE_JOIN:
TrackManager::setPower(POWERMODE::ON);
TrackManager::setJoin(true);
TrackManager::setMainPower(POWERMODE::ON);
TrackManager::setProgPower(POWERMODE::ON);
break;
case OPCODE_UNJOIN:
@@ -1129,11 +1110,7 @@ void RMFT2::loop2() {
case OPCODE_ONROTATE:
#endif
case OPCODE_ONOVERLOAD:
#ifdef BOOSTER_INPUT
case OPCODE_ONRAILSYNCON:
case OPCODE_ONRAILSYNCOFF:
#endif
break;
default:
@@ -1350,24 +1327,12 @@ void RMFT2::clockEvent(int16_t clocktime, bool change) {
void RMFT2::powerEvent(int16_t track, bool overload) {
// Hunt for an ONOVERLOAD for this item
if (Diag::CMD)
DIAG(F("powerEvent : %c"), track + 'A');
DIAG(F("powerEvent : %c"), track);
if (overload) {
onOverloadLookup->handleEvent(F("POWER"),track);
}
}
#ifdef BOOSTER_INPUT
void RMFT2::railsyncEvent(bool on) {
if (Diag::CMD)
DIAG(F("railsyncEvent : %d"), on);
if (on) {
if (onRailSyncOnLookup)
onRailSyncOnLookup->handleEvent(F("RAILSYNCON"), 0);
} else {
if (onRailSyncOffLookup)
onRailSyncOffLookup->handleEvent(F("RAILSYNCOFF"), 0);
}
}
#endif
// This function is used when setting pins so that a SET or RESET
// will cause any blink task on that pin to terminate.
// It will be compiled out of existence if no BLINK feature is used.
@@ -1513,7 +1478,7 @@ void RMFT2::thrungeString(uint32_t strfar, thrunger mode, byte id) {
}
}
void RMFT2::manageRouteState(int16_t id, byte state) {
void RMFT2::manageRouteState(uint16_t id, byte state) {
if (compileFeatures && FEATURE_ROUTESTATE) {
// Route state must be maintained for when new throttles connect.
// locate route id in the Routes lookup
@@ -1525,7 +1490,7 @@ void RMFT2::manageRouteState(int16_t id, byte state) {
CommandDistributor::broadcastRouteState(id,state);
}
}
void RMFT2::manageRouteCaption(int16_t id,const FSH* caption) {
void RMFT2::manageRouteCaption(uint16_t id,const FSH* caption) {
if (compileFeatures && FEATURE_ROUTESTATE) {
// Route state must be maintained for when new throttles connect.
// locate route id in the Routes lookup

View File

@@ -3,7 +3,6 @@
* © 2020-2022 Chris Harlow
* © 2022-2023 Colin Murdoch
* © 2023 Harald Barth
* © 2025 Morten Nielsen
* All rights reserved.
*
* This file is part of CommandStation-EX
@@ -46,7 +45,7 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,OPCODE_TOGGLE_TURNOUT,
OPCODE_ENDIF,OPCODE_ELSE,
OPCODE_DELAY,OPCODE_DELAYMINS,OPCODE_DELAYMS,OPCODE_RANDWAIT,
OPCODE_FON,OPCODE_FOFF,OPCODE_XFON,OPCODE_XFOFF,
OPCODE_FTOGGLE,OPCODE_XFTOGGLE,OPCODE_XFWD,OPCODE_XREV,
OPCODE_FTOGGLE,OPCODE_XFTOGGLE,
OPCODE_RED,OPCODE_GREEN,OPCODE_AMBER,OPCODE_DRIVE,
OPCODE_SERVO,OPCODE_SIGNAL,OPCODE_TURNOUT,OPCODE_WAITFOR,
OPCODE_PAD,OPCODE_FOLLOW,OPCODE_CALL,OPCODE_RETURN,
@@ -73,7 +72,6 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,OPCODE_TOGGLE_TURNOUT,
OPCODE_ACON, OPCODE_ACOF,
OPCODE_ONACON, OPCODE_ONACOF,
OPCODE_ONOVERLOAD,
OPCODE_ONRAILSYNCON,OPCODE_ONRAILSYNCOFF,
OPCODE_ROUTE_ACTIVE,OPCODE_ROUTE_INACTIVE,OPCODE_ROUTE_HIDDEN,
OPCODE_ROUTE_DISABLED,
OPCODE_STASH,OPCODE_CLEAR_STASH,OPCODE_CLEAR_ALL_STASH,OPCODE_PICKUP_STASH,
@@ -189,9 +187,6 @@ class LookList {
static void clockEvent(int16_t clocktime, bool change);
static void rotateEvent(int16_t id, bool change);
static void powerEvent(int16_t track, bool overload);
#ifdef BOOSTER_INPUT
static void railsyncEvent(bool on);
#endif
static bool signalAspectEvent(int16_t address, byte aspect );
// Throttle Info Access functions built by exrail macros
static const byte rosterNameCount;
@@ -259,15 +254,12 @@ private:
static LookList * onRotateLookup;
#endif
static LookList * onOverloadLookup;
#ifdef BOOSTER_INPUT
static LookList * onRailSyncOnLookup;
static LookList * onRailSyncOffLookup;
#endif
static const int countLCCLookup;
static int onLCCLookup[];
static const byte compileFeatures;
static void manageRouteState(int16_t id, byte state);
static void manageRouteCaption(int16_t id, const FSH* caption);
static void manageRouteState(uint16_t id, byte state);
static void manageRouteCaption(uint16_t id, const FSH* caption);
static byte * routeStateArray;
static const FSH ** routeCaptionArray;
static int16_t * stashArray;

View File

@@ -2,7 +2,6 @@
* © 2020-2022 Chris Harlow. All rights reserved.
* © 2022-2023 Colin Murdoch
* © 2023 Harald Barth
* © 2025 Morten Nielsen
*
* This file is part of CommandStation-EX
*
@@ -119,8 +118,6 @@
#undef ONCLOCKTIME
#undef ONCLOCKMINS
#undef ONOVERLOAD
#undef ONRAILSYNCON
#undef ONRAILSYNCOFF
#undef ONGREEN
#undef ONRED
#undef ONROTATE
@@ -197,8 +194,6 @@
#undef XFOFF
#undef XFON
#undef XFTOGGLE
#undef XREV
#undef XFWD
#ifndef RMFT2_UNDEF_ONLY
#define ACTIVATE(addr,subaddr)
@@ -219,12 +214,12 @@
#define BROADCAST(msg)
#define CALL(route)
#define CLEAR_STASH(id)
#define CLEAR_ALL_STASH
#define CLEAR_ALL_STASH(id)
#define CLOSE(id)
#define CONFIGURE_SERVO(vpin,pos1,pos2,profile)
#define DCC_SIGNAL(id,add,subaddr)
#define DCCX_SIGNAL(id,redAspect,amberAspect,greenAspect)
#define DCC_TURNTABLE(id,home,description...)
#define DCC_TURNTABLE(id,home,description)
#define DEACTIVATE(addr,subaddr)
#define DEACTIVATEL(addr)
#define DELAY(mindelay)
@@ -238,7 +233,7 @@
#define ENDTASK
#define ESTOP
#define EXRAIL
#define EXTT_TURNTABLE(id,vpin,home,description...)
#define EXTT_TURNTABLE(id,vpin,home,description)
#define FADE(pin,value,ms)
#define FOFF(func)
#define FOLLOW(route)
@@ -290,8 +285,6 @@
#define ONCLOCKTIME(hours,mins)
#define ONCLOCKMINS(mins)
#define ONOVERLOAD(track_id)
#define ONRAILSYNCON
#define ONRAILSYNCOFF
#define ONDEACTIVATE(addr,subaddr)
#define ONDEACTIVATEL(linear)
#define ONCLOSE(turnout_id)
@@ -316,7 +309,7 @@
#define READ_LOCO
#define RED(signal_id)
#define RESERVE(blockid)
#define RESET(pin,count...)
#define RESET(pin)
#define RESUME
#define RETURN
#define REV(speed)
@@ -342,7 +335,7 @@
#define SERVO2(id,position,duration)
#define SERVO_SIGNAL(vpin,redpos,amberpos,greenpos)
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile,description...)
#define SET(pin,count...)
#define SET(pin)
#define SET_TRACK(track,mode)
#define SET_POWER(track,onoff)
#define SETLOCO(loco)
@@ -372,7 +365,5 @@
#define XFOFF(cab,func)
#define XFON(cab,func)
#define XFTOGGLE(cab,func)
#define XFWD(cab,speed)
#define XREV(cab,speed)
#endif

View File

@@ -3,7 +3,6 @@
* © 2020-2022 Chris Harlow
* © 2022-2023 Colin Murdoch
* © 2023 Harald Barth
* © 2025 Morten Nielsen
* All rights reserved.
*
* This file is part of CommandStation-EX
@@ -517,7 +516,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#ifndef IO_NO_HAL
#define EXTT_TURNTABLE(id,vpin,home,description...) OPCODE_EXTTTURNTABLE,V(id),OPCODE_PAD,V(vpin),OPCODE_PAD,V(home),
#endif
#define FADE(pin,value,ms) OPCODE_SERVO,V(pin),OPCODE_PAD,V(value),OPCODE_PAD,V((int16_t)PCA9685::ProfileType::UseDuration|(int16_t)PCA9685::ProfileType::NoPowerOff),OPCODE_PAD,V(ms/100L),
#define FADE(pin,value,ms) OPCODE_SERVO,V(pin),OPCODE_PAD,V(value),OPCODE_PAD,V(PCA9685::ProfileType::UseDuration|PCA9685::NoPowerOff),OPCODE_PAD,V(ms/100L),
#define FOFF(func) OPCODE_FOFF,V(func),
#define FOLLOW(route) OPCODE_FOLLOW,V(route),
#define FON(func) OPCODE_FON,V(func),
@@ -584,8 +583,6 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define ONCLOCKTIME(hours,mins) OPCODE_ONTIME,V((STRIP_ZERO(hours)*60)+STRIP_ZERO(mins)),
#define ONCLOCKMINS(mins) ONCLOCKTIME(25,mins)
#define ONOVERLOAD(track_id) OPCODE_ONOVERLOAD,V(TRACK_NUMBER_##track_id),
#define ONRAILSYNCON OPCODE_ONRAILSYNCON,0,0,
#define ONRAILSYNCOFF OPCODE_ONRAILSYNCOFF,0,0,
#define ONDEACTIVATE(addr,subaddr) OPCODE_ONDEACTIVATE,V(addr<<2|subaddr),
#define ONDEACTIVATEL(linear) OPCODE_ONDEACTIVATE,V(linear+3),
#define ONGREEN(signal_id) OPCODE_ONGREEN,V(signal_id),
@@ -610,7 +607,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define READ_LOCO OPCODE_READ_LOCO1,0,0,OPCODE_READ_LOCO2,0,0,
#define RED(signal_id) OPCODE_RED,V(signal_id),
#define RESERVE(blockid) OPCODE_RESERVE,V(blockid),
#define RESET(pin,count...) OPCODE_RESET,V(pin),OPCODE_PAD,V(#count[0] ? count+0: 1),
#define RESET(pin) OPCODE_RESET,V(pin),
#define RESUME OPCODE_RESUME,0,0,
#define RETURN OPCODE_RETURN,0,0,
#define REV(speed) OPCODE_REV,V(speed),
@@ -638,7 +635,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define SERVO2(id,position,ms) OPCODE_SERVO,V(id),OPCODE_PAD,V(position),OPCODE_PAD,V(PCA9685::Instant),OPCODE_PAD,V(ms/100L),
#define SERVO_SIGNAL(vpin,redpos,amberpos,greenpos)
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile,description...) OPCODE_SERVOTURNOUT,V(id),OPCODE_PAD,V(pin),OPCODE_PAD,V(activeAngle),OPCODE_PAD,V(inactiveAngle),OPCODE_PAD,V(PCA9685::ProfileType::profile),
#define SET(pin,count...) OPCODE_SET,V(pin),OPCODE_PAD,V(#count[0] ? count+0: 1),
#define SET(pin) OPCODE_SET,V(pin),
#define SET_TRACK(track,mode) OPCODE_SET_TRACK,V(TRACK_MODE_##mode <<8 | TRACK_NUMBER_##track),
#define SET_POWER(track,onoff) OPCODE_SET_POWER,V(TRACK_POWER_##onoff),OPCODE_PAD, V(TRACK_NUMBER_##track),
#define SETLOCO(loco) OPCODE_SETLOCO,V(loco),
@@ -668,8 +665,6 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define XFOFF(cab,func) OPCODE_XFOFF,V(cab),OPCODE_PAD,V(func),
#define XFON(cab,func) OPCODE_XFON,V(cab),OPCODE_PAD,V(func),
#define XFTOGGLE(cab,func) OPCODE_XFTOGGLE,V(cab),OPCODE_PAD,V(func),
#define XFWD(cab,speed) OPCODE_XFWD,V(cab),OPCODE_PAD,V(speed),
#define XREV(cab,speed) OPCODE_XREV,V(cab),OPCODE_PAD,V(speed),
// Build RouteCode
const int StringMacroTracker2=__COUNTER__;

200
EXmDNS.cpp Normal file
View File

@@ -0,0 +1,200 @@
/*
* © 2024 Harald Barth
* © 2024 Paul M. Antoine
* All rights reserved.
*
* This file is part of CommandStation-EX
*
* 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 <Arduino.h>
#include "EthernetInterface.h"
#ifdef DO_MDNS
#include "EXmDNS.h"
// fixed values for mDNS
static IPAddress mdnsMulticastIPAddr = IPAddress(224, 0, 0, 251);
#define MDNS_SERVER_PORT 5353
// dotToLen()
// converts stings of form ".foo.barbar.x" to a string with the
// dots replaced with lenght. So string above would result in
// "\x03foo\x06barbar\x01x" in C notation. If not NULL, *substr
// will point to the beginning of the last component, in this
// example that would be "\x01x".
//
static void dotToLen(char *str, char **substr) {
char *dotplace = NULL;
char *s;
byte charcount = 0;
for (s = str;/*see break*/ ; s++) {
if (*s == '.' || *s == '\0') {
// take care of accumulated
if (dotplace != NULL && charcount != 0) {
*dotplace = charcount;
}
if (*s == '\0')
break;
if (substr && *s == '.')
*substr = s;
// set new values
dotplace = s;
charcount = 0;
} else {
charcount++;
}
}
}
MDNS::MDNS(EthernetUDP& udp) {
_udp = &udp;
}
MDNS::~MDNS() {
_udp->stop();
if (_name) free(_name);
if (_serviceName) free(_serviceName);
if (_serviceProto) free(_serviceProto);
}
int MDNS::begin(const IPAddress& ip, char* name) {
// if we were called very soon after the board was booted, we need to give the
// EthernetShield (WIZnet) some time to come up. Hence, we delay until millis() is at
// least 3000. This is necessary, so that if we need to add a service record directly
// after begin, the announce packet does not get lost in the bowels of the WIZnet chip.
//while (millis() < 3000)
// delay(100);
_ipAddress = ip;
_name = (char *)malloc(strlen(name)+2);
byte n;
for(n = 0; n<strlen(name); n++)
_name[n+1] = name[n];
_name[n+1] = '\0';
_name[0] = '.';
dotToLen(_name, NULL);
return _udp->beginMulticast(mdnsMulticastIPAddr, MDNS_SERVER_PORT);
}
int MDNS::addServiceRecord(const char* name, uint16_t port, MDNSServiceProtocol_t proto) {
// we ignore proto, assume TCP
(void)proto;
_serviceName = (char *)malloc(strlen(name) + 2);
byte n;
for(n = 0; n<strlen(name); n++)
_serviceName[n+1] = name[n];
_serviceName[n+1] = '\0';
_serviceName[0] = '.';
_serviceProto = NULL; //to be filled in
dotToLen(_serviceName, &_serviceProto);
_servicePort = port;
return 1;
}
static char dns_rr_services[] = "\x09_services\x07_dns-sd\x04_udp\x05local";
static char dns_rr_tcplocal[] = "\x04_tcp\x05local";
static char *dns_rr_local = dns_rr_tcplocal + dns_rr_tcplocal[0] + 1;
typedef struct _DNSHeader_t
{
uint16_t xid;
uint16_t flags; // flags condensed
uint16_t queryCount;
uint16_t answerCount;
uint16_t authorityCount;
uint16_t additionalCount;
} __attribute__((__packed__)) DNSHeader_t;
//
// MDNS::run()
// This broadcasts whatever we got evey BROADCASTTIME seconds.
// Why? Too much brokenness i all mDNS implementations available
//
void MDNS::run() {
static long int lastrun = BROADCASTTIME * 1000UL;
unsigned long int now = millis();
if (!(now - lastrun > BROADCASTTIME * 1000UL)) {
return;
}
lastrun = now;
DNSHeader_t dnsHeader = {0, 0, 0, 0, 0, 0};
// DNSHeader_t dnsHeader = { 0 };
_udp->beginPacket(mdnsMulticastIPAddr, MDNS_SERVER_PORT);
// dns header
dnsHeader.flags = HTONS((uint16_t)0x8400); // Response, authorative
dnsHeader.answerCount = HTONS(4 /*5 if TXT but we do not do that */);
_udp->write((uint8_t*)&dnsHeader, sizeof(DNSHeader_t));
// rr #1, the PTR record from generic _services.x.local to service.x.local
_udp->write((uint8_t*)dns_rr_services, sizeof(dns_rr_services));
byte buf[10];
buf[0] = 0x00;
buf[1] = 0x0c; //PTR
buf[2] = 0x00;
buf[3] = 0x01; //IN
*((uint32_t*)(buf+4)) = HTONL(120); //TTL in sec
*((uint16_t*)(buf+8)) = HTONS( _serviceProto[0] + 1 + strlen(dns_rr_tcplocal) + 1);
_udp->write(buf, 10);
_udp->write(_serviceProto,_serviceProto[0]+1);
_udp->write(dns_rr_tcplocal, strlen(dns_rr_tcplocal)+1);
// rr #2, the PTR record from proto.x to name.proto.x
_udp->write(_serviceProto,_serviceProto[0]+1);
_udp->write(dns_rr_tcplocal, strlen(dns_rr_tcplocal)+1);
*((uint16_t*)(buf+8)) = HTONS(strlen(_serviceName) + strlen(dns_rr_tcplocal) + 1); // recycle most of buf
_udp->write(buf, 10);
_udp->write(_serviceName, strlen(_serviceName));
_udp->write(dns_rr_tcplocal, strlen(dns_rr_tcplocal)+1);
// rr #3, the SRV record for the service that points to local name
_udp->write(_serviceName, strlen(_serviceName));
_udp->write(dns_rr_tcplocal, strlen(dns_rr_tcplocal)+1);
buf[1] = 0x21; // recycle most of buf but here SRV
buf[2] = 0x80; // cache flush
*((uint16_t*)(buf+8)) = HTONS(strlen(_name) + strlen(dns_rr_local) + 1 + 6);
_udp->write(buf, 10);
byte srv[6];
// priority and weight
srv[0] = srv[1] = srv[2] = srv[3] = 0;
// port
*((uint16_t*)(srv+4)) = HTONS(_servicePort);
_udp->write(srv, 6);
// target
_udp->write(_name, _name[0]+1);
_udp->write(dns_rr_local, strlen(dns_rr_local)+1);
// rr #4, the A record for the name.local
_udp->write(_name, _name[0]+1);
_udp->write(dns_rr_local, strlen(dns_rr_local)+1);
buf[1] = 0x01; // recycle most of buf but here A
*((uint16_t*)(buf+8)) = HTONS(4);
_udp->write(buf, 10);
byte ip[4];
ip[0] = _ipAddress[0];
ip[1] = _ipAddress[1];
ip[2] = _ipAddress[2];
ip[3] = _ipAddress[3];
_udp->write(ip, 4);
_udp->endPacket();
_udp->flush();
//
}
#endif //DO_MDNS

50
EXmDNS.h Normal file
View File

@@ -0,0 +1,50 @@
/*
* © 2024 Harald Barth
* © 2024 Paul M. Antoine
* All rights reserved.
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
#ifdef DO_MDNS
#define BROADCASTTIME 15 //seconds
// We do this ourselves because every library is different and/or broken...
#define HTONS(x) ((uint16_t)(((x) << 8) | (((x) >> 8) & 0xFF)))
#define HTONL(x) ( ((uint32_t)(x) << 24) | (((uint32_t)(x) << 8) & 0xFF0000) | \
(((uint32_t)(x) >> 8) & 0xFF00) | ((uint32_t)(x) >> 24) )
typedef enum _MDNSServiceProtocol_t
{
MDNSServiceTCP,
MDNSServiceUDP
} MDNSServiceProtocol_t;
class MDNS {
public:
MDNS(EthernetUDP& udp);
~MDNS();
int begin(const IPAddress& ip, char* name);
int addServiceRecord(const char* name, uint16_t port, MDNSServiceProtocol_t proto);
void run();
private:
EthernetUDP *_udp;
IPAddress _ipAddress;
char* _name;
char* _serviceName;
char* _serviceProto;
int _servicePort;
};
#endif //DO_MDNS

View File

@@ -31,13 +31,12 @@
#include "CommandDistributor.h"
#include "WiThrottle.h"
#include "DCCTimer.h"
#if __has_include ( "MDNS_Generic.h")
#include "MDNS_Generic.h"
#define DO_MDNS
EthernetUDP udp;
MDNS mdns(udp);
#endif
#ifdef DO_MDNS
#include "EXmDNS.h"
EthernetUDP udp;
MDNS mdns(udp);
#endif
//extern void looptimer(unsigned long timeout, const FSH* message);
#define looptimer(a,b)
@@ -116,10 +115,10 @@ void EthernetInterface::setup()
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
#ifdef DO_MDNS
mdns.begin(Ethernet.localIP(), WIFI_HOSTNAME); // hostname
if (!mdns.begin(Ethernet.localIP(), (char *)WIFI_HOSTNAME))
DIAG(F("mdns.begin fail")); // hostname
mdns.addServiceRecord(WIFI_HOSTNAME "._withrottle", IP_PORT, MDNSServiceTCP);
// Not sure if we need to run it once, but just in case!
mdns.run();
mdns.run(); // run it right away to get out info ASAP
#endif
connected=true;
}
@@ -144,7 +143,9 @@ void EthernetInterface::acceptClient() { // STM32 version
return;
}
}
DIAG(F("Ethernet OVERFLOW"));
// reached here only if more than MAX_SOCK_NUM clients want to connect
DIAG(F("Ethernet more than %d clients, not accepting new connection"), MAX_SOCK_NUM);
client.stop();
}
#else
void EthernetInterface::acceptClient() { // non-STM32 version

View File

@@ -3,7 +3,7 @@
* © 2021 Neil McKechnie
* © 2021 Mike S
* © 2021 Fred Decker
* © 2020-2022 Harald Barth
* © 2020-2024 Harald Barth
* © 2020-2024 Chris Harlow
* © 2020 Gregor Baues
* All rights reserved.
@@ -31,24 +31,32 @@
#define EthernetInterface_h
#include "defines.h"
#if ETHERNET_ON == true
#include "DCCEXParser.h"
#include <Arduino.h>
//#include <avr/pgmspace.h>
#if defined (ARDUINO_TEENSY41)
#include <NativeEthernet.h> //TEENSY Ethernet Treiber
#include <NativeEthernetUdp.h>
#ifndef MAX_SOCK_NUM
#define MAX_SOCK_NUM 4
#endif
// can't use our MDNS because of a namespace clash with Teensy's NativeEthernet library!
// #define DO_MDNS
#elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI) || defined (ARDUINO_NUCLEO_F4X9ZI)
#include <LwIP.h>
// #include "STM32lwipopts.h"
#include <STM32Ethernet.h>
#include <lwip/netif.h>
extern "C" struct netif gnetif;
#define STM32_ETHERNET
#define MAX_SOCK_NUM 8
#define MAX_SOCK_NUM MAX_NUM_TCP_CLIENTS
#define DO_MDNS
#else
#include "Ethernet.h"
#define DO_MDNS
#endif
#include "RingStream.h"
/**
@@ -77,5 +85,5 @@ class EthernetInterface {
static void dropClient(byte socketnum);
};
#endif // ETHERNET_ON
#endif

View File

@@ -1 +1 @@
#define GITHUB_SHA "devel-202504182148Z"
#define GITHUB_SHA "devel-more-ether-202412052105Z"

View File

@@ -38,7 +38,6 @@
#include "FSH.h"
#include "I2CManager.h"
#include "inttypes.h"
#include "TemplateForEnums.h"
typedef uint16_t VPIN;
// Limit VPIN number to max 32767. Above this number, printing often gives negative values.

View File

@@ -121,7 +121,6 @@ void _begin() {
// Configuration isn't done frequently so we can use blocking I2C calls here, and so buffers can
// be allocated from the stack to reduce RAM allocation.
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
(void)configType; (void)params; // unused
if(_verPrint) DIAG(F("_configure() driver IO_EXSensorCAM v0.%d.%d vpin: %d "), driverVer/100,driverVer%100,vpin);
_verPrint=false; //only give driver versions once
if (paramCount != 1) return false;

View File

@@ -129,7 +129,7 @@ class NeoPixel : public IODevice {
public:
static void create(VPIN vpin, int nPins, uint16_t mode=(NEO_GRB | NEO_KHZ800), I2CAddress i2cAddress=0x60) {
if (checkNoOverlap(vpin, nPins, i2cAddress)) new NeoPixel(vpin, nPins, mode, i2cAddress);
if (checkNoOverlap(vpin, nPins, mode, i2cAddress)) new NeoPixel(vpin, nPins, mode, i2cAddress);
}
private:

View File

@@ -1,130 +0,0 @@
/* Copyright (c) 2023 Harald Barth
*
* This source 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.
*
* This source 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 this software. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "LocoTable.h"
LocoTable::LOCO LocoTable::speedTable[MAX_LOCOS] = { {0,0,0,0,0,0} };
int LocoTable::highestUsedReg = 0;
int LocoTable::lookupSpeedTable(int locoId, bool autoCreate) {
// determine speed reg for this loco
int firstEmpty = MAX_LOCOS;
int reg;
for (reg = 0; reg < MAX_LOCOS; reg++) {
if (speedTable[reg].loco == locoId) break;
if (speedTable[reg].loco == 0 && firstEmpty == MAX_LOCOS) firstEmpty = reg;
}
// return -1 if not found and not auto creating
if (reg == MAX_LOCOS && !autoCreate) return -1;
if (reg == MAX_LOCOS) reg = firstEmpty;
if (reg >= MAX_LOCOS) {
//DIAG(F("Too many locos"));
return -1;
}
if (reg==firstEmpty){
speedTable[reg].loco = locoId;
speedTable[reg].speedCode=128; // default direction forward
speedTable[reg].groupFlags=0;
speedTable[reg].functions=0;
}
if (reg > highestUsedReg) highestUsedReg = reg;
return reg;
}
// returns false only if loco existed but nothing was changed
bool LocoTable::updateLoco(int loco, byte speedCode) {
if (loco==0) {
/*
// broadcast stop/estop but dont change direction
for (int reg = 0; reg < highestUsedReg; reg++) {
if (speedTable[reg].loco==0) continue;
byte newspeed=(speedTable[reg].speedCode & 0x80) | (speedCode & 0x7f);
if (speedTable[reg].speedCode != newspeed) {
speedTable[reg].speedCode = newspeed;
CommandDistributor::broadcastLoco(reg);
}
}
*/
return true;
}
// determine speed reg for this loco
int reg=lookupSpeedTable(loco, false);
if (reg>=0) {
speedTable[reg].speedcounter++;
if (speedTable[reg].speedCode!=speedCode) {
speedTable[reg].speedCode = speedCode;
return true;
} else {
return false;
}
} else {
// new
reg=lookupSpeedTable(loco, true);
if(reg >=0) speedTable[reg].speedCode = speedCode;
return true;
}
}
bool LocoTable::updateFunc(int loco, byte func, int shift) {
unsigned long previous;
unsigned long newfunc;
bool retval = false; // nothing was touched
int reg = lookupSpeedTable(loco, false);
if (reg < 0) { // not found
retval = true;
reg = lookupSpeedTable(loco, true);
newfunc = previous = 0;
} else {
newfunc = previous = speedTable[reg].functions;
}
speedTable[reg].funccounter++;
if(shift == 1) { // special case for light
newfunc &= ~1UL;
newfunc |= ((func & 0B10000) >> 4);
}
newfunc &= ~(0B1111UL << shift);
newfunc |= ((func & 0B1111) << shift);
if (newfunc != previous) {
speedTable[reg].functions = newfunc;
retval = true;
}
return retval;
}
void LocoTable::dumpTable(Stream *output) {
output->print("\n-----------Table---------\n");
for (byte reg = 0; reg <= highestUsedReg; reg++) {
if (speedTable[reg].loco != 0) {
output->print(speedTable[reg].loco);
output->print(' ');
output->print(speedTable[reg].speedCode);
output->print(' ');
output->print(speedTable[reg].functions);
output->print(" #funcpacks:");
output->print(speedTable[reg].funccounter);
output->print(" #speedpacks:");
output->print(speedTable[reg].speedcounter);
speedTable[reg].funccounter = 0;
speedTable[reg].speedcounter = 0;
output->print('\n');
}
}
}

View File

@@ -1,44 +0,0 @@
/* Copyright (c) 2023 Harald Barth
*
* This source 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.
*
* This source 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 this software. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "DCC.h" // fetch MAX_LOCOS from there
class LocoTable {
public:
void forgetLoco(int cab) {
int reg=lookupSpeedTable(cab, false);
if (reg>=0) speedTable[reg].loco=0;
}
static int lookupSpeedTable(int locoId, bool autoCreate);
static bool updateLoco(int loco, byte speedCode);
static bool updateFunc(int loco, byte func, int shift);
static void dumpTable(Stream *output);
private:
struct LOCO
{
int loco;
byte speedCode;
byte groupFlags;
unsigned long functions;
unsigned int funccounter;
unsigned int speedcounter;
};
static LOCO speedTable[MAX_LOCOS];
static int highestUsedReg;
};

View File

@@ -242,7 +242,7 @@ void MotorDriver::setPower(POWERMODE mode) {
// when switching a track On, we need to check the crrentOffset with the pin OFF
if (powerMode==POWERMODE::OFF && currentPin!=UNUSED_PIN) {
senseOffset = ADCee::read(currentPin);
if (Diag::ACK) DIAG(F("Track %c sensOffset=%d"),trackLetter,senseOffset);
DIAG(F("Track %c sensOffset=%d"),trackLetter,senseOffset);
}
IODevice::write(powerPin,invertPower ? LOW : HIGH);

View File

@@ -28,9 +28,12 @@
#include "DCCTimer.h"
#include <wiring_private.h>
#include "TemplateForEnums.h"
// use powers of two so we can do logical and/or on the track modes in if clauses.
// For example TRACK_MODE_DC_INV is (TRACK_MODE_DC|TRACK_MODIFIER_INV)
template<class T> inline T operator~ (T a) { return (T)~(int)a; }
template<class T> inline T operator| (T a, T b) { return (T)((int)a | (int)b); }
template<class T> inline T operator& (T a, T b) { return (T)((int)a & (int)b); }
template<class T> inline T operator^ (T a, T b) { return (T)((int)a ^ (int)b); }
enum TRACK_MODE : byte {
// main modes
TRACK_MODE_NONE = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PROG = 4,

View File

@@ -1,39 +1,77 @@
# What is DCC-EX?
DCC-EX is a team of dedicated enthusiasts producing open source DCC & DC solutions for you to run your complete model railroad layout. Our easy to use, do-it-yourself, and free open source products run on off-the-shelf Arduino technology and are supported by numerous third party hardware and apps like JMRI, Engine Driver, wiThrottle, Rocrail and more.
# What is DCC++ EX?
DCC++ EX is the organization maintaining several codebases that together represent a fully open source DCC system. Currently, this includes the following:
Currently, our products include the following:
* [CommandStation-EX](https://github.com/DCC-EX/CommandStation-EX/releases) - the latest take on the DCC++ command station for controlling your trains. Runs on an Arduino board, and includes advanced features such as a WiThrottle server implementation, turnout operation, general purpose inputs and outputs (I/O), and JMRI integration.
* [exWebThrottle](https://github.com/DCC-EX/exWebThrottle) - a simple web based controller for your DCC++ command station.
* [BaseStation-installer](https://github.com/DCC-EX/BaseStation-Installer) - an installer executable that takes care of downloading and installing DCC++ firmware onto your hardware setup.
* [BaseStation-Classic](https://github.com/DCC-EX/BaseStation-Classic) - the original DCC++ software, packaged in a stable release. No active development, bug fixes only.
* [EX-CommandStation](https://github.com/DCC-EX/CommandStation-EX/releases)
* [EX-WebThrottle](https://github.com/DCC-EX/exWebThrottle)
* [EX-Installer](https://github.com/DCC-EX/EX-Installer)
* [EX-MotoShield8874](https://dcc-ex.com/reference/hardware/motorboards/ex-motor-shield-8874.html#gsc.tab=0)
* [EX-DCCInspector](https://github.com/DCC-EX/DCCInspector-EX)
* [EX-Toolbox](https://github.com/DCC-EX/EX-Toolbox)
* [EX-Turntable](https://github.com/DCC-EX/EX-Turntable)
* [EX-IOExpander](https://github.com/DCC-EX/EX-IOExpander)
* [EX-FastClock](https://github.com/DCC-EX/EX-FastClock)
* [DCCEXProtocol](https://github.com/DCC-EX/DCCEXProtocol)
A basic DCC++ EX hardware setup can use easy to find, widely avalable Arduino boards that you can assemble yourself.
Both CommandStation-EX and BaseStation-Classic support much of the NMRA Digital Command Control (DCC) [standards](http://www.nmra.org/dcc-working-group "NMRA DCC Working Group"), including:
* simultaneous control of multiple locomotives
* 2-byte and 4-byte locomotive addressing
* 28 or 128-step speed throttling
* Activate/de-activate all accessory function addresses 0-2048
* Control of all cab functions F0-F28 and F29-F68
* Main Track: Write configuration variable bytes and set/clear specific configuration variable (CV) bits (aka Programming on Main or POM)
* Programming Track: Same as the main track with the addition of reading configuration variable bytes
* And many more custom features. see [What's new in CommandStation-EX?](#whats-new-in-commandstation-ex)
Details of these projects can be found on [our web site](https://dcc-ex.com/).
# Whats in this Repository?
This repository, CommandStation-EX, contains a complete DCC-EX *EX-CommmandStation* sketch designed for compiling and uploading into an Arduino Uno, Mega, or Nano.
This repository, CommandStation-EX, contains a complete DCC++ EX Commmand Station sketch designed for compiling and uploading into an Arduino Uno, Mega, or Nano.
To utilize this sketch, you can use the following:
1. (recommended for all levels of user) our [automated installer](https://github.com/DCC-EX/EX-Installer)
1. (beginner) our [automated installer](https://github.com/DCC-EX/BaseStation-Installer)
2. (intermediate) download the latest version from the [releases page](https://github.com/DCC-EX/CommandStation-EX/releases)
3. (advanced) use git clone on this repository
Refer to [our web site](https://https://dcc-ex.com/ex-commandstation/get-started/index.html#/) for the hardware required for this project.
Not using the installer? Open the file "CommandStation-EX.ino" in the
Arduino IDE. Please do not rename the folder containing the sketch
code, nor add any files in that folder. The Arduino IDE relies on the
structure and name of the folder to properly display and compile the
code. Rename or copy config.example.h to config.h. If you do not have
the standard setup, you must edit config.h according to the help texts
in config.h.
**We seriously recommend using the EX-Installer**, however if you choose not to use the installer...
## What's new in CommandStation-EX?
* Open the file ``CommandStation-EX.ino`` in the Arduino IDE or Visual Studio Code (VSC). Please do not rename the folder containing the sketch code, nor add any files in that folder. The Arduino IDE relies on the structure and name of the folder to properly display and compile the code.
* Rename or copy ``config.example.h`` to ``config.h``.
* You must edit ``config.h`` according to the help texts in ``config.h``.
* WiThrottle server built in. Connect Engine Driver or WiThrottle clients directly to your Command Station (or through JMRI as before)
* WiFi and Ethernet shield support
* No more jumpers or soldering!
* Direct support for all the most popular motor control boards including single pin (Arduino) or dual pin (IBT_2) type PWM inputs without the need for an adapter circuit
* I2C Display support (LCD and OLED)
* Improved short circuit detection and automatic reset from an overload
* Current reading, sensing and ACK detection settings in milliAmps instead of just pin readings
* Improved adherence to the NMRA DCC specification
* Complete support for all the old commands and front ends like JMRI
* Railcom cutout (beta)
* Simpler, modular, faster code with an API Library for developers for easy expansion
* New features and functions in JMRI
* Ability to join MAIN and PROG tracks into one MAIN track to run your locos
* "Drive-Away" feature - Throttles with support, like Engine Driver, can allow a loco to be programmed on a usable, electrically isolated programming track and then drive off onto the main track
* Diagnostic commands to test decoders that aren't reading or writing correctly
* Support for Uno, Nano, Mega, Nano Every and Teensy microcontrollers
* User Functions: Filter regular commands (like a turnout or output command) and pass it to your own function or accessory
* Support for LCN (layout control nodes)
* mySetup.h file that acts like an Autoexec.Bat command to send startup commands to the CS
* High Accuracty Waveform option for rock steady DCC signals
* New current response outputs current in mA, overlimit current, and maximum board capable current. Support for new current meter in JMRI
* USB Browser based EX-WebThrottle
* New, simpler, function control command
* Number of locos discovery command `<#>`
* Emergency stop command <!>
* Release cabs from memory command <-> all cabs, <- CAB> for just one loco address
* Automatic slot (register) management
* Automation (coming soon)
NOTE: DCC-EX is a major rewrite to the code. We started over and rebuilt it from the ground up! For what that means, you can read [HERE](https://dcc-ex.com/about/rewrite.html).
# More information
You can learn more at the [DCC-EX website](https://dcc-ex.com/)
You can learn more at the [DCC++ EX website](https://dcc-ex.com/)
- November 14, 2020

101
STM32lwipopts.h.copyme Normal file
View File

@@ -0,0 +1,101 @@
/*
* © 2024 Harald Barth
* All rights reserved.
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
//
// Rewrite of the STM32lwipopts.h file from STM
// To be copied into where lwipopts_default.h resides
// typically into STM32Ethernet/src/STM32lwipopts.h
// or STM32Ethernet\src\STM32lwipopts.h
// search for `lwipopts_default.h` and copy this file into the
// same directory but name it STM32lwipopts.h
//
#ifndef __STM32LWIPOPTS_H__
#define __STM32LWIPOPTS_H__
// include this here and then override things we do differnet
#include "lwipopts_default.h"
// we can not include our "defines.h" here
// so we need to duplicate that define
#define MAX_NUM_TCP_CLIENTS_HERE 9
#ifdef MAX_NUM_TCP_CLIENTS
#if MAX_NUM_TCP_CLIENTS != MAX_NUM_TCP_CLIENTS_HERE
#error MAX_NUM_TCP_CLIENTS and MAX_NUM_TCP_CLIENTS_HERE must be same
#endif
#else
#define MAX_NUM_TCP_CLIENTS MAX_NUM_TCP_CLIENTS_HERE
#endif
// increase ARP cache
#undef MEMP_NUM_APR_QUEUE
#define MEMP_NUM_ARP_QUEUE MAX_NUM_TCP_CLIENTS+3 // one for each client (all on different HW) and a few extra
// Example for debug
//#define LWIP_DEBUG 1
//#define TCP_DEBUG LWIP_DBG_ON
// NOT STRICT NECESSARY ANY MORE BUT CAN BE USED TO SAVE RAM
#undef MEM_LIBC_MALLOC
#define MEM_LIBC_MALLOC 1 // use the same malloc as for everything else
#undef MEMP_MEM_MALLOC
#define MEMP_MEM_MALLOC 1 // uses malloc which means no pools which means slower but not mean 32KB up front
#undef MEMP_NUM_TCP_PCB
#define MEMP_NUM_TCP_PCB MAX_NUM_TCP_CLIENTS+1 // one extra so we can reject number N+1 from our code
#define MEMP_NUM_TCP_PCB_LISTEN 6
#undef MEMP_NUM_TCP_SEG
#define MEMP_NUM_TCP_SEG MAX_NUM_TCP_CLIENTS
#undef MEMP_NUM_SYS_TIMEOUT
#define MEMP_NUM_SYS_TIMEOUT MAX_NUM_TCP_CLIENTS+2
#undef PBUF_POOL_SIZE
#define PBUF_POOL_SIZE MAX_NUM_TCP_CLIENTS
#undef LWIO_ICMP
#define LWIP_ICMP 1
#undef LWIP_RAW
#define LWIP_RAW 1 /* PING changed to 1 */
#undef DEFAULT_RAW_RECVMBOX_SIZE
#define DEFAULT_RAW_RECVMBOX_SIZE 3 /* for ICMP PING */
#undef LWIP_DHCP
#define LWIP_DHCP 1
#undef LWIP_UDP
#define LWIP_UDP 1
/*
The STM32F4x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums by hardware:
- To use this feature let the following define uncommented.
- To disable it and process by CPU comment the the checksum.
*/
#if CHECKSUM_GEN_TCP == 1
#error On STM32 TCP checksum should be in HW
#endif
#undef LWIP_IGMP
#define LWIP_IGMP 1
//#define SO_REUSE 1
//#define SO_REUSE_RXTOALL 1
#endif /* __STM32LWIPOPTS_H__ */

View File

@@ -126,33 +126,29 @@ void SerialManager::loop2() {
buffer[0] = '\0';
}
} else { // if (inCommandPayload)
if (bufferLength < (COMMAND_BUFFER_SIZE-1)) {
buffer[bufferLength++] = ch; // advance bufferLength
if (inCommandPayload > PAYLOAD_NORMAL) {
if (inCommandPayload > 32 + 2) { // String way too long
ch = '>'; // we end this nonsense
inCommandPayload = PAYLOAD_NORMAL;
DIAG(F("Parse error: Unbalanced string"));
// fall through to ending parsing below
} else if (ch == '"') { // String end
inCommandPayload = PAYLOAD_NORMAL;
continue; // do not fall through
} else
inCommandPayload++;
}
if (inCommandPayload == PAYLOAD_NORMAL) {
if (ch == '>') {
buffer[bufferLength] = '\0'; // This \0 is after the '>'
DCCEXParser::parse(serial, buffer, NULL); // buffer parsed with trailing '>'
inCommandPayload = PAYLOAD_FALSE;
break;
} else if (ch == '"') {
inCommandPayload = PAYLOAD_STRING;
}
}
} else {
DIAG(F("Parse error: input buffer overflow"));
inCommandPayload = PAYLOAD_FALSE;
if (bufferLength < (COMMAND_BUFFER_SIZE-1))
buffer[bufferLength++] = ch;
if (inCommandPayload > PAYLOAD_NORMAL) {
if (inCommandPayload > 32 + 2) { // String way too long
ch = '>'; // we end this nonsense
inCommandPayload = PAYLOAD_NORMAL;
DIAG(F("Parse error: Unbalanced string"));
// fall through to ending parsing below
} else if (ch == '"') { // String end
inCommandPayload = PAYLOAD_NORMAL;
continue; // do not fall through
} else
inCommandPayload++;
}
if (inCommandPayload == PAYLOAD_NORMAL) {
if (ch == '>') {
buffer[bufferLength] = '\0';
DCCEXParser::parse(serial, buffer, NULL);
inCommandPayload = PAYLOAD_FALSE;
break;
} else if (ch == '"') {
inCommandPayload = PAYLOAD_STRING;
}
}
}
}

View File

@@ -1,241 +0,0 @@
/*
* © 2025 Harald Barth
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
#ifdef ARDUINO_ARCH_ESP32
#include "Sniffer.h"
#include "DIAG.h"
//extern Sniffer *DCCSniffer;
static void packeterror() {
#ifndef WIFI_LED
#ifdef SNIFFER_LED
digitalWrite(SNIFFER_LED,HIGH);
#endif
#endif
}
static void clear_packeterror() {
#ifndef WIFI_LED
#ifdef SNIFFER_LED
digitalWrite(SNIFFER_LED,LOW);
#endif
#endif
}
static bool halfbits2byte(uint16_t b, byte *dccbyte) {
/*
if (b!=0 && b!=0xFFFF) {
Serial.print("[ ");
for(int n=0; n<16; n++) {
Serial.print(b&(1<<n)?"1":"0");
}
Serial.println(" ]");
}
*/
for(byte n=0; n<8; n++) {
switch (b & 0x03) {
case 0x01:
case 0x02:
// broken bits
packeterror();
return false;
break;
case 0x00:
bitClear(*dccbyte, n);
break;
case 0x03:
bitSet(*dccbyte, n);
break;
}
b = b>>2;
}
return true;
}
static void IRAM_ATTR blink_diag(int limit) {
#ifndef WIFI_LED
#ifdef SNIFFER_LED
delay(500);
for (int n=0 ; n<limit; n++) {
digitalWrite(SNIFFER_LED,HIGH);
delay(200);
digitalWrite(SNIFFER_LED,LOW);
delay(200);
}
#endif
#endif
}
static bool IRAM_ATTR cap_ISR_cb(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata,void *user_data) {
if (edata->cap_edge == MCPWM_BOTH_EDGE) {
// should not happen at all
// delays here might crash sketch
blink_diag(2);
return 0;
}
if (user_data) ((Sniffer *)user_data)->processInterrupt(edata->cap_value, edata->cap_edge == MCPWM_POS_EDGE);
//if (DCCSniffer) DCCSniffer->processInterrupt(edata->cap_value, edata->cap_edge == MCPWM_POS_EDGE);
return 0;
}
Sniffer::Sniffer(byte snifferpin) {
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, snifferpin);
// set capture edge, BIT(0) - negative edge, BIT(1) - positive edge
// MCPWM_POS_EDGE|MCPWM_NEG_EDGE should be 3.
//mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_POS_EDGE|MCPWM_NEG_EDGE, 0);
//mcpwm_isr_register(MCPWM_UNIT_0, sniffer_isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL);
//MCPWM0.int_ena.cap0_int_ena = 1; // Enable interrupt on CAP0 signal
mcpwm_capture_config_t MCPWM_cap_config = { //Capture channel configuration
.cap_edge = MCPWM_BOTH_EDGE, // according to mcpwm.h
.cap_prescale = 1, // 1 to 256 (see .h file)
.capture_cb = cap_ISR_cb, // user defined ISR/callback
.user_data = (void *)this // user defined argument to callback
};
#ifndef WIFI_LED
#ifdef SNIFFER_LED
pinMode(SNIFFER_LED ,OUTPUT);
#endif
#endif
blink_diag(3); // so that we know we have SNIFFER_LED
DIAG(F("Init sniffer on pin %d"), snifferpin);
ESP_ERROR_CHECK(mcpwm_capture_enable_channel(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, &MCPWM_cap_config));
}
#define SNIFFER_TIMEOUT 100L // 100 Milliseconds
bool Sniffer::inputActive(){
unsigned long now = millis();
return ((now - lastendofpacket) < SNIFFER_TIMEOUT);
}
#define DCC_TOO_SHORT 4000L // 4000 ticks are 50usec
#define DCC_ONE_LIMIT 6400L // 6400 ticks are 80usec
void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
byte bit = 0;
diffticks = capticks - lastticks;
if (lastedge != posedge) {
if (diffticks < DCC_TOO_SHORT) {
return;
}
if (diffticks < DCC_ONE_LIMIT) {
bit = 1;
} else {
bit = 0;
}
// update state variables for next round
lastticks = capticks;
lastedge = posedge;
bitfield = bitfield << (uint64_t)1;
bitfield = bitfield + (uint64_t)bit;
// now the halfbit is in the bitfield. Analyze...
if ((bitfield & 0xFFFFFF) == 0xFFFFFC){
// This looks at the 24 last halfbits
// and detects a preamble if
// 22 are ONE and 2 are ZERO which is a
// preabmle of 11 ONES and one ZERO
if (inpacket) {
// if we are already inpacket here we
// got a preamble in the middle of a
// packet
packeterror();
} else {
clear_packeterror(); // everything fine again at end of preable after good packet
}
currentbyte = 0;
dcclen = 0;
inpacket = true;
halfbitcounter = 18; // count 18 steps from 17 to 0 and then look at the byte
return;
}
if (inpacket) {
halfbitcounter--;
if (halfbitcounter) {
return; // wait until we have full byte
} else {
// have reached end of byte
//if (currentbyte == 2) debugfield = bitfield;
byte twohalfbits = bitfield & 0x03;
switch (twohalfbits) {
case 0x01:
case 0x02:
// broken bits
inpacket = false;
packeterror();
return;
break;
case 0x00:
case 0x03:
// byte end
uint16_t b = (bitfield & 0x3FFFF)>>2; // take 18 halfbits and use 16 of them
if (!halfbits2byte(b, dccbytes + currentbyte)) {
// broken halfbits
inpacket = false;
packeterror();
return;
}
if (twohalfbits == 0x03) { // end of packet marker
inpacket = false;
dcclen = currentbyte+1;
debugfield = bitfield;
// We have something we want to give to the outpacket queue
// Check length of outpacket queue
if (outpacket.size() > 3) {
// not good, these should have been fetched
// the arbitraty number to check is THREE (see the holy grail)
// blink_diag(1); DO NOT DO THIS HERE -> will crash
packeterror(); // or what to do better?
// take emergency action:
while (!outpacket.empty()) {
outpacket.pop_front();
}
}
lastendofpacket = millis();
DCCPacket temppacket(dccbytes, dcclen);
if (!(temppacket == prevpacket)) {
// we have something new to offer to the fetch routine
// put it into the outpacket queue
outpacket.push_back(temppacket);
prevpacket = temppacket;
}
return;
}
break;
}
halfbitcounter = 18;
currentbyte++; // everything done for this end of byte
if (currentbyte >= MAXDCCPACKETLEN) {
inpacket = false; // this is an error because we should have retured above
packeterror(); // when endof packet marker was active
}
}
}
} else { // lastedge == posedge
// this should not happen, check later
}
}
/*
static void IRAM_ATTR sniffer_isr_handler(void *) {
DCCSniffer.processInterrupt();
}
*/
#endif // ESP32

View File

@@ -1,76 +0,0 @@
/*
* © 2025 Harald Barth
*
* This file is part of CommandStation-EX
*
* 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/>.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <Arduino.h>
#include <list>
#include "driver/mcpwm.h"
#include "soc/mcpwm_struct.h"
#include "soc/mcpwm_reg.h"
#define MAXDCCPACKETLEN 8
#include "DCCPacket.h"
class Sniffer {
public:
Sniffer(byte snifferpin);
void IRAM_ATTR processInterrupt(int32_t capticks, bool posedge);
inline int32_t getTicks() {
noInterrupts();
int32_t i = diffticks;
interrupts();
return i;
};
inline int64_t getDebug() {
noInterrupts();
int64_t i = debugfield;
interrupts();
return i;
};
inline DCCPacket fetchPacket() {
// if there is no new data, this will create a
// packet with length 0 (which is no packet)
DCCPacket p;
noInterrupts();
if (!outpacket.empty()) {
p = outpacket.front();
outpacket.pop_front();
}
interrupts();
return p;
};
bool inputActive();
private:
// keep these vars in processInterrupt only
uint64_t bitfield = 0;
uint64_t debugfield = 0;
int32_t diffticks;
int32_t lastticks;
bool lastedge;
byte currentbyte = 0;
byte dccbytes[MAXDCCPACKETLEN];
byte dcclen = 0;
bool inpacket = false;
// these vars are used as interface to other parts of sniffer
byte halfbitcounter = 0;
std::list<DCCPacket> outpacket;
DCCPacket prevpacket;
volatile unsigned long lastendofpacket = 0; // timestamp millis
};
#endif

View File

@@ -27,7 +27,6 @@ bool Diag::WIFI=false;
bool Diag::WITHROTTLE=false;
bool Diag::ETHERNET=false;
bool Diag::LCN=false;
bool Diag::SNIFFER=false;
void StringFormatter::diag( const FSH* input...) {

View File

@@ -30,7 +30,7 @@ class Diag {
static bool WITHROTTLE;
static bool ETHERNET;
static bool LCN;
static bool SNIFFER;
};
class StringFormatter

View File

@@ -1,26 +0,0 @@
/*
* © 2024, Harald Barth. All rights reserved.
*
* This file is part of DCC-EX
*
* 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 TemplateForEnums
#define TemplateForEnums
template<class T> inline T operator~ (T a) { return (T)~(int)a; }
template<class T> inline T operator| (T a, T b) { return (T)((int)a | (int)b); }
template<class T> inline T operator& (T a, T b) { return (T)((int)a & (int)b); }
template<class T> inline T operator^ (T a, T b) { return (T)((int)a ^ (int)b); }
#endif

View File

@@ -197,7 +197,7 @@ void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {
}
}
bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr, bool offAtChange) {
bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr) {
if (trackToSet>lastTrack || track[trackToSet]==NULL) return false;
// Remember track mode we came from for later
@@ -246,6 +246,9 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
#endif
#ifndef DISABLE_PROG
if (mode & TRACK_MODE_PROG) {
#else
if (false) {
#endif
// only allow 1 track to be prog
FOR_EACH_TRACK(t)
if ( (track[t]->getMode() & TRACK_MODE_PROG) && t != trackToSet) {
@@ -258,7 +261,6 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
} else {
track[trackToSet]->makeProgTrack(false); // only the prog track knows it's type
}
#endif
// When a track is switched, we must clear any side effects of its previous
// state, otherwise trains run away or just dont move.
@@ -356,23 +358,11 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
applyDCSpeed(trackToSet);
}
#ifdef ARDUINO_ARCH_ESP32
#ifndef DISABLE_PROG
if (tempProgTrack == trackToSet && oldmode & TRACK_MODE_MAIN && !(mode & TRACK_MODE_PROG)) {
// If we just take away the prog track, the join should not
// be active either. So do in effect an unjoin
//DIAG(F("Unsync"));
tempProgTrack = MAX_TRACKS+1;
progTrackSyncMain=false;
if (joinRelay!=UNUSED_PIN) digitalWrite(joinRelay,LOW);
}
#endif
#endif
// Turn off power if we changed the mode of this track
if (mode != oldmode && offAtChange) {
if (mode != oldmode)
track[trackToSet]->setPower(POWERMODE::OFF);
}
streamTrackState(NULL,trackToSet);
//DIAG(F("TrackMode=%d"),mode);
return true;
}
@@ -666,9 +656,8 @@ void TrackManager::setJoin(bool joined) {
FOR_EACH_TRACK(t) {
if (track[t]->getMode() & TRACK_MODE_PROG) { // find PROG track
tempProgTrack = t; // remember PROG track
setTrackMode(t, TRACK_MODE_MAIN, 0, false); // 0 = no DC loco; false = do not turn off pwr
// then in some cases setPower() is called
// seperately after the setJoin() as well
setTrackMode(t, TRACK_MODE_MAIN);
track[t]->setPower(POWERMODE::ON); // if joined, always on
break; // there is only one prog track, done
}
}
@@ -676,8 +665,12 @@ void TrackManager::setJoin(bool joined) {
if (tempProgTrack != MAX_TRACKS+1) {
// setTrackMode defaults to power off, so we
// need to preserve that state.
setTrackMode(tempProgTrack, TRACK_MODE_PROG, 0, false); // 0 = no DC loco; false = do not turn off pwr
POWERMODE tPTmode = track[tempProgTrack]->getPower(); // get current power status of this track
setTrackMode(tempProgTrack, TRACK_MODE_PROG); // set track mode back to prog
track[tempProgTrack]->setPower(tPTmode); // set power status as it was before
tempProgTrack = MAX_TRACKS+1;
} else {
DIAG(F("Unjoin but no remembered prog track"));
}
}
#endif

View File

@@ -71,7 +71,7 @@ class TrackManager {
static void setProgPower(POWERMODE mode) {setTrackPower(TRACK_MODE_PROG, mode);}
static const int16_t MAX_TRACKS=8;
static bool setTrackMode(byte track, TRACK_MODE mode, int16_t DCaddr=0, bool offAtChange=true);
static bool setTrackMode(byte track, TRACK_MODE mode, int16_t DCaddr=0);
static bool parseEqualSign(Print * stream, int16_t params, int16_t p[]);
static void loop();
static POWERMODE getMainPower();

View File

@@ -312,6 +312,12 @@
*
*************************************************************************************/
#if defined(DCC_TURNOUTS_RCN_213)
const bool DCCTurnout::rcn213Compliant = true;
#else
const bool DCCTurnout::rcn213Compliant = false;
#endif
// DCCTurnoutData contains data specific to this subclass that is
// written to EEPROM when the turnout is saved.
struct DCCTurnoutData {
@@ -379,10 +385,7 @@
// DCC++ Classic behaviour is that Throw writes a 1 in the packet,
// and Close writes a 0.
// RCN-213 specifies that Throw is 0 and Close is 1.
#ifndef DCC_TURNOUTS_RCN_213
close = !close;
#endif
DCC::setAccessory(_dccTurnoutData.address, _dccTurnoutData.subAddress, close);
DCC::setAccessory(_dccTurnoutData.address, _dccTurnoutData.subAddress, close ^ !rcn213Compliant);
return true;
}

View File

@@ -245,6 +245,8 @@ public:
// Load a VPIN turnout definition from EEPROM. The common Turnout data has already been read at this point.
static Turnout *load(struct TurnoutData *turnoutData);
void print(Print *stream) override;
// Flag whether DCC Accessory packets are to contain 1=close/0=throw(RCN-213) or 1=throw/0-close (DCC++ Classic)
static const bool rcn213Compliant;
protected:
bool setClosedInternal(bool close) override;

View File

@@ -500,9 +500,9 @@ void WiThrottle::getLocoCallback(int16_t locoid) {
char addcmd[20]={'M',stashThrottleChar,'+', addrchar};
itoa(locoid,addcmd+4,10);
stashInstance->multithrottle(stashStream, (byte *)addcmd);
TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away
TrackManager::setMainPower(POWERMODE::ON);
TrackManager::setProgPower(POWERMODE::ON);
TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away
DIAG(F("LocoCallback commit success"));
stashStream->commit();
}

View File

@@ -1,7 +1,7 @@
/*
* © 2022 Paul M. Antoine
* © 2021 Neil McKechnie
* © 2020-2025 Harald Barth
* © 2020-2023 Harald Barth
* © 2020-2021 Fred Decker
* © 2020-2021 Chris Harlow
* © 2023 Nathan Kellenicki
@@ -45,14 +45,15 @@ The configuration file for DCC-EX Command Station
// the correct resistor could damage the sense pin on your Arduino or destroy
// the device.
//
// DEFINE MOTOR_SHIELD_TYPE BELOW. THESE ARE EXAMPLES. Full list in MotorDrivers.h
// DEFINE MOTOR_SHIELD_TYPE BELOW. THESE ARE EXAMPLES. FULL LIST IN MotorDrivers.h
//
// STANDARD_MOTOR_SHIELD : Arduino Motor shield Rev3 based on the L298 with 18V 2A per channel
// POLOLU_MOTOR_SHIELD : Pololu MC33926 Motor Driver (not recommended for prog track)
// FUNDUMOTO_SHIELD : Fundumoto Shield, no current sensing (not recommended, no short protection)
// FIREBOX_MK1 : The Firebox MK1
// FIREBOX_MK1S : The Firebox MK1S
// IBT_2_WITH_ARDUINO : Arduino Motor Shield for PROG and IBT-2 for MAIN
// EX8874_SHIELD : DCC-EX TI DRV8874 based motor shield
// EXCSB1 : DCC-EX CSB-1 hardware
// EXCSB1_WITH_EX8874 : DCC-EX CSB-1 hardware with DCC-EX TI DRV8874 shield
// NO_SHIELD : CS without any motor shield (as an accessory only CS)
// |
// +-----------------------v
//
@@ -80,7 +81,7 @@ The configuration file for DCC-EX Command Station
/////////////////////////////////////////////////////////////////////////////////////
//
// NOTE: Not supported on Arduino Uno or Nano
// NOTE: Only supported on Arduino Mega
// Set to false if you not even want it on the Arduino Mega
//
#define ENABLE_WIFI true
@@ -115,13 +116,13 @@ The configuration file for DCC-EX Command Station
// Your password may not contain ``"'' (double quote, ASCII 0x22).
#define WIFI_PASSWORD "Your network passwd"
//
// WIFI_HOSTNAME: You can change this if you have more than one
// CS to make them show up with different names on the network.
// Otherwise do not touch.
// WIFI_HOSTNAME: You probably don't need to change this
#define WIFI_HOSTNAME "dccex"
//
// WIFI_CHANNEL: The default channel is set to "1". If you need to use an
// alternate channel (we recommend using only 1,6, or 11) you may change it here.
// WIFI_CHANNEL: If the line "#define ENABLE_WIFI true" is uncommented,
// WiFi will be enabled (Mega only). The default channel is set to "1" whether
// this line exists or not. If you need to use an alternate channel (we recommend
// using only 1,6, or 11) you may change it here.
#define WIFI_CHANNEL 1
//
// WIFI_FORCE_AP: If you'd like to specify your own WIFI_SSID in AP mode, set this
@@ -131,12 +132,21 @@ The configuration file for DCC-EX Command Station
/////////////////////////////////////////////////////////////////////////////////////
//
// ENABLE_ETHERNET: Set to true if you have an Arduino Ethernet card (wired) based
// on the W5100/W5500 ethernet chip or an STM32 CS with builin ethernet like the F429ZI.
// This is not for Wifi. You will then need the Arduino Ethernet library as well.
// ENABLE_ETHERNET: Set to true if you have an Arduino Ethernet card (wired). This
// is not for Wifi. You will then need the Arduino Ethernet library as well
//
//#define ENABLE_ETHERNET true
/////////////////////////////////////////////////////////////////////////////////////
//
// MAX_NUM_TCP_CLIENTS: If you on STM32 Ethernet (and only there) want more than
// 9 (*) TCP clients, change this number to for example 20 here **AND** in
// STM32lwiopts.h and follow the instructions in STM32lwiopts.h
//
// (*) It would be 10 if there would not be a bug in LwIP by STM32duino.
//
//#define MAX_NUM_TCP_CLIENTS 20
/////////////////////////////////////////////////////////////////////////////////////
//
@@ -270,9 +280,8 @@ The configuration file for DCC-EX Command Station
// for triggering DCC Accessory Decoders, so that <a addr subaddr 0> generates a
// DCC packet with D=1 (close turnout) and <a addr subaddr 1> generates D=0
// (throw turnout).
//#define DCC_ACCESSORY_COMMAND_REVERSE
//#define DCC_ACCESSORY_RCN_213
//
// HANDLING MULTIPLE SERIAL THROTTLES
// The command station always operates with the default Serial port.
// Diagnostics are only emitted on the default serial port and not broadcast.
@@ -334,7 +343,7 @@ The configuration file for DCC-EX Command Station
// to the sabertooth controller _as_well_. Default: Undefined.
//
//#define SABERTOOTH 1
//
/////////////////////////////////////////////////////////////////////////////////////
//
// SENSORCAM
@@ -346,7 +355,7 @@ The configuration file for DCC-EX Command Station
//#define SENSORCAM2_VPIN 600 //define other CAM's if installed.
//#define CAM2 SENSORCAM2_VPIN+ //for EX-RAIL commands e.g. IFLT(CAM2 020,1)
//
// For smoother power-up, when using the CAM, you may need a STARTUP_DELAY.
// That is described further above.
// For smoother power-up, define a STARTUP_DELAY to allow CAM to initialise ref images
//#define STARTUP_DELAY 5000 // up to 20sec. CS delay
//
/////////////////////////////////////////////////////////////////////////////////////

View File

@@ -239,4 +239,25 @@
#endif
#endif
#if defined(ARDUINO_ARCH_STM32)
// The LwIP library for the STM32 wired ethernet has by default 10 TCP
// clients defined but because of a bug in the library #11 is not
// rejected but kicks out any old connection. By restricting our limit
// to 9 the #10 will be rejected by our code so that the number can
// never get to 11 which would kick an existing connection.
// If you want to change this value, do that in
// config.h AND in STM32lwipopts.h.
#ifndef MAX_NUM_TCP_CLIENTS
#define MAX_NUM_TCP_CLIENTS 9
#endif
#else
#if defined(ARDUINO_ARCH_ESP32)
// Espressif LWIP stack
#define MAX_NUM_TCP_CLIENTS 10
#else
// Wifi shields etc
#define MAX_NUM_TCP_CLIENTS 8
#endif
#endif
#endif //DEFINES_H

View File

@@ -12,10 +12,18 @@
default_envs =
mega2560
uno
unowifiR2
nano
samd21-dev-usb
samd21-zero-usb
ESP32
Nucleo-F411RE
Nucleo-F446RE
Teensy3_2
Teensy3_5
Teensy3_6
Teensy4_0
Teensy4_1
src_dir = .
include_dir = .
@@ -96,7 +104,6 @@ lib_deps =
${env.lib_deps}
arduino-libraries/Ethernet
SPI
MDNS_Generic
lib_ignore = WiFi101
WiFi101_Generic
@@ -115,7 +122,6 @@ framework = arduino
lib_deps =
${env.lib_deps}
arduino-libraries/Ethernet
MDNS_Generic
SPI
lib_ignore = WiFi101
WiFi101_Generic
@@ -194,7 +200,7 @@ monitor_speed = 115200
monitor_echo = yes
[env:Nucleo-F411RE]
platform = ststm32 @ 17.6.0
platform = ststm32 @ ^18.0.0
board = nucleo_f411re
framework = arduino
lib_deps = ${env.lib_deps}
@@ -203,7 +209,7 @@ monitor_speed = 115200
monitor_echo = yes
[env:Nucleo-F446RE]
platform = ststm32 @ 17.6.0
platform = ststm32 @ ^18.0.0
board = nucleo_f446re
framework = arduino
lib_deps = ${env.lib_deps}
@@ -215,7 +221,7 @@ monitor_echo = yes
; tested as yet
;
[env:Nucleo-F401RE]
platform = ststm32 @ 17.6.0
platform = ststm32 @ ^18.0.0
board = nucleo_f401re
framework = arduino
lib_deps = ${env.lib_deps}
@@ -228,7 +234,7 @@ monitor_echo = yes
; installed before you can let PlatformIO see this
;
; [env:Nucleo-F413ZH]
; platform = ststm32 @ 17.6.0
; platform = ststm32 @ ^18.0.0
; board = nucleo_f413zh
; framework = arduino
; lib_deps = ${env.lib_deps}
@@ -240,7 +246,7 @@ monitor_echo = yes
; installed before you can let PlatformIO see this
;
[env:Nucleo-F446ZE]
platform = ststm32 @ 17.6.0
platform = ststm32 @ ^18.0.0
board = nucleo_f446ze
framework = arduino
lib_deps = ${env.lib_deps}
@@ -252,7 +258,7 @@ monitor_echo = yes
; installed before you can let PlatformIO see this
;
; [env:Nucleo-F412ZG]
; platform = ststm32 @ 17.6.0
; platform = ststm32 @ ^18.0.0
; board = nucleo_f412zg
; framework = arduino
; lib_deps = ${env.lib_deps}
@@ -264,19 +270,18 @@ monitor_echo = yes
; Experimental - Ethernet work still in progress
;
[env:Nucleo-F429ZI]
platform = ststm32 @ 17.6.0
platform = ststm32 @ 19.0.0
board = nucleo_f429zi
framework = arduino
lib_deps = ${env.lib_deps}
stm32duino/STM32Ethernet @ ^1.4.0
stm32duino/STM32duino LwIP @ ^2.1.3
MDNS_Generic
lib_ignore = WiFi101
WiFi101_Generic
WiFiEspAT
WiFiMulti_Generic
WiFiNINA_Generic
build_flags = -std=c++17 -Os -g2 -Wunused-variable
build_flags = -std=c++17 -Os -g2 -Wunused-variable -DCUSTOM_PERIPHERAL_PINS
monitor_speed = 115200
monitor_echo = yes
upload_protocol = stlink
@@ -284,7 +289,7 @@ upload_protocol = stlink
; Experimental - Ethernet work still in progress
;
[env:Nucleo-F439ZI]
platform = ststm32 @ 17.6.0
platform = ststm32 @ 19.0.0
; board = nucleo_f439zi
; Temporarily treat it as an F429ZI (they are code compatible) until
; the PR to PlatformIO to update the F439ZI JSON file is available
@@ -294,13 +299,12 @@ framework = arduino
lib_deps = ${env.lib_deps}
stm32duino/STM32Ethernet @ ^1.4.0
stm32duino/STM32duino LwIP @ ^2.1.3
MDNS_Generic
lib_ignore = WiFi101
WiFi101_Generic
WiFiEspAT
WiFiMulti_Generic
WiFiNINA_Generic
build_flags = -std=c++17 -Os -g2 -Wunused-variable
build_flags = -std=c++17 -Os -g2 -Wunused-variable -DCUSTOM_PERIPHERAL_PINS
monitor_speed = 115200
monitor_echo = yes
upload_protocol = stlink

View File

@@ -3,34 +3,7 @@
#include "StringFormatter.h"
#define VERSION "5.4.14"
// 5.4.14 - Bugfix: Keep power status of track when doing join/unjoin, new keep power option for setTrackMode()
// 5.4.13 - Bugfix: SSD1309 OLED controllers artefatcs
// - Less verbose diag by default
// 5.4.12 - Bugfix: Negative route Ids
// 5.4.11 - Feature: Enable sniffer on CSB-1
// 5.4.10 - Bugfix: MEGA DCC waveform starvation (sends too many idles)
// 5.4.9 - Handle non-compliant decoders returning 255 for cv 20 and confusing <R> with bad consist addresses.
// - <W CONSIST longaddr> handles non-compliant decoders which NAK cv 20 writes.
// 5.4.8 - Bugfix: Insert idle packet at end of speed reminder loop; treat all function groups equal
// 5.4.7 - Bugfix: EXRAIL fix CLEAR_ALL_STASH
// 5.4.6 - Bugfix: Do not drop further commands in same packet
// 5.4.5 - ESP32: Better detection of correct IDF version
// - track power is always turned on after setJoin() not by setJoin()
// 5.4.4 - bugfix in parser, input buffer overrun and trailing > that did break <+>
// 5.4.3 - bugfix changeFn for functions 29..31
// 5.4.2 - Reversed turnout bugfix
// 5.4.1 - ESP32 bugfix packet buffer race
// 5.4.0 - New version on master
// 5.2.96 - EXRAIL additions XFWD() and XREV()
// 5.2.95 - Release candidate for 5.4
// 5.2.94 - Bugfix: Less confusion and simpler code around the RCN213 defines
// 5.2.93 - Bugfix ESP32: clear progTrackSyncMain (join flag) when prog track is removed
// 5.2.92 - Bugfix: FADE power off fix, EXRAIL power diagnostic fix.
// 5.2.91 - Bugfix: Neopixel I2C overlap check
// 5.2.90 - Bugfix: EXRAIL EXTT_TURNTABLE() now has description as optional in line with ocumentation (also fixed DCC_TURNTABLE)
// 5.2.89 - EXRAIL SET(vpin[,npins]) RESET(vpin,[,npins]) pin range manipulation
// 5.2.88 - Fix bug where EX-Turntable objects return incorrect angle for home with <JP x>
#define VERSION "5.2.87"
// 5.2.87 - CamParser and IO_EXSensorCam driver
// 5.2.86 - IO_TCA8418 driver for keypad matrix input now fully functioning, including being able to use an interrupt pin
// 5.2.85 - IO_TM1638 driver, SEG7 Exrail macro and _s7 segment pattern generator.