diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index ab6b52f..eef84a9 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -161,6 +161,10 @@ void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) { #endif } +void CommandDistributor::broadcastTurntable(int16_t id, uint8_t position) { + broadcastReply(COMMAND_TYPE, F("\n"), id, position); +} + void CommandDistributor::broadcastClockTime(int16_t time, int8_t rate) { // The JMRI clock command is of the form : PFT65871<;>4 // The CS broadcast is of the form " + if (parseI(stream, params, p)) + return; + break; + default: //anything else will diagnose and drop out to DIAG(F("Opcode=%c params=%d"), opcode, params); for (int i = 0; i < params; i++) @@ -1013,6 +1019,29 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[]) return false; } +// ========================== +// Turntable +bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[]) +{ + switch (params) + { + 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 2: // - rotate to position for DCC turntables + case 3: // rotate to position for EX-Turntable + { + + } + } +} + // CALLBACKS must be static bool DCCEXParser::stashCallback(Print *stream, int16_t p[MAX_COMMAND_PARAMS], RingStream * ringStream) { diff --git a/DCCEXParser.h b/DCCEXParser.h index bb05ebf..bf067a7 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -45,13 +45,14 @@ struct DCCEXParser static int16_t splitValues( int16_t result[MAX_COMMAND_PARAMS], const byte * command, bool usehex); static bool parseT(Print * stream, int16_t params, int16_t p[]); - static bool parseZ(Print * stream, int16_t params, int16_t p[]); - static bool parseS(Print * stream, int16_t params, int16_t p[]); - static bool parsef(Print * stream, int16_t params, int16_t p[]); - static bool parseD(Print * stream, int16_t params, int16_t p[]); + static bool parseZ(Print * stream, int16_t params, int16_t p[]); + static bool parseS(Print * stream, int16_t params, int16_t p[]); + static bool parsef(Print * stream, int16_t params, int16_t p[]); + static bool parseD(Print * stream, int16_t params, int16_t p[]); + static bool parseI(Print * stream, int16_t params, int16_t p[]); - static Print * getAsyncReplyStream(); - static void commitAsyncReplyStream(); + static Print * getAsyncReplyStream(); + static void commitAsyncReplyStream(); static bool stashBusy; static byte stashTarget; diff --git a/IODevice.h b/IODevice.h index 769e111..8b8f3fe 100644 --- a/IODevice.h +++ b/IODevice.h @@ -542,6 +542,7 @@ protected: #include "IO_PCF8575.h" #include "IO_duinoNodes.h" #include "IO_EXIOExpander.h" +#include "IO_EXTurntable.h" #endif // iodevice_h diff --git a/Turntables.cpp b/Turntables.cpp new file mode 100644 index 0000000..45a1d05 --- /dev/null +++ b/Turntables.cpp @@ -0,0 +1,176 @@ +/* + * © 2023 Peter Cole + * All rights reserved. + * + * This file is part of CommandStation-EX + * + * 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 . + */ + +#include "defines.h" +#include +#include "Turntables.h" +#include "StringFormatter.h" +#include "CommandDistributor.h" +#include "EXRAIL2.h" +#include "DCC.h" + + +/* + * Protected static data + */ +Turntable *Turntable::_firstTurntable = 0; + + +/* + * Public static data + */ +int Turntable::_turntablelistHash = 0; + + +/* + * Protected static functions + */ +// Add new turntable to end of list +void Turntable::add(Turntable *tto) { + if (!_firstTurntable) { + _firstTurntable = tto; + } else { + Turntable *ptr = _firstTurntable; + for ( ; ptr->_nextTurntable!=0; ptr=ptr->_nextTurntable) {} + ptr->_nextTurntable = tto; + } + turntablelistHash++; +} + +// Find turntable from list +Turntable *Turntable::get(uint16_t id) { + for (Turntable *tto = _firstTurntable; tto != NULL; tto = tto->_nextTurntable) + if (tto->_turntableData.id == id) return tto; + 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; + } + + delete (EXTTTurntable *)tto; + + turntablelistHash++; + return true; +} + + +/* + * Public static functions + */ +bool Turntable::isPosition(uint16_t id, uint8_t position) { + Turntable *tto = get(id); + if (!tto) return false; + if (tto) { + if (tto->getPosition() == position) { + return true; + } else { + return false; + } + } +} + +bool Turntable::setPositionStateOnly(uint16_t id, uint8_t position) { + Turntable *tto = get(id); + if (!tto) return false; + CommandDistributor::broadcastTurntable(id, position); +#if defined(EXRAIL_ACTIVE) + // RMFT2::turntableEvent(id, position); +#endif +} + +bool Turntable::setPosition(uint16_t id, uint8_t position) { +#if defined(DIAG_IO) + DIAG(F("Turntable(%d, %d)"), id, position); +#endif + Turntable *tto = Turntable::get(id); + if (!tto) return false; + bool ok = tto->setPositionInternal(position); + + if (ok) { + tto->setPositionStateOnly(id, position); + } + return ok; +} + +/************************************************************************************* + * EXTTTurntable - EX-Turntable device. + * + *************************************************************************************/ +// Private constructor +EXTTTurntable::EXTTTurntable(uint16_t id, uint8_t i2caddress, VPIN vpin, long **positions) : + Turntable(id, TURNTABLE_EXTT) +{ + _exttTurntableData.i2caddress = i2caddress; + _exttTurntableData.vpin = vpin; + _exttTurntableData.positions = **positions; +} + +// Create function +#ifndef IO_NO_HAL + Turntable *EXTTTurntable::create(uint16_t id, uint8_t i2caddress, VPIN vpin, long **positions) { + Turntable *tto = get(id); + if (tto) { + if (tto->isType(TURNTABLE_EXTT)) { + EXTTTurntable *extt = (EXTTTurntable *)tto; + extt->_exttTurntableData.i2caddress = i2caddress; + extt->_exttTurntableData.vpin = vpin; + extt->_exttTurntableData.positions = positions; + return tto; + } else { + remove(id); + } + } + tto = (Turntable *)new EXTTTurntable(id, i2caddress, vpin, **positions); + DIAG(F("Turntable 0x%x"), tto); + return tto; +#else + (void)id; + (void)i2caddress; + (void)vpin; + (void)positions; + return NULL; +#endif + } + + void EXTTTurntable::print(Print *stream) { + StringFormatter::send(stream, F("\n"), _turntableData.id, _exttTurntableData.i2caddress, _exttTurntableData.vpin); + } + + // EX-Turntable specific code for moving to the specified position + bool EXTTTurntable::setPositionInternal(uint8_t position, uint8_t activity) { +#ifndef IO_NO_HAL + // Get step value from positions + int value = _exttTurntableData.positions[position]; + // Set position via device driver + EXTurntable::_writeAnalogue(vpin, value, activity, 0); +#else + (void)position; +#endif + return true; + } diff --git a/Turntables.h b/Turntables.h new file mode 100644 index 0000000..5a6f346 --- /dev/null +++ b/Turntables.h @@ -0,0 +1,164 @@ +/* + * © 2023 Peter Cole + * All rights reserved. + * + * This file is part of CommandStation-EX + * + * 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 . + */ + +#ifndef TURNTABLES_H +#define TURNTABLES_H + +#include +#include "IODevice.h" +#include "StringFormatter.h" + +// Turntable type definitions +// EXTT = EX-Turntable +// DCC = DCC accessory turntables - to be added later +enum { + TURNTABLE_EXTT = 1, + // TURNTABLE_DCC = 2, +}; + +/************************************************************************************* + * Turntable - Base class for turntables. + * + *************************************************************************************/ + +class Turntable { +protected: + /* + * Object data + */ + + // Data common to all turntable types + struct TurntableData { + union { + struct { + bool hidden : 1; + uint8_t turntableType : 2; + uint8_t position : 5; // Allows up to 38 positions including 0/home + }; + uint8_t flags; + }; + uint16_t id; + } _turntableData; + + // Pointer to next turntable object + Turntable *_nextTurntable = 0; + + /* + * Constructor + */ + Turntable(uint16_t id, uint8_t turntableType) { + _turntableData.id = id; + _turntableData.turntableType = turntableType; + _turntableData.hidden = false; + add(this); + } + + /* + * Static data + */ + static Turntable *_firstTurntable; + static int _turntablelistHash; + + /* + * Virtual functions + */ + virtual bool setPositionInternal(uint8_t position, uint8_t activity) = 0; + + /* + * Static functions + */ + static void add(Turntable *tto); + +public: + static Turntable *get(uint16_t id); + + /* + * Static data + */ + static int turntablelistHash; + + /* + * Public base class functions + */ + inline uint8_t getPosition() { return _turntableData.position; } + inline bool isHidden() { return _turntableData.hidden; } + inline void setHidden(bool h) {_turntableData.hidden=h; } + inline bool isType(uint8_t type) { return _turntableData.turntableType == type; } + inline uint16_t getId() { return _turntableData.id; } + inline Turntable *next() { return _nextTurntable; } + void printState(Print *stream); + + /* + * Virtual functions + */ + virtual void print(Print *stream) { + (void)stream; // suppress compiler warnings + } + virtual ~Turntable() {} // Destructor + + /* + * 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); + static bool setPositionStateOnly(uint16_t id, uint8_t position); + inline static Turntable *first() { return _firstTurntable; } + static bool printAll(Print *stream) { + bool gotOne = false; + for (Turntable *tto = _firstTurntable; tto != 0; tto = tto->_nextTurntable) + if (!tto->isHidden()) { + gotOne = true; + StringFormatter::send(stream, F("\n"), tto->getId(), tto->getPosition()); + } + return gotOne; + } + +}; + +/************************************************************************************* + * EXTTTurntable - EX-Turntable device. + * + *************************************************************************************/ +class EXTTTurntable : public Turntable { +private: + // EXTTTurntableData contains device specific data + struct EXTTTurntableData { + uint8_t i2caddress; + VPIN vpin; + long **positions; // Array of longs to store step positions + } _exttTurntableData; + + // Constructor + EXTTTurntable(uint16_t id, uint8_t i2caddress, VPIN vpin, long **positions); + +public: + // Create function + static Turntable *create(uint16_t id, uint8_t i2caddress, VPIN vpin, long **positions); + void print(Print *stream) override; + +protected: + // EX-Turntable specific code for setting position + bool setPositionInternal(uint8_t position, uint8_t activity) override; + +}; + +#endif