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

save current, works for now

This commit is contained in:
travis-farmer 2024-12-29 06:02:06 -05:00
parent a6fcad2ecf
commit 764380ae57
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 26 additions and 10 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, int cycleTime) {
EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin) {
_serial = &serial;
_baud = baud;
_txPin = txPin;
_busNo = busNo;
_cycleTime = cycleTime * 1000UL;
_retryTime = 1000000UL; // 1 second
bufferLength=0;
inCommandPayload=PAYLOAD_FALSE;
// Add device to HAL device chain
@ -89,10 +89,21 @@ 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;
@ -167,6 +178,7 @@ void EXIO485::_loop(unsigned long currentMicros) {
int nodeTo = received_data[0];
if (nodeTo == 0) { // for master.
flagProc = true;
}
}
@ -258,6 +270,7 @@ 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 _cycleTime; // target time between successive read/write cycles, microseconds
unsigned long _retryTime; // 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,12 +418,13 @@ struct Task {
int crcPassFail;
bool completed;
bool processed;
unsigned long currentRetryTimer;
};
static const int MAX_TASKS = 1000;
static const int MAX_TASKS = 100; // we don't want to run out of task slots, but memory?
long taskIDCntr = 1;
long CurrentTaskID = -1;
int taskResendCount = 0;
Task taskBuffer[MAX_TASKS]; // Buffer to hold up to 100 tasks
Task taskBuffer[MAX_TASKS];
int currentTaskIndex = 0;
void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) {
@ -448,6 +449,7 @@ 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;
@ -485,6 +487,7 @@ 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;
}
@ -520,8 +523,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, int cycleTime=500) {
new EXIO485(busNo, serial, baud, txPin, cycleTime);
static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1) {
new EXIO485(busNo, serial, baud, txPin);
}
HardwareSerial* _serial;
int _CommMode = 0;
@ -599,7 +602,7 @@ struct Task {
}
protected:
EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime);
EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin);
public: