@@ -145,9 +145,33 @@ class led_controller_impl {
145
145
case msg::RGB_BLINK: fill_blink (led_rgb{.r {message.rgb [0 ]}, .g {message.rgb [1 ]}, .b {message.rgb [2 ]}}, message.cpm ); break ;
146
146
case msg::RGB_BREATH: fill_fade (led_rgb{.r {message.rgb [0 ]}, .g {message.rgb [1 ]}, .b {message.rgb [2 ]}}, message.cpm );break ;
147
147
}
148
+ clamp ();
148
149
update ();
149
150
++counter;
150
151
}
152
+ led_rgb clamp_rgb (led_rgb rgb) const {
153
+ auto const max_channel_value{std::max ({rgb.r , rgb.g , rgb.b })};
154
+ auto const acc_channel_value (static_cast <float >(rgb.r ) + static_cast <float >(rgb.g ) + static_cast <float >(rgb.b ));
155
+
156
+ auto const channel_wise_ratio{static_cast <float >(clamp_threshold) / max_channel_value};
157
+ auto const agg_ratio{3 * static_cast <float >(clamp_threshold) / acc_channel_value};
158
+ auto const clamp_ratio = std::min (channel_wise_ratio, agg_ratio);
159
+ if (1 .0f <= clamp_ratio) {
160
+ return rgb;
161
+ }
162
+
163
+ return led_rgb{
164
+ .r =static_cast <uint8_t >(clamp_ratio * rgb.r + 0 .5f ),
165
+ .g =static_cast <uint8_t >(clamp_ratio * rgb.g + 0 .5f ),
166
+ .b =static_cast <uint8_t >(clamp_ratio * rgb.b + 0 .5f )
167
+ };
168
+ }
169
+ void clamp () {
170
+ for (uint32_t i{0 }; i < PIXELS; ++i) {
171
+ pixeldata[LED_LEFT][i] = clamp_rgb (pixeldata[LED_LEFT][i]);
172
+ pixeldata[LED_RIGHT][i] = clamp_rgb (pixeldata[LED_RIGHT][i]);
173
+ }
174
+ }
151
175
void update () {
152
176
std::copy (&pixeldata[LED_LEFT][0 ], &pixeldata[LED_LEFT][PIXELS_BACK], &pixeldata_back[LED_LEFT][0 ]);
153
177
std::copy (&pixeldata[LED_RIGHT][0 ], &pixeldata[LED_RIGHT][PIXELS_BACK], &pixeldata_back[LED_RIGHT][0 ]);
@@ -337,6 +361,7 @@ class led_controller_impl {
337
361
const device *dev[4 ]{nullptr , nullptr , nullptr , nullptr };
338
362
led_rgb pixeldata[2 ][PIXELS], pixeldata_back[2 ][PIXELS_BACK];
339
363
uint32_t counter{0 };
364
+ static constexpr uint8_t clamp_threshold{0x80 };
340
365
static const led_rgb emergency_stop, amr_mode, agv_mode, mission_pause, path_blocked, manual_drive;
341
366
static const led_rgb dock_mode, waiting_for_job, orange, sequence, move_actuator, lockdown, black;
342
367
} impl;
0 commit comments