From 6b7c2ccdf0d56966bddb531d6072b0133fce4958 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Tue, 14 Jun 2022 17:35:29 +0100 Subject: [PATCH] I2C address checks cleaned up --- IODevice.cpp | 11 ++++------- IODevice.h | 11 ++++------- IO_GPIOBase.h | 6 +----- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/IODevice.cpp b/IODevice.cpp index c6af137..f76bbb0 100644 --- a/IODevice.cpp +++ b/IODevice.cpp @@ -61,14 +61,11 @@ void IODevice::begin() { // Allocates 32 pins 100-131 PCA9685::create(100, 16, 0x40); 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 // Allocates 32 pins 164-195 MCP23017::create(164, 16, 0x20); MCP23017::create(180, 16, 0x21); -MCP23017::create(196, 16, 0x40); // should fail // Call the begin() methods of each configured device in turn 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. // returns true if pins DONT overlap with existing device 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); +#endif VPIN lastPin=firstPin+nPins-1; 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 } - bool IODevice::_matchI2CAddress(uint8_t i2cAddress) { - // Overridden for I2c devices. - (void) i2cAddress; - return false; +bool IODevice::_matchI2CAddress(uint8_t i2cAddress) { + return (i2cAddress && i2cAddress==_I2CAddress); } //================================================================================================================== diff --git a/IODevice.h b/IODevice.h index 77325c2..07a632a 100644 --- a/IODevice.h +++ b/IODevice.h @@ -168,6 +168,7 @@ protected: _firstVpin = firstVpin; _nPins = nPins; _nextEntryTime = 0; + _I2CAddress=0; } // Method to perform initialisation of the device (optionally implemented within device class) @@ -220,7 +221,7 @@ protected: // Common object fields. VPIN _firstVpin; int _nPins; - + uint8_t _I2CAddress; // Flag whether the device supports callbacks. bool _hasCallback = false; @@ -229,7 +230,7 @@ protected: int16_t _gpioInterruptPin = -1; // 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. static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1, uint8_t i2cAddress=0); @@ -245,7 +246,6 @@ private: bool owns(VPIN vpin); // Method to find device handling Vpin static IODevice *findDevice(VPIN vpin); - uint8_t _I2CAddress; IODevice *_nextDevice = 0; unsigned long _nextEntryTime; static IODevice *_firstDevice; @@ -287,10 +287,7 @@ private: void updatePosition(uint8_t pin); void writeDevice(uint8_t pin, int value); void _display() override; - virtual bool _matchI2CAddress(uint8_t i2caddress) override { - return i2caddress && i2caddress==_I2CAddress; - } - uint8_t _I2CAddress; // 0x40-0x43 possible + struct ServoData { uint16_t activePosition : 12; // Config parameter diff --git a/IO_GPIOBase.h b/IO_GPIOBase.h index d2a1339..1a66b3d 100644 --- a/IO_GPIOBase.h +++ b/IO_GPIOBase.h @@ -45,13 +45,9 @@ protected: int _read(VPIN vpin) override; void _display() 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 - uint8_t _I2CAddress; + // Allocate enough space for all input pins T _portInputState; T _portOutputState;