From d7a17b10b4ccfa70c3a0dbb51fe7edc6ae435461 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Fri, 13 May 2022 00:05:25 +0200 Subject: [PATCH] use ugly macros to make PORTX code portable --- MotorDriver.cpp | 6 +++--- MotorDriver.h | 25 +++++++++++++++++++++++++ TrackManager.cpp | 28 +++++++++++++++------------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 0f5b60e..30dd1be 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -41,15 +41,15 @@ MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8 getFastPin(F("SIG"),signalPin,fastSignalPin); pinMode(signalPin, OUTPUT); - if (fastSignalPin.inout == &PORTA) { + if (HAVE_PORTA(fastSignalPin.inout == &PORTA)) { DIAG(F("Found PORTA pin %d"),signalPin); fastSignalPin.inout = &fakePORTA; } - if (fastSignalPin.inout == &PORTB) { + if (HAVE_PORTB(fastSignalPin.inout == &PORTB)) { DIAG(F("Found PORTB pin %d"),signalPin); fastSignalPin.inout = &fakePORTB; } - if (fastSignalPin.inout == &PORTC) { + if (HAVE_PORTC(fastSignalPin.inout == &PORTC)) { DIAG(F("Found PORTC pin %d"),signalPin); fastSignalPin.inout = &fakePORTC; } diff --git a/MotorDriver.h b/MotorDriver.h index 61ddf5c..db229d5 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -30,6 +30,31 @@ #define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH) #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 #ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h diff --git a/TrackManager.cpp b/TrackManager.cpp index 93a92e2..0c5117b 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -84,19 +84,21 @@ void TrackManager::addTrack(byte t, MotorDriver* driver) { lastTrack=t; track[t]->setPower(POWERMODE::OFF); } -} +} + +// defined in Motordriver.cpp extern byte fakePORTA; extern byte fakePORTB; extern byte fakePORTC; void TrackManager::setDCCSignal( bool on) { - fakePORTA=PORTA; - fakePORTB=PORTB; - fakePORTC=PORTC; + HAVE_PORTA(fakePORTA=PORTA); + HAVE_PORTB(fakePORTB=PORTB); + HAVE_PORTC(fakePORTC=PORTC); APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on)); - PORTA=fakePORTA; - PORTB=fakePORTB; - PORTC=fakePORTC; + HAVE_PORTA(PORTA=fakePORTA); + HAVE_PORTB(PORTB=fakePORTB); + HAVE_PORTC(PORTC=fakePORTC); } void TrackManager::setCutout( bool on) { @@ -106,13 +108,13 @@ void TrackManager::setCutout( bool on) { } void TrackManager::setPROGSignal( bool on) { - fakePORTA=PORTA; - fakePORTB=PORTB; - fakePORTC=PORTC; + HAVE_PORTA(fakePORTA=PORTA); + HAVE_PORTB(fakePORTB=PORTB); + HAVE_PORTC(fakePORTC=PORTC); APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on)); - PORTA=fakePORTA; - PORTB=fakePORTB; - PORTC=fakePORTC; + HAVE_PORTA(PORTA=fakePORTA); + HAVE_PORTB(PORTB=fakePORTB); + HAVE_PORTC(PORTC=fakePORTC); } void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {