From ef937dcacff630a70755767690b68238b0aaae6b Mon Sep 17 00:00:00 2001 From: Asbelos Date: Tue, 14 Jun 2022 15:23:27 +0100 Subject: [PATCH] Privatize HAL constructors Forces caller to go via create function which includes overlap checks before class is instantiated. --- .gitignore | 1 + IODevice.h | 14 +++++++------- IO_AnalogueInputs.h | 8 ++++---- IO_DFPlayer.h | 13 +++++++------ IO_ExampleSerial.h | 2 +- IO_HCSR04.h | 18 +++++++++--------- IO_MCP23008.h | 2 +- IO_MCP23017.h | 5 ++--- IO_PCF8574.h | 2 +- IO_VL53L0X.h | 13 +++++++------ 10 files changed, 40 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 5e5d355..1f24578 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ myAutomation.h myFilter.cpp myLayout.h .vscode/extensions.json +.vscode/extensions.json diff --git a/IODevice.h b/IODevice.h index 396fc46..a688ba3 100644 --- a/IODevice.h +++ b/IODevice.h @@ -260,8 +260,6 @@ private: class PCA9685 : public IODevice { public: static void create(VPIN vpin, int nPins, uint8_t I2CAddress); - // Constructor - PCA9685(VPIN vpin, int nPins, uint8_t I2CAddress); enum ProfileType : uint8_t { Instant = 0, // Moves immediately between positions (if duration not specified) UseDuration = 0, // Use specified duration @@ -273,6 +271,8 @@ public: }; private: + // Constructor + PCA9685(VPIN vpin, int nPins, uint8_t I2CAddress); // Device-specific initialisation void _begin() override; bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override; @@ -320,10 +320,10 @@ private: class DCCAccessoryDecoder: public IODevice { public: static void create(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress); - // Constructor - DCCAccessoryDecoder(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress); private: + // Constructor + DCCAccessoryDecoder(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress); // Device-specific write function. void _begin() override; void _write(VPIN vpin, int value) override; @@ -343,13 +343,13 @@ public: addDevice(new ArduinoPins(firstVpin, nPins)); } - // Constructor - ArduinoPins(VPIN firstVpin, int nPins); - static void fastWriteDigital(uint8_t pin, uint8_t value); static bool fastReadDigital(uint8_t pin); private: + // Constructor + ArduinoPins(VPIN firstVpin, int nPins); + // Device-specific pin configuration bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override; // Device-specific write function. diff --git a/IO_AnalogueInputs.h b/IO_AnalogueInputs.h index ade497b..5849501 100644 --- a/IO_AnalogueInputs.h +++ b/IO_AnalogueInputs.h @@ -59,6 +59,10 @@ **********************************************************************************************/ class ADS111x: public IODevice { public: + static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) { + if (checkNoOverlap(firstVpin,nPins)) new ADS111x(firstVpin, nPins, i2cAddress); + } +private: ADS111x(VPIN firstVpin, int nPins, uint8_t i2cAddress) { _firstVpin = firstVpin; _nPins = min(nPins,4); @@ -68,10 +72,6 @@ public: _value[i] = -1; addDevice(this); } - static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) { - if (checkNoOverlap(firstVpin,nPins)) new ADS111x(firstVpin, nPins, i2cAddress); - } -private: void _begin() { // Initialise ADS device if (I2CManager.exists(_i2cAddress)) { diff --git a/IO_DFPlayer.h b/IO_DFPlayer.h index 44d4b5c..575668b 100644 --- a/IO_DFPlayer.h +++ b/IO_DFPlayer.h @@ -69,6 +69,12 @@ private: unsigned long _commandSendTime; // Allows timeout processing public: + + static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) { + if (checkNoOverlap(firstVpin,nPins)) new DFPlayer(firstVpin, nPins, serial); + } + +protected: // Constructor DFPlayer(VPIN firstVpin, int nPins, HardwareSerial &serial) : IODevice(firstVpin, nPins), @@ -77,12 +83,7 @@ public: addDevice(this); } - static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) { - if (checkNoOverlap(firstVpin,nPins)) new DFPlayer(firstVpin, nPins, serial); - } - -protected: - void _begin() override { + void _begin() override { _serial->begin(9600); _deviceState = DEVSTATE_INITIALISING; diff --git a/IO_ExampleSerial.h b/IO_ExampleSerial.h index 582a51c..9b20399 100644 --- a/IO_ExampleSerial.h +++ b/IO_ExampleSerial.h @@ -36,10 +36,10 @@ class IO_ExampleSerial : public IODevice { public: - IO_ExampleSerial(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud); static void create(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud); protected: + IO_ExampleSerial(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud); void _begin() override; void _loop(unsigned long currentMicros) override; void _write(VPIN vpin, int value) override; diff --git a/IO_HCSR04.h b/IO_HCSR04.h index f413991..5b9b169 100644 --- a/IO_HCSR04.h +++ b/IO_HCSR04.h @@ -73,6 +73,14 @@ private: const uint16_t factor = 58; // ms/cm 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) { + if (checkNoOverlap(vpin) && checkNoOverlap(trigPin) && checkNoOverlap(echoPin)) + new HCSR04(vpin, trigPin, echoPin, onThreshold, offThreshold); + } + +protected: // Constructor perfroms static initialisation of the device object HCSR04 (VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) { _firstVpin = vpin; @@ -83,15 +91,7 @@ public: _offThreshold = offThreshold; addDevice(this); } - - // Static create function provides alternative way to create object - static void create(VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) { - if (checkNoOverlap(vpin) && checkNoOverlap(trigPin) && checkNoOverlap(echoPin)) - new HCSR04(vpin, trigPin, echoPin, onThreshold, offThreshold); - } - -protected: - // _begin function called to perform dynamic initialisation of the device + // _begin function called to perform dynamic initialisation of the device void _begin() override { pinMode(_trigPin, OUTPUT); pinMode(_echoPin, INPUT); diff --git a/IO_MCP23008.h b/IO_MCP23008.h index 8062128..916ebbe 100644 --- a/IO_MCP23008.h +++ b/IO_MCP23008.h @@ -28,6 +28,7 @@ public: if (checkNoOverlap(firstVpin, nPins)) new MCP23008(firstVpin, nPins, I2CAddress, interruptPin); } +private: // Constructor MCP23008(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) : GPIOBase((FSH *)F("MCP23008"), firstVpin, min(nPins, 8), I2CAddress, interruptPin) { @@ -37,7 +38,6 @@ public: outputBuffer[0] = REG_GPIO; } -private: void _writeGpioPort() override { I2CManager.write(_I2CAddress, 2, REG_GPIO, _portOutputState); } diff --git a/IO_MCP23017.h b/IO_MCP23017.h index 4ad84cb..e6ff5be 100644 --- a/IO_MCP23017.h +++ b/IO_MCP23017.h @@ -33,7 +33,8 @@ public: static void create(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1) { if (checkNoOverlap(vpin, nPins)) new MCP23017(vpin, min(nPins,16), I2CAddress, interruptPin); } - + +private: // Constructor MCP23017(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1) : GPIOBase((FSH *)F("MCP23017"), vpin, nPins, I2CAddress, interruptPin) @@ -42,8 +43,6 @@ public: outputBuffer, sizeof(outputBuffer)); outputBuffer[0] = REG_GPIOA; } - -private: void _writeGpioPort() override { I2CManager.write(_I2CAddress, 3, REG_GPIOA, _portOutputState, _portOutputState>>8); } diff --git a/IO_PCF8574.h b/IO_PCF8574.h index 5a9dc05..d3f98c8 100644 --- a/IO_PCF8574.h +++ b/IO_PCF8574.h @@ -46,13 +46,13 @@ public: if (checkNoOverlap(firstVpin, nPins)) new PCF8574(firstVpin, nPins, I2CAddress, interruptPin); } +private: PCF8574(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) : GPIOBase((FSH *)F("PCF8574"), firstVpin, min(nPins, 8), I2CAddress, interruptPin) { requestBlock.setReadParams(_I2CAddress, inputBuffer, 1); } -private: // The pin state is '1' if the pin is an input or if it is an output set to 1. Zero otherwise. void _writeGpioPort() override { I2CManager.write(_I2CAddress, 1, _portOutputState | ~_portMode); diff --git a/IO_VL53L0X.h b/IO_VL53L0X.h index 71f81e0..47204d5 100644 --- a/IO_VL53L0X.h +++ b/IO_VL53L0X.h @@ -127,7 +127,13 @@ private: }; const uint8_t VL53L0X_I2C_DEFAULT_ADDRESS=0x29; -public: + + public: + static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) { + if (checkNoOverlap(firstVpin, nPins)) new VL53L0X(firstVpin, nPins, i2cAddress, onThreshold, offThreshold, xshutPin); + } + +protected: VL53L0X(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) { _firstVpin = firstVpin; _nPins = min(nPins, 3); @@ -138,11 +144,6 @@ public: _value = 0; addDevice(this); } - static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) { - if (checkNoOverlap(firstVpin, nPins)) new VL53L0X(firstVpin, nPins, i2cAddress, onThreshold, offThreshold, xshutPin); - } - -protected: void _begin() override { if (_xshutPin == VPIN_NONE) { // Check if device is already responding on the nominated address.