diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 92d8c88..0f5b60e 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -27,7 +27,11 @@ bool MotorDriver::usePWM=false; bool MotorDriver::commonFaultPin=false; - + +volatile byte fakePORTA; +volatile byte fakePORTB; +volatile byte fakePORTC; + MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) { powerPin=power_pin; @@ -36,7 +40,20 @@ MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8 signalPin=signal_pin; getFastPin(F("SIG"),signalPin,fastSignalPin); pinMode(signalPin, OUTPUT); - + + if (fastSignalPin.inout == &PORTA) { + DIAG(F("Found PORTA pin %d"),signalPin); + fastSignalPin.inout = &fakePORTA; + } + if (fastSignalPin.inout == &PORTB) { + DIAG(F("Found PORTB pin %d"),signalPin); + fastSignalPin.inout = &fakePORTB; + } + if (fastSignalPin.inout == &PORTC) { + DIAG(F("Found PORTC pin %d"),signalPin); + fastSignalPin.inout = &fakePORTC; + } + signalPin2=signal_pin2; if (signalPin2!=UNUSED_PIN) { dualSignal=true; @@ -50,6 +67,7 @@ MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8 invertBrake=brake_pin < 0; brakePin=invertBrake ? 0-brake_pin : brake_pin; getFastPin(F("BRAKE"),brakePin,fastBrakePin); + // if brake is used for railcom cutout we need to do PORTX register trick here as well pinMode(brakePin, OUTPUT); setBrake(true); // start with brake on in case we hace DC stuff going on } diff --git a/TrackManager.cpp b/TrackManager.cpp index 74b3a7d..93a92e2 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -85,18 +85,34 @@ void TrackManager::addTrack(byte t, MotorDriver* driver) { track[t]->setPower(POWERMODE::OFF); } } +extern byte fakePORTA; +extern byte fakePORTB; +extern byte fakePORTC; void TrackManager::setDCCSignal( bool on) { - APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on)); + fakePORTA=PORTA; + fakePORTB=PORTB; + fakePORTC=PORTC; + APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on)); + PORTA=fakePORTA; + PORTB=fakePORTB; + PORTC=fakePORTC; } void TrackManager::setCutout( bool on) { (void) on; + // TODO Cutout needs fake ports as well // TODO APPLY_BY_MODE(TRACK_MODE_MAIN,setCutout(on)); } void TrackManager::setPROGSignal( bool on) { - APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on)); + fakePORTA=PORTA; + fakePORTB=PORTB; + fakePORTC=PORTC; + APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on)); + PORTA=fakePORTA; + PORTB=fakePORTB; + PORTC=fakePORTC; } void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {