| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <map> |
| 24 |
|
| 25 |
#include "DataTypes.h" |
| 26 |
|
| 27 |
namespace Hardware |
| 28 |
{ |
| 29 |
namespace Protocols |
| 30 |
{ |
| 31 |
class Z21LocoCacheEntry |
| 32 |
{ |
| 33 |
public: |
| 34 |
Z21LocoCacheEntry() |
| 35 |
: speed(MinSpeed), |
| 36 |
orientation(OrientationRight), |
| 37 |
functions(0), |
| 38 |
protocol(ProtocolNone) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
Z21LocoCacheEntry(const Speed speed, const Orientation orientation, const Protocol protocol) |
| 43 |
: speed(speed), |
| 44 |
orientation(orientation), |
| 45 |
functions(0), |
| 46 |
protocol(protocol) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
Speed speed; |
| 51 |
Orientation orientation; |
| 52 |
uint32_t functions; |
| 53 |
Protocol protocol; |
| 54 |
}; |
| 55 |
|
| 56 |
class Z21LocoCache |
| 57 |
{ |
| 58 |
public: |
| 59 |
Z21LocoCache& operator=(const Z21LocoCache&) = delete; |
| 60 |
|
| 61 |
Z21LocoCacheEntry GetData(const Address address) |
| 62 |
{ |
| 63 |
if (cache.count(address) == 1) |
| 64 |
{ |
| 65 |
return cache[address]; |
| 66 |
} |
| 67 |
Z21LocoCacheEntry entry; |
| 68 |
return entry; |
| 69 |
} |
| 70 |
|
| 71 |
void SetSpeed(const Address address, const Speed speed) |
| 72 |
{ |
| 73 |
Z21LocoCacheEntry entry = GetData(address); |
| 74 |
entry.speed = speed; |
| 75 |
cache[address] = entry; |
| 76 |
} |
| 77 |
|
| 78 |
Speed GetSpeed(const Address address) |
| 79 |
{ |
| 80 |
if (cache.count(address) == 0) |
| 81 |
{ |
| 82 |
return MinSpeed; |
| 83 |
} |
| 84 |
return cache[address].speed; |
| 85 |
} |
| 86 |
|
| 87 |
void SetOrientation(const Address address, const Orientation orientation) |
| 88 |
{ |
| 89 |
Z21LocoCacheEntry entry = GetData(address); |
| 90 |
entry.orientation = orientation; |
| 91 |
cache[address] = entry; |
| 92 |
} |
| 93 |
|
| 94 |
Orientation GetOrientation(const Address address) |
| 95 |
{ |
| 96 |
if (cache.count(address) == 0) |
| 97 |
{ |
| 98 |
return OrientationRight; |
| 99 |
} |
| 100 |
return cache[address].orientation; |
| 101 |
} |
| 102 |
|
| 103 |
void SetSpeedOrientationProtocol(const Address address, const Speed speed, |
| 104 |
const Orientation orientation, const Protocol protocol) |
| 105 |
{ |
| 106 |
Z21LocoCacheEntry entry(speed, orientation, protocol); |
| 107 |
cache[address] = entry; |
| 108 |
} |
| 109 |
|
| 110 |
void SetFunction(const Address address, |
| 111 |
const DataModel::LocoFunctionNr function, |
| 112 |
const bool on) |
| 113 |
{ |
| 114 |
Z21LocoCacheEntry entry = GetData(address); |
| 115 |
uint32_t mask = ~(1 << function); |
| 116 |
entry.functions &= mask; |
| 117 |
entry.functions |= on << function; |
| 118 |
cache[address] = entry; |
| 119 |
} |
| 120 |
|
| 121 |
uint32_t GetFunctions(const Address address) |
| 122 |
{ |
| 123 |
if (cache.count(address) == 0) |
| 124 |
{ |
| 125 |
return 0; |
| 126 |
} |
| 127 |
return cache[address].functions; |
| 128 |
} |
| 129 |
|
| 130 |
void SetProtocol(const Address address, const Protocol protocol) |
| 131 |
{ |
| 132 |
Z21LocoCacheEntry entry = GetData(address); |
| 133 |
entry.protocol = protocol; |
| 134 |
cache[address] = entry; |
| 135 |
} |
| 136 |
|
| 137 |
Protocol GetProtocol(const Address address) |
| 138 |
{ |
| 139 |
if (cache.count(address) == 0) |
| 140 |
{ |
| 141 |
return ProtocolNone; |
| 142 |
} |
| 143 |
return cache[address].protocol; |
| 144 |
} |
| 145 |
|
| 146 |
private: |
| 147 |
std::map<Address, Z21LocoCacheEntry> cache; |
| 148 |
}; |
| 149 |
} |
| 150 |
} |
| 151 |
|