1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-26 01:35:41 +03:00

Add simple LED animation when disarmed. Fixed led grid size

calculation.
This commit is contained in:
Dominic Clifton 2014-07-26 18:21:58 +01:00
parent c9b5334f16
commit 5086b46f1f
3 changed files with 68 additions and 6 deletions

View file

@ -40,6 +40,16 @@ void setLedColor(uint16_t index, const rgbColor24bpp_t *color)
ledColorBuffer[index].rgb = color->rgb; ledColorBuffer[index].rgb = color->rgb;
} }
/**
* use this after you set the color
*/
void setLedBrightness(uint16_t index, const uint8_t scalePercent)
{
ledColorBuffer[index].rgb.r = ((uint16_t)ledColorBuffer[index].rgb.r * scalePercent / 100);
ledColorBuffer[index].rgb.g = ((uint16_t)ledColorBuffer[index].rgb.g * scalePercent / 100);
ledColorBuffer[index].rgb.b = ((uint16_t)ledColorBuffer[index].rgb.b * scalePercent / 100);
}
void setStripColor(const rgbColor24bpp_t *color) void setStripColor(const rgbColor24bpp_t *color)
{ {
uint16_t index; uint16_t index;

View file

@ -49,6 +49,7 @@ void ws2811LedStripDMAEnable(void);
void ws2811UpdateStrip(void); void ws2811UpdateStrip(void);
void setLedColor(uint16_t index, const rgbColor24bpp_t *color); void setLedColor(uint16_t index, const rgbColor24bpp_t *color);
void setLedBrightness(uint16_t index, const uint8_t scalePercent);
void setStripColor(const rgbColor24bpp_t *color); void setStripColor(const rgbColor24bpp_t *color);
void setStripColors(const rgbColor24bpp_t *colors); void setStripColors(const rgbColor24bpp_t *colors);

View file

@ -134,9 +134,11 @@ uint8_t highestXValueForWest;
uint8_t lowestXValueForEast; uint8_t lowestXValueForEast;
// timers // timers
uint32_t nextAnimationUpdateAt = 0;
uint32_t nextIndicatorFlashAt = 0; uint32_t nextIndicatorFlashAt = 0;
uint32_t nextBatteryFlashAt = 0; uint32_t nextBatteryFlashAt = 0;
#define LED_STRIP_20HZ ((1000 * 1000) / 20)
#define LED_STRIP_10HZ ((1000 * 1000) / 10) #define LED_STRIP_10HZ ((1000 * 1000) / 10)
#define LED_STRIP_5HZ ((1000 * 1000) / 5) #define LED_STRIP_5HZ ((1000 * 1000) / 5)
@ -363,6 +365,48 @@ void applyLedIndicatorLayer(uint8_t indicatorFlashState)
} }
} }
static uint8_t frameCounter = 0;
static uint8_t previousRow;
static uint8_t currentRow;
static uint8_t nextRow;
static void updateLedAnimationState(void)
{
uint8_t animationFrames = ledGridHeight;
previousRow = (frameCounter + animationFrames - 1) % animationFrames;
currentRow = frameCounter;
nextRow = (frameCounter + 1) % animationFrames;
frameCounter = (frameCounter + 1) % animationFrames;
}
static void applyLedAnimationLayer(void)
{
const ledConfig_t *ledConfig;
if (f.ARMED) {
return;
}
uint8_t ledIndex;
for (ledIndex = 0; ledIndex < WS2811_LED_STRIP_LENGTH; ledIndex++) {
ledConfig = &ledConfigs[ledIndex];
if (LED_Y(ledConfig) == previousRow) {
setLedColor(ledIndex, &white);
setLedBrightness(ledIndex, 50);
} else if (LED_Y(ledConfig) == currentRow) {
setLedColor(ledIndex, &white);
} else if (LED_Y(ledConfig) == nextRow) {
setLedBrightness(ledIndex, 50);
}
}
}
void updateLedStrip(void) void updateLedStrip(void)
{ {
if (!isWS2811LedStripReady()) { if (!isWS2811LedStripReady()) {
@ -371,10 +415,11 @@ void updateLedStrip(void)
uint32_t now = micros(); uint32_t now = micros();
bool animationUpdateNow = (int32_t)(now - nextAnimationUpdateAt) >= 0L;
bool indicatorFlashNow = (int32_t)(now - nextIndicatorFlashAt) >= 0L; bool indicatorFlashNow = (int32_t)(now - nextIndicatorFlashAt) >= 0L;
bool batteryFlashNow = (int32_t)(now - nextBatteryFlashAt) >= 0L; bool batteryFlashNow = (int32_t)(now - nextBatteryFlashAt) >= 0L;
if (!(batteryFlashNow || indicatorFlashNow)) { if (!(batteryFlashNow || indicatorFlashNow || animationUpdateNow)) {
return; return;
} }
@ -423,12 +468,18 @@ void updateLedStrip(void)
applyLedIndicatorLayer(indicatorFlashState); applyLedIndicatorLayer(indicatorFlashState);
if (animationUpdateNow) {
nextAnimationUpdateAt = now + LED_STRIP_20HZ;
updateLedAnimationState();
}
applyLedAnimationLayer();
ws2811UpdateStrip(); ws2811UpdateStrip();
} }
void determineLedStripDimensions() void determineLedStripDimensions()
{ {
// TODO iterate over ledConfigs and determine programatically
ledGridWidth = 0; ledGridWidth = 0;
ledGridHeight = 0; ledGridHeight = 0;
@ -438,11 +489,11 @@ void determineLedStripDimensions()
for (ledIndex = 0; ledIndex < WS2811_LED_STRIP_LENGTH; ledIndex++) { for (ledIndex = 0; ledIndex < WS2811_LED_STRIP_LENGTH; ledIndex++) {
ledConfig = &ledConfigs[ledIndex]; ledConfig = &ledConfigs[ledIndex];
if (LED_X(ledConfig) > ledGridWidth) { if (LED_X(ledConfig) >= ledGridWidth) {
ledGridWidth = LED_X(ledConfig); ledGridWidth = LED_X(ledConfig) + 1;
} }
if (LED_Y(ledConfig) > ledGridHeight) { if (LED_Y(ledConfig) >= ledGridHeight) {
ledGridHeight = LED_X(ledConfig); ledGridHeight = LED_Y(ledConfig) + 1;
} }
} }
} }