diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index e6bd814..835dd32 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -1035,18 +1035,27 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[]) case 0: // list turntable objects return Turntable::printAll(stream); - case 1: // delete turntable - if (!Turntable::remove(p[0])) - return false; - StringFormatter::send(stream, F("\n")); - return true; + case 1: // nothing here for the moment at least + return false; case 2: // - rotate to position for DCC turntables DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]); return true; case 3: // 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; 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) { 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; + 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) { DIAG(F("Create DCC turntable %d at base address %d with %d positions"), p[0], p[2], params - 2); } else { diff --git a/Turntables.cpp b/Turntables.cpp index 2f2a05e..1ab5a70 100644 --- a/Turntables.cpp +++ b/Turntables.cpp @@ -62,22 +62,23 @@ Turntable *Turntable::get(uint16_t id) { return NULL; } -// Remove specified turntable from list and delete it -bool Turntable::remove(uint16_t id) { - Turntable *tto, *pp=NULL; +// Add a position +void Turntable::addPosition(uint16_t value) { + _turntablePositions.insert(value); +} - for (tto=_firstTurntable; tto!=NULL && tto->_turntableData.id!=id; pp=tto, tto=tto->_nextTurntable) {} - if (tto == NULL) return false; - if (tto == _firstTurntable) { - _firstTurntable = tto->_nextTurntable; - } else { - pp->_nextTurntable = tto->_nextTurntable; +// Get value for position +uint16_t Turntable::getPositionValue(size_t position) { + TurntablePosition* currentPosition = _turntablePositions.getHead(); + for (size_t i = 0; i < position && currentPosition; i++) { + currentPosition = currentPosition->next; } - delete (EXTTTurntable *)tto; - - turntablelistHash++; - return true; + if (currentPosition) { + return currentPosition->data; + } else { + return false; + } } /* @@ -142,8 +143,6 @@ EXTTTurntable::EXTTTurntable(uint16_t id, uint8_t i2caddress, VPIN vpin) : extt->_exttTurntableData.i2caddress = i2caddress; extt->_exttTurntableData.vpin = vpin; return tto; - } else { - remove(id); } } tto = (Turntable *)new EXTTTurntable(id, i2caddress, vpin); diff --git a/Turntables.h b/Turntables.h index c7bb394..3708d2a 100644 --- a/Turntables.h +++ b/Turntables.h @@ -33,6 +33,41 @@ enum { // 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. * @@ -57,15 +92,12 @@ protected: uint16_t id; } _turntableData; - // Linked list to store either positions (EXTT) or DCC addresses (DCC) - struct Position { - int16_t position; - Position *next; - }; - // Pointer to next turntable object Turntable *_nextTurntable = 0; + // Linked list for positions + TurntablePositionList _turntablePositions; + /* * Constructor */ @@ -73,6 +105,7 @@ protected: _turntableData.id = id; _turntableData.turntableType = turntableType; _turntableData.hidden = false; + _turntableData.position = 0; add(this); } @@ -110,7 +143,8 @@ public: inline uint16_t getId() { return _turntableData.id; } inline Turntable *next() { return _nextTurntable; } void printState(Print *stream); - + void addPosition(uint16_t value); + uint16_t getPositionValue(size_t position); /* * Virtual functions @@ -125,7 +159,6 @@ public: * Public static functions */ 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 setPosition(uint16_t id, uint8_t position, uint8_t activity=0); static bool setPositionStateOnly(uint16_t id, uint8_t position);