]> www.fi.muni.cz Git - tinyboard.git/blob - projects/step-up/control.c
WDT-based timing instead of ADC-based
[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 pattern_t on_pattern [] = {
7         { 1, 0x10 },
8         PATTERN_END
9 };
10
11 static pattern_t blink_pattern[] = {
12         { 1, 0x1 },
13         { 0, 0x2 },
14         { 1, 0x1 },
15         { 0, 0x8 },
16         { 1, 0x1 },
17         { 0, 0x8 },
18         PATTERN_END
19 };
20
21 static pattern_t slow_pattern[] = {
22         { 1, 0x1 },
23         { 0, 0x1F },
24         PATTERN_END
25 };
26
27 static unsigned char light_mode;
28 static union {
29         unsigned char errors;
30         struct {
31                 unsigned char shutdown_in_progress : 1;
32                 unsigned char pwmled_error : 1;
33                 unsigned char battery_low : 1;
34         };
35 } e;
36
37 void set_error(unsigned char err)
38 {
39         switch(err) {
40         case ERR_BATTERY:
41                 e.battery_low = 1;
42                 pwmled_set_target(0);
43                 pattern_reload();
44                 break;
45         case ERR_PWMLED:
46                 e.pwmled_error = 1;
47                 break;
48         }
49 }
50
51 void init_control()
52 {
53         light_mode = 1;
54         e.errors = 0;
55         short_press();
56 }
57
58 void long_press_start()
59 {
60         e.shutdown_in_progress = 1;
61         pattern_reload();
62 }
63
64 void short_press()
65 {
66         if (e.battery_low)
67                 return;
68
69         if (++light_mode >= 2*N_PWMLED_MODES)
70                 light_mode = 0;
71
72         pwmled_set_target(light_mode < N_PWMLED_MODES
73                 ? light_mode
74                 : light_mode - N_PWMLED_MODES);
75         pattern_reload();
76 }
77
78 void long_press()
79 {
80         power_down();
81 }
82
83 pattern_t *pwmled_pattern_select()
84 {
85         if (e.shutdown_in_progress)
86                 return NULL;
87
88         if (e.battery_low)
89                 return slow_pattern;
90
91         if (light_mode == 0) {
92                 return slow_pattern;
93         } else if (light_mode < N_PWMLED_MODES) {
94                 return blink_pattern;
95         } else {
96                 return on_pattern;
97         }
98 }
99
100 pattern_t *status_led_pattern_select()
101 {
102         if (e.shutdown_in_progress)
103                 return on_pattern;
104
105         if (e.pwmled_error)
106                 return number_pattern(3, 1);
107
108         if (e.battery_low)
109                 return number_pattern(1, 1);
110
111         return number_pattern(battery_gauge(), 0);
112 }
113
114 #if 0
115 void brake_on()
116 {
117         braking = 1;
118         gpio_set(0, 1);
119         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
120         led_set_pattern(0, pwmled0_pattern_select());
121 }
122
123 void brake_off()
124 {
125         braking = 0;
126         gpio_set(0, 0);
127         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
128         led_set_pattern(0, pwmled0_pattern_select());
129 }
130
131 #endif