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

code cleaned

This commit is contained in:
travis-farmer 2024-12-28 03:00:23 -05:00
parent 495c9407a8
commit 23eb35290f
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 101 additions and 146 deletions

View File

@ -1,6 +1,5 @@
/*
* © 2024, Travis Farmer. All rights reserved.
* © 2021 Chris Harlow
*
* This file is part of DCC++EX API
*
@ -48,14 +47,7 @@ EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8
_busList = this;
}
/* -= _loop =-
//
// Main loop function for EXIO485.
// Work through list of nodes. For each node, in separate loop entries
// When the slot time has finished, move on to the next device.
*/
// CRC-16 implementation (replace with your preferred CRC library if needed)
// CRC-16 implementation
uint16_t EXIO485::crc16(uint8_t *data, uint16_t length) {
uint16_t crc = 0xFFFF;
for (uint16_t i = 0; i < length; i++) {
@ -71,10 +63,18 @@ uint16_t EXIO485::crc16(uint8_t *data, uint16_t length) {
return crc;
}
/* -= _loop =-
//
// Main loop function for EXIO485.
// Work through list of nodes. For each node, in separate loop entries
// When the slot time has finished, move on to the next device.
*/
void EXIO485::_loop(unsigned long currentMicros) {
_currentMicros = currentMicros;
if (_currentNode == NULL) _currentNode = _nodeListStart;
if (!hasTasks() && _currentNode->isInitialised()) {
if (!hasTasks() && _currentNode->isInitialised()) { // no tasks? lets poll for data
uint8_t buffA[3];
buffA[0] = (_currentNode->getNodeID());
buffA[1] = (0);
@ -86,24 +86,19 @@ void EXIO485::_loop(unsigned long currentMicros) {
buffB[2] = (EXIORDAN);
addTask(buffB, 3, EXIORDAN);
_currentNode = _currentNode->getNext();
//DIAG(F("Polling"));
}
if ( hasTasks()){
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;
if (!currentTask->rxMode) { // Check if a task was found
if (!currentTask->rxMode) {
currentTask->crcPassFail = 0;
uint16_t response_crc = crc16((uint8_t*)currentTask->commandArray, currentTask->byteCount-1);
//delayUntil(_currentMicros+10000UL);
ArduinoPins::fastWriteDigital(_txPin,HIGH);
if (_txPin != -1) ArduinoPins::fastWriteDigital(_txPin,HIGH);
// Send response data with CRC
_serial->write(0xFE);
_serial->write(0xFE);
@ -116,10 +111,9 @@ void EXIO485::_loop(unsigned long currentMicros) {
_serial->write(0xFD);
_serial->write(0xFD);
_serial->flush();
ArduinoPins::fastWriteDigital(_txPin,LOW);
if (_txPin != -1) ArduinoPins::fastWriteDigital(_txPin,LOW);
// delete task command after sending, for now
currentTask->rxMode = true;
//DIAG(F("Task %d"), currentTask->taskID);
} else {
if ( _serial->available()) {
@ -165,18 +159,15 @@ void EXIO485::_loop(unsigned long currentMicros) {
crcPass = true;
}
flagEnded = false;
}
}
// Check CRC validity
if (crcPass) {
// Data received successfully, process it (e.g., print)
int nodeTo = received_data[0];
if (nodeTo == 0) { // for master. master does not retransmit, or a loop will runaway.
if (nodeTo == 0) { // for master.
flagProc = true;
}
}
if (flagProc) {
@ -267,11 +258,7 @@ void EXIO485::_loop(unsigned long currentMicros) {
break;
}
}
}
}
}
}

View File

@ -1,6 +1,5 @@
/*
* © 2024, Travis Farmer. All rights reserved.
* © 2021 Chris Harlow
*
* This file is part of DCC++EX API
*
@ -39,7 +38,7 @@
*
* firstVPIN = first vpin in block allocated to this device
* numVPINs = number of vpins
* nodeID = 1-251
* nodeID = 1-252
*/
#ifndef IO_EXIO485_H
@ -406,12 +405,6 @@ private:
uint16_t _receiveDataIndex = 0; // Index of next data byte to be received.
EXIO485 *_nextBus = NULL; // Pointer to next bus instance in list.
// Helper function for error handling
void reportError(uint8_t status, bool fail=true) {
DIAG(F("EX-IOExpander485 Node:%d Error"), _currentNode->getNodeID());
if (fail)
_deviceState = DEVSTATE_FAILED;
}
int byteCounter = 0;
public:
struct Task {
@ -432,19 +425,7 @@ static const int MAX_TASKS = 50;
int taskResendCount = 0;
Task taskBuffer[MAX_TASKS]; // Buffer to hold up to 100 tasks
int currentTaskIndex = 0;
void initTask() {
for (int i = 0; i <MAX_TASKS; i++) {
taskBuffer[i].completed = true;
taskBuffer[i].processed = false;
taskBuffer[i].byteCount = 0;
taskBuffer[i].crcPassFail = 0;
taskBuffer[i].gotCallback = false;
taskBuffer[i].retFlag = 0;
taskBuffer[i].rxMode = false;
taskBuffer[i].taskID = i;
memset(taskBuffer[i].commandArray,0,ARRAY_SIZE);
}
}
void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) {
// Find an empty slot in the buffer
int emptySlot = -1;
@ -567,25 +548,12 @@ uint16_t crc16(uint8_t *data, uint16_t length);
unsigned long taskCounter=0ul;
// Device-specific initialisation
void _begin() override {
//initTask();
_serial->begin(_baud, SERIAL_8N1);
if (_txPin >0) {
pinMode(_txPin, OUTPUT);
digitalWrite(_txPin, LOW);
}
#if defined(EXIO485_STM_OK)
pinMode(EXIO485_STM_OK, OUTPUT);
ArduinoPins::fastWriteDigital(EXIO485_STM_OK,LOW);
#endif
#if defined(EXIO485_STM_FAIL)
pinMode(EXIO485_STM_FAIL, OUTPUT);
ArduinoPins::fastWriteDigital(EXIO485_STM_FAIL,LOW);
#endif
#if defined(EXIO485_STM_COMM)
pinMode(EXIO485_STM_COMM, OUTPUT);
ArduinoPins::fastWriteDigital(EXIO485_STM_COMM,LOW);
#endif
#if defined(DIAG_IO)
_display();