1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-04-21 12:31:19 +02:00
CommandStation-EX/IO_RSproto.h
2024-12-22 05:20:25 -05:00

499 lines
16 KiB
C++

/*
* © 2024, Travis Farmer. All rights reserved.
* © 2021 Chris Harlow
*
* This file is part of DCC++EX API
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* RSproto
* =======
* To define a RSproto, example syntax:
* RSproto::create(busNo, serial, baud[, pin]);
*
* busNo = the Bus no of the instance. should = 0, unless more than one bus configured for some reason.
* serial = serial port to be used (e.g. Serial3)
* baud = baud rate (9600, 19200, 28800, 57600 or 115200)
* cycletime = minimum time between successive updates/reads of a node in millisecs (default 500ms)
* pin = pin number connected to RSproto module's DE and !RE terminals for half-duplex operation (default -1)
* if omitted (default), hardware MUST support full-duplex opperation!
*
*
* RSprotoNode
* ========
* To define a RSproto node and associate it with a RSproto bus,
* RSprotonode::create(firstVPIN, numVPINs, nodeID);
*
* firstVPIN = first vpin in block allocated to this device
* numVPINs = number of vpins
* nodeID = 1-251
*/
#ifndef IO_RS485_H
#define IO_RS485_H
#include "IODevice.h"
class RSproto;
class RSprotonode;
#ifndef COMMAND_BUFFER_SIZE
#define COMMAND_BUFFER_SIZE 900
#endif
/**********************************************************************
* taskBuffer class
*
* this stores the task list, and processes the data within it for
* sending. it also handles the incomming data responce.
* Data Frame:
* 0xFD : toNode : fromNode : ~data packet~ : 0xFE
* Start: TO : FROM : DATA : End
*
* Data frame must always start with the Start byte, follow with the
* destination (toNode), follow with the Source (fromNode), contain
* the data packet, and follow with the End byte.
*
*
* Data Packet:
* Command Byte : ~Command Params~
*
* Data Packet must always precede the parameters with the Command byte.
* this way the data is processed by the correct routine.
**********************************************************************/
/**********************************************************************
* RSprotonode class
*
* This encapsulates the state associated with a single RSproto node,
* which includes the nodeID, number of discrete inputs and coils, and
* the states of the discrete inputs and coils.
**********************************************************************/
class RSprotonode : public IODevice {
private:
uint8_t _busNo;
uint8_t _nodeID;
char _type;
RSprotonode *_next = NULL;
bool _initialised = false;
RSproto *bus;
HardwareSerial* _serial;
enum {
EXIOINIT = 0xE0, // Flag to initialise setup procedure
EXIORDY = 0xE1, // Flag we have completed setup procedure, also for EX-IO to ACK setup
EXIODPUP = 0xE2, // Flag we're sending digital pin pullup configuration
EXIOVER = 0xE3, // Flag to get version
EXIORDAN = 0xE4, // Flag to read an analogue input
EXIOWRD = 0xE5, // Flag for digital write
EXIORDD = 0xE6, // Flag to read digital input
EXIOENAN = 0xE7, // Flag to enable an analogue pin
EXIOINITA = 0xE8, // Flag we're receiving analogue pin mappings
EXIOPINS = 0xE9, // Flag we're receiving pin counts for buffers
EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM)
EXIOERR = 0xEF, // Flag we've received an error
};
static const int ARRAY_SIZE = 254;
public:
static RSprotonode *_nodeList;
enum ProfileType : int {
Instant = 0, // Moves immediately between positions (if duration not specified)
UseDuration = 0, // Use specified duration
Fast = 1, // Takes around 500ms end-to-end
Medium = 2, // 1 second end-to-end
Slow = 3, // 2 seconds end-to-end
Bounce = 4, // For semaphores/turnouts with a bit of bounce!!
NoPowerOff = 0x80, // Flag to be ORed in to suppress power off after move.
};
uint8_t _numDigitalPins = 0;
uint8_t getnumDigialPins() {
return _numDigitalPins;
}
void setnumDigitalPins(uint8_t value) {
_numDigitalPins = value;
}
uint8_t _numAnaloguePins = 0;
uint8_t getnumAnalogPins() {
return _numAnaloguePins;
}
void setnumAnalogPins(uint8_t value) {
_numAnaloguePins = value;
}
uint8_t _majorVer = 0;
uint8_t getMajVer() {
return _majorVer;
}
void setMajVer(uint8_t value) {
_majorVer = value;
}
uint8_t _minorVer = 0;
uint8_t getMinVer() {
return _minorVer;
}
void setMinVer(uint8_t value) {
_minorVer = value;
}
uint8_t _patchVer = 0;
uint8_t getPatVer() {
return _patchVer;
}
void setPatVer(uint8_t value) {
_patchVer = value;
}
uint8_t* _digitalInputStates = NULL;
uint8_t getdigitalInputStates(int index) {
return _digitalInputStates[index];
}
void setdigitalInputStates(uint8_t value, int index) {
_digitalInputStates[index] = value;
}
bool cleandigitalPinStates(int size) {
if (_digitalPinBytes > 0) free(_digitalInputStates);
if ((_digitalInputStates = (byte*) calloc(size, 1)) != NULL) {
return true;
} else return false;
}
uint8_t* _analogueInputStates = NULL;
uint8_t getanalogInputStates(int index) {
return _analogueInputStates[index];
}
void setanalogInputStates(uint8_t value, int index) {
_analogueInputStates[index] = value;
}
uint8_t* _analogueInputBuffer = NULL; // buffer for I2C input transfers
uint8_t getanalogInpuBuffer(int index) {
return _analogueInputBuffer[index];
}
void setanalogInputBuffer(uint8_t value, int index) {
_analogueInputBuffer[index] = value;
memcpy(_analogueInputStates, _analogueInputBuffer, _analoguePinBytes);
}
uint8_t _readCommandBuffer[4]; // unused?
uint8_t _digitalPinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
uint8_t getdigitalPinBytes() {
return _digitalPinBytes;
}
void setdigitalPinBytes(uint8_t value) {
_digitalPinBytes = value;
}
uint8_t _analoguePinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
uint8_t getanalogPinBytes() {
return _analoguePinBytes;
}
void setanalogPinBytes(uint8_t value) {
_analoguePinBytes = value;
}
uint8_t* _analoguePinMap = NULL;
uint8_t getanalogPinMap(int index) {
return _analoguePinMap[index];
}
void setanalogPinMap(uint8_t value, int index) {
_analoguePinMap[index] = value;
}
bool cleanAnalogStates(int size) {
if (_analoguePinBytes > 0) {
free(_analogueInputBuffer);
free(_analogueInputStates);
free(_analoguePinMap);
}
_analogueInputStates = (uint8_t*) calloc(size, 1);
_analogueInputBuffer = (uint8_t*) calloc(size, 1);
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
if (_analogueInputStates != NULL && _analogueInputBuffer != NULL && _analoguePinMap != NULL) return true;
else return false;
}
int resFlag[255];
bool _initalized;
static void create(VPIN firstVpin, int nPins, uint8_t nodeID) {
if (checkNoOverlap(firstVpin, nPins)) new RSprotonode(firstVpin, nPins, nodeID);
}
RSprotonode(VPIN firstVpin, int nPins, uint8_t nodeID);
uint8_t getNodeID() {
return _nodeID;
}
RSprotonode *getNext() {
return _next;
}
void setNext(RSprotonode *node) {
_next = node;
}
bool isInitialised() {
return _initialised;
}
void setInitialised() {
_initialised = true;
}
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override;
int _configureAnalogIn(VPIN vpin) override;
void _begin() override;
int _read(VPIN vpin) override;
void _write(VPIN vpin, int value) override;
int _readAnalogue(VPIN vpin) override;
void _writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) override;
uint8_t getBusNumber() {
return _busNo;
}
void _display() override {
DIAG(F("EX-IOExpander485 node:%d Vpins %u-%u %S"), _nodeID, (int)_firstVpin, (int)_firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
}
};
/**********************************************************************
* RSproto class
*
* This encapsulates the properties state of the bus and the
* transmission and reception of data across that bus. Each RSproto
* object owns a set of RSprotonode objects which represent the nodes
* attached to that bus.
**********************************************************************/
class RSproto : public IODevice {
private:
// Here we define the device-specific variables.
uint8_t _busNo;
unsigned long _cycleStartTime = 0;
unsigned long _timeoutStart = 0;
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)
unsigned long _byteTransmitTime; // time in us for transmission of one byte
int _operationCount = 0;
int _refreshOperation = 0;
byte bufferLength;
static const int ARRAY_SIZE = 254;
int buffer[ARRAY_SIZE];
byte inCommandPayload;
static RSproto *_busList; // linked list of defined bus instances
bool waitReceive = false;
int _waitCounter = 0;
int _waitCounterB = 0;
int _waitA;
unsigned long _charTimeout;
unsigned long _frameTimeout;
enum {RDS_IDLE, RDS_DIGITAL, RDS_ANALOGUE}; // Read operation states
uint8_t _readState = RDS_IDLE;
unsigned long _lastDigitalRead = 0;
unsigned long _lastAnalogueRead = 0;
const unsigned long _digitalRefresh = 10000UL; // Delay refreshing digital inputs for 10ms
const unsigned long _analogueRefresh = 50000UL; // Delay refreshing analogue inputs for 50ms
RSprotonode *_nodeListStart = NULL, *_nodeListEnd = NULL;
RSprotonode *_currentNode = NULL;
uint16_t _receiveDataIndex = 0; // Index of next data byte to be received.
RSproto *_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 {
static const int ARRAY_SIZE = 254;
int taskID;
uint8_t commandArray[ARRAY_SIZE];
int byteCount;
uint8_t retFlag;
bool gotCallback;
bool rxMode;
int crcPassFail;
bool completed = false;
};
int taskIDCntr = 0;
Task taskBuffer[100]; // Buffer to hold up to 100 tasks
int taskCount = 0;
void addTask(int id, const uint8_t* cmd, int byteCount, uint8_t retFlag, bool gotCallBack=false, bool rxMode=false, int crcPassFail=0) {
if (taskCount < 100) { // Check if buffer is not full
taskBuffer[taskCount].taskID = id;
memcpy(taskBuffer[taskCount].commandArray, cmd, ARRAY_SIZE);
taskBuffer[taskCount].commandArray[ARRAY_SIZE] = 0; // Ensure null-termination
taskBuffer[taskCount].byteCount = byteCount;
taskBuffer[taskCount].retFlag = retFlag;
taskBuffer[taskCount].gotCallback = false;
taskBuffer[taskCount].rxMode = false;
taskBuffer[taskCount].crcPassFail = 0;
taskBuffer[taskCount].completed = false;
taskCount++;
} else {
Serial.println("Task buffer overflow!");
}
}
Task getNextTask() {
for (int i = 0; i < taskCount; i++) {
if (!taskBuffer[i].completed) {
return taskBuffer[i];
}
}
// Return a default task if no uncompleted tasks found
Task emptyTask;
return emptyTask;
}
void markTaskCompleted(int taskID) {
for (int i = 0; i < taskCount; i++) {
if (taskBuffer[i].taskID == taskID) {
taskBuffer[i].completed = true;
break;
}
}
}
bool flagEnd = false;
bool flagEnded = false;
bool flagStart = false;
bool flagStarted = false;
bool rxStart = false;
bool rxEnd = false;
bool crcPass = false;
bool flagProc = false;
uint8_t received_data[ARRAY_SIZE];
uint16_t received_crc;
uint8_t crc[2];
uint16_t crc16(uint8_t *data, uint16_t length);
void remove_nulls(char *str, int len);
int getCharsLeft(char *str, char position);
void parseRx(uint8_t * outArray, uint8_t retFlag);
// EX-IOExpander protocol flags
enum {
EXIOINIT = 0xE0, // Flag to initialise setup procedure
EXIORDY = 0xE1, // Flag we have completed setup procedure, also for EX-IO to ACK setup
EXIODPUP = 0xE2, // Flag we're sending digital pin pullup configuration
EXIOVER = 0xE3, // Flag to get version
EXIORDAN = 0xE4, // Flag to read an analogue input
EXIOWRD = 0xE5, // Flag for digital write
EXIORDD = 0xE6, // Flag to read digital input
EXIOENAN = 0xE7, // Flag to enable an analogue pin
EXIOINITA = 0xE8, // Flag we're receiving analogue pin mappings
EXIOPINS = 0xE9, // Flag we're receiving pin counts for buffers
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 RSproto(busNo, serial, baud, txPin, cycleTime);
}
HardwareSerial* _serial;
int _CommMode = 0;
int _opperation = 0;
uint16_t _pullup;
uint16_t _pin;
int8_t _txPin;
bool _busy = false;
unsigned long _baud;
int taskCnt = 0;
uint8_t initBuffer[1] = {0xFE};
unsigned long taskCounter=0ul;
// Device-specific initialisation
void _begin() override {
_serial->begin(_baud, SERIAL_8N1);
if (_txPin >0) {
pinMode(_txPin, OUTPUT);
digitalWrite(_txPin, LOW);
}
#if defined(RSproto_STM_OK)
pinMode(RSproto_STM_OK, OUTPUT);
ArduinoPins::fastWriteDigital(RSproto_STM_OK,LOW);
#endif
#if defined(RSproto_STM_FAIL)
pinMode(RSproto_STM_FAIL, OUTPUT);
ArduinoPins::fastWriteDigital(RSproto_STM_FAIL,LOW);
#endif
#if defined(RSproto_STM_COMM)
pinMode(RSproto_STM_COMM, OUTPUT);
ArduinoPins::fastWriteDigital(RSproto_STM_COMM,LOW);
#endif
#if defined(DIAG_IO)
_display();
#endif
}
// Loop function (overriding IODevice::_loop(unsigned long))
void _loop(unsigned long currentMicros) override;
// Display information about the device
void _display() override {
DIAG(F("EX-IOExpander485 Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1,
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("OK"));
}
// Locate RSprotonode object with specified nodeID.
RSprotonode *findNode(uint8_t nodeID) {
for (RSprotonode *node = _nodeListStart; node != NULL; node = node->getNext()) {
if (node->getNodeID() == nodeID)
return node;
}
return NULL;
}
bool nodesInitialized() {
bool retval = true;
for (RSprotonode *node = _nodeListStart; node != NULL; node = node->getNext()) {
if (node->_initalized == false)
retval = false;
}
return retval;
}
// Add new RSprotonode to the list of nodes for this bus.
void addNode(RSprotonode *newNode) {
if (!_nodeListStart)
_nodeListStart = newNode;
if (!_nodeListEnd)
_nodeListEnd = newNode;
else
_nodeListEnd->setNext(newNode);
//DIAG(F("RSproto: 260h nodeID:%d _nodeListStart:%d _nodeListEnd:%d"), newNode, _nodeListStart, _nodeListEnd);
}
protected:
RSproto(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime);
public:
uint8_t getBusNumber() {
return _busNo;
}
RSproto *getNext() {
return _nextBus;
}
static RSproto *findBus(uint8_t busNo) {
for (RSproto *bus = _busList; bus != NULL; bus = bus->getNext()) {
if (bus->getBusNumber() == busNo)
return bus;
}
return NULL;
}
};
#endif // IO_RSproto_H