Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
- bug when removing locos from a cosist
- add getByAddress() to Consist
- tidy up of the setTrackType() code
  • Loading branch information
flash62au committed Dec 16, 2023
1 parent 0558565 commit ff432e4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ This ensures that the user of the throttle sees the accurate results of what the
DCCEXProtocol_Track_type
------------------------------------

This example demonstrates how client throttle software can change the Track type of any track/chanbel. (MAIN\|PROG\|DC\|DCX\|NONE)
This example demonstrates how client throttle software can change the Track type of any track/channel. (MAIN\|PROG\|DC\|DCX\|NONE)

----

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DCCEXProtocol
version=0.0.2
version=0.0.3
author=Peter Akers <[email protected]>, David Zuhn <[email protected]>
maintainer=Peter Akers <[email protected]>
sentence=DCC-EX Native Protocol implementation
Expand Down
12 changes: 12 additions & 0 deletions src/DCCEXLoco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,15 @@ void Consist::removeLoco(Loco* loco) {
_first=current->getNext();
}
delete current;
_locoCount--;
break;
}
previous=current;
current=current->getNext();
}
if (!_first) {
_first=nullptr;
_locoCount = 0;
}
}

Expand All @@ -265,6 +267,7 @@ void Consist::removeAllLocos() {
current=next;
}
_first=nullptr;
_locoCount = 0;
}

void Consist::setLocoFacing(Loco* loco, Facing facing) {
Expand Down Expand Up @@ -313,6 +316,15 @@ ConsistLoco* Consist::getFirst() {
return _first;
}

ConsistLoco* Consist::getByAddress(int address) {
for (ConsistLoco* cl=_first; cl; cl=cl->_next) {
if (cl->getLoco()->getAddress()==address) {
return cl;
}
}
return nullptr;
}

// Private methods

void Consist::_addLocoToConsist(ConsistLoco* conLoco) {
Expand Down
4 changes: 4 additions & 0 deletions src/DCCEXLoco.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ class Consist {
/// @return Pointer to the first ConsistLoco object
ConsistLoco* getFirst();

/// @brief Get the loco in the consist with the specified address
/// @return Pointer to the first ConsistLoco object
ConsistLoco* getByAddress(int address);

private:
char* _name;
int _locoCount;
Expand Down
39 changes: 22 additions & 17 deletions src/DCCEXProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,23 @@ void DCCEXProtocol::powerTrackOff(char track) {

void DCCEXProtocol::setTrackType(char track, TrackManagerMode type, int address) {
if (_delegate) {
if (type == MAIN) {
sprintf(_outboundCommand, "<= %c MAIN>", track);
} else if (type == PROG) {
sprintf(_outboundCommand, "<= %c PROG>", track);
} else if (type == DC) {
sprintf(_outboundCommand, "<= %c DC %d>", track, address);
} else if (type == DCX) {
sprintf(_outboundCommand, "<= %c DCX %d>", track, address);
} else if (type == NONE) {
sprintf(_outboundCommand, "<= %c NONE>", track);
} else {
return;
switch (type) {
case MAIN:
sprintf(_outboundCommand, "<= %c MAIN>", track);
break;
case PROG:
sprintf(_outboundCommand, "<= %c PROG>", track);
break;
case DC:
sprintf(_outboundCommand, "<= %c DC %d>", track, address);
break;
case DCX:
sprintf(_outboundCommand, "<= %c DCX %d>", track, address);
break;
case NONE:
sprintf(_outboundCommand, "<= %c NONE>", track);
break;
default: return;
}
_sendCommand();
}
Expand Down Expand Up @@ -1105,11 +1110,11 @@ void DCCEXProtocol::_processTrackType() {
int _type = DCCEXInbound::getNumber(1);
TrackManagerMode _trackType;
switch (_type) {
case 2698315: _trackType = MAIN;
case 2788330: _trackType = PROG;
case 2183: _trackType = DC;
case 71999: _trackType = DCX;
case 2857034: _trackType = NONE;
case 2698315: _trackType = MAIN; break;
case 2788330: _trackType = PROG; break;
case 2183: _trackType = DC; break;
case 71999: _trackType = DCX; break;
case 2857034: _trackType = NONE; break;
default: return;
}
int _address = 0;
Expand Down

0 comments on commit ff432e4

Please sign in to comment.