};
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 */
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];
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];
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++) {
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;