From a17c02444d4157b8ce9a657af43b1663a9ddcefc Mon Sep 17 00:00:00 2001 From: peteGSX Date: Sun, 29 Jan 2023 10:06:01 +1000 Subject: [PATCH 1/3] Refactored, analogue tested --- IO_EXIOExpander.h | 97 ++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index d54feac..724ea42 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -54,22 +54,18 @@ */ class EXIOExpander : public IODevice { public: - static void create(VPIN vpin, int nPins, uint8_t i2cAddress, int numDigitalPins, int numAnaloguePins) { - if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress, numDigitalPins, numAnaloguePins); + static void create(VPIN vpin, int nPins, uint8_t i2cAddress) { + if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress); } private: // Constructor - EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress, int numDigitalPins, int numAnaloguePins) { + EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) { _firstVpin = firstVpin; _nPins = nPins; _i2cAddress = i2cAddress; - _numDigitalPins = numDigitalPins; - _numAnaloguePins = numAnaloguePins; - _digitalPinBytes = (numDigitalPins+7)/8; - _analoguePinBytes = numAnaloguePins * 2; + _digitalPinBytes = (nPins+7)/8; _digitalInputStates=(byte*) calloc(_digitalPinBytes,1); - _analogueInputStates=(byte*) calloc(_analoguePinBytes,1); addDevice(this); } @@ -77,20 +73,26 @@ private: // Initialise EX-IOExander device I2CManager.begin(); if (I2CManager.exists(_i2cAddress)) { - _digitalOutBuffer[0] = EXIOINIT; - _digitalOutBuffer[1] = _numDigitalPins; - _digitalOutBuffer[2] = _numAnaloguePins; - // Send config, if EXIORDY returned, we're good, otherwise go offline - I2CManager.read(_i2cAddress, _commandBuffer, 1, _digitalOutBuffer, 3); - if (_commandBuffer[0] != EXIORDY) { + _command2Buffer[0] = EXIOINIT; + _command2Buffer[1] = _nPins; + // Send config, if EXIOINITA returned, we're good, setup analogue input buffer, otherwise go offline + I2CManager.read(_i2cAddress, _receive2Buffer, 2, _command2Buffer, 2); + if (_receive2Buffer[0] == EXIOINITA) { + _numAnaloguePins = _receive2Buffer[1]; + _analoguePinBytes = _numAnaloguePins * 2; + _analogueInputStates = (byte*) calloc(_analoguePinBytes, 1); + _analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1); + } else { DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress); _deviceState = DEVSTATE_FAILED; return; } + // We now need to retrieve the analogue pin map + _command1Buffer[0] = EXIOINITA; + I2CManager.read(_i2cAddress, _analoguePinMap, _numAnaloguePins, _command1Buffer, 1); // Attempt to get version, if we don't get it, we don't care, don't go offline - // Using digital in buffer in reverse to save RAM - _commandBuffer[0] = EXIOVER; - I2CManager.read(_i2cAddress, _versionBuffer, 3, _commandBuffer, 1); + _command1Buffer[0] = EXIOVER; + I2CManager.read(_i2cAddress, _versionBuffer, 3, _command1Buffer, 1); _majorVer = _versionBuffer[0]; _minorVer = _versionBuffer[1]; _patchVer = _versionBuffer[2]; @@ -105,13 +107,10 @@ private: } } + // Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override { if (configType != CONFIGURE_INPUT) return false; if (paramCount != 1) return false; - if (vpin >= _firstVpin + _numDigitalPins) { - DIAG(F("EX-IOExpander ERROR: Vpin %d is an analogue pin, cannot use as a digital pin"), vpin); - return false; - } bool pullup = params[0]; int pin = vpin - _firstVpin; _digitalOutBuffer[0] = EXIODPUP; @@ -121,37 +120,39 @@ private: return true; } - // We only use this to detect incorrect use of analogue pins + // Analogue input pin configuration, used to enable on EX-IOExpander device int _configureAnalogIn(VPIN vpin) override { - if (vpin < _firstVpin + _numDigitalPins) { - DIAG(F("EX-IOExpander ERROR: Vpin %d is a digital pin, cannot use as an analogue pin"), vpin); - return false; - } int pin = vpin - _firstVpin; - _analogueOutBuffer[0] = EXIOENAN; - _analogueOutBuffer[1] = pin; - I2CManager.write(_i2cAddress, _analogueOutBuffer, 2); + _command2Buffer[0] = EXIOENAN; + _command2Buffer[1] = pin; + I2CManager.write(_i2cAddress, _command2Buffer, 2); return true; } + // Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads) void _loop(unsigned long currentMicros) override { (void)currentMicros; // remove warning - _commandBuffer[0] = EXIORDD; - I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _commandBuffer, 1); - _commandBuffer[0] = EXIORDAN; - I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _commandBuffer, 1); + _command1Buffer[0] = EXIORDD; + I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1); + _command1Buffer[0] = EXIORDAN; + I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1); } + // Obtain the correct analogue input value int _readAnalogue(VPIN vpin) override { - if (vpin < _firstVpin + _numDigitalPins) return false; - int pin = vpin - _firstVpin - _numDigitalPins; - uint8_t _pinLSBByte = pin * 2; + int pin = vpin - _firstVpin; + uint8_t _pinLSBByte; + for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) { + if (_analoguePinMap[aPin] == pin) { + _pinLSBByte = aPin * 2; + } + } uint8_t _pinMSBByte = _pinLSBByte + 1; return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte]; } + // Obtain the correct digital input value int _read(VPIN vpin) override { - if (vpin >= _firstVpin + _numDigitalPins) return false; int pin = vpin - _firstVpin; uint8_t pinByte = pin / 8; bool value = _digitalInputStates[pinByte] >> (pin - pinByte * 8); @@ -159,7 +160,6 @@ private: } void _write(VPIN vpin, int value) override { - if (vpin >= _firstVpin + _numDigitalPins) return; int pin = vpin - _firstVpin; _digitalOutBuffer[0] = EXIOWRD; _digitalOutBuffer[1] = pin; @@ -168,25 +168,14 @@ private: } void _display() override { - int _firstAnalogue, _lastAnalogue; - if (_numAnaloguePins == 0) { - _firstAnalogue = 0; - _lastAnalogue = 0; - } else { - _firstAnalogue = _firstVpin + _numDigitalPins; - _lastAnalogue = _firstVpin + _nPins - 1; - } - DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d: %d Digital Vpins %d-%d, %d Analogue Vpins %d-%d %S"), + DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d Vpins %d-%d %S"), _i2cAddress, _majorVer, _minorVer, _patchVer, - _numDigitalPins, _firstVpin, _firstVpin + _numDigitalPins - 1, - _numAnaloguePins, _firstAnalogue, _lastAnalogue, + (int)_firstVpin, (int)_firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("")); } uint8_t _i2cAddress; - uint8_t _numDigitalPins; uint8_t _numAnaloguePins; - byte _analogueOutBuffer[2]; byte _digitalOutBuffer[3]; uint8_t _versionBuffer[3]; uint8_t _majorVer = 0; @@ -196,7 +185,10 @@ private: byte* _analogueInputStates; uint8_t _digitalPinBytes = 0; uint8_t _analoguePinBytes = 0; - byte _commandBuffer[1]; + byte _command1Buffer[1]; + byte _command2Buffer[2]; + byte _receive2Buffer[2]; + uint8_t* _analoguePinMap; enum { EXIOINIT = 0xE0, // Flag to initialise setup procedure @@ -207,6 +199,7 @@ private: EXIOWRD = 0xE5, // Flag for digital write EXIORDD = 0xE6, // Flag to read digital input EXIOENAN = 0xE7, // Flag eo enable an analogue pin + EXIOINITA = 0xE8, // Flag we're receiving analogue pin info }; }; From d8a1bcaf34b6a66b015045e3e02ff3e62876e470 Mon Sep 17 00:00:00 2001 From: peteGSX Date: Sun, 29 Jan 2023 10:06:01 +1000 Subject: [PATCH 2/3] Refactored, analogue tested --- IO_EXIOExpander.h | 97 ++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index d54feac..724ea42 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -54,22 +54,18 @@ */ class EXIOExpander : public IODevice { public: - static void create(VPIN vpin, int nPins, uint8_t i2cAddress, int numDigitalPins, int numAnaloguePins) { - if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress, numDigitalPins, numAnaloguePins); + static void create(VPIN vpin, int nPins, uint8_t i2cAddress) { + if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress); } private: // Constructor - EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress, int numDigitalPins, int numAnaloguePins) { + EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) { _firstVpin = firstVpin; _nPins = nPins; _i2cAddress = i2cAddress; - _numDigitalPins = numDigitalPins; - _numAnaloguePins = numAnaloguePins; - _digitalPinBytes = (numDigitalPins+7)/8; - _analoguePinBytes = numAnaloguePins * 2; + _digitalPinBytes = (nPins+7)/8; _digitalInputStates=(byte*) calloc(_digitalPinBytes,1); - _analogueInputStates=(byte*) calloc(_analoguePinBytes,1); addDevice(this); } @@ -77,20 +73,26 @@ private: // Initialise EX-IOExander device I2CManager.begin(); if (I2CManager.exists(_i2cAddress)) { - _digitalOutBuffer[0] = EXIOINIT; - _digitalOutBuffer[1] = _numDigitalPins; - _digitalOutBuffer[2] = _numAnaloguePins; - // Send config, if EXIORDY returned, we're good, otherwise go offline - I2CManager.read(_i2cAddress, _commandBuffer, 1, _digitalOutBuffer, 3); - if (_commandBuffer[0] != EXIORDY) { + _command2Buffer[0] = EXIOINIT; + _command2Buffer[1] = _nPins; + // Send config, if EXIOINITA returned, we're good, setup analogue input buffer, otherwise go offline + I2CManager.read(_i2cAddress, _receive2Buffer, 2, _command2Buffer, 2); + if (_receive2Buffer[0] == EXIOINITA) { + _numAnaloguePins = _receive2Buffer[1]; + _analoguePinBytes = _numAnaloguePins * 2; + _analogueInputStates = (byte*) calloc(_analoguePinBytes, 1); + _analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1); + } else { DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress); _deviceState = DEVSTATE_FAILED; return; } + // We now need to retrieve the analogue pin map + _command1Buffer[0] = EXIOINITA; + I2CManager.read(_i2cAddress, _analoguePinMap, _numAnaloguePins, _command1Buffer, 1); // Attempt to get version, if we don't get it, we don't care, don't go offline - // Using digital in buffer in reverse to save RAM - _commandBuffer[0] = EXIOVER; - I2CManager.read(_i2cAddress, _versionBuffer, 3, _commandBuffer, 1); + _command1Buffer[0] = EXIOVER; + I2CManager.read(_i2cAddress, _versionBuffer, 3, _command1Buffer, 1); _majorVer = _versionBuffer[0]; _minorVer = _versionBuffer[1]; _patchVer = _versionBuffer[2]; @@ -105,13 +107,10 @@ private: } } + // Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override { if (configType != CONFIGURE_INPUT) return false; if (paramCount != 1) return false; - if (vpin >= _firstVpin + _numDigitalPins) { - DIAG(F("EX-IOExpander ERROR: Vpin %d is an analogue pin, cannot use as a digital pin"), vpin); - return false; - } bool pullup = params[0]; int pin = vpin - _firstVpin; _digitalOutBuffer[0] = EXIODPUP; @@ -121,37 +120,39 @@ private: return true; } - // We only use this to detect incorrect use of analogue pins + // Analogue input pin configuration, used to enable on EX-IOExpander device int _configureAnalogIn(VPIN vpin) override { - if (vpin < _firstVpin + _numDigitalPins) { - DIAG(F("EX-IOExpander ERROR: Vpin %d is a digital pin, cannot use as an analogue pin"), vpin); - return false; - } int pin = vpin - _firstVpin; - _analogueOutBuffer[0] = EXIOENAN; - _analogueOutBuffer[1] = pin; - I2CManager.write(_i2cAddress, _analogueOutBuffer, 2); + _command2Buffer[0] = EXIOENAN; + _command2Buffer[1] = pin; + I2CManager.write(_i2cAddress, _command2Buffer, 2); return true; } + // Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads) void _loop(unsigned long currentMicros) override { (void)currentMicros; // remove warning - _commandBuffer[0] = EXIORDD; - I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _commandBuffer, 1); - _commandBuffer[0] = EXIORDAN; - I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _commandBuffer, 1); + _command1Buffer[0] = EXIORDD; + I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1); + _command1Buffer[0] = EXIORDAN; + I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1); } + // Obtain the correct analogue input value int _readAnalogue(VPIN vpin) override { - if (vpin < _firstVpin + _numDigitalPins) return false; - int pin = vpin - _firstVpin - _numDigitalPins; - uint8_t _pinLSBByte = pin * 2; + int pin = vpin - _firstVpin; + uint8_t _pinLSBByte; + for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) { + if (_analoguePinMap[aPin] == pin) { + _pinLSBByte = aPin * 2; + } + } uint8_t _pinMSBByte = _pinLSBByte + 1; return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte]; } + // Obtain the correct digital input value int _read(VPIN vpin) override { - if (vpin >= _firstVpin + _numDigitalPins) return false; int pin = vpin - _firstVpin; uint8_t pinByte = pin / 8; bool value = _digitalInputStates[pinByte] >> (pin - pinByte * 8); @@ -159,7 +160,6 @@ private: } void _write(VPIN vpin, int value) override { - if (vpin >= _firstVpin + _numDigitalPins) return; int pin = vpin - _firstVpin; _digitalOutBuffer[0] = EXIOWRD; _digitalOutBuffer[1] = pin; @@ -168,25 +168,14 @@ private: } void _display() override { - int _firstAnalogue, _lastAnalogue; - if (_numAnaloguePins == 0) { - _firstAnalogue = 0; - _lastAnalogue = 0; - } else { - _firstAnalogue = _firstVpin + _numDigitalPins; - _lastAnalogue = _firstVpin + _nPins - 1; - } - DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d: %d Digital Vpins %d-%d, %d Analogue Vpins %d-%d %S"), + DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d Vpins %d-%d %S"), _i2cAddress, _majorVer, _minorVer, _patchVer, - _numDigitalPins, _firstVpin, _firstVpin + _numDigitalPins - 1, - _numAnaloguePins, _firstAnalogue, _lastAnalogue, + (int)_firstVpin, (int)_firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("")); } uint8_t _i2cAddress; - uint8_t _numDigitalPins; uint8_t _numAnaloguePins; - byte _analogueOutBuffer[2]; byte _digitalOutBuffer[3]; uint8_t _versionBuffer[3]; uint8_t _majorVer = 0; @@ -196,7 +185,10 @@ private: byte* _analogueInputStates; uint8_t _digitalPinBytes = 0; uint8_t _analoguePinBytes = 0; - byte _commandBuffer[1]; + byte _command1Buffer[1]; + byte _command2Buffer[2]; + byte _receive2Buffer[2]; + uint8_t* _analoguePinMap; enum { EXIOINIT = 0xE0, // Flag to initialise setup procedure @@ -207,6 +199,7 @@ private: EXIOWRD = 0xE5, // Flag for digital write EXIORDD = 0xE6, // Flag to read digital input EXIOENAN = 0xE7, // Flag eo enable an analogue pin + EXIOINITA = 0xE8, // Flag we're receiving analogue pin info }; }; From 28caa9e8d3cc4c067bd92c75a8c3bb2da4cc84b6 Mon Sep 17 00:00:00 2001 From: peteGSX Date: Sun, 29 Jan 2023 19:26:33 +1000 Subject: [PATCH 3/3] Brief PWM start --- IO_EXIOExpander.h | 76 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index 724ea42..f284bc1 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -1,5 +1,5 @@ /* - * © 2021, Peter Cole. All rights reserved. + * © 2022, Peter Cole. All rights reserved. * * This file is part of EX-CommandStation * @@ -26,19 +26,14 @@ * (Note the device driver is included by default) * * void halSetup() { -* // EXIOExpander::create(vpin, num_vpins, i2c_address, digitalPinCount, analoguePinCount); -* EXIOExpander::create(800, 18, 0x65, 12, 8); +* // EXIOExpander::create(vpin, num_vpins, i2c_address); +* EXIOExpander::create(800, 18, 0x65); * } * -* Note when defining the number of digital and analogue pins, there is no way to sanity check -* this from the device driver, and it is up to the user to define the correct values here. -* -* All pins available on the EX-IOExpander device must be accounted for. -* -* Vpins are allocated to digital pins first, and then analogue pins, so digital pins will -* populate the first part of the specified vpin range, with the analogue pins populating the -* last part of the vpin range. -* Eg. for a default Nano, 800 - 811 are digital (D2 - D13), 812 to 817 are analogue (A0 - A3, A6/A7). +* All pins on an EX-IOExpander device are allocated according to the pin map for the specific +* device in use. There is no way for the device driver to sanity check pins are used for the +* correct purpose, however the EX-IOExpander device's pin map will prevent pins being used +* incorrectly (eg. A6/7 on Nano cannot be used for digital input/output). */ #ifndef IO_EX_IOEXPANDER_H @@ -76,12 +71,15 @@ private: _command2Buffer[0] = EXIOINIT; _command2Buffer[1] = _nPins; // Send config, if EXIOINITA returned, we're good, setup analogue input buffer, otherwise go offline - I2CManager.read(_i2cAddress, _receive2Buffer, 2, _command2Buffer, 2); - if (_receive2Buffer[0] == EXIOINITA) { - _numAnaloguePins = _receive2Buffer[1]; + I2CManager.read(_i2cAddress, _receive3Buffer, 3, _command2Buffer, 2); + if (_receive3Buffer[0] == EXIOINITA) { + _numAnaloguePins = _receive3Buffer[1]; + _numPWMPins = _receive3Buffer[2]; _analoguePinBytes = _numAnaloguePins * 2; _analogueInputStates = (byte*) calloc(_analoguePinBytes, 1); _analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1); + _servoData = (struct ServoData*) calloc(_numPWMPins, 14); + } else { DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress); _deviceState = DEVSTATE_FAILED; @@ -109,15 +107,24 @@ private: // Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override { - if (configType != CONFIGURE_INPUT) return false; if (paramCount != 1) return false; - bool pullup = params[0]; int pin = vpin - _firstVpin; - _digitalOutBuffer[0] = EXIODPUP; - _digitalOutBuffer[1] = pin; - _digitalOutBuffer[2] = pullup; - I2CManager.write(_i2cAddress, _digitalOutBuffer, 3); - return true; + if (configType == CONFIGURE_INPUT) { + bool pullup = params[0]; + _digitalOutBuffer[0] = EXIODPUP; + _digitalOutBuffer[1] = pin; + _digitalOutBuffer[2] = pullup; + I2CManager.write(_i2cAddress, _digitalOutBuffer, 3); + return true; + } else if (configType == CONFIGURE_SERVO) { + DIAG(F("Configure servo at pin %d"), (int)pin); + for (int i = 0; i < paramCount; i++) { + DIAG(F("Param %d is %x"), (int)i, params[i]); + } + return true; + } else { + return false; + } } // Analogue input pin configuration, used to enable on EX-IOExpander device @@ -175,7 +182,7 @@ private: } uint8_t _i2cAddress; - uint8_t _numAnaloguePins; + uint8_t _numAnaloguePins = 0; byte _digitalOutBuffer[3]; uint8_t _versionBuffer[3]; uint8_t _majorVer = 0; @@ -187,8 +194,29 @@ private: uint8_t _analoguePinBytes = 0; byte _command1Buffer[1]; byte _command2Buffer[2]; - byte _receive2Buffer[2]; + byte _receive3Buffer[3]; uint8_t* _analoguePinMap; + uint8_t _numPWMPins = 0; + + struct ServoData { + uint16_t activePosition : 12; // Config parameter + uint16_t inactivePosition : 12; // Config parameter + uint16_t currentPosition : 12; + uint16_t fromPosition : 12; + uint16_t toPosition : 12; + uint8_t profile; // Config parameter + uint16_t stepNumber; // Index of current step (starting from 0) + uint16_t numSteps; // Number of steps in animation, or 0 if none in progress. + uint8_t currentProfile; // profile being used for current animation. + uint16_t duration; // time (tenths of a second) for animation to complete. + } ServoData; // 14 bytes per element, i.e. per pin in use + + struct ServoData* _servoData; + + static const uint8_t _catchupSteps = 5; // number of steps to wait before switching servo off + static const byte FLASH _bounceProfile[30]; + + const unsigned int refreshInterval = 50; // refresh every 50ms enum { EXIOINIT = 0xE0, // Flag to initialise setup procedure