From 1711108ee0f72e5a8d215d626531b44dc555c549 Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Wed, 26 May 2021 21:05:26 +0200 Subject: [PATCH] Added decoder function types: AlwaysOff, AlwaysOn --- lang/en-us.txt | 2 ++ server/src/enum/decoderfunctiontype.hpp | 23 ++++++++++++++- .../src/hardware/decoder/decoderfunction.cpp | 28 ++++++++++++++++++- .../src/hardware/decoder/decoderfunction.hpp | 4 +++ .../traintastic/enum/decoderfunctiontype.hpp | 6 +++- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lang/en-us.txt b/lang/en-us.txt index 3e94d52c..6e809eaa 100644 --- a/lang/en-us.txt +++ b/lang/en-us.txt @@ -107,6 +107,8 @@ decoder_function:value=Value decoder_function_list:add=Add function decoder_function_list:f_hash=F# +decoder_function_type:always_off=Always off +decoder_function_type:always_on=Always on decoder_function_type:light=Light decoder_function_type:momentary=Momentary decoder_function_type:mute=Mute diff --git a/server/src/enum/decoderfunctiontype.hpp b/server/src/enum/decoderfunctiontype.hpp index 43a2be8a..7ff59cec 100644 --- a/server/src/enum/decoderfunctiontype.hpp +++ b/server/src/enum/decoderfunctiontype.hpp @@ -26,13 +26,34 @@ #include #include -inline constexpr std::array decoderFunctionTypeValues{{ +inline constexpr std::array decoderFunctionTypeValues{{ DecoderFunctionType::OnOff, DecoderFunctionType::Momentary, DecoderFunctionType::Light, DecoderFunctionType::Sound, DecoderFunctionType::Mute, DecoderFunctionType::Smoke, + DecoderFunctionType::AlwaysOff, + DecoderFunctionType::AlwaysOn, }}; +constexpr bool isAlwaysOffOrOn(DecoderFunctionType type) +{ + switch(type) + { + case DecoderFunctionType::AlwaysOff: + case DecoderFunctionType::AlwaysOn: + return true; + + case DecoderFunctionType::OnOff: + case DecoderFunctionType::Momentary: + case DecoderFunctionType::Light: + case DecoderFunctionType::Sound: + case DecoderFunctionType::Mute: + case DecoderFunctionType::Smoke: + break; + } + return false; +} + #endif diff --git a/server/src/hardware/decoder/decoderfunction.cpp b/server/src/hardware/decoder/decoderfunction.cpp index 702d8059..72a1d01d 100644 --- a/server/src/hardware/decoder/decoderfunction.cpp +++ b/server/src/hardware/decoder/decoderfunction.cpp @@ -38,7 +38,11 @@ DecoderFunction::DecoderFunction(Decoder& decoder, std::string_view _id) : m_decoder{decoder}, number{this, "number", 0, PropertyFlags::ReadWrite | PropertyFlags::Store}, name{this, "name", "", PropertyFlags::ReadWrite | PropertyFlags::Store}, - type{this, "type", DecoderFunctionType::OnOff, PropertyFlags::ReadWrite | PropertyFlags::Store}, + type{this, "type", DecoderFunctionType::OnOff, PropertyFlags::ReadWrite | PropertyFlags::Store, + [this](DecoderFunctionType) + { + typeChanged(); + }}, value{this, "value", false, PropertyFlags::ReadWrite | PropertyFlags::StoreState, [this](bool) { @@ -56,6 +60,7 @@ DecoderFunction::DecoderFunction(Decoder& decoder, std::string_view _id) : Attributes::addEnabled(type, editable); Attributes::addValues(type, decoderFunctionTypeValues); m_interfaceItems.add(type); + Attributes::addEnabled(value, true); m_interfaceItems.add(value); } @@ -66,6 +71,12 @@ void DecoderFunction::save(WorldSaver& saver, nlohmann::json& data, nlohmann::js data["decoder"] = m_decoder.id.toJSON(); } +void DecoderFunction::loaded() +{ + IdObject::loaded(); + typeChanged(); +} + void DecoderFunction::worldEvent(WorldState state, WorldEvent event) { IdObject::worldEvent(state, event); @@ -76,3 +87,18 @@ void DecoderFunction::worldEvent(WorldState state, WorldEvent event) name.setAttributeEnabled(editable); type.setAttributeEnabled(editable); } + +void DecoderFunction::typeChanged() +{ + switch(type) + { + case DecoderFunctionType::AlwaysOff: + value = false; + break; + + case DecoderFunctionType::AlwaysOn: + value = true; + break; + } + Attributes::setEnabled(value, !isAlwaysOffOrOn(type)); +} diff --git a/server/src/hardware/decoder/decoderfunction.hpp b/server/src/hardware/decoder/decoderfunction.hpp index e575c010..f5024e2d 100644 --- a/server/src/hardware/decoder/decoderfunction.hpp +++ b/server/src/hardware/decoder/decoderfunction.hpp @@ -31,10 +31,14 @@ class Decoder; class DecoderFunction : public IdObject { + private: + void typeChanged(); + protected: Decoder& m_decoder; void save(WorldSaver& saver, nlohmann::json& data, nlohmann::json& state) const override; + void loaded() override; void worldEvent(WorldState state, WorldEvent event) final; // void addToWorld() final { Output::addToWorld(); } diff --git a/shared/src/traintastic/enum/decoderfunctiontype.hpp b/shared/src/traintastic/enum/decoderfunctiontype.hpp index bdfbed46..2d3dc12a 100644 --- a/shared/src/traintastic/enum/decoderfunctiontype.hpp +++ b/shared/src/traintastic/enum/decoderfunctiontype.hpp @@ -34,11 +34,13 @@ enum class DecoderFunctionType : uint8_t Sound = 3, Mute = 4, Smoke = 5, + AlwaysOff = 6, + AlwaysOn = 7, }; ENUM_NAME(DecoderFunctionType, "decoder_function_type") -ENUM_VALUES(DecoderFunctionType, 6, +ENUM_VALUES(DecoderFunctionType, 8, { {DecoderFunctionType::OnOff, "on_off"}, {DecoderFunctionType::Momentary, "momentary"}, @@ -46,6 +48,8 @@ ENUM_VALUES(DecoderFunctionType, 6, {DecoderFunctionType::Sound, "sound"}, {DecoderFunctionType::Mute, "mute"}, {DecoderFunctionType::Smoke, "smoke"}, + {DecoderFunctionType::AlwaysOff, "always_off"}, + {DecoderFunctionType::AlwaysOn, "always_on"}, }) #endif