• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 23088148108

14 Mar 2026 12:40PM UTC coverage: 26.661% (-0.2%) from 26.84%
23088148108

push

github

reinder
[cbus] added input support for short/long events

0 of 133 new or added lines in 4 files covered. (0.0%)

587 existing lines in 20 files now uncovered.

8246 of 30929 relevant lines covered (26.66%)

185.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/server/src/hardware/input/inputcontroller.hpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2021-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#ifndef TRAINTASTIC_SERVER_HARDWARE_INPUT_INPUTCONTROLLER_HPP
23
#define TRAINTASTIC_SERVER_HARDWARE_INPUT_INPUTCONTROLLER_HPP
24

25
#include <cstdint>
26
#include <vector>
27
#include <unordered_map>
28
#include <memory>
29
#include <optional>
30
#include "inputlocation.hpp"
31
#include "../../core/objectproperty.hpp"
32
#include "../../enum/tristate.hpp"
33
#include "../../enum/simulateinputaction.hpp"
34

35
#ifdef interface
36
  #undef interface // interface is defined in combaseapi.h
37
#endif
38

39
class IdObject;
40
class Input;
41
class InputMonitor;
42

43
class InputList;
44
enum class InputListColumn;
45

46
class InputController
47
{
48
  public:
49
    struct InputMapKey
50
    {
51
      InputChannel channel;
52
      InputLocation location;
53

UNCOV
54
      inline bool operator ==(const InputMapKey other) const noexcept
×
55
      {
UNCOV
56
        return channel == other.channel && location == other.location;
×
57
      }
58
    };
59

60
    struct InputMapKeyHash
61
    {
UNCOV
62
      std::size_t operator()(const InputMapKey& value) const noexcept
×
63
      {
64
        auto hash = std::hash<InputChannel>{}(value.channel);
×
UNCOV
65
        hash ^= value.location.index();
×
66
        std::visit(
×
UNCOV
67
          [&hash](auto const& v)
×
68
          {
UNCOV
69
            hash ^= std::hash<std::decay_t<decltype(v)>>{}(v);
×
UNCOV
70
          }, value.location);
×
UNCOV
71
        return hash;
×
72
      }
73
    };
74

75
    using InputMap = std::unordered_map<InputMapKey, std::shared_ptr<Input>, InputMapKeyHash>;
76

77
  private:
78
    std::shared_ptr<InputController> shared_ptr();
79
    IdObject& interface();
80

81
  protected:
82
    InputMap m_inputs;
83
    std::unordered_map<InputChannel, std::weak_ptr<InputMonitor>> m_inputMonitors;
84

85
    InputController(IdObject& interface);
86

87
    void addToWorld(InputListColumn columns);
88
    void destroying();
89

90
  public:
91
    ObjectProperty<InputList> inputs;
92

93
    /**
94
     *
95
     */
UNCOV
96
    inline const InputMap& inputMap() const { return m_inputs; }
×
97

98
    /**
99
     *
100
     */
101
    virtual std::span<const InputChannel> inputChannels() const = 0;
102

103
    /**
104
     *
105
     */
106
    bool isInputChannel(InputChannel channel) const;
107

108
    virtual bool isInputLocation(InputChannel channel, const InputLocation& location) const;
109

110
    /**
111
     *
112
     */
113
    virtual std::pair<uint32_t, uint32_t> inputAddressMinMax(InputChannel channel) const = 0;
114

115
    /**
116
     *
117
     */
118
    [[nodiscard]] virtual bool isInputAvailable(InputChannel channel, const InputLocation& location) const;
119

120
    /**
121
     * \brief Try get the lowest unused input address
122
     *
123
     * \return An usused address or `std::nullopt` if no unused address is available.
124
     */
125
    std::optional<uint32_t> getUnusedInputAddress(InputChannel channel) const;
126

127
    /**
128
     * \brief Get an input.
129
     *
130
     * For each channel/location combination an input object is create once,
131
     * if an input is requested multiple time they all share the same instance.
132
     * Once the object that uses the input is no longer using it,
133
     * it must be released using \ref releaseInput .
134
     * The input object will be destroyed when the are zero users.
135
     *
136
     * \param[in] channel The input channel.
137
     * \param[in] location The input location.
138
     * \param[in] usedBy The object the will use the input.
139
     * \return An input object if the channel/address combination is valid, \c nullptr otherwise.
140
     */
141
    std::shared_ptr<Input> getInput(InputChannel channel, const InputLocation& location, Object& usedBy);
142

143
    /**
144
     * \brief Release an input.
145
     *
146
     * \param[in] input The input to release.
147
     * \param[in] usedBy The object no longer using the input.
148
     * \see getInput
149
     */
150
    void releaseInput(Input& output, Object& usedBy);
151

152
    /**
153
     * @brief Update the input value
154
     *
155
     * This function should be called by the hardware layer whenever the input value changes.
156
     *
157
     * @param[in] channel Input channel
158
     * @param[in] location Input location
159
     * @param[in] value New input value
160
     */
161
    void updateInputValue(InputChannel channel, const InputLocation& location, TriState value);
162

163
    /**
164
     *
165
     *
166
     */
167
    std::shared_ptr<InputMonitor> inputMonitor(InputChannel channel);
168

169
    /**
170
     * \brief Simulate input change
171
     * \param[in] channel Input channel
172
     * \param[in] location Input location
173
     * \param[in] action Simulation action to perform
174
     */
UNCOV
175
    virtual void inputSimulateChange(InputChannel /*channel*/, const InputLocation& /*location*/, SimulateInputAction /*action*/) {}
×
176
};
177

178
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc