Debian Codegraph @

Hardware/Protocols/P50xCache.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 namespace Hardware
24 {
25 namespace Protocols
26 {
27 class P50xCacheEntry
28 {
29 public:
30 inline P50xCacheEntry()
31 : speed(0),
32 orientationF0(0),
33 functions(0)
34 {
35 }
36
37 unsigned char speed;
38 unsigned char orientationF0;
39 union
40 {
41 uint32_t functions;
42 unsigned char function[4];
43 };
44 };
45
46 class P50xCache
47 {
48 public:
49 P50xCache(const P50xCache&) = delete;
50 P50xCache& operator=(const P50xCache&) = delete;
51
52 inline P50xCache()
53 {
54 }
55
56 void SetSpeed(const Address address, const Speed speed)
57 {
58 P50xCacheEntry entry = GetData(address);
59
60 if (speed == 0)
61 {
62 entry.speed = 0;
63 }
64 else if (speed > 1000)
65 {
66 entry.speed = 127;
67 }
68 else
69 {
70 entry.speed = (speed >> 3) + 2;
71 }
72
73 cache[address] = entry;
74 }
75
76 void SetOrientation(const Address address, const Orientation orientation)
77 {
78 P50xCacheEntry entry = GetData(address);
79
80 entry.orientationF0 &= ~(1 << 5);
81 entry.orientationF0 |= static_cast<unsigned char>(orientation) << 5;
82
83 cache[address] = entry;
84 }
85
86 void SetFunction(const Address address,
87 const DataModel::LocoFunctionNr function,
88 const DataModel::LocoFunctionState on)
89 {
90 bool onInternal = static_cast<bool>(on);
91 P50xCacheEntry entry = GetData(address);
92
93 if (function == 0)
94 {
95 entry.orientationF0 &= ~(1 << 4);
96 entry.orientationF0 |= static_cast<unsigned char>(onInternal) << 4;
97 }
98 else
99 {
100 unsigned char shift = function - 1;
101 entry.functions &= ~(1 << shift);
102 entry.functions |= static_cast<uint32_t>(onInternal) << shift;
103 }
104
105 cache[address] = entry;
106 }
107
108 inline P50xCacheEntry GetData(const Address address) const
109 {
110 return cache.count(address) == 0 ? P50xCacheEntry() : cache.at(address);
111 }
112
113 private:
114 std::map<Address, P50xCacheEntry> cache;
115 };
116 } // namespace
117 } // namespace
118