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

Channel2 support

This commit is contained in:
Asbelos 2024-10-22 09:55:57 +01:00
parent dd42390ae9
commit 9ccf1fe41a
7 changed files with 85 additions and 35 deletions

36
DCC.cpp
View File

@ -897,6 +897,42 @@ DCC::LOCO DCC::speedTable[MAX_LOCOS];
int DCC::lastLocoReminder = 0; int DCC::lastLocoReminder = 0;
int DCC::highestUsedReg = 0; int DCC::highestUsedReg = 0;
void DCC::setLocoInBlock(int loco, uint16_t blockid, bool exclusive) {
// update block loco is in, tell exrail leaving old block, and entering new.
// NOTE: The loco table scanning is really inefficient and needs rewriting
// This was done once in the momentum poc.
#ifdef EXRAIL_ACTIVE
int reg=lookupSpeedTable(loco,true);
if (reg<0) return;
auto oldBlock=speedTable[reg].blockOccupied;
if (oldBlock==blockid) return;
if (oldBlock) RMFT2::blockEvent(oldBlock,loco,false);
speedTable[reg].blockOccupied=blockid;
if (blockid) RMFT2::blockEvent(blockid,loco,true);
if (exclusive) {
for (int reg = 0; reg <= highestUsedReg; reg++) {
if (speedTable[reg].loco!=loco && speedTable[reg].blockOccupied==blockid) {
RMFT2::blockEvent(blockid,speedTable[reg].loco,false);
speedTable[reg].blockOccupied=0;
}
}
}
#endif
}
void DCC::clearBlock(uint16_t blockid) {
// Railcom reports block empty... tell Exrail about all leavers
#ifdef EXRAIL_ACTIVE
for (int reg = 0; reg <= highestUsedReg; reg++) {
if (speedTable[reg].blockOccupied==blockid) {
RMFT2::blockEvent(blockid,speedTable[reg].loco,false);
speedTable[reg].blockOccupied=0;
}
}
#endif
}
void DCC::displayCabList(Print * stream) { void DCC::displayCabList(Print * stream) {

4
DCC.h
View File

@ -102,12 +102,14 @@ public:
byte speedCode; byte speedCode;
byte groupFlags; byte groupFlags;
uint32_t functions; uint32_t functions;
uint16_t blockOccupied; // railcom detected block
}; };
static LOCO speedTable[MAX_LOCOS]; static LOCO speedTable[MAX_LOCOS];
static int lookupSpeedTable(int locoId, bool autoCreate=true); static int lookupSpeedTable(int locoId, bool autoCreate=true);
static byte cv1(byte opcode, int cv); static byte cv1(byte opcode, int cv);
static byte cv2(int cv); static byte cv2(int cv);
static void setLocoInBlock(int locoid, uint16_t blockid, bool exclusive);
static void clearBlock(uint16_t blockid);
private: private:
static byte loopStatus; static byte loopStatus;
static void setThrottle2(uint16_t cab, uint8_t speedCode); static void setThrottle2(uint16_t cab, uint8_t speedCode);

View File

@ -1205,6 +1205,10 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
Diag::CMD = onOff; Diag::CMD = onOff;
return true; return true;
case "RAILCOM"_hk: // <D RAILCOM ON/OFF>
Diag::RAILCOM = onOff;
return true;
#ifdef HAS_ENOUGH_MEMORY #ifdef HAS_ENOUGH_MEMORY
case "WIFI"_hk: // <D WIFI ON/OFF> case "WIFI"_hk: // <D WIFI ON/OFF>
Diag::WIFI = onOff; Diag::WIFI = onOff;

View File

@ -51,10 +51,9 @@
#include "Railcom.h" #include "Railcom.h"
#include "defines.h" #include "defines.h"
#include "FSH.h" #include "FSH.h"
#include "EXRAIL3.h" #include "DCC.h"
#include "DIAG.h" #include "DIAG.h"
#define DIAG_I2CRailcom_data
/** Table for 8-to-6 decoding of railcom data. This table can be indexed by the /** Table for 8-to-6 decoding of railcom data. This table can be indexed by the
@ -143,31 +142,35 @@ Railcom::Railcom(uint16_t blockvpin) {
haveHigh=false; haveHigh=false;
haveLow=false; haveLow=false;
packetsWithNoData=0; packetsWithNoData=0;
locoOnTrack=0; lastChannel1Loco=0;
vpin=blockvpin; vpin=blockvpin;
} }
// Process is called by a raw data collector.
void Railcom::process(uint8_t * inbound, uint8_t length) { void Railcom::process(uint8_t * inbound, uint8_t length) {
#ifdef DIAG_I2CRailcom_data
static const char hexchars[]="0123456789ABCDEF";
if (length>2) {
Serial.print("R ");
for (byte i=0;i<length;i++) {
if (i==2) Serial.write(' ');
Serial.write(hexchars[inbound[i]>>4]);
Serial.write(hexchars[inbound[i]& 0x0F ]);
}
Serial.println();
}
#endif
if (length<2 || (inbound[0]==0 && inbound[1]==0)) { if (length<2 || (inbound[0]==0 && inbound[1]==0)) {
noData(); noData();
return; return;
} }
if (Diag::RAILCOM) {
static const char hexchars[]="0123456789ABCDEF";
if (length>2) {
USB_SERIAL.print(F("<*R "));
for (byte i=0;i<length;i++) {
if (i==2) Serial.write(' ');
USB_SERIAL.write(hexchars[inbound[i]>>4]);
USB_SERIAL.write(hexchars[inbound[i]& 0x0F ]);
}
USB_SERIAL.print(F(" *>\n"));
}
}
auto v1=GETHIGHFLASH(decode,inbound[0]); auto v1=GETHIGHFLASH(decode,inbound[0]);
auto v2=(length>1) ? GETHIGHFLASH(decode,inbound[1]):INV; auto v2=(length>1) ? GETHIGHFLASH(decode,inbound[1]):INV;
uint16_t packet=(v1<<6) | (v2 & 0x3f); uint16_t packet=(v1<<6) | (v2 & 0x3f);
@ -188,25 +191,28 @@ void Railcom::process(uint8_t * inbound, uint8_t length) {
return; /* ignore*/ return; /* ignore*/
} }
else { else {
// channel1 is unreadable so maybe multiple locos in block
if (length>2 && GETHIGHFLASH(decode,inbound[0])!=INV) {
// it looks like we have channel2 data
auto thisLoco=DCCWaveform::getRailcomLastLocoAddress();
if (Diag::RAILCOM) DIAG(F("c2=%d"),thisLoco);
if (thisLoco) DCC::setLocoInBlock(thisLoco,vpin,false); // this loco is in block, but not exclusive
return;
}
// channel1 no good and no channel2
noData(); noData();
return; // need more data return;
} }
if (haveHigh && haveLow) { if (haveHigh && haveLow) {
uint16_t thisLoco=((holdoverHigh<<8)| holdoverLow); uint16_t thisLoco=((holdoverHigh<<8)| holdoverLow);
if (locoOnTrack!=thisLoco) { if (thisLoco!=lastChannel1Loco) {
// Previous loco (if any) is exiting block // the exclusive DCC call is quite expensive, we dont want to call it every packet
blockEvent(false); DCC::setLocoInBlock(thisLoco,vpin,true); // only this loco is in block
locoOnTrack=thisLoco; lastChannel1Loco=thisLoco;
blockEvent(true);
} }
} }
} }
void Railcom::blockEvent(bool entering) {
#ifdef EXRAIL_ACTIVE
if (locoOnTrack) RMFT3::blockEvent(vpin,locoOnTrack,entering);
#endif
}
void Railcom::noData() { void Railcom::noData() {
if (packetsWithNoData>MAX_WAIT_FOR_GLITCH) return; if (packetsWithNoData>MAX_WAIT_FOR_GLITCH) return;
@ -214,9 +220,9 @@ void Railcom::noData() {
// treat as no loco // treat as no loco
haveHigh=false; haveHigh=false;
haveLow=false; haveLow=false;
// Previous loco (if any) is exiting block lastChannel1Loco=0;
blockEvent(false); // Previous locos (if any) is exiting block
locoOnTrack=0; DCC::clearBlock(vpin);
} }
packetsWithNoData++; packetsWithNoData++;
} }

View File

@ -33,13 +33,12 @@ class Railcom {
void process(uint8_t * inbound,uint8_t length); void process(uint8_t * inbound,uint8_t length);
private: private:
void blockEvent(bool entering);
void noData(); void noData();
uint16_t locoOnTrack;
uint16_t vpin; uint16_t vpin;
uint8_t holdoverHigh,holdoverLow; uint8_t holdoverHigh,holdoverLow;
bool haveHigh,haveLow; bool haveHigh,haveLow;
uint8_t packetsWithNoData; uint8_t packetsWithNoData;
uint16_t lastChannel1Loco;
static const byte MAX_WAIT_FOR_GLITCH=20; // number of dead or empty packets before assuming loco=0 static const byte MAX_WAIT_FOR_GLITCH=20; // number of dead or empty packets before assuming loco=0
}; };

View File

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

View File

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