Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Velocity.cxx
Go to the documentation of this file.
1
34#include "openlcb/Velocity.hxx"
35
36namespace openlcb
37{
38
49{
50 uint8_t result;
51 uint32_t tmp = mph() + 0.5;
52
53 if (tmp == 0)
54 {
55 result = 0;
56 }
57 else if (tmp > 126)
58 {
59 result = 127;
60 }
61 else
62 {
63 result = (uint8_t)(tmp + 1);
64 }
65
66 result |= std::signbit(velocity) ? 0x00 : 0x80;
67 return result;
68}
69
78void Velocity::set_dcc_128(uint8_t value)
79{
80 if ((value & 0x7F) <= 1)
81 {
82 velocity = 0;
83 }
84 else
85 {
86 velocity = (value & 0x07F) - 1;
88 }
89
90 if ((value & 0x80) == 0)
91 {
93 }
94}
95
106{
107 uint8_t result;
108 uint32_t tmp = ((speed() * MPH_FACTOR * 28) / 128) + 0.5;
109
110 if (tmp == 0)
111 {
112 result = 0;
113 }
114 else if (tmp > 28)
115 {
116 result = 31;
117 }
118 else
119 {
120 result = (uint8_t)(tmp + 3);
121 }
122
123 result |= result & 0x01 ? 0xA0 : 0x80;
124
125 result >>= 1;
126
127 result |= std::signbit(velocity) ? 0x00 : 0x20;
128 return result;
129}
130
139void Velocity::set_dcc_28(uint8_t value)
140{
141 value <<= 1;
142
143 value |= value & 0x20 ? 0x01 : 0x00;
144
145 if ((value & 0x1F) <= 3)
146 {
147 velocity = 0;
148 }
149 else
150 {
151 velocity = (value & 0x01F) - 3;
152 velocity *= 128;
153 velocity /= (28 * MPH_FACTOR);
154 }
155
156 if ((value & 0x40) == 0)
157 {
159 }
160}
161
172{
173 uint8_t result;
174 uint32_t tmp = ((speed() * MPH_FACTOR * 14) / 128) + 0.5;
175
176 if (tmp == 0)
177 {
178 result = 0;
179 }
180 else if (tmp > 14)
181 {
182 result = 15;
183 }
184 else
185 {
186 result = (uint8_t)(tmp + 1);
187 }
188
189 result |= 0x40;
190
191 result |= std::signbit(velocity) ? 0x00 : 0x20;
192 return result;
193}
194
203void Velocity::set_dcc_14(uint8_t value)
204{
205 if ((value & 0x0F) <= 1)
206 {
207 velocity = 0;
208 }
209 else
210 {
211 velocity = (value & 0x0F) - 1;
212 velocity *= 128;
213 velocity /= (14 * MPH_FACTOR);
214 }
215
216 if ((value & 0x20) == 0)
217 {
219 }
220}
221
222}; /* namespace openlcb */
#define MPH_FACTOR
Conversion factor for MPH.
Definition Velocity.hxx:51
float speed() const
Return the speed independent of direction.
Definition Velocity.hxx:156
void set_dcc_28(uint8_t value)
Set the speed from DCC 28 speed step format.
Definition Velocity.cxx:139
float velocity
Floating point representation of velocity.
Definition Velocity.hxx:474
float mph() const
Convert the native meters/sec representation into mile per hour.
Definition Velocity.hxx:206
void forward()
Set the direction to forward.
Definition Velocity.hxx:186
uint8_t get_dcc_28()
Get the speed in DCC 28 speed step format.
Definition Velocity.cxx:105
void set_dcc_128(uint8_t value)
Set the speed from DCC 128 speed step format.
Definition Velocity.cxx:78
uint8_t get_dcc_128()
Get the speed in DCC 128 speed step format.
Definition Velocity.cxx:48
void set_dcc_14(uint8_t value)
Set the speed from DCC 14 speed step format.
Definition Velocity.cxx:203
uint8_t get_dcc_14()
Get the speed in DCC 14 speed step format.
Definition Velocity.cxx:171