1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00

Implement status checks

This commit is contained in:
peteGSX 2023-03-13 08:38:28 +10:00
parent 0cc07ed1df
commit 95d0120204

View File

@ -155,16 +155,25 @@ private:
// 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
if (_deviceState == DEVSTATE_FAILED) return; if (_deviceState == DEVSTATE_FAILED) return; // If device failed, return
if (_i2crb.isBusy()) return; uint8_t status = _i2crb.status;
if (_commandFlag) { if (status == I2C_STATUS_PENDING) return; // If device busy, return
_command1Buffer[0] = EXIORDD; if (status == I2C_STATUS_OK) { // If device ok, read input data
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1, &_i2crb); if (_commandFlag) {
_command1Buffer[0] = EXIORDD;
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1, &_i2crb);
} else {
_command1Buffer[0] = EXIORDAN;
byte _tempAnalogue[_analoguePinBytes]; // Setup temp buffer so reads come from known state
I2CManager.read(_i2cAddress, _tempAnalogue, _analoguePinBytes, _command1Buffer, 1, &_i2crb);
memcpy(_analogueInputStates, _tempAnalogue, _analoguePinBytes); // Copy temp buffer to states
}
_commandFlag = !_commandFlag;
// Need to delay here: digital in IO_Base 4000UL, analogue in IO_AnalogueInputs 10000UL (fast) or 1000000UL(slow)
} else { } else {
_command1Buffer[0] = EXIORDAN; DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1, &_i2crb); _deviceState = DEVSTATE_FAILED;
} }
_commandFlag = !_commandFlag;
} }
// Obtain the correct analogue input value // Obtain the correct analogue input value
@ -196,9 +205,14 @@ private:
_digitalOutBuffer[0] = EXIOWRD; _digitalOutBuffer[0] = EXIOWRD;
_digitalOutBuffer[1] = pin; _digitalOutBuffer[1] = pin;
_digitalOutBuffer[2] = value; _digitalOutBuffer[2] = value;
I2CManager.read(_i2cAddress, _command1Buffer, 1, _digitalOutBuffer, 3, &_i2crb); uint8_t status = I2CManager.read(_i2cAddress, _command1Buffer, 1, _digitalOutBuffer, 3);
if (_command1Buffer[0] != EXIORDY) { if (status != I2C_STATUS_OK) {
DIAG(F("Vpin %d cannot be used as a digital output pin"), (int)vpin); DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
_deviceState = DEVSTATE_FAILED;
} else {
if (_command1Buffer[0] != EXIORDY) {
DIAG(F("Vpin %d cannot be used as a digital output pin"), (int)vpin);
}
} }
} }
@ -216,9 +230,14 @@ private:
_servoBuffer[4] = profile; _servoBuffer[4] = profile;
_servoBuffer[5] = duration & 0xFF; _servoBuffer[5] = duration & 0xFF;
_servoBuffer[6] = duration >> 8; _servoBuffer[6] = duration >> 8;
I2CManager.read(_i2cAddress, _command1Buffer, 1, _servoBuffer, 7, &_i2crb); uint8_t status = I2CManager.read(_i2cAddress, _command1Buffer, 1, _servoBuffer, 7);
if (_command1Buffer[0] != EXIORDY) { if (status != I2C_STATUS_OK) {
DIAG(F("Vpin %d cannot be used as a servo/PWM pin"), (int)vpin); DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
_deviceState = DEVSTATE_FAILED;
} else {
if (_command1Buffer[0] != EXIORDY) {
DIAG(F("Vpin %d cannot be used as a servo/PWM pin"), (int)vpin);
}
} }
} }
@ -248,7 +267,7 @@ private:
byte _servoBuffer[7]; byte _servoBuffer[7];
uint8_t* _analoguePinMap; uint8_t* _analoguePinMap;
I2CRB _i2crb; I2CRB _i2crb;
bool _commandFlag = 0; bool _commandFlag = 1;
// EX-IOExpander protocol flags // EX-IOExpander protocol flags
enum { enum {