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