]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pattern.c
mudflap for dual rearlights
[bike-lights.git] / firmware / 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         { 4, 0x1 },
27         { 0, 0x1 },
28         { 4, 0x1 },
29         { 0, 0x8 },
30         { 1, 0x1 },
31         { 0, 0x1 },
32         { 1, 0x1 },
33         { 0, 0x8 },
34         PATTERN_END
35 };
36
37 pattern_t boot_pattern[] = {
38         { 1, 0x6 },
39         { 0, 0x6 },
40         { 1, 0x3 },
41         { 0, 0x3 },
42         { 1, 0x2 },
43         { 0, 0x2 },
44         { 1, 0x1 },
45         { 0, 0x1 },
46         { 1, 0x1 },
47         { 0, 0x1 },
48         { 1, 0x1 },
49         { 0, 0x1 },
50         { 1, 0x1 },
51         { 0, 0x1 },
52         { 1, 0x10 },
53         { 0, 0x10 },
54         PATTERN_END
55 };
56
57 pattern_t pattern_num[] = {
58         { 1, 0x1 }, /* 10 */
59         { 0, 0x4 },
60         { 1, 0x1 }, /*  9 */
61         { 0, 0x4 },
62         { 1, 0x1 }, /*  8 */
63         { 0, 0x4 },
64         { 1, 0x1 }, /*  7 */
65         { 0, 0x4 },
66         { 1, 0x1 }, /*  6 */
67         { 0, 0x4 },
68         { 1, 0x1 }, /*  5 */
69         { 0, 0x4 },
70         { 1, 0x1 }, /*  4 */
71         { 0, 0x4 },
72         { 1, 0x1 }, /*  3 */
73         { 0, 0x4 },
74         { 1, 0x1 }, /*  2 */
75         { 0, 0x4 },
76         { 1, 0x1 }, /*  1 */
77         { 0, 0x1F },
78         PATTERN_END
79 };
80
81 static unsigned char test_running;
82
83 static void led_set_mode(unsigned char n, unsigned char mode)
84 {
85         if (n < N_PWMLEDS) {
86                 pwmled_set_mode(n, mode);
87         } else if (n < N_LEDS) {
88                 gpio_set(n - N_PWMLEDS, mode);
89         }
90 }
91
92 void led_set_pattern(unsigned char n, pattern_t *pattern)
93 {
94         if (!pattern)
95                 pattern = off_pattern;
96
97         led_patterns[n] = pattern;
98         led_counters[n] = pattern->duration;
99         led_set_mode(n, pattern->mode);
100 }
101
102 void pattern_init()
103 {
104         unsigned char i;
105
106         for (i = 0; i < N_LEDS; i++) {
107                 led_counters[i] = 0;
108                 led_patterns[i] = off_pattern;
109         }
110         led_set_pattern(N_PWMLEDS+1, boot_pattern);
111         test_running = 0;
112 }
113
114 static pattern_t *number_pattern(unsigned char num)
115 {
116         if (num >= 9)
117                 num = 9;
118
119         return pattern_num + sizeof(pattern_num)/sizeof(pattern_t)
120                 - 1 - 2*num;
121 }
122
123 static inline pattern_t *pattern_select(unsigned char n)
124 {
125         if (n < N_PWMLEDS && !pwmled_enabled(n))
126                 return off_pattern; // Don't mess with non-enabled LEDs
127         else if (n == 2)
128                 return mode1_pattern;
129         else if (n == 3)
130                 return number_pattern(1+ambient_zone);
131         return off_pattern;
132 }
133
134 void patterns_next_tick()
135 {
136         unsigned char i;
137
138         for (i = 0; i < N_LEDS; i++) {
139                 if (led_counters[i] == 0) {
140                         pattern_t *p = led_patterns[i];
141                         p++;
142                         if (p->duration == 0) { // END
143                                 p = pattern_select(i);
144                         }
145                         led_set_pattern(i, p);
146                 }
147
148                 led_counters[i]--;
149         }
150 }
151
152 void led_set_status(unsigned char status)
153 {
154         led_set_pattern(N_PWMLEDS+1, number_pattern(status));
155 }