From 414e109f9dcecb4bb07aea5f09a55f32bc9bdbfc Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 4 Sep 2021 09:05:01 +0200 Subject: [PATCH] pwmSpeed() as access method instead of public pointer --- DCC.cpp | 16 ++++------------ DCCWaveform.h | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/DCC.cpp b/DCC.cpp index 77c92c8..801e64d 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -58,11 +58,11 @@ void DCC::begin(const FSH * motorShieldName, MotorDriver * mainDriver, MotorDriv DCCWaveform::begin(mainDriver,progDriver); #ifdef DCdistrict - DCCWaveform::mainTrack.motorDriver->setBrake(255); + DCCWaveform::mainTrack.pwmSpeed(0); #else - DCCWaveform::mainTrack.motorDriver->setBrake(0); + DCCWaveform::mainTrack.pwmSpeed(255); #endif - DCCWaveform::progTrack.motorDriver->setBrake(0); + DCCWaveform::progTrack.pwmSpeed(255); } @@ -77,15 +77,7 @@ void DCC::setJoinRelayPin(byte joinRelayPin) { void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) { #ifdef DCdistrict if (cab == DCdistrict) { - uint8_t brake; - if (tSpeed <= 1) - brake = 255; - else if (tSpeed >= 126) - brake = 0; - else - brake = 2 * (127-tSpeed); - DCCWaveform::mainTrack.motorDriver->setSignal(tDirection); - DCCWaveform::mainTrack.motorDriver->setBrake(brake); + DCCWaveform::mainTrack.pwmSpeed(tSpeed, tDirection); } #else #error fooar diff --git a/DCCWaveform.h b/DCCWaveform.h index 0cbb5ff..db58574 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -107,7 +107,27 @@ class DCCWaveform { inline void setMaxAckPulseDuration(unsigned int i) { maxAckPulseDuration = i; } - MotorDriver* motorDriver; +#ifdef DCdistrict + inline void pwmSpeed(uint8_t tSpeed) { + // DCC Speed with 0,1 stop and speed steps 2 to 127 + uint8_t brake; + if (!motorDriver) + return; + if (tSpeed <= 1) + brake = 255; + else if (tSpeed >= 127) + brake = 0; + else + brake = 2 * (128-tSpeed); + motorDriver->setBrake(brake); + } + inline void pwmSpeed(uint8_t tSpeed, bool tDirection) { + if (!motorDriver) + return; + pwmSpeed(tSpeed); + motorDriver->setSignal(tDirection); + } +#endif private: @@ -122,7 +142,7 @@ class DCCWaveform { void checkAck(); bool isMainTrack; -// MotorDriver* motorDriver; + MotorDriver* motorDriver; // Transmission controller byte transmitPacket[MAX_PACKET_SIZE+1]; // +1 for checksum byte transmitLength;