]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pwmled.c: factored out probing to a separate function
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 7 Dec 2012 21:51:20 +0000 (22:51 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 7 Dec 2012 21:51:20 +0000 (22:51 +0100)
firmware/pwmled.c

index d70013e53eb29b4e1cb3faae35f9c61e0f6085fd..076d032c7b4a713e4a9db98e79ddf6381ed835ab 100644 (file)
@@ -5,7 +5,7 @@
 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;
@@ -80,7 +80,7 @@ void init_pwmled()
                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;
@@ -115,6 +115,69 @@ void pwmled_set_mode(unsigned char n, unsigned char mode)
        }
 }
 
+#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;
@@ -141,53 +204,18 @@ void pwmled_adc(unsigned char n, uint16_t adcval)
        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);
 }