]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/pattern.c
firmware: control logic moved to a separate module
[bike-lights.git] / firmware / pattern.c
index 3aec77b947bec7b52edea8f6b1cd470504a54b66..59692bf32a97e07f6f468d878e33cf9c1c7e980d 100644 (file)
@@ -1,42 +1,12 @@
 #include <avr/io.h>
+#include <stdlib.h> // for NULL
 
 #include "lights.h"
 
 static unsigned char led_counters[N_LEDS];
 static pattern_t *led_patterns[N_LEDS];
 
-pattern_t off_pattern[] = {
-       { 0, PATTERN_FOREVER },
-};
-
-pattern_t zero_pattern[] = {
-       { 0, 0x10 },
-       PATTERN_END
-};
-
-pattern_t on_pattern[] = {
-       { 1, PATTERN_FOREVER },
-};
-
-pattern_t blink_pattern[] = {
-       { 1, 0x4 },
-       { 0, 0x8 },
-       PATTERN_END
-};
-
-pattern_t mode1_pattern[] = {
-       { 2, 0x1 },
-       { 0, 0x1 },
-       { 2, 0x1 },
-       { 0, 0x8 },
-       { 1, 0x1 },
-       { 0, 0x1 },
-       { 1, 0x1 },
-       { 0, 0x8 },
-       PATTERN_END
-};
-
-pattern_t boot_pattern[] = {
+static pattern_t boot_pattern[] = {
        { 1, 0x6 },
        { 0, 0x6 },
        { 1, 0x3 },
@@ -56,7 +26,7 @@ pattern_t boot_pattern[] = {
        PATTERN_END
 };
 
-pattern_t pattern_num[] = {
+static pattern_t pattern_num[] = {
        { 0, 0x5 },
        { 1, 0x1 }, /* 10 */
        { 0, 0x5 },
@@ -81,7 +51,30 @@ pattern_t pattern_num[] = {
        PATTERN_END
 };
 
-static unsigned char test_running;
+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 },
+       PATTERN_END
+};
 
 static void led_set_mode(unsigned char n, unsigned char mode)
 {
@@ -94,10 +87,13 @@ static void led_set_mode(unsigned char n, unsigned char mode)
 
 void led_set_pattern(unsigned char n, pattern_t *pattern)
 {
-       if (!pattern)
-               pattern = off_pattern;
-
        led_patterns[n] = pattern;
+
+       if (!pattern) {
+               led_set_mode(n, 0);
+               return;
+       }
+
        led_counters[n] = pattern->duration;
        led_set_mode(n, pattern->mode);
 }
@@ -107,30 +103,45 @@ void init_pattern()
        unsigned char i;
 
        for (i = 0; i < N_LEDS; i++)
-               led_set_pattern(i, zero_pattern);
+               led_set_pattern(i, NULL);
 
        led_set_pattern(N_PWMLEDS+1, boot_pattern);
-       test_running = 0;
 }
 
-pattern_t *number_pattern(unsigned char num)
+pattern_t *number_pattern(unsigned char num, unsigned char inv)
 {
        if (num >= 9)
                num = 9;
 
-       return pattern_num + sizeof(pattern_num)/sizeof(pattern_t)
-               - 2 - 2*num;
+       if (inv) {
+               return pattern_invnum
+                       + sizeof(pattern_invnum)/sizeof(pattern_t)
+                       - 2 - 2*num;
+       } else {
+               return pattern_num
+                       + sizeof(pattern_num)/sizeof(pattern_t)
+                       - 2 - 2*num;
+       }
+}
+
+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();
+       default: return NULL;
+       }
 }
 
-static inline pattern_t *pattern_select(unsigned char n)
+void pattern_reload()
 {
-       if (n < N_PWMLEDS)
-               return mode1_pattern;
-       else if (n == 3)
-               return status_pattern_select(0);
-       else if (n == 4)
-               return status_pattern_select(1);
-       return off_pattern;
+       unsigned char i;
+
+       for (i = 0; i < N_LEDS; i++)
+               led_set_pattern(i, pattern_select(i));
 }
 
 void patterns_next_tick()
@@ -138,6 +149,9 @@ void patterns_next_tick()
        unsigned char i;
 
        for (i = 0; i < N_LEDS; i++) {
+               if (!led_patterns[i])
+                       continue;
+
                if (led_counters[i] == 0) {
                        pattern_t *p = led_patterns[i];
                        p++;