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