| 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 |
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 |
} |
| 117 |
} |
| 118 |
|