static void applyLedHsv(uint32_t mask, const hsvColor_t *color) { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; if ((*ledConfig & mask) == mask) setLedHsv(ledIndex, color); } }
bool isOverlayTypeUsed(ledOverlayId_e overlayType) { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; if (ledGetOverlayBit(ledConfig, overlayType)) { return true; } } return false; }
static hsvColor_t* getDirectionalModeColor(const int ledIndex, const modeColorIndexes_t *modeColors) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; const int ledDirection = ledGetDirection(ledConfig); for (unsigned i = 0; i < LED_DIRECTION_COUNT; i++) { if (ledDirection & (1 << i)) { return &ledStripConfigMutable()->colors[modeColors->color[i]]; } } return NULL; }
static quadrant_e getLedQuadrant(const int ledIndex) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; int x = ledGetX(ledConfig); int y = ledGetY(ledConfig); int quad = 0; if (y <= highestYValueForNorth) quad |= QUADRANT_NORTH; else if (y >= lowestYValueForSouth) quad |= QUADRANT_SOUTH; if (x >= lowestXValueForEast) quad |= QUADRANT_EAST; else if (x <= highestXValueForWest) quad |= QUADRANT_WEST; return quad; }
static void applyLedIndicatorLayer(bool updateNow, timeUs_t *timer) { static bool flash = 0; if (updateNow) { if (rxIsReceivingSignal()) { // calculate update frequency int scale = MAX(ABS(rcCommand[ROLL]), ABS(rcCommand[PITCH])); // 0 - 500 scale = scale - INDICATOR_DEADBAND; // start increasing frequency right after deadband *timer += HZ_TO_US(5 + (45 * scale) / (500 - INDICATOR_DEADBAND)); // 5 - 50Hz update, 2.5 - 25Hz blink flash = !flash; } else { *timer += HZ_TO_US(5); } } if (!flash) return; const hsvColor_t *flashColor = &HSV(ORANGE); // TODO - use user color? quadrant_e quadrants = 0; if (rcCommand[ROLL] > INDICATOR_DEADBAND) { quadrants |= QUADRANT_EAST; } else if (rcCommand[ROLL] < -INDICATOR_DEADBAND) { quadrants |= QUADRANT_WEST; } if (rcCommand[PITCH] > INDICATOR_DEADBAND) { quadrants |= QUADRANT_NORTH; } else if (rcCommand[PITCH] < -INDICATOR_DEADBAND) { quadrants |= QUADRANT_SOUTH; } for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_INDICATOR)) { if (getLedQuadrant(ledIndex) & quadrants) setLedHsv(ledIndex, flashColor); } } }
static void applyLarsonScannerLayer(bool updateNow, timeUs_t *timer) { static larsonParameters_t larsonParameters = { 0, 0, 1 }; if (updateNow) { larsonScannerNextStep(&larsonParameters, 15); *timer += HZ_TO_US(60); } int scannerLedIndex = 0; for (unsigned i = 0; i < ledCounts.count; i++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[i]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_LARSON_SCANNER)) { hsvColor_t ledColor; getLedHsv(i, &ledColor); ledColor.v = brightnessForLarsonIndex(&larsonParameters, scannerLedIndex); setLedHsv(i, &ledColor); scannerLedIndex++; } } }
STATIC_UNIT_TESTED void updateLedCount(void) { int count = 0, countRing = 0, countScanner= 0; for (int ledIndex = 0; ledIndex < LED_MAX_STRIP_LENGTH; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; if (!(*ledConfig)) break; count++; if (ledGetFunction(ledConfig) == LED_FUNCTION_THRUST_RING) countRing++; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_LARSON_SCANNER)) countScanner++; } ledCounts.count = count; ledCounts.ring = countRing; ledCounts.larson = countScanner; }
STATIC_UNIT_TESTED void updateDimensions(void) { int maxX = 0; int minX = LED_XY_MASK; int maxY = 0; int minY = LED_XY_MASK; for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; int ledX = ledGetX(ledConfig); maxX = MAX(ledX, maxX); minX = MIN(ledX, minX); int ledY = ledGetY(ledConfig); maxY = MAX(ledY, maxY); minY = MIN(ledY, minY); } ledGridRows = maxY - minY + 1; if (minX < maxX) { lowestXValueForEast = (minX + maxX) / 2 + 1; highestXValueForWest = (minX + maxX - 1) / 2; } else { lowestXValueForEast = LED_XY_MASK / 2; highestXValueForWest = lowestXValueForEast - 1; } if (minY < maxY) { lowestYValueForSouth = (minY + maxY) / 2 + 1; highestYValueForNorth = (minY + maxY - 1) / 2; } else { lowestYValueForSouth = LED_XY_MASK / 2; highestYValueForNorth = lowestYValueForSouth - 1; } }
// blink twice, then wait ; either always or just when landing static void applyLedBlinkLayer(bool updateNow, timeUs_t *timer) { const uint16_t blinkPattern = 0x8005; // 0b1000000000000101; static uint16_t blinkMask; if (updateNow) { blinkMask = blinkMask >> 1; if (blinkMask <= 1) blinkMask = blinkPattern; *timer += HZ_TO_US(10); } bool ledOn = (blinkMask & 1); // b_b_____... if (!ledOn) { for (int i = 0; i < ledCounts.count; ++i) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[i]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_BLINK)) { setLedHsv(i, getSC(LED_SCOLOR_BLINKBACKGROUND)); } } } }
// get specialColor by index static const hsvColor_t* getSC(ledSpecialColorIds_e index) { return &ledStripConfig()->colors[ledStripConfig()->specialColors.color[index]]; }
static void applyLedVtxLayer(bool updateNow, timeUs_t *timer) { static uint16_t frequency = 0; static uint8_t power = 255; static uint8_t pit = 255; static uint8_t showSettings = false; static uint16_t lastCheck = 0; static bool blink = false; const vtxDevice_t *vtxDevice = vtxCommonDevice(); if (!vtxDevice) { return; } uint8_t band = 255, channel = 255; uint16_t check = 0; if (updateNow) { // keep counter running, so it stays in sync with vtx vtxCommonGetBandAndChannel(vtxDevice, &band, &channel); vtxCommonGetPowerIndex(vtxDevice, &power); vtxCommonGetPitMode(vtxDevice, &pit); frequency = vtx58frequencyTable[band - 1][channel - 1]; //subtracting 1 from band and channel so that correct frequency is returned. //might not be correct for tramp but should fix smart audio. // check if last vtx values have changed. check = pit + (power << 1) + (band << 4) + (channel << 8); if (!showSettings && check != lastCheck) { // display settings for 3 seconds. showSettings = 15; } lastCheck = check; // quick way to check if any settings changed. if (showSettings) { showSettings--; } blink = !blink; *timer += HZ_TO_US(5); // check 5 times a second } hsvColor_t color = {0, 0, 0}; if (showSettings) { // show settings uint8_t vtxLedCount = 0; for (int i = 0; i < ledCounts.count && vtxLedCount < 6; ++i) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[i]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_VTX)) { if (vtxLedCount == 0) { color.h = HSV(GREEN).h; color.s = HSV(GREEN).s; color.v = blink ? 15 : 0; // blink received settings } else if (vtxLedCount > 0 && power >= vtxLedCount && !pit) { // show power color.h = HSV(ORANGE).h; color.s = HSV(ORANGE).s; color.v = blink ? 15 : 0; // blink received settings } else { // turn rest off color.h = HSV(BLACK).h; color.s = HSV(BLACK).s; color.v = HSV(BLACK).v; } setLedHsv(i, &color); ++vtxLedCount; } } } else { // show frequency // calculate the VTX color based on frequency int colorIndex = 0; if (frequency <= 5672) { colorIndex = COLOR_WHITE; } else if (frequency <= 5711) { colorIndex = COLOR_RED; } else if (frequency <= 5750) { colorIndex = COLOR_ORANGE; } else if (frequency <= 5789) { colorIndex = COLOR_YELLOW; } else if (frequency <= 5829) { colorIndex = COLOR_GREEN; } else if (frequency <= 5867) { colorIndex = COLOR_BLUE; } else if (frequency <= 5906) { colorIndex = COLOR_DARK_VIOLET; } else { colorIndex = COLOR_DEEP_PINK; } hsvColor_t color = ledStripConfig()->colors[colorIndex]; color.v = pit ? (blink ? 15 : 0) : 255; // blink when in pit mode applyLedHsv(LED_MOV_OVERLAY(LED_FLAG_OVERLAY(LED_OVERLAY_VTX)), &color); } }
static void applyLedFixedLayers(void) { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { const ledConfig_t *ledConfig = &ledStripConfig()->ledConfigs[ledIndex]; hsvColor_t color = *getSC(LED_SCOLOR_BACKGROUND); int fn = ledGetFunction(ledConfig); int hOffset = HSV_HUE_MAX + 1; switch (fn) { case LED_FUNCTION_COLOR: color = ledStripConfig()->colors[ledGetColor(ledConfig)]; hsvColor_t nextColor = ledStripConfig()->colors[(ledGetColor(ledConfig) + 1 + LED_CONFIGURABLE_COLOR_COUNT) % LED_CONFIGURABLE_COLOR_COUNT]; hsvColor_t previousColor = ledStripConfig()->colors[(ledGetColor(ledConfig) - 1 + LED_CONFIGURABLE_COLOR_COUNT) % LED_CONFIGURABLE_COLOR_COUNT]; if (ledGetOverlayBit(ledConfig, LED_OVERLAY_THROTTLE)) { //smooth fade with selected Aux channel of all HSV values from previousColor through color to nextColor int centerPWM = (PWM_RANGE_MIN + PWM_RANGE_MAX) / 2; if (auxInput < centerPWM) { color.h = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.h, color.h); color.s = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.s, color.s); color.v = scaleRange(auxInput, PWM_RANGE_MIN, centerPWM, previousColor.v, color.v); } else { color.h = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.h, nextColor.h); color.s = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.s, nextColor.s); color.v = scaleRange(auxInput, centerPWM, PWM_RANGE_MAX, color.v, nextColor.v); } } break; case LED_FUNCTION_FLIGHT_MODE: for (unsigned i = 0; i < ARRAYLEN(flightModeToLed); i++) if (!flightModeToLed[i].flightMode || FLIGHT_MODE(flightModeToLed[i].flightMode)) { const hsvColor_t *directionalColor = getDirectionalModeColor(ledIndex, &ledStripConfig()->modeColors[flightModeToLed[i].ledMode]); if (directionalColor) { color = *directionalColor; } break; // stop on first match } break; case LED_FUNCTION_ARM_STATE: color = ARMING_FLAG(ARMED) ? *getSC(LED_SCOLOR_ARMED) : *getSC(LED_SCOLOR_DISARMED); break; case LED_FUNCTION_BATTERY: color = HSV(RED); hOffset += scaleRange(calculateBatteryPercentageRemaining(), 0, 100, -30, 120); break; case LED_FUNCTION_RSSI: color = HSV(RED); hOffset += scaleRange(getRssi() * 100, 0, 1023, -30, 120); break; default: break; } if ((fn != LED_FUNCTION_COLOR) && ledGetOverlayBit(ledConfig, LED_OVERLAY_THROTTLE)) { hOffset += scaleRange(auxInput, PWM_RANGE_MIN, PWM_RANGE_MAX, 0, HSV_HUE_MAX + 1); } color.h = (color.h + hOffset) % (HSV_HUE_MAX + 1); setLedHsv(ledIndex, &color); } }