]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pattern.c: synchronized patterns
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 6 Mar 2013 21:51:55 +0000 (22:51 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 6 Mar 2013 21:53:38 +0000 (22:53 +0100)
- status LEDs should not blink together
- PWM LEDs should start synchronized, in order to allow for compensation
of battery voltage variations when one output is on
compared to when more outputs are on

firmware/pattern.c

index a8ab5bb585634e96e1aed0be1660dcafdda6d551..2b83420d4fd69b5ecedc57b50a5a8a91fdfd1709 100644 (file)
@@ -148,24 +148,52 @@ void pattern_reload()
                led_set_pattern(i, pattern_select(i));
 }
 
+static void inline pattern_finished(unsigned char n)
+{
+       unsigned char i;
+
+       led_patterns[n] = NULL;
+
+       if (n < N_PWMLEDS) {
+               for (i = 0; i < N_PWMLEDS; i++)
+                       if (led_patterns[i])
+                               return;
+
+               /* all pwmleds finished; restart them */
+               for (i = 0; i < N_PWMLEDS; i++)
+                       led_set_pattern(i, pattern_select(i));
+       } else if (n == 3) {
+               if (!led_patterns[4])
+                       led_set_pattern(4, pattern_select(4));
+       } else if (n == 4) {
+               if (!led_patterns[3])
+                       led_set_pattern(3, pattern_select(3));
+       } else {
+               led_set_pattern(n, pattern_select(n));
+       }
+}
+
 void patterns_next_tick()
 {
        unsigned char i;
 
        for (i = 0; i < N_LEDS; i++) {
-               if (!led_patterns[i])
+               if (!led_patterns[i]) {
+                       pattern_finished(i);
                        continue;
+               }
 
-               if (led_counters[i] == 0) {
+               if (--led_counters[i] == 0) {
                        pattern_t *p = led_patterns[i];
                        p++;
                        if (p->duration == 0) { // END
-                               p = pattern_select(i);
+                               /* Keep the last state, wait for others */
+                               pattern_finished(i);
+                               continue;
                        }
                        led_set_pattern(i, p);
                }
 
-               led_counters[i]--;
        }
 }