diff --git a/IODevice.cpp b/IODevice.cpp index f13404c..de6b021 100644 --- a/IODevice.cpp +++ b/IODevice.cpp @@ -274,6 +274,18 @@ IODevice *IODevice::findDevice(VPIN vpin) { } return NULL; } + +// Private helper function to check for vpin overlap. Run during setup only. +// returns true if pins DONT overlap with existing device +bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins) { + for (VPIN testPin=firstPin; testPin< (firstPin+nPins); testPin++) + if (findDevice(testPin)) { + DIAG(F("WARNING HAL Pin %d overlap, re-definition of pins %d to %d ignored."), + testPin, firstPin, firstPin+nPins); + return false; + } + return true; +} //================================================================================================================== // Static data diff --git a/IODevice.h b/IODevice.h index 193b86b..396fc46 100644 --- a/IODevice.h +++ b/IODevice.h @@ -228,6 +228,9 @@ protected: // pin low if an input changes state. int16_t _gpioInterruptPin = -1; + // Method to check if pins will overlap before creating new device. + static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1); + // Static support function for subclass creation static void addDevice(IODevice *newDevice); @@ -239,7 +242,7 @@ private: bool owns(VPIN vpin); // Method to find device handling Vpin static IODevice *findDevice(VPIN vpin); - + IODevice *_nextDevice = 0; unsigned long _nextEntryTime; static IODevice *_firstDevice; diff --git a/IO_AnalogueInputs.h b/IO_AnalogueInputs.h index 85c224a..ade497b 100644 --- a/IO_AnalogueInputs.h +++ b/IO_AnalogueInputs.h @@ -69,7 +69,7 @@ public: addDevice(this); } static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) { - new ADS111x(firstVpin, nPins, i2cAddress); + if (checkNoOverlap(firstVpin,nPins)) new ADS111x(firstVpin, nPins, i2cAddress); } private: void _begin() { diff --git a/IO_DCCAccessory.cpp b/IO_DCCAccessory.cpp index 6e739cd..214a2f9 100644 --- a/IO_DCCAccessory.cpp +++ b/IO_DCCAccessory.cpp @@ -26,8 +26,8 @@ #define ADDRESS(packedaddr) ((packedaddr) >> 2) #define SUBADDRESS(packedaddr) ((packedaddr) % 4) -void DCCAccessoryDecoder::create(VPIN vpin, int nPins, int DCCAddress, int DCCSubaddress) { - new DCCAccessoryDecoder(vpin, nPins, DCCAddress, DCCSubaddress); +void DCCAccessoryDecoder::create(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress) { + if (checkNoOverlap(firstVpin,nPins)) new DCCAccessoryDecoder(firstVpin, nPins, DCCAddress, DCCSubaddress); } // Constructors diff --git a/IO_DFPlayer.h b/IO_DFPlayer.h index bdf0626..44d4b5c 100644 --- a/IO_DFPlayer.h +++ b/IO_DFPlayer.h @@ -78,7 +78,7 @@ public: } static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) { - new DFPlayer(firstVpin, nPins, serial); + if (checkNoOverlap(firstVpin,nPins)) new DFPlayer(firstVpin, nPins, serial); } protected: diff --git a/IO_ExampleSerial.cpp b/IO_ExampleSerial.cpp index efcc3bf..12476db 100644 --- a/IO_ExampleSerial.cpp +++ b/IO_ExampleSerial.cpp @@ -36,7 +36,7 @@ IO_ExampleSerial::IO_ExampleSerial(VPIN firstVpin, int nPins, HardwareSerial *se // Static create method for one module. void IO_ExampleSerial::create(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud) { - new IO_ExampleSerial(firstVpin, nPins, serial, baud); + if (checkNoOverlap(firstVpin,nPins)) new IO_ExampleSerial(firstVpin, nPins, serial, baud); } // Device-specific initialisation diff --git a/IO_HCSR04.h b/IO_HCSR04.h index 9bbd2f8..f413991 100644 --- a/IO_HCSR04.h +++ b/IO_HCSR04.h @@ -86,7 +86,8 @@ public: // Static create function provides alternative way to create object static void create(VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) { - new HCSR04(vpin, trigPin, echoPin, onThreshold, offThreshold); + if (checkNoOverlap(vpin) && checkNoOverlap(trigPin) && checkNoOverlap(echoPin)) + new HCSR04(vpin, trigPin, echoPin, onThreshold, offThreshold); } protected: diff --git a/IO_MCP23008.h b/IO_MCP23008.h index 18ff12f..8062128 100644 --- a/IO_MCP23008.h +++ b/IO_MCP23008.h @@ -25,7 +25,7 @@ class MCP23008 : public GPIOBase { public: static void create(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) { - new MCP23008(firstVpin, nPins, I2CAddress, interruptPin); + if (checkNoOverlap(firstVpin, nPins)) new MCP23008(firstVpin, nPins, I2CAddress, interruptPin); } // Constructor diff --git a/IO_MCP23017.h b/IO_MCP23017.h index 930b051..4ad84cb 100644 --- a/IO_MCP23017.h +++ b/IO_MCP23017.h @@ -31,7 +31,7 @@ class MCP23017 : public GPIOBase { public: static void create(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1) { - new MCP23017(vpin, min(nPins,16), I2CAddress, interruptPin); + if (checkNoOverlap(vpin, nPins)) new MCP23017(vpin, min(nPins,16), I2CAddress, interruptPin); } // Constructor diff --git a/IO_PCA9685.cpp b/IO_PCA9685.cpp index 55065b7..ae6d3dd 100644 --- a/IO_PCA9685.cpp +++ b/IO_PCA9685.cpp @@ -39,7 +39,7 @@ static void writeRegister(byte address, byte reg, byte value); // Create device driver instance. void PCA9685::create(VPIN firstVpin, int nPins, uint8_t I2CAddress) { - new PCA9685(firstVpin, nPins, I2CAddress); + if (checkNoOverlap(firstVpin, nPins)) new PCA9685(firstVpin, nPins, I2CAddress); } // Configure a port on the PCA9685. diff --git a/IO_PCF8574.h b/IO_PCF8574.h index 58c4654..5a9dc05 100644 --- a/IO_PCF8574.h +++ b/IO_PCF8574.h @@ -43,7 +43,7 @@ class PCF8574 : public GPIOBase { public: static void create(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) { - new PCF8574(firstVpin, nPins, I2CAddress, interruptPin); + if (checkNoOverlap(firstVpin, nPins)) new PCF8574(firstVpin, nPins, I2CAddress, interruptPin); } PCF8574(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) diff --git a/IO_VL53L0X.h b/IO_VL53L0X.h index bcdbc49..71f81e0 100644 --- a/IO_VL53L0X.h +++ b/IO_VL53L0X.h @@ -139,7 +139,7 @@ public: addDevice(this); } static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) { - new VL53L0X(firstVpin, nPins, i2cAddress, onThreshold, offThreshold, xshutPin); + if (checkNoOverlap(firstVpin, nPins)) new VL53L0X(firstVpin, nPins, i2cAddress, onThreshold, offThreshold, xshutPin); } protected: