]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/pattern.c
braking is handled behind the patterns inside pattern.c
[bike-lights.git] / firmware / pattern.c
index ab63e3482e3516cb69577fcfb1240aa9793afd21..c9f4ded4ef230142fc77adecf7669ade8ec1074a 100644 (file)
@@ -85,11 +85,41 @@ pattern_t off_pattern[] = {
        PATTERN_END
 };
 
-static void inline pwmleds_set_mode(unsigned char mode)
+/*
+ * This is tricky: we use a single pattern for all three pwmleds,
+ * but on some occasions, we want to be able to modify only a single
+ * pwmled status without affecting other outputs. For example, during
+ * braking, we want to modify only the rear pwmled status. We don't
+ * use a separate "braking" pattern for every other pattern used, but instead
+ * we change the pwmled0 status regardless of the original value when
+ * braking. The rule is the following:
+ * - if during braking the pwmled2 (front) is at mode 2, we switch it to
+ *   mode 3 (which has the same target current) to avoid flicker.
+ * - if pwmled0 (rear) is off, we set it to mode 2
+ *     (if it is with mode 1, we keep it at mode 1)
+ * TODO: something similar should be done for the "entering the dark area"
+ *     condition, where we want to switch pwmled2 (front) on, to mode 2.
+ */
+void pwmleds_update_mode()
 {
-       pwmled_set_mode(0, mode & 3);
-       pwmled_set_mode(1, (mode >> 2) & 1);
-       pwmled_set_mode(2, (mode >> 3) & 3);
+       unsigned char mode, mode0, mode1, mode2;
+
+       mode = led_patterns[0]->mode;
+
+       mode0 = mode & 3;
+       mode1 = (mode >> 2) & 1;
+       mode2 = (mode >> 3) & 3;
+
+       if (braking) {
+               if (!mode0)
+                       mode0 = 2;
+               if (mode2 == 2)
+                       mode2 = 3;
+       }
+
+       pwmled_set_mode(0, mode0);
+       pwmled_set_mode(1, mode1);
+       pwmled_set_mode(2, mode2);
 }
 
 void led_set_pattern(unsigned char n, pattern_t *pattern)
@@ -102,7 +132,7 @@ void led_set_pattern(unsigned char n, pattern_t *pattern)
        led_counters[n] = fibonacci[pattern->duration_fib];
 
        if (n == 0) {
-               pwmleds_set_mode(pattern->mode);
+               pwmleds_update_mode();
        } else if (n < N_LEDS) {
                gpio_set(n - 1, pattern->mode);
        }