typedef struct {
uint16_t target, pwm;
int16_t err_sum;
- unsigned char mode, state, probe_steps;
+ unsigned char mode, state, probe_steady;
uint16_t mode_pwm[N_PWMLED_MODES];
int16_t err_sums[N_PWMLED_MODES];
} pwmled_t;
led->pwm = 0;
led->mode = 1;
led->state = ST_PROBING;
- led->probe_steps = 0;
+ led->probe_steady = 0;
for (j = 0; j < N_PWMLED_MODES; j++) {
led->mode_pwm[j] = 0;
}
}
+#define PWMLED_PROBE_STEADY_COUNT 10
+
+static inline unsigned char pwmled_probed_ok(unsigned char n, uint16_t old_pwm)
+{
+ pwmled_t *led = pwmleds + n;
+
+ if (led->pwm == old_pwm) {
+ if (led->probe_steady < PWMLED_PROBE_STEADY_COUNT)
+ led->probe_steady++;
+ } else {
+ led->probe_steady = 0;
+ }
+
+ if (led->probe_steady < PWMLED_PROBE_STEADY_COUNT
+ && old_pwm <= led->pwm)
+ return 0;
+
+ // probed OK
+ led->mode_pwm[led->mode - 1] = led->pwm;
+ led->err_sums[led->mode - 1] = 0;
+
+ // next mode to probe?
+ if (led->mode < N_PWMLED_MODES) {
+ led->probe_steady = 0;
+ led->err_sum = 0;
+
+ led->mode++;
+ led->target = adc_vals[n*N_PWMLED_MODES+led->mode-1];
+
+ return 0;
+ } else {
+ unsigned char i;
+
+ led->state = ST_OFF;
+ pwm_off(n);
+
+ log_byte(0xF0);
+ log_byte(n);
+ log_word(jiffies);
+
+ for (i = 0; i < N_PWMLED_MODES; i++)
+ log_word(led->mode_pwm[i]);
+
+ log_flush();
+
+ pattern_reload();
+
+ return 1;
+ }
+}
+
+static inline void pwmled_err(unsigned char n)
+{
+ pwmleds[n].state = ST_DISABLED;
+ pwm_off(n);
+
+ log_byte(0xF1);
+ log_byte(n);
+ log_word(jiffies);
+ log_flush();
+}
+
+
void pwmled_adc(unsigned char n, uint16_t adcval)
{
pwmled_t *led = pwmleds + n;
sum -= led->pwm << shift;
led->err_sum = sum;
- if (led->state == ST_PROBING) {
- if (led->pwm == old_pwm) {
- if (led->probe_steps < 10)
- led->probe_steps++;
- } else {
- led->probe_steps = 0;
- }
-
- if (led->probe_steps >= 10 || old_pwm > led->pwm) {
-
- led->mode_pwm[led->mode - 1] = led->pwm;
-
- if (led->mode < N_PWMLED_MODES) {
- led->probe_steps = 0;
- led->err_sum = 0;
-
- led->mode++;
- led->target = adc_vals[n*N_PWMLED_MODES+led->mode-1];
- } else {
- unsigned char i;
-
- led->state = ST_OFF;
-
- log_byte(0xF0);
- log_byte(n);
- log_word(jiffies);
-
- for (i = 0; i < N_PWMLED_MODES; i++)
- log_word(led->mode_pwm[i]);
-
- log_flush();
- }
- }
+ if (led->pwm > (PWM_MAX << PWM_STEP_SHIFT)-5) {
+ pwmled_err(n);
+ return;
}
+ if (led->state == ST_PROBING)
+ if (pwmled_probed_ok(n, old_pwm))
+ return;
+
if (led->pwm == old_pwm)
return;
- if (led->pwm > (PWM_MAX << PWM_STEP_SHIFT)/2) {
- pwm_off(n);
- led->state = ST_DISABLED;
- log_byte(0xF1);
- log_byte(n);
- log_word(jiffies);
- log_flush();
- }
-
pwm_set(n, led->pwm);
}