]> www.fi.muni.cz Git - tinyboard.git/blob - projects/step-up/control.c
Buttons, status LED, control implemented.
[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, shutdown_in_progress;
28
29 void init_control()
30 {
31         light_mode = 1;
32         shutdown_in_progress = 0;
33         short_press();
34 }
35
36 void long_press_start()
37 {
38         shutdown_in_progress = 1;
39         pattern_reload();
40 }
41
42 void short_press()
43 {
44         if (++light_mode >= 2*N_PWMLED_MODES)
45                 light_mode = 0;
46
47         pwmled_set_target(light_mode < N_PWMLED_MODES
48                 ? light_mode
49                 : light_mode - N_PWMLED_MODES);
50         pattern_reload();
51 }
52
53 void long_press()
54 {
55         shutdown_in_progress = 0;
56         pattern_reload();
57 }
58
59 pattern_t *pwmled_pattern_select()
60 {
61         if (shutdown_in_progress)
62                 return NULL;
63
64         if (light_mode == 0) {
65                 return slow_pattern;
66         } else if (light_mode < N_PWMLED_MODES) {
67                 return blink_pattern;
68         } else {
69                 return on_pattern;
70         }
71 }
72
73 pattern_t *status_led_pattern_select()
74 {
75         if (shutdown_in_progress)
76                 return on_pattern;
77
78         return number_pattern(light_mode+1, 0);
79 }
80
81 #if 0
82 void brake_on()
83 {
84         braking = 1;
85         gpio_set(0, 1);
86         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
87         led_set_pattern(0, pwmled0_pattern_select());
88 }
89
90 void brake_off()
91 {
92         braking = 0;
93         gpio_set(0, 0);
94         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
95         led_set_pattern(0, pwmled0_pattern_select());
96 }
97
98 #endif