mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-24 13:21:23 +01:00
Refactored, analogue tested
This commit is contained in:
parent
d41b5e0938
commit
53215b496e
@ -71,15 +71,12 @@ private:
|
|||||||
_command2Buffer[0] = EXIOINIT;
|
_command2Buffer[0] = EXIOINIT;
|
||||||
_command2Buffer[1] = _nPins;
|
_command2Buffer[1] = _nPins;
|
||||||
// Send config, if EXIOINITA returned, we're good, setup analogue input buffer, otherwise go offline
|
// Send config, if EXIOINITA returned, we're good, setup analogue input buffer, otherwise go offline
|
||||||
I2CManager.read(_i2cAddress, _receive3Buffer, 3, _command2Buffer, 2);
|
I2CManager.read(_i2cAddress, _receive2Buffer, 2, _command2Buffer, 2);
|
||||||
if (_receive3Buffer[0] == EXIOINITA) {
|
if (_receive2Buffer[0] == EXIOINITA) {
|
||||||
_numAnaloguePins = _receive3Buffer[1];
|
_numAnaloguePins = _receive2Buffer[1];
|
||||||
_numPWMPins = _receive3Buffer[2];
|
|
||||||
_analoguePinBytes = _numAnaloguePins * 2;
|
_analoguePinBytes = _numAnaloguePins * 2;
|
||||||
_analogueInputStates = (byte*) calloc(_analoguePinBytes, 1);
|
_analogueInputStates = (byte*) calloc(_analoguePinBytes, 1);
|
||||||
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
|
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
|
||||||
_servoData = (struct ServoData*) calloc(_numPWMPins, 14);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress);
|
DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress);
|
||||||
_deviceState = DEVSTATE_FAILED;
|
_deviceState = DEVSTATE_FAILED;
|
||||||
@ -105,26 +102,17 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use
|
||||||
// Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use
|
// 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 {
|
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
|
||||||
if (paramCount != 1) return false;
|
if (paramCount != 1) return false;
|
||||||
|
bool pullup = params[0];
|
||||||
int pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
if (configType == CONFIGURE_INPUT) {
|
_digitalOutBuffer[0] = EXIODPUP;
|
||||||
bool pullup = params[0];
|
_digitalOutBuffer[1] = pin;
|
||||||
_digitalOutBuffer[0] = EXIODPUP;
|
_digitalOutBuffer[2] = pullup;
|
||||||
_digitalOutBuffer[1] = pin;
|
I2CManager.write(_i2cAddress, _digitalOutBuffer, 3);
|
||||||
_digitalOutBuffer[2] = pullup;
|
return true;
|
||||||
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
|
// Analogue input pin configuration, used to enable on EX-IOExpander device
|
||||||
@ -133,9 +121,13 @@ private:
|
|||||||
_command2Buffer[0] = EXIOENAN;
|
_command2Buffer[0] = EXIOENAN;
|
||||||
_command2Buffer[1] = pin;
|
_command2Buffer[1] = pin;
|
||||||
I2CManager.write(_i2cAddress, _command2Buffer, 2);
|
I2CManager.write(_i2cAddress, _command2Buffer, 2);
|
||||||
|
_command2Buffer[0] = EXIOENAN;
|
||||||
|
_command2Buffer[1] = pin;
|
||||||
|
I2CManager.write(_i2cAddress, _command2Buffer, 2);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads)
|
||||||
// Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads)
|
// Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads)
|
||||||
void _loop(unsigned long currentMicros) override {
|
void _loop(unsigned long currentMicros) override {
|
||||||
(void)currentMicros; // remove warning
|
(void)currentMicros; // remove warning
|
||||||
@ -143,10 +135,22 @@ private:
|
|||||||
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1);
|
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1);
|
||||||
_command1Buffer[0] = EXIORDAN;
|
_command1Buffer[0] = EXIORDAN;
|
||||||
I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1);
|
I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 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
|
||||||
// Obtain the correct analogue input value
|
// Obtain the correct analogue input value
|
||||||
int _readAnalogue(VPIN vpin) override {
|
int _readAnalogue(VPIN vpin) override {
|
||||||
|
int pin = vpin - _firstVpin;
|
||||||
|
uint8_t _pinLSBByte;
|
||||||
|
for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) {
|
||||||
|
if (_analoguePinMap[aPin] == pin) {
|
||||||
|
_pinLSBByte = aPin * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
int pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
uint8_t _pinLSBByte;
|
uint8_t _pinLSBByte;
|
||||||
for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) {
|
for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) {
|
||||||
@ -158,6 +162,7 @@ private:
|
|||||||
return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte];
|
return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Obtain the correct digital input value
|
||||||
// Obtain the correct digital input value
|
// Obtain the correct digital input value
|
||||||
int _read(VPIN vpin) override {
|
int _read(VPIN vpin) override {
|
||||||
int pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
@ -178,11 +183,12 @@ private:
|
|||||||
DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d Vpins %d-%d %S"),
|
DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d Vpins %d-%d %S"),
|
||||||
_i2cAddress, _majorVer, _minorVer, _patchVer,
|
_i2cAddress, _majorVer, _minorVer, _patchVer,
|
||||||
(int)_firstVpin, (int)_firstVpin+_nPins-1,
|
(int)_firstVpin, (int)_firstVpin+_nPins-1,
|
||||||
|
(int)_firstVpin, (int)_firstVpin+_nPins-1,
|
||||||
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t _i2cAddress;
|
uint8_t _i2cAddress;
|
||||||
uint8_t _numAnaloguePins = 0;
|
uint8_t _numAnaloguePins;
|
||||||
byte _digitalOutBuffer[3];
|
byte _digitalOutBuffer[3];
|
||||||
uint8_t _versionBuffer[3];
|
uint8_t _versionBuffer[3];
|
||||||
uint8_t _majorVer = 0;
|
uint8_t _majorVer = 0;
|
||||||
@ -194,29 +200,8 @@ private:
|
|||||||
uint8_t _analoguePinBytes = 0;
|
uint8_t _analoguePinBytes = 0;
|
||||||
byte _command1Buffer[1];
|
byte _command1Buffer[1];
|
||||||
byte _command2Buffer[2];
|
byte _command2Buffer[2];
|
||||||
byte _receive3Buffer[3];
|
byte _receive2Buffer[2];
|
||||||
uint8_t* _analoguePinMap;
|
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 {
|
enum {
|
||||||
EXIOINIT = 0xE0, // Flag to initialise setup procedure
|
EXIOINIT = 0xE0, // Flag to initialise setup procedure
|
||||||
|
Loading…
Reference in New Issue
Block a user