From e15cb822c9a62b4f740ea3951f45bd764f051e9c Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 19 Jul 2013 23:03:57 +0200 Subject: [PATCH] pwmled.c: mode stabilization When changing brightness via pwmled_set_brightness() below, we want to converge to the target value as fast as possible. Also, we would like to somehow initialize the mode 3, which is used as "mode 2 + other PWMLED on". So after the brightness is set, we also set pwmleds[n].modes_not_yet_stable to MODE_STABILIZATION_TIME. When modes_not_yet_stable is non-zero, we allow only mode 2 to be set regardless of what is fed to pwmled_set_mode. We will then converge to the target value of mode 2 only, and after MODE_STABILIZATION_TIME ADC measurements, we copy the mode_pwm value to all other modes. Only then it is allowed to set the other modes. --- firmware/pwmled.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/firmware/pwmled.c b/firmware/pwmled.c index 30b10f1..3d35c87 100644 --- a/firmware/pwmled.c +++ b/firmware/pwmled.c @@ -11,10 +11,26 @@ typedef struct { }; uint16_t mode_pwm[N_PWMLED_MODES]; int16_t err_sums[N_PWMLED_MODES]; + unsigned char modes_not_yet_stable; } pwmled_t; pwmled_t pwmleds[N_PWMLEDS]; +/* + * Mode stabilization: + * when changing brightness via pwmled_set_brightness() below, + * we want to converge to the target value as fast as possible. Also, + * we would like to somehow initialize the mode 3, which is used as + * "mode 2 + other PWMLED on". So after the brightness is set, + * we also set pwmleds[n].modes_not_yet_stable to MODE_STABILIZATION_TIME. + * When modes_not_yet_stable is non-zero, we allow only mode 2 to be set + * regardless of what is fed to pwmled_set_mode. We will then converge + * to the target value of mode 2 only, and after MODE_STABILIZATION_TIME + * ADC measurements, we copy the mode_pwm value to all other modes. + * Only then it is allowed to set the other modes. + */ +#define MODE_STABILIZATION_TIME (2*16) // two seconds worth of measurements + #define PWMLED2_TESTING_WITH_350MA_LED #define SENSE_MOHM 33 /* 0.033 Ohm */ @@ -119,6 +135,8 @@ void pwmled_set_mode(unsigned char n, unsigned char mode) led->mode = mode; if (mode > 0 && mode <= N_PWMLED_MODES) { + if (led->modes_not_yet_stable) // only mode 2 when !stable + mode = 2; led->target = adc_vals[n*N_PWMLED_MODES + mode - 1]; led->state = ST_ON; led->pwm = led->mode_pwm[mode - 1]; @@ -145,10 +163,14 @@ void pwmled_set_brightness(uint16_t brightness) CHECK_BRIGHTNESS(i, brightness & 0x7, adc_targets_0); adc_vals[0] = adc_targets_0[i]; CHECK_BRIGHTNESS(i, (brightness >> 3) & 0x7, adc_targets_0); - adc_vals[1] = adc_targets_0[i]; + if (adc_vals[1] != adc_targets_0[i]) { + adc_vals[1] = adc_targets_0[i]; + pwmleds[0].modes_not_yet_stable = MODE_STABILIZATION_TIME; + } adc_vals[2] = adc_vals[1]; CHECK_BRIGHTNESS(i, (brightness >> 6) & 0x7, adc_targets_1); + // we use only one mode, so no modes_not_yet_stable handling here adc_vals[3] = adc_targets_1[i]; adc_vals[4] = adc_vals[3]; adc_vals[5] = adc_vals[3]; @@ -156,7 +178,10 @@ void pwmled_set_brightness(uint16_t brightness) CHECK_BRIGHTNESS(i, (brightness >> 9) & 0x7, adc_targets_2); adc_vals[6] = adc_targets_2[i]; CHECK_BRIGHTNESS(i, (brightness >> 12) & 0x7, adc_targets_2); - adc_vals[7] = adc_targets_2[i]; + if (adc_vals[7] != adc_targets_2[i]) { + adc_vals[7] = adc_targets_2[i]; + pwmleds[2].modes_not_yet_stable = MODE_STABILIZATION_TIME; + } adc_vals[8] = adc_vals[7]; for (i = 0; i < N_PWMLEDS; i++) { @@ -275,6 +300,16 @@ void pwmled_adc(unsigned char n, uint16_t adcval) if (pwmled_probed_ok(n, old_pwm)) return; + if (led->modes_not_yet_stable) { + if (!--led->modes_not_yet_stable) { + // reached stability, copy mode 2 to mode 3 (-1) + led->mode_pwm[0] = led->pwm; + led->mode_pwm[2] = led->pwm; + led->err_sums[0] = 0; + led->err_sums[2] = 0; + } + } + if (led->pwm == old_pwm) return; -- 2.39.3