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

I2C address checks cleaned up

This commit is contained in:
Asbelos 2022-06-14 17:35:29 +01:00
parent 08eaa8ddb7
commit 6b7c2ccdf0
3 changed files with 9 additions and 19 deletions

View File

@ -61,14 +61,11 @@ void IODevice::begin() {
// Allocates 32 pins 100-131 // Allocates 32 pins 100-131
PCA9685::create(100, 16, 0x40); PCA9685::create(100, 16, 0x40);
PCA9685::create(116, 16, 0x41); PCA9685::create(116, 16, 0x41);
PCA9685::create(132, 16, 0x41); // should fail
PCA9685::create(118, 4, 0x42); // should fail
// Predefine two MCP23017 module 0x20/0x21 // Predefine two MCP23017 module 0x20/0x21
// Allocates 32 pins 164-195 // Allocates 32 pins 164-195
MCP23017::create(164, 16, 0x20); MCP23017::create(164, 16, 0x20);
MCP23017::create(180, 16, 0x21); MCP23017::create(180, 16, 0x21);
MCP23017::create(196, 16, 0x40); // should fail
// Call the begin() methods of each configured device in turn // Call the begin() methods of each configured device in turn
for (IODevice *dev=_firstDevice; dev!=NULL; dev = dev->_nextDevice) { for (IODevice *dev=_firstDevice; dev!=NULL; dev = dev->_nextDevice) {
@ -280,7 +277,9 @@ IODevice *IODevice::findDevice(VPIN vpin) {
// Private helper function to check for vpin overlap. Run during setup only. // Private helper function to check for vpin overlap. Run during setup only.
// returns true if pins DONT overlap with existing device // returns true if pins DONT overlap with existing device
bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins, uint8_t i2cAddress) { bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins, uint8_t i2cAddress) {
#ifdef DIAG_IO
DIAG(F("Check no overlap %d %d 0x%x"), firstPin,nPins,i2cAddress); DIAG(F("Check no overlap %d %d 0x%x"), firstPin,nPins,i2cAddress);
#endif
VPIN lastPin=firstPin+nPins-1; VPIN lastPin=firstPin+nPins-1;
for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) { for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) {
@ -303,10 +302,8 @@ bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins, uint8_t i2cAddress)
return true; // no overlaps... OK to go on with constructor return true; // no overlaps... OK to go on with constructor
} }
bool IODevice::_matchI2CAddress(uint8_t i2cAddress) { bool IODevice::_matchI2CAddress(uint8_t i2cAddress) {
// Overridden for I2c devices. return (i2cAddress && i2cAddress==_I2CAddress);
(void) i2cAddress;
return false;
} }
//================================================================================================================== //==================================================================================================================

View File

@ -168,6 +168,7 @@ protected:
_firstVpin = firstVpin; _firstVpin = firstVpin;
_nPins = nPins; _nPins = nPins;
_nextEntryTime = 0; _nextEntryTime = 0;
_I2CAddress=0;
} }
// Method to perform initialisation of the device (optionally implemented within device class) // Method to perform initialisation of the device (optionally implemented within device class)
@ -220,7 +221,7 @@ protected:
// Common object fields. // Common object fields.
VPIN _firstVpin; VPIN _firstVpin;
int _nPins; int _nPins;
uint8_t _I2CAddress;
// Flag whether the device supports callbacks. // Flag whether the device supports callbacks.
bool _hasCallback = false; bool _hasCallback = false;
@ -229,7 +230,7 @@ protected:
int16_t _gpioInterruptPin = -1; int16_t _gpioInterruptPin = -1;
// non-i2c hal drivers return false, i2c drivers override this in IO_GPIOBase // non-i2c hal drivers return false, i2c drivers override this in IO_GPIOBase
virtual bool _matchI2CAddress(uint8_t i2cAddress); bool _matchI2CAddress(uint8_t i2cAddress);
// Method to check if pins will overlap before creating new device. // Method to check if pins will overlap before creating new device.
static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1, uint8_t i2cAddress=0); static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1, uint8_t i2cAddress=0);
@ -245,7 +246,6 @@ private:
bool owns(VPIN vpin); bool owns(VPIN vpin);
// Method to find device handling Vpin // Method to find device handling Vpin
static IODevice *findDevice(VPIN vpin); static IODevice *findDevice(VPIN vpin);
uint8_t _I2CAddress;
IODevice *_nextDevice = 0; IODevice *_nextDevice = 0;
unsigned long _nextEntryTime; unsigned long _nextEntryTime;
static IODevice *_firstDevice; static IODevice *_firstDevice;
@ -287,10 +287,7 @@ private:
void updatePosition(uint8_t pin); void updatePosition(uint8_t pin);
void writeDevice(uint8_t pin, int value); void writeDevice(uint8_t pin, int value);
void _display() override; void _display() override;
virtual bool _matchI2CAddress(uint8_t i2caddress) override {
return i2caddress && i2caddress==_I2CAddress;
}
uint8_t _I2CAddress; // 0x40-0x43 possible
struct ServoData { struct ServoData {
uint16_t activePosition : 12; // Config parameter uint16_t activePosition : 12; // Config parameter

View File

@ -45,13 +45,9 @@ protected:
int _read(VPIN vpin) override; int _read(VPIN vpin) override;
void _display() override; void _display() override;
void _loop(unsigned long currentMicros) override; void _loop(unsigned long currentMicros) override;
virtual bool _matchI2CAddress(uint8_t i2cAddress) override {
DIAG(F("MatchI2c %x %x"), i2cAddress, _I2CAddress);
return (i2cAddress && i2cAddress==_I2CAddress);
}
// Data fields // Data fields
uint8_t _I2CAddress;
// Allocate enough space for all input pins // Allocate enough space for all input pins
T _portInputState; T _portInputState;
T _portOutputState; T _portOutputState;