]> www.fi.muni.cz Git - bike-lights.git/blob - pattern.c
363868610dfc8f3487c15ce694c5c76f60f943ad
[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 static void led_set_mode(unsigned char n, unsigned char mode)
81 {
82         if (n < N_PWMLEDS) {
83                 pwmled_set_mode(n, mode);
84         } else if (n < N_LEDS) {
85                 gpio_set(n - N_PWMLEDS, mode);
86         }
87 }
88
89 void led_set_pattern(unsigned char n, pattern_t *pattern)
90 {
91         if (!pattern)
92                 pattern = off_pattern;
93
94         led_patterns[n] = pattern;
95         led_counters[n] = pattern->duration;
96         led_set_mode(n, pattern->mode);
97 }
98
99 void pattern_init()
100 {
101         unsigned char i;
102
103         for (i = 0; i < N_LEDS; i++) {
104                 led_counters[i] = 0;
105                 led_patterns[i] = off_pattern;
106         }
107         led_set_pattern(N_PWMLEDS+1, boot_pattern);
108         test_running = 0;
109 }
110
111 static pattern_t *number_pattern(unsigned char num)
112 {
113         if (num >= 9)
114                 num = 9;
115
116         return pattern_num + sizeof(pattern_num)/sizeof(pattern_t)
117                 - 1 - 2*num;
118 }
119
120 static inline pattern_t *pattern_select(unsigned char n)
121 {
122         if (n < N_PWMLEDS && !pwmled_enabled(n))
123                 return off_pattern; // Don't mess with non-enabled LEDs
124
125         if (n == 2) {
126                 if (test_running) {
127                         log_byte(0xF7);
128                         log_flush();
129                         return off_pattern;
130                 } else {
131                         test_running = 1;
132                         log_byte(0xF6);
133                         return mode1_pattern;
134                 }
135         }
136         return number_pattern(1+ambient_zone);
137 }
138
139 void patterns_next_tick()
140 {
141         unsigned char i;
142
143         for (i = 0; i < N_LEDS; i++) {
144                 if (led_counters[i] == 0) {
145                         pattern_t *p = led_patterns[i];
146                         p++;
147                         if (p->duration == 0) { // END
148                                 p = pattern_select(i);
149                         }
150                         led_set_pattern(i, p);
151                 }
152
153                 led_counters[i]--;
154         }
155 }
156