1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

use ugly macros to make PORTX code portable

This commit is contained in:
Harald Barth 2022-05-13 00:05:25 +02:00
parent af0d381e45
commit d7a17b10b4
3 changed files with 43 additions and 16 deletions

View File

@ -41,15 +41,15 @@ MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8
getFastPin(F("SIG"),signalPin,fastSignalPin); getFastPin(F("SIG"),signalPin,fastSignalPin);
pinMode(signalPin, OUTPUT); pinMode(signalPin, OUTPUT);
if (fastSignalPin.inout == &PORTA) { if (HAVE_PORTA(fastSignalPin.inout == &PORTA)) {
DIAG(F("Found PORTA pin %d"),signalPin); DIAG(F("Found PORTA pin %d"),signalPin);
fastSignalPin.inout = &fakePORTA; fastSignalPin.inout = &fakePORTA;
} }
if (fastSignalPin.inout == &PORTB) { if (HAVE_PORTB(fastSignalPin.inout == &PORTB)) {
DIAG(F("Found PORTB pin %d"),signalPin); DIAG(F("Found PORTB pin %d"),signalPin);
fastSignalPin.inout = &fakePORTB; fastSignalPin.inout = &fakePORTB;
} }
if (fastSignalPin.inout == &PORTC) { if (HAVE_PORTC(fastSignalPin.inout == &PORTC)) {
DIAG(F("Found PORTC pin %d"),signalPin); DIAG(F("Found PORTC pin %d"),signalPin);
fastSignalPin.inout = &fakePORTC; fastSignalPin.inout = &fakePORTC;
} }

View File

@ -30,6 +30,31 @@
#define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH) #define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH)
#define isLOW(fastpin) (!isHIGH(fastpin)) #define isLOW(fastpin) (!isHIGH(fastpin))
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
#define HAVE_PORTA(X) X
#define HAVE_PORTB(X) X
#define HAVE_PORTC(X) X
#endif
#if defined(ARDUINO_AVR_UNO)
#define HAVE_PORTB(X) X
#endif
// if macros not defined as pass-through we define
// them here as someting that is valid as a
// statement and evaluates to false.
#ifndef HAVE_PORTA
#define HAVE_PORTA(X) byte TOKENPASTE2(Unique_, __LINE__) __attribute__((unused)) =0
#endif
#ifndef HAVE_PORTB
#define HAVE_PORTB(X) byte TOKENPASTE2(Unique_, __LINE__) __attribute__((unused)) =0
#endif
#ifndef HAVE_PORTC
#define HAVE_PORTC(X) byte TOKENPASTE2(Unique_, __LINE__) __attribute__((unused)) =0
#endif
// Virtualised Motor shield 1-track hardware Interface // Virtualised Motor shield 1-track hardware Interface
#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h #ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h

View File

@ -84,19 +84,21 @@ void TrackManager::addTrack(byte t, MotorDriver* driver) {
lastTrack=t; lastTrack=t;
track[t]->setPower(POWERMODE::OFF); track[t]->setPower(POWERMODE::OFF);
} }
} }
// defined in Motordriver.cpp
extern byte fakePORTA; extern byte fakePORTA;
extern byte fakePORTB; extern byte fakePORTB;
extern byte fakePORTC; extern byte fakePORTC;
void TrackManager::setDCCSignal( bool on) { void TrackManager::setDCCSignal( bool on) {
fakePORTA=PORTA; HAVE_PORTA(fakePORTA=PORTA);
fakePORTB=PORTB; HAVE_PORTB(fakePORTB=PORTB);
fakePORTC=PORTC; HAVE_PORTC(fakePORTC=PORTC);
APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on)); APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on));
PORTA=fakePORTA; HAVE_PORTA(PORTA=fakePORTA);
PORTB=fakePORTB; HAVE_PORTB(PORTB=fakePORTB);
PORTC=fakePORTC; HAVE_PORTC(PORTC=fakePORTC);
} }
void TrackManager::setCutout( bool on) { void TrackManager::setCutout( bool on) {
@ -106,13 +108,13 @@ void TrackManager::setCutout( bool on) {
} }
void TrackManager::setPROGSignal( bool on) { void TrackManager::setPROGSignal( bool on) {
fakePORTA=PORTA; HAVE_PORTA(fakePORTA=PORTA);
fakePORTB=PORTB; HAVE_PORTB(fakePORTB=PORTB);
fakePORTC=PORTC; HAVE_PORTC(fakePORTC=PORTC);
APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on)); APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on));
PORTA=fakePORTA; HAVE_PORTA(PORTA=fakePORTA);
PORTB=fakePORTB; HAVE_PORTB(PORTB=fakePORTB);
PORTC=fakePORTC; HAVE_PORTC(PORTC=fakePORTC);
} }
void TrackManager::setDCSignal(int16_t cab, byte speedbyte) { void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {