1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00

Privatize HAL constructors

Forces caller to go via create function which includes overlap checks before class is instantiated.
This commit is contained in:
Asbelos 2022-06-14 15:23:27 +01:00
parent e11d2d08d1
commit ef937dcacf
10 changed files with 40 additions and 38 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ myAutomation.h
myFilter.cpp myFilter.cpp
myLayout.h myLayout.h
.vscode/extensions.json .vscode/extensions.json
.vscode/extensions.json

View File

@ -260,8 +260,6 @@ private:
class PCA9685 : public IODevice { class PCA9685 : public IODevice {
public: public:
static void create(VPIN vpin, int nPins, uint8_t I2CAddress); static void create(VPIN vpin, int nPins, uint8_t I2CAddress);
// Constructor
PCA9685(VPIN vpin, int nPins, uint8_t I2CAddress);
enum ProfileType : uint8_t { enum ProfileType : uint8_t {
Instant = 0, // Moves immediately between positions (if duration not specified) Instant = 0, // Moves immediately between positions (if duration not specified)
UseDuration = 0, // Use specified duration UseDuration = 0, // Use specified duration
@ -273,6 +271,8 @@ public:
}; };
private: private:
// Constructor
PCA9685(VPIN vpin, int nPins, uint8_t I2CAddress);
// Device-specific initialisation // Device-specific initialisation
void _begin() override; void _begin() override;
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override; bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override;
@ -320,10 +320,10 @@ private:
class DCCAccessoryDecoder: public IODevice { class DCCAccessoryDecoder: public IODevice {
public: public:
static void create(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress); static void create(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress);
// Constructor
DCCAccessoryDecoder(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress);
private: private:
// Constructor
DCCAccessoryDecoder(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress);
// Device-specific write function. // Device-specific write function.
void _begin() override; void _begin() override;
void _write(VPIN vpin, int value) override; void _write(VPIN vpin, int value) override;
@ -343,13 +343,13 @@ public:
addDevice(new ArduinoPins(firstVpin, nPins)); addDevice(new ArduinoPins(firstVpin, nPins));
} }
// Constructor
ArduinoPins(VPIN firstVpin, int nPins);
static void fastWriteDigital(uint8_t pin, uint8_t value); static void fastWriteDigital(uint8_t pin, uint8_t value);
static bool fastReadDigital(uint8_t pin); static bool fastReadDigital(uint8_t pin);
private: private:
// Constructor
ArduinoPins(VPIN firstVpin, int nPins);
// Device-specific pin configuration // Device-specific pin configuration
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override; bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override;
// Device-specific write function. // Device-specific write function.

View File

@ -59,6 +59,10 @@
**********************************************************************************************/ **********************************************************************************************/
class ADS111x: public IODevice { class ADS111x: public IODevice {
public: 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) { ADS111x(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
_firstVpin = firstVpin; _firstVpin = firstVpin;
_nPins = min(nPins,4); _nPins = min(nPins,4);
@ -68,10 +72,6 @@ public:
_value[i] = -1; _value[i] = -1;
addDevice(this); addDevice(this);
} }
static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
if (checkNoOverlap(firstVpin,nPins)) new ADS111x(firstVpin, nPins, i2cAddress);
}
private:
void _begin() { void _begin() {
// Initialise ADS device // Initialise ADS device
if (I2CManager.exists(_i2cAddress)) { if (I2CManager.exists(_i2cAddress)) {

View File

@ -69,6 +69,12 @@ private:
unsigned long _commandSendTime; // Allows timeout processing unsigned long _commandSendTime; // Allows timeout processing
public: public:
static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) {
if (checkNoOverlap(firstVpin,nPins)) new DFPlayer(firstVpin, nPins, serial);
}
protected:
// Constructor // Constructor
DFPlayer(VPIN firstVpin, int nPins, HardwareSerial &serial) : DFPlayer(VPIN firstVpin, int nPins, HardwareSerial &serial) :
IODevice(firstVpin, nPins), IODevice(firstVpin, nPins),
@ -77,12 +83,7 @@ public:
addDevice(this); addDevice(this);
} }
static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) { void _begin() override {
if (checkNoOverlap(firstVpin,nPins)) new DFPlayer(firstVpin, nPins, serial);
}
protected:
void _begin() override {
_serial->begin(9600); _serial->begin(9600);
_deviceState = DEVSTATE_INITIALISING; _deviceState = DEVSTATE_INITIALISING;

View File

@ -36,10 +36,10 @@
class IO_ExampleSerial : public IODevice { class IO_ExampleSerial : public IODevice {
public: public:
IO_ExampleSerial(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud);
static void create(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud); static void create(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud);
protected: protected:
IO_ExampleSerial(VPIN firstVpin, int nPins, HardwareSerial *serial, unsigned long baud);
void _begin() override; void _begin() override;
void _loop(unsigned long currentMicros) override; void _loop(unsigned long currentMicros) override;
void _write(VPIN vpin, int value) override; void _write(VPIN vpin, int value) override;

View File

@ -73,6 +73,14 @@ private:
const uint16_t factor = 58; // ms/cm const uint16_t factor = 58; // ms/cm
public: 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 // Constructor perfroms static initialisation of the device object
HCSR04 (VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) { HCSR04 (VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) {
_firstVpin = vpin; _firstVpin = vpin;
@ -83,15 +91,7 @@ public:
_offThreshold = offThreshold; _offThreshold = offThreshold;
addDevice(this); addDevice(this);
} }
// _begin function called to perform dynamic initialisation of the device
// 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
void _begin() override { void _begin() override {
pinMode(_trigPin, OUTPUT); pinMode(_trigPin, OUTPUT);
pinMode(_echoPin, INPUT); pinMode(_echoPin, INPUT);

View File

@ -28,6 +28,7 @@ public:
if (checkNoOverlap(firstVpin, nPins)) new MCP23008(firstVpin, nPins, I2CAddress, interruptPin); if (checkNoOverlap(firstVpin, nPins)) new MCP23008(firstVpin, nPins, I2CAddress, interruptPin);
} }
private:
// Constructor // Constructor
MCP23008(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) MCP23008(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1)
: GPIOBase<uint8_t>((FSH *)F("MCP23008"), firstVpin, min(nPins, 8), I2CAddress, interruptPin) { : GPIOBase<uint8_t>((FSH *)F("MCP23008"), firstVpin, min(nPins, 8), I2CAddress, interruptPin) {
@ -37,7 +38,6 @@ public:
outputBuffer[0] = REG_GPIO; outputBuffer[0] = REG_GPIO;
} }
private:
void _writeGpioPort() override { void _writeGpioPort() override {
I2CManager.write(_I2CAddress, 2, REG_GPIO, _portOutputState); I2CManager.write(_I2CAddress, 2, REG_GPIO, _portOutputState);
} }

View File

@ -33,7 +33,8 @@ public:
static void create(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1) { 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); if (checkNoOverlap(vpin, nPins)) new MCP23017(vpin, min(nPins,16), I2CAddress, interruptPin);
} }
private:
// Constructor // Constructor
MCP23017(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1) MCP23017(VPIN vpin, int nPins, uint8_t I2CAddress, int interruptPin=-1)
: GPIOBase<uint16_t>((FSH *)F("MCP23017"), vpin, nPins, I2CAddress, interruptPin) : GPIOBase<uint16_t>((FSH *)F("MCP23017"), vpin, nPins, I2CAddress, interruptPin)
@ -42,8 +43,6 @@ public:
outputBuffer, sizeof(outputBuffer)); outputBuffer, sizeof(outputBuffer));
outputBuffer[0] = REG_GPIOA; outputBuffer[0] = REG_GPIOA;
} }
private:
void _writeGpioPort() override { void _writeGpioPort() override {
I2CManager.write(_I2CAddress, 3, REG_GPIOA, _portOutputState, _portOutputState>>8); I2CManager.write(_I2CAddress, 3, REG_GPIOA, _portOutputState, _portOutputState>>8);
} }

