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:
parent
0cc07ed1df
commit
95d0120204
@ -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 (status == I2C_STATUS_PENDING) return; // If device busy, return
|
||||||
|
if (status == I2C_STATUS_OK) { // If device ok, read input data
|
||||||
if (_commandFlag) {
|
if (_commandFlag) {
|
||||||
_command1Buffer[0] = EXIORDD;
|
_command1Buffer[0] = EXIORDD;
|
||||||
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1, &_i2crb);
|
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1, &_i2crb);
|
||||||
} else {
|
} else {
|
||||||
_command1Buffer[0] = EXIORDAN;
|
_command1Buffer[0] = EXIORDAN;
|
||||||
I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1, &_i2crb);
|
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;
|
_commandFlag = !_commandFlag;
|
||||||
|
// Need to delay here: digital in IO_Base 4000UL, analogue in IO_AnalogueInputs 10000UL (fast) or 1000000UL(slow)
|
||||||
|
} else {
|
||||||
|
DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
|
||||||
|
_deviceState = DEVSTATE_FAILED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtain the correct analogue input value
|
// Obtain the correct analogue input value
|
||||||
@ -196,11 +205,16 @@ 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 (status != I2C_STATUS_OK) {
|
||||||
|
DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
|
||||||
|
_deviceState = DEVSTATE_FAILED;
|
||||||
|
} else {
|
||||||
if (_command1Buffer[0] != EXIORDY) {
|
if (_command1Buffer[0] != EXIORDY) {
|
||||||
DIAG(F("Vpin %d cannot be used as a digital output pin"), (int)vpin);
|
DIAG(F("Vpin %d cannot be used as a digital output pin"), (int)vpin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) override {
|
void _writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) override {
|
||||||
if (_deviceState == DEVSTATE_FAILED) return;
|
if (_deviceState == DEVSTATE_FAILED) return;
|
||||||
@ -216,11 +230,16 @@ 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 (status != I2C_STATUS_OK) {
|
||||||
|
DIAG(F("EX-IOExpander I2C:%s Error:%d %S"), _I2CAddress.toString(), status, I2CManager.getErrorMessage(status));
|
||||||
|
_deviceState = DEVSTATE_FAILED;
|
||||||
|
} else {
|
||||||
if (_command1Buffer[0] != EXIORDY) {
|
if (_command1Buffer[0] != EXIORDY) {
|
||||||
DIAG(F("Vpin %d cannot be used as a servo/PWM pin"), (int)vpin);
|
DIAG(F("Vpin %d cannot be used as a servo/PWM pin"), (int)vpin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _display() override {
|
void _display() override {
|
||||||
DIAG(F("EX-IOExpander I2C:%s v%d.%d.%d Vpins %d-%d %S"),
|
DIAG(F("EX-IOExpander I2C:%s v%d.%d.%d Vpins %d-%d %S"),
|
||||||
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user