From 28caa9e8d3cc4c067bd92c75a8c3bb2da4cc84b6 Mon Sep 17 00:00:00 2001 From: peteGSX Date: Sun, 29 Jan 2023 19:26:33 +1000 Subject: [PATCH] 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