diff --git a/tests/unit/test_compartment_lights_switch.cpp b/tests/unit/test_compartment_lights_switch.cpp new file mode 100644 index 000000000..b9ba78398 --- /dev/null +++ b/tests/unit/test_compartment_lights_switch.cpp @@ -0,0 +1,227 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +#include "vehicle/compartmentlightsswitch.h" + +using test::definition; +using test::vehicle; + +// the compartment lights commands of the cab (TTrain::OnCommand_compartmentlightsenable/disable/toggle): whether the +// command for one request (on: CompartmentLights.is_enabled, off: is_disabled) also withdraws the other one. In a cab +// without compartmentlights_sw: and without the push buttons (the CD 810 cab A) the keyboard toggle, ctrl+;, turned +// the lights on and off once, then never again: the off request stayed set alongside the on one. In a cab with the +// compartmentlightson_sw: / compartmentlightsoff_sw: push buttons (the EN57-2000) the toggle sent the key release to the +// other command than the press, picked again from the state the press had changed: the pressed button never withdrew +// its request and the lights were off only while ctrl+; was held. + +TEST_CASE("compartment lights switch: which command withdraws the other request") { + // a two-state compartmentlights_sw: holds one request at a time + CHECK(compartment_lights_switch::withdraws_other_request(true, true, false)); + CHECK(compartment_lights_switch::withdraws_other_request(true, true, true)); + // a push type compartmentlights_sw: withdraws both requests itself, when it's released + CHECK_FALSE(compartment_lights_switch::withdraws_other_request(true, false, false)); + CHECK_FALSE(compartment_lights_switch::withdraws_other_request(true, false, true)); + // no compartmentlights_sw:, the push button of the other request withdraws it when it's released + CHECK_FALSE(compartment_lights_switch::withdraws_other_request(false, false, true)); + CHECK_FALSE(compartment_lights_switch::withdraws_other_request(false, true, true)); + // no compartmentlights_sw: and no push button: nothing else would withdraw it + CHECK(compartment_lights_switch::withdraws_other_request(false, false, false)); + CHECK(compartment_lights_switch::withdraws_other_request(false, true, false)); +} + +namespace { + +// the compartment lights controls a cab's .mmd may have +struct cab_controls { + bool has_switch{false}; // compartmentlights_sw: + bool switch_is_toggle{true}; // its type (a gauge is a toggle type unless declared otherwise) + bool has_on_button{false}; // compartmentlightson_sw: + bool has_off_button{false}; // compartmentlightsoff_sw: +}; + +// the effect of the cab's commands on the vehicle, as TTrain::OnCommand_compartmentlightsenable/disable/toggle do it +// NOTE: LightsCheck() runs between the press and the release, as the vehicle update does between two frames +void lights_check(vehicle &Car) { + Car->LightsCheck(0.01); +} + +void enable_press(vehicle &Car, cab_controls const &Cab) { + Car->CompartmentLightsSwitch(true, range_t::local); + if (compartment_lights_switch::withdraws_other_request(Cab.has_switch, Cab.switch_is_toggle, Cab.has_off_button)) { + Car->CompartmentLightsSwitchOff(false, range_t::local); + } +} + +void enable_release(vehicle &Car, cab_controls const &Cab) { + using enum range_t; + if (Cab.has_switch && false == Cab.switch_is_toggle) { + Car->CompartmentLightsSwitch(false, local); + Car->CompartmentLightsSwitchOff(false, local); + } + if (Cab.has_on_button) { + Car->CompartmentLightsSwitch(false, local); + } +} + +void disable_press(vehicle &Car, cab_controls const &Cab) { + Car->CompartmentLightsSwitchOff(true, range_t::local); + if (compartment_lights_switch::withdraws_other_request(Cab.has_switch, Cab.switch_is_toggle, Cab.has_on_button)) { + Car->CompartmentLightsSwitch(false, range_t::local); + } +} + +void disable_release(vehicle &Car, cab_controls const &Cab) { + using enum range_t; + if (Cab.has_switch && false == Cab.switch_is_toggle) { + Car->CompartmentLightsSwitch(false, local); + Car->CompartmentLightsSwitchOff(false, local); + } + if (Cab.has_off_button) { + Car->CompartmentLightsSwitchOff(false, local); + } +} + +// a click on the control of the command (or its own key): the press, then the release +void enable(vehicle &Car, cab_controls const &Cab) { + enable_press(Car, Cab); + lights_check(Car); + enable_release(Car, Cab); + lights_check(Car); +} + +void disable(vehicle &Car, cab_controls const &Cab) { + disable_press(Car, Cab); + lights_check(Car); + disable_release(Car, Cab); + lights_check(Car); +} + +// the keyboard toggle picks the command from the state on the press, the key held down for a few frames; the release +// goes to both commands, the press changed the state it would be picked from +void toggle(vehicle &Car, cab_controls const &Cab) { + if (auto const &lights{Car->CompartmentLights}; false == lights.is_active && false == lights.is_enabled) { + enable_press(Car, Cab); + } + else { + disable_press(Car, Cab); + } + for (auto frame = 0; frame < 3; ++frame) { + lights_check(Car); + } + enable_release(Car, Cab); + disable_release(Car, Cab); + lights_check(Car); +} + +// the Elf (21WE-a), its compartment lights started by hand, with the converter running +void power_up(vehicle &Car) { + REQUIRE(Car->CompartmentLights.start_type == start_t::manual); + Car->Power110vIsAvailable = true; + lights_check(Car); + REQUIRE_FALSE(Car->CompartmentLights.is_active); +} + +} // namespace + +TEST_CASE("compartment lights switch: the keyboard toggle works every time in a cab without the switch") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{}; // no compartmentlights_sw:, no push buttons, like the CD 810 cab A + + for (auto press = 1; press <= 6; ++press) { + CAPTURE(press); + toggle(car, cab); + // odd presses turn the lights on, even ones off + CHECK(car->CompartmentLights.is_active == (press % 2 == 1)); + // one request at a time + CHECK_FALSE((car->CompartmentLights.is_enabled && car->CompartmentLights.is_disabled)); + } +} + +TEST_CASE("compartment lights switch: the keyboard toggle in a cab with the two-state switch, as before") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{.has_switch = true, .switch_is_toggle = true}; + + for (auto press = 1; press <= 4; ++press) { + CAPTURE(press); + toggle(car, cab); + CHECK(car->CompartmentLights.is_active == (press % 2 == 1)); + // the switch holds the request of its position + CHECK(car->CompartmentLights.is_enabled == (press % 2 == 1)); + CHECK(car->CompartmentLights.is_disabled == (press % 2 == 0)); + } +} + +TEST_CASE("compartment lights switch: the push type switch withdraws both requests when released, as before") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{.has_switch = true, .switch_is_toggle = false}; + + for (auto press = 1; press <= 4; ++press) { + CAPTURE(press); + toggle(car, cab); + CHECK(car->CompartmentLights.is_active == (press % 2 == 1)); + CHECK_FALSE(car->CompartmentLights.is_enabled); + CHECK_FALSE(car->CompartmentLights.is_disabled); + } +} + +TEST_CASE("compartment lights switch: the on and off push buttons latch the lights, as before") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{.has_on_button = true, .has_off_button = true}; + + for (auto round = 1; round <= 2; ++round) { + CAPTURE(round); + // each button withdraws its own request when it's released, the lights stay as they were put + enable(car, cab); + CHECK(car->CompartmentLights.is_active); + CHECK_FALSE(car->CompartmentLights.is_enabled); + CHECK_FALSE(car->CompartmentLights.is_disabled); + disable(car, cab); + CHECK_FALSE(car->CompartmentLights.is_active); + CHECK_FALSE(car->CompartmentLights.is_enabled); + CHECK_FALSE(car->CompartmentLights.is_disabled); + } +} + +TEST_CASE("compartment lights switch: the keyboard toggle latches the lights in a cab with the on and off push buttons") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{.has_on_button = true, .has_off_button = true}; // no compartmentlights_sw:, like the EN57-2000 + + for (auto press = 1; press <= 6; ++press) { + CAPTURE(press); + toggle(car, cab); + // odd presses turn the lights on, even ones off, and they stay so after the key is released + CHECK(car->CompartmentLights.is_active == (press % 2 == 1)); + // both push buttons are back in the neutral position, neither request is left behind + CHECK_FALSE(car->CompartmentLights.is_enabled); + CHECK_FALSE(car->CompartmentLights.is_disabled); + } +} + +TEST_CASE("compartment lights switch: the keyboard toggle and the on and off push buttons work together") { + vehicle car{definition::fiz("elf_v1/21we-a")}; + power_up(car); + cab_controls const cab{.has_on_button = true, .has_off_button = true}; + + toggle(car, cab); + REQUIRE(car->CompartmentLights.is_active); + // the off button turns off what the key turned on... + disable(car, cab); + CHECK_FALSE(car->CompartmentLights.is_active); + // ...and the key turns on what the off button turned off + toggle(car, cab); + CHECK(car->CompartmentLights.is_active); + toggle(car, cab); + CHECK_FALSE(car->CompartmentLights.is_active); + enable(car, cab); + CHECK(car->CompartmentLights.is_active); + toggle(car, cab); + CHECK_FALSE(car->CompartmentLights.is_active); + CHECK_FALSE(car->CompartmentLights.is_enabled); + CHECK_FALSE(car->CompartmentLights.is_disabled); +} diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 080e697c4..24d3132f6 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/horn.h" #include "vehicle/linebreaker.h" #include "vehicle/holdcoillever.h" +#include "vehicle/compartmentlightsswitch.h" #include "vehicle/odometer.h" #include "vehicle/mainctrlact.h" @@ -6636,6 +6637,16 @@ void TTrain::OnCommand_compartmentlightstoggle(TTrain *Train, command_data const return; } + if (Command.action == GLFW_RELEASE) + { + // the press changed the state the command is picked from below, so a release picked from it went to the other + // command and the push button that was pressed never withdrew its request. The release goes to both commands: + // each returns its own push type control to the neutral position and withdraws its request + OnCommand_compartmentlightsenable(Train, Command); + OnCommand_compartmentlightsdisable(Train, Command); + return; + } + // keep the switch from flipping back and forth if key is held down if (false == Train->mvOccupied->CompartmentLights.is_active && false == Train->mvOccupied->CompartmentLights.is_enabled) { @@ -6655,13 +6666,11 @@ void TTrain::OnCommand_compartmentlightsenable(TTrain *Train, command_data const if (Command.action == GLFW_PRESS) { Train->mvOccupied->CompartmentLightsSwitch(true); - if (Train->m_controlmapper.contains("compartmentlights_sw:")) + // withdraw the off request, unless a push type control in the cab withdraws it when released + if (compartment_lights_switch::withdraws_other_request(Train->m_controlmapper.contains("compartmentlights_sw:"), Train->ggCompartmentLightsButton.is_toggle(), + Train->m_controlmapper.contains("compartmentlightsoff_sw:"))) { - auto const istoggle{(static_cast(Train->ggCompartmentLightsButton.type()) & static_cast(TGaugeType::toggle)) != 0}; - if (istoggle) - { - Train->mvOccupied->CompartmentLightsSwitchOff(false); - } + Train->mvOccupied->CompartmentLightsSwitchOff(false); } // visual feedback if (Train->m_controlmapper.contains("compartmentlights_sw:")) @@ -6696,13 +6705,11 @@ void TTrain::OnCommand_compartmentlightsdisable(TTrain *Train, command_data cons if (Command.action == GLFW_PRESS) { Train->mvOccupied->CompartmentLightsSwitchOff(true); - if (Train->m_controlmapper.contains("compartmentlights_sw:")) + // withdraw the on request, unless a push type control in the cab withdraws it when released + if (compartment_lights_switch::withdraws_other_request(Train->m_controlmapper.contains("compartmentlights_sw:"), Train->ggCompartmentLightsButton.is_toggle(), + Train->m_controlmapper.contains("compartmentlightson_sw:"))) { - auto const istoggle{(static_cast(Train->ggCompartmentLightsButton.type()) & static_cast(TGaugeType::toggle)) != 0}; - if (istoggle) - { - Train->mvOccupied->CompartmentLightsSwitch(false); - } + Train->mvOccupied->CompartmentLightsSwitch(false); } // visual feedback if (Train->m_controlmapper.contains("compartmentlights_sw:")) diff --git a/vehicle/compartmentlightsswitch.h b/vehicle/compartmentlightsswitch.h new file mode 100644 index 000000000..0ef6684c0 --- /dev/null +++ b/vehicle/compartmentlightsswitch.h @@ -0,0 +1,27 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +// the compartment lights get two requests: on (CompartmentLights.is_enabled) and off (is_disabled), and the off one +// wins. A two-state compartmentlights_sw: holds one of them, so turning it to one position withdraws the other request. +// A push type compartmentlights_sw: and the separate compartmentlightson_sw: / compartmentlightsoff_sw: push buttons +// withdraw their own request when they're released, so the other command leaves it alone. A cab with none of these +// controls (the keyboard toggle, ctrl+;, is all there is) acts as if it had the two-state switch, otherwise nothing +// would ever withdraw the off request and the lights couldn't be turned on again. +namespace compartment_lights_switch { + +// whether the command for one of the requests also withdraws the other one, from the cab's compartmentlights_sw: +// (whether it's there and whether it's a toggle type switch) and whether the cab has the push button of the other +// request (compartmentlightsoff_sw: for the on command, compartmentlightson_sw: for the off command) +inline bool withdraws_other_request(bool const Hasswitch, bool const Switchistoggle, bool const Hasotherbutton) { + return Hasswitch ? Switchistoggle : false == Hasotherbutton; +} + +} // namespace compartment_lights_switch