]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pattern.c
firmware: buttons-press and pwrdown rework
[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, 0x1F },
10         PATTERN_END
11 };
12
13 pattern_t on_pattern[] = {
14         { 1, 0x1F },
15         PATTERN_END
16 };
17
18 pattern_t blink_pattern[] = {
19         { 1, 0x4 },
20         { 0, 0x8 },
21         PATTERN_END
22 };
23
24 pattern_t mode1_pattern[] = {
25         { 4, 0x1 },
26         { 0, 0x1 },
27         { 4, 0x1 },
28         { 0, 0x8 },
29         { 1, 0x1 },
30         { 0, 0x1 },
31         { 1, 0x1 },
32         { 0, 0x8 },
33         PATTERN_END
34 };
35
36 pattern_t boot_pattern[] = {
37         { 1, 0x6 },
38         { 0, 0x6 },
39         { 1, 0x3 },
40         { 0, 0x3 },
41         { 1, 0x2 },
42         { 0, 0x2 },
43         { 1, 0x1 },
44         { 0, 0x1 },
45         { 1, 0x1 },
46         { 0, 0x1 },
47         { 1, 0x1 },
48         { 0, 0x1 },
49         { 1, 0x1 },
50         { 0, 0x1 },
51         { 1, 0x10 },
52         { 0, 0x10 },
53         PATTERN_END
54 };
55
56 pattern_t pattern_num[] = {
57         { 0, 0x5 },
58         { 1, 0x1 }, /* 10 */
59         { 0, 0x5 },
60         { 1, 0x1 }, /*  9 */
61         { 0, 0x5 },
62         { 1, 0x1 }, /*  8 */
63         { 0, 0x5 },
64         { 1, 0x1 }, /*  7 */
65         { 0, 0x5 },
66         { 1, 0x1 }, /*  6 */
67         { 0, 0x5 },
68         { 1, 0x1 }, /*  5 */
69         { 0, 0x5 },
70         { 1, 0x1 }, /*  4 */
71         { 0, 0x5 },
72         { 1, 0x1 }, /*  3 */
73         { 0, 0x5 },
74         { 1, 0x1 }, /*  2 */
75         { 0, 0x5 },
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                 - 2 - 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 }