Skip to content

Commit

Permalink
Some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
peteGSX committed Aug 26, 2023
1 parent 57d4655 commit b823a64
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
26 changes: 20 additions & 6 deletions DCCEXParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,25 +1035,39 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
case 0: // <I> list turntable objects
return Turntable::printAll(stream);

case 1: // <I id> delete turntable
if (!Turntable::remove(p[0]))
return false;
StringFormatter::send(stream, F("<O>\n"));
return true;
case 1: // <I id> nothing here for the moment at least
return false;

case 2: // <I id position> - rotate to position for DCC turntables
DIAG(F("Rotate DCC turntable %d to position %d"), p[0], p[1]);
return true;

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;

default: // If we're here, it must be creating a turntable object
{
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 {
Expand Down
31 changes: 15 additions & 16 deletions Turntables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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;
}
// Add a position
void Turntable::addPosition(uint16_t value) {
_turntablePositions.insert(value);
}

delete (EXTTTurntable *)tto;
// 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;
}

turntablelistHash++;
return true;
if (currentPosition) {
return currentPosition->data;
} else {
return false;
}
}

/*
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 41 additions & 8 deletions Turntables.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -57,22 +92,20 @@ class Turntable {
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
*/
Turntable(uint16_t id, uint8_t turntableType) {
_turntableData.id = id;
_turntableData.turntableType = turntableType;
_turntableData.hidden = false;
_turntableData.position = 0;
add(this);
}

Expand Down Expand Up @@ -110,7 +143,8 @@ class Turntable {
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
Expand All @@ -125,7 +159,6 @@ class Turntable {
* 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);
Expand Down

0 comments on commit b823a64

Please sign in to comment.