Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust forget commands to respect the direction of engines #242

Open
wants to merge 1 commit into
base: TrackManager
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions DCC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void DCC::begin(const FSH * motorShieldName) {


void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) {
byte speedCode = (tSpeed & 0x7F) + tDirection * 128;
byte speedCode = calculateSpeedByte(tSpeed,tDirection);
setThrottle2(cab, speedCode);
TrackManager::setDCSignal(cab,speedCode); // in case this is a dcc track on this addr
// retain speed for loco reminders
Expand Down Expand Up @@ -540,15 +540,24 @@ void DCC::setLocoId(int id,ACK_CALLBACK callback) {
DCCACK::Setup(id | 0xc000,LONG_LOCO_ID_PROG, callback);
}

void DCC::forgetLoco(int cab) { // removes any speed reminders for this loco
setThrottle2(cab,1); // ESTOP this loco if still on track
int reg=lookupSpeedTable(cab);
if (reg>=0) speedTable[reg].loco=0;
setThrottle2(cab,1); // ESTOP if this loco still on track
}
void DCC::forgetAllLocos() { // removes all speed reminders
setThrottle2(0,1); // ESTOP all locos still on track
for (int i=0;i<MAX_LOCOS;i++) speedTable[i].loco=0;
void DCC::forgetLoco(int cab){ // removes any speed reminders for this loco
auto direction = getThrottleDirection(cab);
setThrottle2(cab, calculateSpeedByte(1, direction)); // ESTOP this loco if still on track
int reg = lookupSpeedTable(cab);
if (reg >= 0)
speedTable[reg].loco = 0;
setThrottle2(cab, calculateSpeedByte(1, direction)); // ESTOP if this loco still on track
}

void DCC::forgetAllLocos() { // removes all speed reminders
for (int i = 0; i < MAX_LOCOS; i++) {
auto &locoId = speedTable[i].loco;
if (locoId != 0) {
auto direction = getThrottleDirection(locoId);
setThrottle2(locoId, calculateSpeedByte(1, direction));
locoId = 0;
}
}
}

byte DCC::loopStatus=0;
Expand Down Expand Up @@ -621,6 +630,10 @@ bool DCC::issueReminder(int reg) {

///// Private helper functions below here /////////////////////

inline byte DCC::calculateSpeedByte(uint8_t tSpeed, bool tDirection) {
return (tSpeed & 0x7F) + tDirection * 128;
}

byte DCC::cv1(byte opcode, int cv) {
cv--;
return (highByte(cv) & (byte)0x03) | opcode;
Expand Down
1 change: 1 addition & 0 deletions DCC.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class DCC
static byte cv2(int cv);

private:
static byte calculateSpeedByte(uint8_t tSpeed, bool tDirection);
static byte loopStatus;
static void setThrottle2(uint16_t cab, uint8_t speedCode);
static void updateLocoReminder(int loco, byte speedCode);
Expand Down