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