Files
spice2x-r3d/src/spice2x/util/tapeled.cpp
T
bicarusandGitHub 6838e69ebe popn: rest of the new cab lights (#651)
## Link to GitHub Issue or related Pull Request, if one exists
#618 

## Description of change
Add rest of the LED strip lights.

## Testing
Tested U region.
2026-04-18 22:17:20 -07:00

89 lines
3.1 KiB
C++

#include "tapeled.h"
namespace tapeledutils {
led_tape_color_pick_algorithm TAPE_LED_ALGORITHM = TAPE_LED_USE_MIDDLE;
bool is_enabled() {
return (TAPE_LED_ALGORITHM != TAPE_LED_USE_NONE);
}
// for bi2x-style byte array of all colors and LEDs at once
static rgb_float3_t pick_color_from_led_tape_internal(uint8_t *data, size_t data_size, uint8_t divisor) {
rgb_float3_t result = {0.f, 0.f, 0.f};
if (TAPE_LED_ALGORITHM == TAPE_LED_USE_AVERAGE) {
// calculate average color
size_t avg_ri = 0;
size_t avg_gi = 0;
size_t avg_bi = 0;
for (size_t i = 0; i < data_size; i++) {
const auto color = &data[i * 3];
avg_ri += color[0];
avg_gi += color[1];
avg_bi += color[2];
}
// normalize
const float avg_mult = 1.f / (data_size * divisor);
result.r = avg_ri * avg_mult;
result.g = avg_gi * avg_mult;
result.b = avg_bi * avg_mult;
} else if (TAPE_LED_ALGORITHM == TAPE_LED_USE_FIRST ||
TAPE_LED_ALGORITHM == TAPE_LED_USE_MIDDLE ||
TAPE_LED_ALGORITHM == TAPE_LED_USE_LAST ) {
// pick one LED
const uint8_t *color;
switch (TAPE_LED_ALGORITHM) {
case TAPE_LED_USE_FIRST:
color = &data[0];
break;
case TAPE_LED_USE_LAST:
color = &data[(data_size - 1) * 3];
break;
case TAPE_LED_USE_MIDDLE:
default:
color = &data[(data_size / 2) * 3];
break;
}
// normalize
const float single_mult = 1.f / divisor;
result.r = color[0] * single_mult;
result.g = color[1] * single_mult;
result.b = color[2] * single_mult;
}
return result;
}
rgb_float3_t pick_color_from_led_tape(tape_led &light, uint8_t *data, size_t data_size) {
const auto max_value = (light.max_value > 0) ? light.max_value : 0xFF;
return pick_color_from_led_tape_internal(data, data_size, max_value);
}
rgb_float3_t pick_color_from_led_tape(uint8_t *data, size_t data_size) {
return pick_color_from_led_tape_internal(data, data_size, 0xFF);
}
// for bi2a-style that calls for each individual LED
size_t get_led_index_using_avg_algo(size_t data_size) {
size_t index_to_use;
if (TAPE_LED_ALGORITHM == TAPE_LED_USE_FIRST) {
index_to_use = 0;
} else if (TAPE_LED_ALGORITHM == TAPE_LED_USE_LAST) {
index_to_use = data_size - 1;
} else if (TAPE_LED_ALGORITHM == TAPE_LED_USE_MIDDLE) {
index_to_use = (size_t)(data_size / 2);
} else {
// TAPE_LED_USE_AVERAGE can't work for this model since we don't cache the entire tape
// LED array, so just use the middle-of-tape value instead
index_to_use = (size_t)(data_size / 2);
}
return index_to_use;
}
}