View File

@ -46,13 +46,13 @@ public:
if (checkNoOverlap(firstVpin, nPins)) new PCF8574(firstVpin, nPins, I2CAddress, interruptPin); if (checkNoOverlap(firstVpin, nPins)) new PCF8574(firstVpin, nPins, I2CAddress, interruptPin);
} }
private:
PCF8574(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1) PCF8574(VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin=-1)
: GPIOBase<uint8_t>((FSH *)F("PCF8574"), firstVpin, min(nPins, 8), I2CAddress, interruptPin) : GPIOBase<uint8_t>((FSH *)F("PCF8574"), firstVpin, min(nPins, 8), I2CAddress, interruptPin)
{ {
requestBlock.setReadParams(_I2CAddress, inputBuffer, 1); 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. // 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 { void _writeGpioPort() override {
I2CManager.write(_I2CAddress, 1, _portOutputState | ~_portMode); I2CManager.write(_I2CAddress, 1, _portOutputState | ~_portMode);

View File

@ -127,7 +127,13 @@ private:
}; };
const uint8_t VL53L0X_I2C_DEFAULT_ADDRESS=0x29; 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) { VL53L0X(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) {
_firstVpin = firstVpin; _firstVpin = firstVpin;
_nPins = min(nPins, 3); _nPins = min(nPins, 3);
@ -138,11 +144,6 @@ public:
_value = 0; _value = 0;
addDevice(this); 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 { void _begin() override {
if (_xshutPin == VPIN_NONE) { if (_xshutPin == VPIN_NONE) {
// Check if device is already responding on the nominated address. // Check if device is already responding on the nominated address.