]> www.fi.muni.cz Git - bike-lights.git/blob - pattern.c
feedback loop with some testing code
[bike-lights.git] / pattern.c
1 #include <avr/io.h>
2
3 #include "lights.h"
4
5 typedef struct {
6         unsigned char mode: 3;
7         unsigned char duration: 5;
8 } pattern_t;
9
10 static unsigned char led_counters[N_LEDS];
11 static pattern_t *led_patterns[N_LEDS];
12
13 #define PATTERN_END { 0, 0 }
14 pattern_t off_pattern[] = {
15         { 0, 0x1F },
16         PATTERN_END
17 };
18
19 pattern_t blink_pattern[] = {
20         { 1, 0x4 },
21         { 0, 0x8 },
22         PATTERN_END
23 };
24
25 pattern_t mode1_pattern[] = {
26         { 1, 0x1 },
27         { 0, 0x1 },
28         { 4, 0x1 },
29         { 0, 0x1 },
30         { 1, 0x1 },
31         PATTERN_END
32 };
33
34 pattern_t boot_pattern[] = {
35         { 1, 0x6 },
36         { 0, 0x6 },
37         { 1, 0x3 },
38         { 0, 0x3 },
39         { 1, 0x2 },
40         { 0, 0x2 },
41         { 1, 0x1 },
42         { 0, 0x1 },
43         { 1, 0x1 },
44         { 0, 0x1 },
45         { 1, 0x1 },
46         { 0, 0x1 },
47         { 1, 0x1 },
48         { 0, 0x1 },
49         { 1, 0x10 },
50         { 0, 0x10 },
51         PATTERN_END
52 };
53
54 pattern_t pattern_num[] = {
55         { 1, 0x1 }, /* 10 */
56         { 0, 0x4 },
57         { 1, 0x1 }, /*  9 */
58         { 0, 0x4 },
59         { 1, 0x1 }, /*  8 */
60         { 0, 0x4 },
61         { 1, 0x1 }, /*  7 */
62         { 0, 0x4 },
63         { 1, 0x1 }, /*  6 */
64         { 0, 0x4 },
65         { 1, 0x1 }, /*  5 */
66         { 0, 0x4 },
67         { 1, 0x1 }, /*  4 */
68         { 0, 0x4 },
69         { 1, 0x1 }, /*  3 */
70         { 0, 0x4 },
71         { 1, 0x1 }, /*  2 */
72         { 0, 0x4 },
73         { 1, 0x1 }, /*  1 */
74         { 0, 0x1F },
75         PATTERN_END
76 };
77
78 static unsigned char test_running;
79
80 void pattern_init()
81 {
82         unsigned char i;
83
84         for (i = 0; i < N_LEDS; i++) {
85                 led_counters[i] = 0;
86                 led_patterns[i] = off_pattern;
87         }
88         led_patterns[N_PWMLEDS] = boot_pattern;
89         led_counters[N_PWMLEDS] = boot_pattern->duration;
90         gpio_set(GPIO_LED2, 1);
91         test_running = 0;
92 }
93
94 static inline pattern_t *pattern_select(unsigned char n)
95 {
96         if (n < N_PWMLEDS && !pwmled_enabled(n))
97                 return off_pattern; // Don't mess with non-enabled LEDs
98
99         if (n == 2) {
100                 if (test_running) {
101                         log_byte(0xF7);
102                         log_flush();
103                         return off_pattern;
104                 } else {
105                         test_running = 1;
106                         log_byte(0xF6);
107                         return mode1_pattern;
108                 }
109         }
110         return pattern_num + sizeof(pattern_num)/sizeof(pattern_t)
111                 - 1 - 2*(1+ambient_zone);
112 }
113
114 static void led_set_mode(unsigned char n, unsigned char mode)
115 {
116         if (n < N_PWMLEDS) {
117                 pwmled_set_mode(n, mode);
118         } else if (n == N_PWMLEDS) {
119                 gpio_set(GPIO_LED2, mode);
120         }
121         // TODO LED 1
122 }
123
124 static void inline led_set_level(unsigned char n, unsigned char level)
125 {
126         if (n == N_PWMLEDS) {
127                 gpio_set(GPIO_LED2, 1);
128         }
129 }
130
131 void patterns_next_tick()
132 {
133         unsigned char i;
134
135         for (i = 0; i < N_LEDS; i++) {
136                 if (led_counters[i] == 0) {
137                         led_patterns[i]++;
138                         if (led_patterns[i]->duration == 0) { // END
139                                 led_patterns[i] = pattern_select(i);
140                         }
141                         led_counters[i] = led_patterns[i]->duration;
142                         led_set_mode(i, led_patterns[i]->mode);
143                 }
144
145                 led_counters[i]--;
146         }
147 }
148