]> www.fi.muni.cz Git - bike-lights.git/blob - pattern.c
8552240d304165b2dd0378f4b88ea6b2cc083a32
[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, 0x1F },
27         PATTERN_END
28 };
29
30 pattern_t boot_pattern[] = {
31         { 1, 0x6 },
32         { 0, 0x6 },
33         { 1, 0x3 },
34         { 0, 0x3 },
35         { 1, 0x2 },
36         { 0, 0x2 },
37         { 1, 0x1 },
38         { 0, 0x1 },
39         { 1, 0x1 },
40         { 0, 0x1 },
41         { 1, 0x1 },
42         { 0, 0x1 },
43         { 1, 0x1 },
44         { 0, 0x1 },
45         { 1, 0x10 },
46         { 0, 0x10 },
47         PATTERN_END
48 };
49
50 pattern_t pattern_num[] = {
51         { 1, 0x1 }, /* 10 */
52         { 0, 0x4 },
53         { 1, 0x1 }, /*  9 */
54         { 0, 0x4 },
55         { 1, 0x1 }, /*  8 */
56         { 0, 0x4 },
57         { 1, 0x1 }, /*  7 */
58         { 0, 0x4 },
59         { 1, 0x1 }, /*  6 */
60         { 0, 0x4 },
61         { 1, 0x1 }, /*  5 */
62         { 0, 0x4 },
63         { 1, 0x1 }, /*  4 */
64         { 0, 0x4 },
65         { 1, 0x1 }, /*  3 */
66         { 0, 0x4 },
67         { 1, 0x1 }, /*  2 */
68         { 0, 0x4 },
69         { 1, 0x1 }, /*  1 */
70         { 0, 0x1F },
71         PATTERN_END
72 };
73
74 void pattern_init()
75 {
76         unsigned char i;
77
78         for (i = 0; i < N_LEDS; i++) {
79                 led_counters[i] = 0;
80                 led_patterns[i] = off_pattern;
81         }
82         led_patterns[N_PWMLEDS] = boot_pattern;
83         led_counters[N_PWMLEDS] = boot_pattern->duration;
84         gpio_set(GPIO_LED2, 1);
85 }
86
87 static inline pattern_t *pattern_select(unsigned char n)
88 {
89         return pattern_num + sizeof(pattern_num)/sizeof(pattern_t)
90                 - 1 - 2*(1+ambient_zone);;
91 }
92
93 static void inline led_off(unsigned char n)
94 {
95         if (n == N_PWMLEDS) {
96                 gpio_set(GPIO_LED2, 0);
97         }
98 }
99
100 static void inline led_set_level(unsigned char n, unsigned char level)
101 {
102         if (n == N_PWMLEDS) {
103                 gpio_set(GPIO_LED2, 1);
104         }
105 }
106
107 void patterns_next_tick()
108 {
109         unsigned char i;
110
111         for (i = 0; i < N_LEDS; i++) {
112                 if (led_counters[i] == 0) {
113                         led_patterns[i]++;
114                         if (led_patterns[i]->duration == 0) { // END
115                                 led_patterns[i] = pattern_select(i);
116                         }
117                         led_counters[i] = led_patterns[i]->duration;
118                         if (led_patterns[i]->mode == 0) {
119                                 led_off(i);
120                         } else {
121                                 led_set_level(i, led_patterns[i]->mode - 1);
122                         }
123                 }
124
125                 led_counters[i]--;
126         }
127 }
128