]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/pattern.c
PWM LEDs driven by a single pattern
[bike-lights.git] / firmware / pattern.c
index 43a1150e1255635768c36a3aaa00550308fc4923..669225860c4d11583fba63693e653ecc60fe063e 100644 (file)
@@ -6,87 +6,93 @@
 static unsigned char led_counters[N_LEDS];
 static pattern_t *led_patterns[N_LEDS];
 
+static unsigned char fibonacci[8] = {
+       0, 1, 2, 3, 5, 8, 13, 21,
+};
+
 static pattern_t boot_pattern[] = {
-       { 1, 0x6 },
-       { 0, 0x6 },
-       { 1, 0x3 },
-       { 0, 0x3 },
-       { 1, 0x2 },
-       { 0, 0x2 },
-       { 1, 0x1 },
-       { 0, 0x1 },
-       { 1, 0x1 },
-       { 0, 0x1 },
-       { 1, 0x1 },
-       { 0, 0x1 },
-       { 1, 0x1 },
-       { 0, 0x1 },
-       { 1, 0x10 },
-       { 0, 0x10 },
+       { 1, D_5 },
+       { 0, D_5 },
+       { 1, D_3 },
+       { 0, D_3 },
+       { 1, D_2 },
+       { 0, D_2 },
+       { 1, D_1 },
+       { 0, D_1 },
+       { 1, D_1 },
+       { 0, D_1 },
+       { 1, D_1 },
+       { 0, D_1 },
+       { 1, D_1 },
+       { 0, D_1 },
+       { 1, D_8 },
+       { 0, D_8 },
        PATTERN_END
 };
 
 static pattern_t pattern_num[] = {
-       { 0, 0x5 },
-       { 1, 0x1 }, /* 10 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  9 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  8 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  7 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  6 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  5 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  4 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  3 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  2 */
-       { 0, 0x5 },
-       { 1, 0x1 }, /*  1 */
-       { 0, 0x1E },
+       { 0, D_5 },
+       { 1, D_1 }, /* 10 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  9 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  8 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  7 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  6 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  5 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  4 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  3 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  2 */
+       { 0, D_5 },
+       { 1, D_1 }, /*  1 */
+       { 0, D_13 },
        PATTERN_END
 };
 
 static pattern_t pattern_invnum[] = {
-       { 1, 0x5 },
-       { 0, 0x1 }, /* 10 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  9 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  8 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  7 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  6 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  5 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  4 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  3 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  2 */
-       { 1, 0x5 },
-       { 0, 0x1 }, /*  1 */
-       { 1, 0x1E },
+       { 1, D_5 },
+       { 0, D_1 }, /* 10 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  9 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  8 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  7 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  6 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  5 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  4 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  3 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  2 */
+       { 1, D_5 },
+       { 0, D_1 }, /*  1 */
+       { 1, D_13 },
        PATTERN_END
 };
 
 pattern_t off_pattern[] = {
-       { 0, 0x1E },
+       { 0, D_1 },
        PATTERN_END
 };
 
 static void led_set_mode(unsigned char n, unsigned char mode)
 {
-       if (n < N_PWMLEDS) {
-               pwmled_set_mode(n, mode);
+       if (n == 0) {
+               pwmled_set_mode(0, mode & 3);
+               pwmled_set_mode(1, (mode >> 2) & 1);
+               pwmled_set_mode(2, (mode >> 3) & 3);
        } else if (n < N_LEDS) {
-               gpio_set(n - N_PWMLEDS, mode);
+               gpio_set(n - 1, mode);
        }
 }
 
@@ -97,7 +103,8 @@ void led_set_pattern(unsigned char n, pattern_t *pattern)
 
        led_patterns[n] = pattern;
 
-       led_counters[n] = pattern->duration;
+       led_counters[n] = fibonacci[pattern->duration_fib];
+
        led_set_mode(n, pattern->mode);
 }
 
@@ -108,7 +115,7 @@ void init_pattern()
        for (i = 0; i < N_LEDS; i++)
                led_set_pattern(i, NULL);
 
-       led_set_pattern(N_PWMLEDS+1, boot_pattern);
+       led_set_pattern(N_ILLUM_LED, boot_pattern);
 }
 
 pattern_t *number_pattern(unsigned char num, unsigned char inv)
@@ -130,12 +137,10 @@ pattern_t *number_pattern(unsigned char num, unsigned char inv)
 static pattern_t *pattern_select(unsigned char n)
 {
        switch(n) {
-       case 0: return pwmled0_pattern_select();
-       case 1: return pwmled1_pattern_select();
-       case 2: return pwmled2_pattern_select();
-       case 3: return status_led_pattern_select();
-       case 4: return illumination_led_pattern_select();
-       case 6: return laser_pattern_select();
+       case 0: return pwmled_pattern_select();
+       case N_STATUS_LED: return status_led_pattern_select();
+       case N_ILLUM_LED:  return illumination_led_pattern_select();
+       case N_LASER_LED:  return laser_pattern_select();
        default: return NULL;
        }
 }
@@ -148,25 +153,48 @@ 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 == 0) {
+               led_set_pattern(0, pattern_select(0));
+       } else if (n == N_STATUS_LED) {
+               if (!led_patterns[N_ILLUM_LED])
+                       led_set_pattern(N_ILLUM_LED,
+                               pattern_select(N_ILLUM_LED));
+       } else if (n == N_ILLUM_LED) {
+               if (!led_patterns[N_STATUS_LED])
+                       led_set_pattern(N_STATUS_LED,
+                               pattern_select(N_STATUS_LED));
+       } 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);
+                       if (p->duration_fib == 0) { // END
+                               /* Keep the last state, wait for others */
+                               pattern_finished(i);
+                               continue;
                        }
                        led_set_pattern(i, p);
                }
 
-               if (led_counters[i] < PATTERN_FOREVER)
-                       led_counters[i]--;
        }
 }