mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-12 19:10:32 +03:00
Update ledstrip.c
This commit is contained in:
parent
049d51ba8e
commit
ac0edec76a
1 changed files with 33 additions and 66 deletions
|
@ -1067,96 +1067,63 @@ static void applyRainbowLayer(bool updateNow, timeUs_t *timer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct auroraWave_s {
|
typedef struct {
|
||||||
uint16_t ttl;
|
uint16_t ttl, age, width;
|
||||||
uint16_t age;
|
float center, speedFactor;
|
||||||
float center;
|
bool goingLeft, alive;
|
||||||
float speedFactor;
|
|
||||||
uint16_t width;
|
|
||||||
bool goingLeft;
|
|
||||||
bool alive;
|
|
||||||
} auroraWave_t;
|
} auroraWave_t;
|
||||||
|
|
||||||
static void initAuroraWave(auroraWave_t *wave, int segmentLength) {
|
static void initAuroraWave(auroraWave_t *wave, int segmentLength) {
|
||||||
wave->ttl = rand() % 1001 + 500; // Zufallszahl zwischen 500 und 1500
|
wave->ttl = rand() % 1001 + 500;
|
||||||
wave->age = 0;
|
wave->age = 0;
|
||||||
wave->width = rand() % (segmentLength / ledStripConfig()->w_width_factor_aurora) + (segmentLength / 20); // Zufallszahl für Breite
|
wave->width = rand() % (segmentLength / ledStripConfig()->w_width_factor_aurora) + (segmentLength / 20);
|
||||||
wave->center = (rand() % 101) / 100.0 * segmentLength; // Zufallszahl zwischen 0 und segmentLength
|
wave->center = rand() / (float)RAND_MAX * segmentLength;
|
||||||
wave->speedFactor = (rand() % 21 + 10) / 100.0 * ledStripConfig()->w_max_speed_aurora / 255.0; // Zufallszahl für Geschwindigkeit
|
wave->speedFactor = (rand() % 21 + 10) / 2550.0 * ledStripConfig()->w_max_speed_aurora;
|
||||||
wave->goingLeft = rand() % 2 == 0; // Zufallswert 0 oder 1
|
wave->goingLeft = rand() % 2;
|
||||||
wave->alive = true;
|
wave->alive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void updateAuroraWave(auroraWave_t *wave, int segmentLength) {
|
static void updateAuroraWave(auroraWave_t *wave, int segmentLength) {
|
||||||
if (wave->goingLeft) {
|
wave->center += wave->goingLeft ? -wave->speedFactor : wave->speedFactor;
|
||||||
wave->center -= wave->speedFactor;
|
wave->alive = ++wave->age <= wave->ttl && wave->center + wave->width >= 0 && wave->center - wave->width <= segmentLength;
|
||||||
} else {
|
|
||||||
wave->center += wave->speedFactor;
|
|
||||||
}
|
|
||||||
|
|
||||||
wave->age++;
|
|
||||||
if (wave->age > wave->ttl || wave->center + wave->width < 0 || wave->center - wave->width > segmentLength) {
|
|
||||||
wave->alive = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int brightnessForAuroraIndex(auroraWave_t *wave, uint8_t ledIndex) {
|
static int brightnessForAuroraIndex(const auroraWave_t *wave, uint8_t ledIndex) {
|
||||||
if (!wave->alive) return 0;
|
float offset = fabsf(ledIndex - wave->center);
|
||||||
|
return (wave->alive && offset <= wave->width)
|
||||||
float offset = fabs(ledIndex - wave->center);
|
? (int)((1 - offset / wave->width) * (wave->ttl - wave->age) / wave->ttl * 255)
|
||||||
if (offset > wave->width) {
|
: 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
float offsetFactor = offset / wave->width;
|
|
||||||
float ageFactor = (float)(wave->ttl - wave->age) / wave->ttl;
|
|
||||||
|
|
||||||
return (int)((1 - offsetFactor) * ageFactor * 255);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void applyAuroraLayer(bool updateNow, timeUs_t *timer) {
|
static void applyAuroraLayer(bool updateNow, timeUs_t *timer) {
|
||||||
static auroraWave_t *auroraWaves = NULL;
|
static auroraWave_t *waves = NULL;
|
||||||
static uint8_t waveCount = 0;
|
static uint8_t waveCount = 0;
|
||||||
|
|
||||||
// Initialisierung nur einmal durchführen
|
if (!waves) {
|
||||||
if (auroraWaves == NULL) {
|
|
||||||
waveCount = ledStripConfig()->w_max_count_aurora;
|
waveCount = ledStripConfig()->w_max_count_aurora;
|
||||||
auroraWaves = (auroraWave_t *)malloc(waveCount * sizeof(auroraWave_t));
|
if (!(waves = malloc(waveCount * sizeof(auroraWave_t)))) return;
|
||||||
if (auroraWaves == NULL) {
|
memset(waves, 0, waveCount * sizeof(auroraWave_t)); // Initialisiert alle Wellen
|
||||||
// Fehler: Speicher konnte nicht allokiert werden
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Array initialisieren (optional, falls nötig)
|
|
||||||
for (int i = 0; i < waveCount; i++) {
|
|
||||||
auroraWaves[i].alive = false; // Beispielwert
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateNow) {
|
if (updateNow) {
|
||||||
for (int i = 0; i < waveCount; i++) {
|
for (uint8_t i = 0; i < waveCount; i++)
|
||||||
if (!auroraWaves[i].alive) {
|
waves[i].alive ? updateAuroraWave(&waves[i], ledCounts.count)
|
||||||
initAuroraWave(&auroraWaves[i], ledCounts.count);
|
: initAuroraWave(&waves[i], ledCounts.count);
|
||||||
} else {
|
|
||||||
updateAuroraWave(&auroraWaves[i], ledCounts.count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*timer += HZ_TO_US(LED_OVERLAY_AURORA_RATE_HZ);
|
*timer += HZ_TO_US(LED_OVERLAY_AURORA_RATE_HZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) {
|
for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) {
|
||||||
int totalBrightness = 0;
|
int brightness = 0;
|
||||||
const ledConfig_t *ledConfig = &ledStripStatusModeConfig()->ledConfigs[ledIndex];
|
const ledConfig_t *config = &ledStripStatusModeConfig()->ledConfigs[ledIndex];
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < waveCount; i++)
|
||||||
|
brightness += brightnessForAuroraIndex(&waves[i], ledIndex);
|
||||||
|
|
||||||
for (int j = 0; j < waveCount; j++) {
|
if (ledGetOverlayBit(config, LED_OVERLAY_AURORA)) {
|
||||||
totalBrightness += brightnessForAuroraIndex(&auroraWaves[j], ledIndex);
|
hsvColor_t color;
|
||||||
}
|
getLedHsv(ledIndex, &color);
|
||||||
|
color.v = brightness > 255 ? 255 : brightness;
|
||||||
if (ledGetOverlayBit(ledConfig, LED_OVERLAY_AURORA)) {
|
setLedHsv(ledIndex, &color);
|
||||||
hsvColor_t ledColor;
|
|
||||||
getLedHsv(ledIndex, &ledColor);
|
|
||||||
ledColor.v = totalBrightness > 255 ? 255 : totalBrightness;
|
|
||||||
setLedHsv(ledIndex, &ledColor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue