Merge pull request #1616 from mikeller/cleanup_ledstrip_timer

Cleaned up led strip timer / delay handling.
This commit is contained in:
Martin Budden 2016-11-22 09:29:23 +01:00 committed by GitHub
commit 1419666139
1 changed files with 41 additions and 35 deletions

View File

@ -92,7 +92,9 @@ static void ledStripDisable(void);
//#define USE_LED_ANIMATION //#define USE_LED_ANIMATION
#define HZ_TO_MICROS(hz) ((int32_t)((1000 * 1000) / (hz))) #define HZ_TO_US(hz) ((int32_t)((1000 * 1000) / (hz)))
#define MAX_TIMER_DELAY (5 * 1000 * 1000)
#if LED_MAX_STRIP_LENGTH > WS2811_LED_STRIP_LENGTH #if LED_MAX_STRIP_LENGTH > WS2811_LED_STRIP_LENGTH
# error "Led strip length must match driver" # error "Led strip length must match driver"
@ -535,7 +537,7 @@ static void applyLedWarningLayer(bool updateNow, uint32_t *timer)
if (!ARMING_FLAG(ARMED) && !ARMING_FLAG(OK_TO_ARM)) if (!ARMING_FLAG(ARMED) && !ARMING_FLAG(OK_TO_ARM))
warningFlags |= 1 << WARNING_ARMING_DISABLED; warningFlags |= 1 << WARNING_ARMING_DISABLED;
} }
*timer += HZ_TO_MICROS(10); *timer += HZ_TO_US(10);
} }
if (warningFlags) { if (warningFlags) {
@ -567,27 +569,31 @@ static void applyLedBatteryLayer(bool updateNow, uint32_t *timer)
static bool flash = false; static bool flash = false;
int state; int state;
int frequency = 1; int timerDelayUs = HZ_TO_US(1);
if (updateNow) { if (updateNow) {
state = getBatteryState(); state = getBatteryState();
switch (state) { switch (state) {
case BATTERY_OK: case BATTERY_OK:
flash = false; flash = true;
frequency = 1; timerDelayUs = HZ_TO_US(1);
break;
case BATTERY_WARNING: break;
frequency = 2; case BATTERY_WARNING:
break; flash = !flash;
default: timerDelayUs = HZ_TO_US(2);
frequency = 8;
break; break;
} default:
flash = !flash; flash = !flash;
timerDelayUs = HZ_TO_US(8);
break;
}
} }
*timer += HZ_TO_MICROS(frequency); *timer += timerDelayUs;
if (!flash) { if (!flash) {
hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND);
@ -600,24 +606,24 @@ static void applyLedRssiLayer(bool updateNow, uint32_t *timer)
static bool flash = false; static bool flash = false;
int state; int state;
int frequency = 1; int timerDelay = HZ_TO_US(1);
if (updateNow) { if (updateNow) {
state = (rssi * 100) / 1023; state = (rssi * 100) / 1023;
if (state > 50) { if (state > 50) {
flash = false; flash = true;
frequency = 1; timerDelay = HZ_TO_US(1);
} else if (state > 20) { } else if (state > 20) {
frequency = 2; flash = !flash;
timerDelay = HZ_TO_US(2);
} else { } else {
frequency = 8; flash = !flash;
timerDelay = HZ_TO_US(8);
} }
flash = !flash;
} }
*timer += timerDelay;
*timer += HZ_TO_MICROS(frequency);
if (!flash) { if (!flash) {
hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND); hsvColor_t *bgc = getSC(LED_SCOLOR_BACKGROUND);
@ -642,7 +648,7 @@ static void applyLedGpsLayer(bool updateNow, uint32_t *timer)
gpsFlashCounter++; gpsFlashCounter++;
gpsPauseCounter = 1; gpsPauseCounter = 1;
} }
*timer += HZ_TO_MICROS(2.5f); *timer += HZ_TO_US(2.5f);
} }
const hsvColor_t *gpsColor; const hsvColor_t *gpsColor;
@ -674,11 +680,11 @@ static void applyLedIndicatorLayer(bool updateNow, uint32_t *timer)
// calculate update frequency // calculate update frequency
int scale = MAX(ABS(rcCommand[ROLL]), ABS(rcCommand[PITCH])); // 0 - 500 int scale = MAX(ABS(rcCommand[ROLL]), ABS(rcCommand[PITCH])); // 0 - 500
scale = scale - INDICATOR_DEADBAND; // start increasing frequency right after deadband scale = scale - INDICATOR_DEADBAND; // start increasing frequency right after deadband
*timer += HZ_TO_MICROS(5 + (45 * scale) / (500 - INDICATOR_DEADBAND)); // 5 - 50Hz update, 2.5 - 25Hz blink *timer += HZ_TO_US(5 + (45 * scale) / (500 - INDICATOR_DEADBAND)); // 5 - 50Hz update, 2.5 - 25Hz blink
flash = !flash; flash = !flash;
} else { } else {
*timer += HZ_TO_MICROS(5); // try again soon *timer += HZ_TO_US(5);
} }
} }
@ -736,7 +742,7 @@ static void applyLedThrustRingLayer(bool updateNow, uint32_t *timer)
if (updateNow) { if (updateNow) {
rotationPhase = rotationPhase > 0 ? rotationPhase - 1 : ledCounts.ringSeqLen - 1; rotationPhase = rotationPhase > 0 ? rotationPhase - 1 : ledCounts.ringSeqLen - 1;
*timer += HZ_TO_MICROS(5 + (45 * scaledThrottle) / 100); // 5 - 50Hz update rate *timer += HZ_TO_US(5 + (45 * scaledThrottle) / 100); // 5 - 50Hz update rate
} }
for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) { for (int ledIndex = 0; ledIndex < ledCounts.count; ledIndex++) {
@ -804,7 +810,7 @@ static void applyLarsonScannerLayer(bool updateNow, uint32_t *timer)
if (updateNow) { if (updateNow) {
larsonScannerNextStep(&larsonParameters, 15); larsonScannerNextStep(&larsonParameters, 15);
*timer += HZ_TO_MICROS(60); *timer += HZ_TO_US(60);
} }
int scannerLedIndex = 0; int scannerLedIndex = 0;
@ -833,7 +839,7 @@ static void applyLedBlinkLayer(bool updateNow, uint32_t *timer)
if (blinkMask <= 1) if (blinkMask <= 1)
blinkMask = blinkPattern; blinkMask = blinkPattern;
*timer += HZ_TO_MICROS(10); *timer += HZ_TO_US(10);
} }
bool ledOn = (blinkMask & 1); // b_b_____... bool ledOn = (blinkMask & 1); // b_b_____...
@ -856,7 +862,7 @@ static void applyLedAnimationLayer(bool updateNow, uint32_t *timer)
const int animationFrames = ledGridHeight; const int animationFrames = ledGridHeight;
if(updateNow) { if(updateNow) {
frameCounter = (frameCounter + 1 < animationFrames) ? frameCounter + 1 : 0; frameCounter = (frameCounter + 1 < animationFrames) ? frameCounter + 1 : 0;
*timer += HZ_TO_MICROS(20); *timer += HZ_TO_US(20);
} }
if (ARMING_FLAG(ARMED)) if (ARMING_FLAG(ARMED))
@ -944,12 +950,12 @@ void ledStripUpdate(uint32_t currentTime)
uint32_t timActive = 0; uint32_t timActive = 0;
for (timId_e timId = 0; timId < timTimerCount; timId++) { for (timId_e timId = 0; timId < timTimerCount; timId++) {
// sanitize timer value, so that it can be safely incremented. Handles inital timerVal value. // sanitize timer value, so that it can be safely incremented. Handles inital timerVal value.
// max delay is limited to 5s
int32_t delta = cmp32(now, timerVal[timId]); int32_t delta = cmp32(now, timerVal[timId]);
if (delta < 0 && delta > -HZ_TO_MICROS(0.2f)) // max delay is limited to 5s
if (delta < 0 && delta > -MAX_TIMER_DELAY)
continue; // not ready yet continue; // not ready yet
timActive |= 1 << timId; timActive |= 1 << timId;
if (delta >= HZ_TO_MICROS(10) || delta < 0) { if (delta >= 100 * 1000 || delta < 0) {
timerVal[timId] = now; timerVal[timId] = now;
} }
} }