1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-25 05:31:24 +01:00

Some progress

This commit is contained in:
peteGSX 2023-08-26 10:26:01 +10:00
parent 57d4655d54
commit b823a647ac
3 changed files with 75 additions and 29 deletions

View File

@ -1035,18 +1035,27 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
case 0: // <I> list turntable objects case 0: // <I> list turntable objects
return Turntable::printAll(stream); return Turntable::printAll(stream);
case 1: // <I id> delete turntable case 1: // <I id> nothing here for the moment at least
if (!Turntable::remove(p[0])) return false;
return false;
StringFormatter::send(stream, F("<O>\n"));
return true;
case 2: // <I id position> - rotate to position for DCC turntables case 2: // <I id position> - rotate to position for DCC turntables
DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]); DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]);
return true; return true;
case 3: // <I id position activity> rotate to position for EX-Turntable case 3: // <I id position activity> rotate to position for EX-Turntable
DIAG(F("Rotate EXTT turntable %d to angle %d with activity %d"), p[0], p[1], p[2]); {
Turntable *tto = Turntable::get(p[0]);
if (tto) {
uint16_t value = tto->getPositionValue(p[1]);
if (value) {
DIAG(F("Position %d value is %d"), p[1], value);
} else {
return false;
}
} else {
return false;
}
}
return true; return true;
default: // If we're here, it must be creating a turntable object default: // If we're here, it must be creating a turntable object
@ -1054,6 +1063,11 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
if (params > 5 && params < 41 && p[1] == HASH_KEYWORD_EXTT) { if (params > 5 && params < 41 && p[1] == HASH_KEYWORD_EXTT) {
DIAG(F("Create EXTT turntable %d on vpin %d and address %d with %d positions"), p[0], p[2], p[3], params - 4); DIAG(F("Create EXTT turntable %d on vpin %d and address %d with %d positions"), p[0], p[2], p[3], params - 4);
if (!EXTTTurntable::create(p[0], (uint8_t)p[1], (VPIN)p[2])) return false; if (!EXTTTurntable::create(p[0], (uint8_t)p[1], (VPIN)p[2])) return false;
Turntable *tto = Turntable::get(p[0]);
for ( uint8_t i = 0; i < params - 4; i++) {
tto->addPosition(p[i + 3]);
DIAG(F("Add position %d"), p[i + 3]);
}
} else if (params > 3 && params < 39 && p[1] == HASH_KEYWORD_DCC) { } else if (params > 3 && params < 39 && p[1] == HASH_KEYWORD_DCC) {
DIAG(F("Create DCC turntable %d at base address %d with %d positions"), p[0], p[2], params - 2); DIAG(F("Create DCC turntable %d at base address %d with %d positions"), p[0], p[2], params - 2);
} else { } else {

View File

@ -62,22 +62,23 @@ Turntable *Turntable::get(uint16_t id) {
return NULL; return NULL;
} }
// Remove specified turntable from list and delete it // Add a position
bool Turntable::remove(uint16_t id) { void Turntable::addPosition(uint16_t value) {
Turntable *tto, *pp=NULL; _turntablePositions.insert(value);
}
for (tto=_firstTurntable; tto!=NULL && tto->_turntableData.id!=id; pp=tto, tto=tto->_nextTurntable) {} // Get value for position
if (tto == NULL) return false; uint16_t Turntable::getPositionValue(size_t position) {
if (tto == _firstTurntable) { TurntablePosition* currentPosition = _turntablePositions.getHead();
_firstTurntable = tto->_nextTurntable; for (size_t i = 0; i < position && currentPosition; i++) {
} else { currentPosition = currentPosition->next;
pp->_nextTurntable = tto->_nextTurntable;
} }
delete (EXTTTurntable *)tto; if (currentPosition) {
return currentPosition->data;
turntablelistHash++; } else {
return true; return false;
}
} }
/* /*
@ -142,8 +143,6 @@ EXTTTurntable::EXTTTurntable(uint16_t id, uint8_t i2caddress, VPIN vpin) :
extt->_exttTurntableData.i2caddress = i2caddress; extt->_exttTurntableData.i2caddress = i2caddress;
extt->_exttTurntableData.vpin = vpin; extt->_exttTurntableData.vpin = vpin;
return tto; return tto;
} else {
remove(id);
} }
} }
tto = (Turntable *)new EXTTTurntable(id, i2caddress, vpin); tto = (Turntable *)new EXTTTurntable(id, i2caddress, vpin);

View File

@ -33,6 +33,41 @@ enum {
// TURNTABLE_DCC = 2, // TURNTABLE_DCC = 2,
}; };
/*************************************************************************************
* Turntable positions.
*
*************************************************************************************/
struct TurntablePosition {
uint16_t data;
TurntablePosition* next;
TurntablePosition(uint16_t value) : data(value), next(nullptr) {}
};
class TurntablePositionList {
public:
TurntablePositionList() : head(nullptr) {}
void insert(uint16_t value) {
TurntablePosition* newPosition = new TurntablePosition(value);
if(!head) {
head = newPosition;
} else {
newPosition->next = head;
head = newPosition;
}
}
TurntablePosition* getHead() {
return head;
}
private:
TurntablePosition* head;
};
/************************************************************************************* /*************************************************************************************
* Turntable - Base class for turntables. * Turntable - Base class for turntables.
* *
@ -57,15 +92,12 @@ protected:
uint16_t id; uint16_t id;
} _turntableData; } _turntableData;
// Linked list to store either positions (EXTT) or DCC addresses (DCC)
struct Position {
int16_t position;
Position *next;
};
// Pointer to next turntable object // Pointer to next turntable object
Turntable *_nextTurntable = 0; Turntable *_nextTurntable = 0;
// Linked list for positions
TurntablePositionList _turntablePositions;
/* /*
* Constructor * Constructor
*/ */
@ -73,6 +105,7 @@ protected:
_turntableData.id = id; _turntableData.id = id;
_turntableData.turntableType = turntableType; _turntableData.turntableType = turntableType;
_turntableData.hidden = false; _turntableData.hidden = false;
_turntableData.position = 0;
add(this); add(this);
} }
@ -110,7 +143,8 @@ public:
inline uint16_t getId() { return _turntableData.id; } inline uint16_t getId() { return _turntableData.id; }
inline Turntable *next() { return _nextTurntable; } inline Turntable *next() { return _nextTurntable; }
void printState(Print *stream); void printState(Print *stream);
void addPosition(uint16_t value);
uint16_t getPositionValue(size_t position);
/* /*
* Virtual functions * Virtual functions
@ -125,7 +159,6 @@ public:
* Public static functions * Public static functions
*/ */
inline static bool exists(uint16_t id) { return get(id) != 0; } inline static bool exists(uint16_t id) { return get(id) != 0; }
static bool remove(uint16_t id);
static bool isPosition(uint16_t id, uint8_t position); static bool isPosition(uint16_t id, uint8_t position);
static bool setPosition(uint16_t id, uint8_t position, uint8_t activity=0); static bool setPosition(uint16_t id, uint8_t position, uint8_t activity=0);
static bool setPositionStateOnly(uint16_t id, uint8_t position); static bool setPositionStateOnly(uint16_t id, uint8_t position);