]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pattern selecting reworked
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 29 Aug 2012 22:52:37 +0000 (00:52 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 31 Aug 2012 21:08:11 +0000 (23:08 +0200)
pattern.c

index 23d747e707044b3c10a71a1a148c15325d99b774..363868610dfc8f3487c15ce694c5c76f60f943ad 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -77,6 +77,25 @@ pattern_t pattern_num[] = {
 
 static unsigned char test_running;
 
+static void led_set_mode(unsigned char n, unsigned char mode)
+{
+       if (n < N_PWMLEDS) {
+               pwmled_set_mode(n, mode);
+       } else if (n < N_LEDS) {
+               gpio_set(n - N_PWMLEDS, mode);
+       }
+}
+
+void led_set_pattern(unsigned char n, pattern_t *pattern)
+{
+       if (!pattern)
+               pattern = off_pattern;
+
+       led_patterns[n] = pattern;
+       led_counters[n] = pattern->duration;
+       led_set_mode(n, pattern->mode);
+}
+
 void pattern_init()
 {
        unsigned char i;
@@ -85,9 +104,7 @@ void pattern_init()
                led_counters[i] = 0;
                led_patterns[i] = off_pattern;
        }
-       led_patterns[N_PWMLEDS+1] = boot_pattern;
-       led_counters[N_PWMLEDS+1] = boot_pattern->duration;
-       gpio_set(GPIO_LED2, 1);
+       led_set_pattern(N_PWMLEDS+1, boot_pattern);
        test_running = 0;
 }
 
@@ -119,27 +136,18 @@ static inline pattern_t *pattern_select(unsigned char n)
        return number_pattern(1+ambient_zone);
 }
 
-static void led_set_mode(unsigned char n, unsigned char mode)
-{
-       if (n < N_PWMLEDS) {
-               pwmled_set_mode(n, mode);
-       } else if (n < N_LEDS) {
-               gpio_set(n - N_PWMLEDS, mode);
-       }
-}
-
 void patterns_next_tick()
 {
        unsigned char i;
 
        for (i = 0; i < N_LEDS; i++) {
                if (led_counters[i] == 0) {
-                       led_patterns[i]++;
-                       if (led_patterns[i]->duration == 0) { // END
-                               led_patterns[i] = pattern_select(i);
+                       pattern_t *p = led_patterns[i];
+                       p++;
+                       if (p->duration == 0) { // END
+                               p = pattern_select(i);
                        }
-                       led_counters[i] = led_patterns[i]->duration;
-                       led_set_mode(i, led_patterns[i]->mode);
+                       led_set_pattern(i, p);
                }
 
                led_counters[i]--;