From 71db228d40a76a5011507d03e326a88ea8f38be8 Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Sun, 19 Jan 2025 13:12:38 +0100 Subject: [PATCH] throttle: removed functions wrapping, access function via train/decoder --- .../hardware/protocol/withrottle/kernel.cpp | 48 ++++++++-- server/src/hardware/throttle/throttle.cpp | 22 ----- server/src/hardware/throttle/throttle.hpp | 4 - .../hardware/throttle/throttlefunction.cpp | 95 ------------------- .../hardware/throttle/throttlefunction.hpp | 51 ---------- 5 files changed, 40 insertions(+), 180 deletions(-) delete mode 100644 server/src/hardware/throttle/throttlefunction.cpp delete mode 100644 server/src/hardware/throttle/throttlefunction.hpp diff --git a/server/src/hardware/protocol/withrottle/kernel.cpp b/server/src/hardware/protocol/withrottle/kernel.cpp index 73fa48fe..ad504bfd 100644 --- a/server/src/hardware/protocol/withrottle/kernel.cpp +++ b/server/src/hardware/protocol/withrottle/kernel.cpp @@ -332,11 +332,11 @@ void Kernel::receiveFrom(std::string_view message, IOHandler::ClientId clientId) postSendTo(throttleCommand(multiThrottleId, '+', address.address, address.isLong), clientId); std::unordered_map functionNames; - for(const auto& f : *throttle->functions) + for(const auto& f : *throttle->decoder()->functions) functionNames.emplace(f->number.value(), f->name.value()); postSendTo(throttleFuctionNames(multiThrottleId, address.address, address.isLong, functionNames), clientId); - for(const auto& f : *throttle->functions) + for(const auto& f : *throttle->decoder()->functions) postSendTo(throttleFunction(multiThrottleId, address.address, address.isLong, f->number, f->value), clientId); if(throttle->decoder()->emergencyStop) @@ -580,14 +580,46 @@ void Kernel::multiThrottleAction(IOHandler::ClientId clientId, char multiThrottl { if(const auto& throttle = getThottle(clientId, multiThrottleId); throttle && throttle->acquired()) { - if(const auto& function = throttle->getFunction(number)) + if(const auto& function = throttle->decoder()->getFunction(number)) { - if(force) + if(force) // set + { function->value = value; - else if(value) - function->press(); - else - function->release(); + } + else if(value) // press + { + switch(function->type.value()) + { + case DecoderFunctionType::Hold: + case DecoderFunctionType::Momentary: + function->value = false; + break; + + case DecoderFunctionType::OnOff: + // toggle when button is pushed, do nothing on release + function->value = !function->value; + break; + + case DecoderFunctionType::AlwaysOff: + case DecoderFunctionType::AlwaysOn: + break; // do nothing + } + } + else // release + { + switch(function->type.value()) + { + case DecoderFunctionType::Hold: + case DecoderFunctionType::Momentary: + function->value = false; + break; + + case DecoderFunctionType::OnOff: + case DecoderFunctionType::AlwaysOff: + case DecoderFunctionType::AlwaysOn: + break; // do nothing + } + } } } }); diff --git a/server/src/hardware/throttle/throttle.cpp b/server/src/hardware/throttle/throttle.cpp index 7a19f3f2..101d67a9 100644 --- a/server/src/hardware/throttle/throttle.cpp +++ b/server/src/hardware/throttle/throttle.cpp @@ -68,7 +68,6 @@ Throttle::Throttle(World& world, std::string_view _id) { return acquired(); }} - , functions{*this, "functions", {}, PropertyFlags::ReadOnly} , train{this, "train", nullptr, PropertyFlags::ReadOnly} , emergencyStop{*this, "emergency_stop", [this]() @@ -276,28 +275,7 @@ Throttle::AcquireResult Throttle::acquire(std::shared_ptr decoder, bool m_decoder = std::move(decoder); - for(auto function : *m_decoder->functions) - { - const auto& type = function->type.value(); - if(type != DecoderFunctionType::AlwaysOff && type != DecoderFunctionType::AlwaysOn) - { - functions.appendInternal(std::make_shared(*this, function->number)); - } - } - Attributes::setEnabled({emergencyStop, throttle, stop, setDirection}, true); return AcquireResult::Success; } - -const std::shared_ptr& Throttle::getFunction(uint32_t number) const -{ - static const std::shared_ptr noFunction; - - for(const auto& function : *functions) - if(function->number == number) - return function; - - return noFunction; -} - diff --git a/server/src/hardware/throttle/throttle.hpp b/server/src/hardware/throttle/throttle.hpp index 661c5f9b..dd44971e 100644 --- a/server/src/hardware/throttle/throttle.hpp +++ b/server/src/hardware/throttle/throttle.hpp @@ -25,7 +25,6 @@ #include "../../core/idobject.hpp" #include -#include "throttlefunction.hpp" #include "../../core/property.hpp" #include "../../core/objectproperty.hpp" #include "../../core/objectvectorproperty.hpp" @@ -71,7 +70,6 @@ class Throttle : public IdObject Property name; Property direction; Property throttle; - ObjectVectorProperty functions; ObjectProperty train; Method emergencyStop; Method stop; @@ -91,8 +89,6 @@ class Throttle : public IdObject { return m_decoder; } - - const std::shared_ptr& getFunction(uint32_t number) const; }; #endif diff --git a/server/src/hardware/throttle/throttlefunction.cpp b/server/src/hardware/throttle/throttlefunction.cpp deleted file mode 100644 index c77eb1d0..00000000 --- a/server/src/hardware/throttle/throttlefunction.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/** - * server/src/hardware/throttle/throttlefunction.cpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 2022 Reinder Feenstra - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "throttlefunction.hpp" -#include "throttle.hpp" -#include "../../core/method.tpp" -#include "../decoder/decoder.hpp" - -ThrottleFunction::ThrottleFunction(Throttle& throttle, uint32_t number_) - : m_throttle{throttle} - , number{this, "number", number_, PropertyFlags::ReadOnly} - , name{this, "name", {}, PropertyFlags::ReadOnly} - , value{this, "value", false, PropertyFlags::ReadWrite} - , press{*this, "press", - [this]() - { - if(!m_throttle.m_decoder) - return; - - if(auto function = m_throttle.m_decoder->getFunction(number)) - { - switch(function->type.value()) - { - case DecoderFunctionType::Hold: - case DecoderFunctionType::Momentary: - function->value = false; - break; - - case DecoderFunctionType::OnOff: - // toggle when button is pushed, do nothing on release - function->value = !function->value; - break; - - case DecoderFunctionType::AlwaysOff: - case DecoderFunctionType::AlwaysOn: - break; // do nothing - } - } - }} - , release{*this, "release", - [this]() - { - if(!m_throttle.m_decoder) - return; - - if(auto function = m_throttle.m_decoder->getFunction(number)) - { - switch(function->type.value()) - { - case DecoderFunctionType::Hold: - case DecoderFunctionType::Momentary: - function->value = false; - break; - - case DecoderFunctionType::OnOff: - case DecoderFunctionType::AlwaysOff: - case DecoderFunctionType::AlwaysOn: - break; // do nothing - } - } - }} -{ - if(const auto& decoder = m_throttle.m_decoder) - { - if(auto function = decoder->getFunction(number)) - { - name.setValueInternal(function->name); - value.setValueInternal(function->value); - } - } -} - -std::string ThrottleFunction::getObjectId() const -{ - return m_throttle.getObjectId().append(".").append(m_throttle.functions.name()).append(".f").append(std::to_string(number)); -} diff --git a/server/src/hardware/throttle/throttlefunction.hpp b/server/src/hardware/throttle/throttlefunction.hpp deleted file mode 100644 index b40176d0..00000000 --- a/server/src/hardware/throttle/throttlefunction.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/** - * server/src/hardware/throttle/throttlefunction.hpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 2022 Reinder Feenstra - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#ifndef TRAINTASTIC_SERVER_HARDWARE_THROTTLE_THROTTLEFUNCTION_HPP -#define TRAINTASTIC_SERVER_HARDWARE_THROTTLE_THROTTLEFUNCTION_HPP - -#include "../../core/object.hpp" -#include "../../core/property.hpp" -#include "../../core/method.hpp" - -class Throttle; - -class ThrottleFunction : public Object -{ - CLASS_ID("throttle_function") - - private: - Throttle& m_throttle; - - public: - Property number; - Property name; - Property value; - Method press; - Method release; - - ThrottleFunction(Throttle& throttle, uint32_t number); - - std::string getObjectId() const final; -}; - -#endif