]> www.fi.muni.cz Git - tinyboard.git/blob - projects/step-up/pattern.c
pattern.c: remove unused variable
[tinyboard.git] / projects / step-up / pattern.c
1 #include <avr/io.h>
2 #include <stdlib.h> // for NULL
3
4 #include "lights.h"
5
6 #define N_LEDS 2
7 static unsigned char led_counters[N_LEDS];
8 static pattern_t *led_patterns[N_LEDS];
9
10 static pattern_t pattern_num[] = {
11         { 0, 0x5 },
12         { 1, 0x1 }, /* 10 */
13         { 0, 0x5 },
14         { 1, 0x1 }, /*  9 */
15         { 0, 0x5 },
16         { 1, 0x1 }, /*  8 */
17         { 0, 0x5 },
18         { 1, 0x1 }, /*  7 */
19         { 0, 0x5 },
20         { 1, 0x1 }, /*  6 */
21         { 0, 0x5 },
22         { 1, 0x1 }, /*  5 */
23         { 0, 0x5 },
24         { 1, 0x1 }, /*  4 */
25         { 0, 0x5 },
26         { 1, 0x1 }, /*  3 */
27         { 0, 0x5 },
28         { 1, 0x1 }, /*  2 */
29         { 0, 0x5 },
30         { 1, 0x1 }, /*  1 */
31         { 0, 0xF },
32         PATTERN_END
33 };
34
35 static pattern_t pattern_invnum[] = {
36         { 1, 0x5 },
37         { 0, 0x1 }, /* 10 */
38         { 1, 0x5 },
39         { 0, 0x1 }, /*  9 */
40         { 1, 0x5 },
41         { 0, 0x1 }, /*  8 */
42         { 1, 0x5 },
43         { 0, 0x1 }, /*  7 */
44         { 1, 0x5 },
45         { 0, 0x1 }, /*  6 */
46         { 1, 0x5 },
47         { 0, 0x1 }, /*  5 */
48         { 1, 0x5 },
49         { 0, 0x1 }, /*  4 */
50         { 1, 0x5 },
51         { 0, 0x1 }, /*  3 */
52         { 1, 0x5 },
53         { 0, 0x1 }, /*  2 */
54         { 1, 0x5 },
55         { 0, 0x1 }, /*  1 */
56         { 1, 0xF },
57         PATTERN_END
58 };
59
60 pattern_t off_pattern[] = {
61         { 0, 0x1 },
62         PATTERN_END
63 };
64
65 pattern_t on1_pattern[] = {
66         { 1, 1 },
67         { 0, 2 },
68         { 1, 1 },
69         { 0, 8 },
70         { 1, 1 },
71         { 0, 8 },
72         PATTERN_END
73 };
74
75 static void led_set_mode(unsigned char n, unsigned char mode)
76 {
77         switch (n) {
78         case 0: pwmled_on_off(mode); break;
79         case 1: status_led_on_off(mode); break;
80         }
81 }
82
83 void led_set_pattern(unsigned char n, pattern_t *pattern)
84 {
85         if (!pattern)
86                 pattern = off_pattern;
87
88         led_patterns[n] = pattern;
89
90         led_counters[n] = pattern->duration;
91         led_set_mode(n, pattern->mode);
92 }
93
94 void init_pattern()
95 {
96         unsigned char i;
97
98         for (i = 0; i < N_LEDS; i++)
99                 led_set_pattern(i, NULL);
100 }
101
102 pattern_t *number_pattern(unsigned char num, unsigned char inv)
103 {
104         if (num >= 10)
105                 num = 10;
106
107         if (inv) {
108                 return pattern_invnum
109                         + sizeof(pattern_invnum)/sizeof(pattern_t)
110                         - 2 - 2*num;
111         } else {
112                 return pattern_num
113                         + sizeof(pattern_num)/sizeof(pattern_t)
114                         - 2 - 2*num;
115         }
116 }
117
118 static pattern_t *pattern_select(unsigned char n)
119 {
120         switch(n) {
121         case 0: return pwmled_pattern_select();
122         case 1: return status_led_pattern_select();
123         default: return NULL;
124         }
125 }
126
127 void pattern_reload()
128 {
129         unsigned char i;
130
131         for (i = 0; i < N_LEDS; i++)
132                 led_set_pattern(i, pattern_select(i));
133 }
134
135 static void inline pattern_finished(unsigned char n)
136 {
137         led_patterns[n] = NULL;
138         led_set_pattern(n, pattern_select(n));
139 }
140
141 void patterns_next_tick()
142 {
143         unsigned char i;
144
145         for (i = 0; i < N_LEDS; i++) {
146                 if (!led_patterns[i]) {
147                         pattern_finished(i);
148                         continue;
149                 }
150
151                 if (--led_counters[i] == 0) {
152                         pattern_t *p = led_patterns[i];
153                         p++;
154                         if (p->duration == 0) { // END
155                                 /* Keep the last state, wait for others */
156                                 pattern_finished(i);
157                                 continue;
158                         }
159                         led_set_pattern(i, p);
160                 }
161
162         }
163 }
164