From 232ac993ecf58451457d6b959cd0845616ff18b0 Mon Sep 17 00:00:00 2001 From: peteGSX <97784652+peteGSX@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:45:11 +1000 Subject: [PATCH] Separate add from create --- DCCEXParser.cpp | 65 +++++++++++++++++++++++++++++++------------------ Turntables.cpp | 14 +++++------ Turntables.h | 18 ++++++++------ 3 files changed, 57 insertions(+), 40 deletions(-) diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index a37e5d0..24a7914 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -98,6 +98,7 @@ const int16_t HASH_KEYWORD_WIFI = -5583; const int16_t HASH_KEYWORD_ETHERNET = -30767; const int16_t HASH_KEYWORD_WIT = 31594; const int16_t HASH_KEYWORD_EXTT = 8573; +const int16_t HASH_KEYWORD_ADD = 3201; int16_t DCCEXParser::stashP[MAX_COMMAND_PARAMS]; bool DCCEXParser::stashBusy; @@ -714,7 +715,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) // todesc = RMFT2::getTurntableDescription(id); #endif if (todesc == NULL) todesc = F(""); - StringFormatter::send(stream, F(" %d %d %d %d"), id, type, pos, posCount); + StringFormatter::send(stream, F(" %d %d %d"), id, type, pos); for (uint8_t p = 0; p < posCount; p++) { int16_t value = tto->getPositionValue(p); StringFormatter::send(stream, F(" %d"), value); @@ -1059,10 +1060,12 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[]) // ========================== // Turntable - no support if no HAL // - list all +// - broadcast type and current position +// - create DCC - This is TBA // - operate (DCC) // - operate (EXTT) -// - create EXTT -// - create DCC? - This TBA +// - add position +// - create EXTT #ifndef IO_NO_HAL bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[]) { @@ -1071,47 +1074,58 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[]) case 0: // list turntable objects return Turntable::printAll(stream); - case 1: // nothing here for the moment at least + case 1: // broadcast type and current position return false; case 2: // - rotate to position for DCC turntables { - Turntable *tto = Turntable::get(p[0]); - if (tto) { - if (tto->getPosition() == p[1]) return true; - uint16_t value = tto->getPositionValue(p[1]); - if (value) { - DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]); + if (p[1] == HASH_KEYWORD_DCC) { // Create a DCC turntable + DIAG(F("Create DCC turntable %d"), p[0]); + } else { // Otherwise move a DCC turntable + Turntable *tto = Turntable::get(p[0]); + if (tto) { + if (tto->getPosition() == p[1]) return true; + uint16_t value = tto->getPositionValue(p[1]); + if (value) { + DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]); + } else { + return false; + } } else { return false; } - } else { - return false; } } return true; - case 3: // rotate to position for EX-Turntable + case 3: { - Turntable *tto = Turntable::get(p[0]); - if (tto) { - if (!tto->setPosition(p[0], p[1], p[2])) return false; - } else { - return false; + if (p[1] == HASH_KEYWORD_ADD) { // add position value to turntable + Turntable *tto = Turntable::get(p[0]); + if (tto) { + tto->addPosition(p[2]); + StringFormatter::send(stream, F("\n")); + } else { + return false; + } + } else { // rotate to position for EX-Turntable + Turntable *tto = Turntable::get(p[0]); + if (tto) { + if (!tto->setPosition(p[0], p[1], p[2])) return false; + } else { + return false; + } } } return true; - default: // If we're here, it must be creating a turntable object + case 5: // create an EXTT turntable { - if (params > 5 && params < 41 && p[1] == HASH_KEYWORD_EXTT) { + if (p[1] == HASH_KEYWORD_EXTT) { if (Turntable::get(p[0])) return false; if (!EXTTTurntable::create(p[0], (VPIN)p[2], (uint8_t)p[3])) return false; Turntable *tto = Turntable::get(p[0]); - for (uint8_t i = params - 5; i > 0; i--) { - tto->addPosition(p[i + 4]); - } - tto->addPosition(p[4]); // Allow setting a value for the home angle for throttles to draw it + tto->addPosition(p[4]); } 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 { @@ -1119,6 +1133,9 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[]) } } return true; + + default: // Anything else is invalid + return false; } } #endif diff --git a/Turntables.cpp b/Turntables.cpp index 6b4260f..a7d42ab 100644 --- a/Turntables.cpp +++ b/Turntables.cpp @@ -70,17 +70,15 @@ void Turntable::addPosition(uint16_t value) { } // Get value for position -uint16_t Turntable::getPositionValue(size_t position) { +uint16_t Turntable::getPositionValue(uint8_t position) { TurntablePosition* currentPosition = _turntablePositions.getHead(); - for (size_t i = 0; i < position && currentPosition; i++) { + while (currentPosition) { + if (currentPosition->index == position) { + return currentPosition->data; + } currentPosition = currentPosition->next; } - - if (currentPosition) { - return currentPosition->data; - } else { - return false; - } + return false; } // Get the count of positions associated with the turntable diff --git a/Turntables.h b/Turntables.h index 67017df..36f5b90 100644 --- a/Turntables.h +++ b/Turntables.h @@ -32,8 +32,8 @@ // EXTT = EX-Turntable // DCC = DCC accessory turntables - to be added later enum { - TURNTABLE_EXTT = 1, - // TURNTABLE_DCC = 2, + TURNTABLE_EXTT = 0, + TURNTABLE_DCC = 1, }; /************************************************************************************* @@ -41,18 +41,19 @@ enum { * *************************************************************************************/ struct TurntablePosition { + uint8_t index; uint16_t data; TurntablePosition* next; - TurntablePosition(uint16_t value) : data(value), next(nullptr) {} + TurntablePosition(uint8_t idx, uint16_t value) : index(idx), data(value), next(nullptr) {} }; class TurntablePositionList { public: - TurntablePositionList() : head(nullptr) {} + TurntablePositionList() : head(nullptr), currentIndex(0) {} void insert(uint16_t value) { - TurntablePosition* newPosition = new TurntablePosition(value); + TurntablePosition* newPosition = new TurntablePosition(currentIndex++, value); if(!head) { head = newPosition; } else { @@ -67,6 +68,7 @@ public: private: TurntablePosition* head; + uint8_t currentIndex; }; @@ -87,8 +89,8 @@ protected: union { struct { bool hidden : 1; - uint8_t turntableType : 2; - uint8_t position : 5; // Allows up to 38 positions including 0/home + bool turntableType : 1; + uint8_t position : 6; // Allows up to 63 positions including 0/home }; uint8_t flags; }; @@ -148,7 +150,7 @@ public: inline Turntable *next() { return _nextTurntable; } void printState(Print *stream); void addPosition(uint16_t value); - uint16_t getPositionValue(size_t position); + uint16_t getPositionValue(uint8_t position); uint8_t getPositionCount(); /*