1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-04-21 20:41:19 +02:00

Compare commits

..

1 Commits

2 changed files with 10 additions and 26 deletions

View File

@ -30,13 +30,13 @@ static const byte PAYLOAD_STRING = 2;
************************************************************/
// Constructor for EXIO485
EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin) {
EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime) {
_serial = &serial;
_baud = baud;
_txPin = txPin;
_busNo = busNo;
_retryTime = 1000000UL; // 1 second
_cycleTime = cycleTime * 1000UL;
bufferLength=0;
inCommandPayload=PAYLOAD_FALSE;
// Add device to HAL device chain
@ -89,21 +89,10 @@ void EXIO485::_loop(unsigned long currentMicros) {
}
if ( hasTasks()){ // do we have any tasks on the docket
_cycleStartTimeA = _currentMicros;
if (CurrentTaskID == -1) CurrentTaskID = getNextTaskId();
Task* currentTask = getTaskById(CurrentTaskID);
if (currentTask == nullptr) return; // dead task
if (!currentTask->completed && _currentMicros - _cycleStartTimeA >= _retryTime) { // after CRC pass, timer is reset
if (currentTask->currentRetryTimer == 0UL) { // first trigger
currentTask->currentRetryTimer = _currentMicros; // set timer
} else if (_currentMicros - currentTask->currentRetryTimer >= _retryTime) {
currentTask->currentRetryTimer = _currentMicros; // reset timer
currentTask->rxMode = false; // resend data
DIAG(F("EX-IOExplorer485: Fail RX, Retry TX. Task: %d"), CurrentTaskID);
}
}
if (!currentTask->rxMode) {
currentTask->crcPassFail = 0;
@ -178,7 +167,6 @@ void EXIO485::_loop(unsigned long currentMicros) {
int nodeTo = received_data[0];
if (nodeTo == 0) { // for master.
flagProc = true;
}
}
@ -270,7 +258,6 @@ void EXIO485::_loop(unsigned long currentMicros) {
break;
}
}
_cycleStartTimeA = currentMicros; // reset timer so we do not resend data
}
}
}

View File

@ -371,7 +371,7 @@ private:
unsigned long _cycleStartTime = 0;
unsigned long _cycleStartTimeA = 0;
unsigned long _timeoutStart = 0;
unsigned long _retryTime; // target time between successive read/write cycles, microseconds
unsigned long _cycleTime; // target time between successive read/write cycles, microseconds
unsigned long _timeoutPeriod; // timeout on read responses, in microseconds.
unsigned long _currentMicros; // last value of micros() from _loop function.
unsigned long _postDelay; // delay time after transmission before switching off transmitter (in us)
@ -418,13 +418,12 @@ struct Task {
int crcPassFail;
bool completed;
bool processed;
unsigned long currentRetryTimer;
};
static const int MAX_TASKS = 100; // we don't want to run out of task slots, but memory?
static const int MAX_TASKS = 1000;
long taskIDCntr = 1;
long CurrentTaskID = -1;
int taskResendCount = 0;
Task taskBuffer[MAX_TASKS];
Task taskBuffer[MAX_TASKS]; // Buffer to hold up to 100 tasks
int currentTaskIndex = 0;
void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) {
@ -449,7 +448,6 @@ struct Task {
taskBuffer[emptySlot].gotCallback = false;
taskBuffer[emptySlot].completed = false;
taskBuffer[emptySlot].processed = false;
taskBuffer[emptySlot].currentRetryTimer = 0UL;
taskIDCntr++;
if (taskIDCntr >= 5000000) taskIDCntr = 1;
taskBuffer[emptySlot].taskID = taskIDCntr;
@ -487,7 +485,6 @@ struct Task {
if (taskBuffer[i].taskID == id) {
taskBuffer[i].completed = true; // completed
taskBuffer[i].taskID = -1; // unassigned
taskBuffer[i].currentRetryTimer = 0UL; // stop timer
CurrentTaskID = getNextTaskId();
break;
}
@ -523,8 +520,8 @@ struct Task {
EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM)
EXIOERR = 0xEF, // Flag we've received an error
};
static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1) {
new EXIO485(busNo, serial, baud, txPin);
static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1, int cycleTime=500) {
new EXIO485(busNo, serial, baud, txPin, cycleTime);
}
HardwareSerial* _serial;
int _CommMode = 0;
@ -602,7 +599,7 @@ struct Task {
}
protected:
EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin);
EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime);
public: