rgb-led-string: Christmas tree mod after real-world testing
[tinyboard.git] / projects / step-up / control.c
1 #include <inttypes.h>
2 #include <stdlib.h> // for NULL
3
4 #include "lights.h"
5
6 static unsigned char on_pattern[] = {
7         /* 8 bits */
8         0b11111111,
9 };
10
11 #define IVA
12
13 static unsigned char blink_pattern[] = {
14 #ifdef IVA
15         /* ../...-/..-/.../-.-/.-// */
16         /* 137 bits, 19.7% on */
17         0b10000100,
18         0b00000000,
19         0b10000100,
20         0b00100001,
21         0b11000000,
22         0b00001000,
23         0b01000011,
24         0b10000000,
25         0b00010000,
26         0b10000100,
27         0b00000000,
28         0b11100001,
29         0b00001110,
30         0b00000000,
31         0b01000011,
32         0b10000000,
33         0b00000000,
34         0b00000000,
35 #else /* FILIP */
36         /* ..-./../.-../../.--.// */
37         /* 124 bits, 19.4% on */
38         0b10000100,
39         0b00111000,
40         0b01000000,
41         0b00001000,
42         0b01000000,
43         0b00001000,
44         0b01110000,
45         0b10000100,
46         0b00000000,
47         0b10000100,
48         0b00000000,
49         0b10000111,
50         0b00001110,
51         0b00010000,
52         0b00000000,
53         0b00000000,
54 #endif
55 };
56
57 static unsigned char slow_pattern[] = {
58         /* 24 bits */
59         0b00010000,
60         0b00000000,
61         0b00000000,
62 };
63
64 static unsigned char light_mode;
65 static union {
66         unsigned char errors;
67         struct {
68                 unsigned char shutdown_in_progress : 1;
69                 unsigned char pwmled_error : 1;
70                 unsigned char battery_low : 1;
71         };
72 } e;
73
74 void set_error(unsigned char err)
75 {
76         switch(err) {
77         case ERR_BATTERY:
78                 e.battery_low = 1;
79                 pwmled_set_target(0);
80                 pattern_reload();
81                 break;
82         case ERR_PWMLED:
83                 e.pwmled_error = 1;
84                 break;
85         }
86 }
87
88 void init_control()
89 {
90         light_mode = 1;
91         e.errors = 0;
92         short_press();
93 }
94
95 void long_press_start()
96 {
97         e.shutdown_in_progress = 1;
98         pattern_reload();
99 }
100
101 void short_press()
102 {
103         if (e.battery_low)
104                 return;
105
106         if (++light_mode >= 2*N_PWMLED_MODES)
107                 light_mode = 0;
108
109         pwmled_set_target(light_mode < N_PWMLED_MODES
110                 ? light_mode
111                 : light_mode - N_PWMLED_MODES);
112         pattern_reload();
113 }
114
115 void long_press()
116 {
117         power_down();
118 }
119
120 void pwmled_pattern_select(unsigned char led)
121 {
122         if (e.shutdown_in_progress) {
123                 led_set_pattern(led, 0, 0, NULL);
124         } else if (e.battery_low) {
125                 led_set_pattern(led, 24, 0, slow_pattern);
126         } else if (light_mode == 0) {
127                 led_set_pattern(led, 24, 0, slow_pattern);
128         } else if (light_mode < N_PWMLED_MODES) {
129 #ifdef IVA
130                 led_set_pattern(led, 137, 0, blink_pattern);
131 #else /* FILIP */
132                 led_set_pattern(led, 124, 0, blink_pattern);
133 #endif
134         } else {
135                 led_set_pattern(led, 8, 0, on_pattern);
136         }
137 }
138
139 void status_led_pattern_select(unsigned char led)
140 {
141         if (e.shutdown_in_progress) {
142                 led_set_pattern(led, 8, 0, on_pattern);
143         } else if (e.pwmled_error) {
144                 led_set_number_pattern(led, 1, 1);
145         } else if (e.battery_low) {
146                 led_set_number_pattern(led, 1, 1);
147         } else {
148                 led_set_number_pattern(led, battery_gauge(), 0);
149         }
150 }
151
152 #if 0
153 void brake_on()
154 {
155         braking = 1;
156         gpio_set(0, 1);
157         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
158         led_set_pattern(0, pwmled0_pattern_select());
159 }
160
161 void brake_off()
162 {
163         braking = 0;
164         gpio_set(0, 0);
165         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
166         led_set_pattern(0, pwmled0_pattern_select());
167 }
168
169 #endif