9fefde3bead63f3ed84f427d331d75990a63306b
[fw/altos] / src / telelco-v2.0 / ao_lco_v2.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_lco.h>
21 #include <ao_event.h>
22 #include <ao_seven_segment.h>
23 #include <ao_quadrature.h>
24 #include <ao_radio_cmac.h>
25 #include <ao_adc_single.h>
26
27 #define AO_LCO_PAD_DIGIT        0
28 #define AO_LCO_BOX_DIGIT_1      1
29 #define AO_LCO_BOX_DIGIT_10     2
30
31 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
32 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
33
34 /* UI values */
35 static uint8_t  ao_lco_select_mode;
36 #define AO_LCO_SELECT_PAD       0
37 #define AO_LCO_SELECT_BOX       1
38
39 static uint8_t  ao_lco_display_mutex;
40
41 void
42 ao_lco_set_pad(uint8_t pad)
43 {
44         ao_mutex_get(&ao_lco_display_mutex);
45         ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
46         ao_mutex_put(&ao_lco_display_mutex);
47 }
48
49 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
50                                  (0 << 1) |     \
51                                  (1 << 2) |     \
52                                  (1 << 3) |     \
53                                  (1 << 4) |     \
54                                  (1 << 5) |     \
55                                  (1 << 6))
56
57
58 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
59                                  (0 << 1) |     \
60                                  (0 << 2) |     \
61                                  (1 << 3) |     \
62                                  (1 << 4) |     \
63                                  (0 << 5) |     \
64                                  (0 << 6))
65
66 void
67 ao_lco_set_box(uint16_t box)
68 {
69         ao_mutex_get(&ao_lco_display_mutex);
70         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
71         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
72         ao_mutex_put(&ao_lco_display_mutex);
73 }
74
75 void
76 ao_lco_set_voltage(uint16_t decivolts)
77 {
78         uint8_t tens, ones, tenths;
79
80         PRINTD("voltage %d\n", decivolts);
81         tenths = decivolts % 10;
82         ones = (decivolts / 10) % 10;
83         tens = (decivolts / 100) % 10;
84         ao_mutex_get(&ao_lco_display_mutex);
85         ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
86         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
87         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
88         ao_mutex_put(&ao_lco_display_mutex);
89 }
90
91 void
92 ao_lco_set_display(void)
93 {
94         if (ao_lco_pad == 0) {
95                 ao_lco_set_voltage(ao_pad_query.battery);
96         } else {
97                 ao_lco_set_pad(ao_lco_pad);
98                 ao_lco_set_box(ao_lco_box);
99         }
100 }
101
102 uint8_t
103 ao_lco_box_present(uint16_t box)
104 {
105         if (box >= AO_PAD_MAX_BOXES)
106                 return 0;
107         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
108 }
109
110 static void
111 ao_lco_set_select(void)
112 {
113         if (ao_lco_armed) {
114                 ao_led_off(AO_LED_PAD);
115                 ao_led_off(AO_LED_BOX);
116         } else {
117                 switch (ao_lco_select_mode) {
118                 case AO_LCO_SELECT_PAD:
119                         ao_led_off(AO_LED_BOX);
120                         ao_led_on(AO_LED_PAD);
121                         break;
122                 case AO_LCO_SELECT_BOX:
123                         ao_led_off(AO_LED_PAD);
124                         ao_led_on(AO_LED_BOX);
125                         break;
126                 default:
127                         break;
128                 }
129         }
130 }
131
132 static void
133 ao_lco_step_box(int8_t dir)
134 {
135         int16_t new_box = ao_lco_box;
136
137         do {
138                 new_box += dir;
139                 if (new_box > ao_lco_max_box)
140                         new_box = ao_lco_min_box;
141                 else if (new_box < ao_lco_min_box)
142                         new_box = ao_lco_max_box;
143                 if (new_box == ao_lco_box)
144                         break;
145         } while (!ao_lco_box_present(new_box));
146         if (ao_lco_box != new_box) {
147                 ao_lco_box = new_box;
148                 ao_lco_pad = 1;
149                 ao_lco_channels[ao_lco_box] = 0;
150                 ao_lco_set_display();
151         }
152 }
153
154 static struct ao_task   ao_lco_drag_task;
155
156 static void
157 ao_lco_drag_monitor(void)
158 {
159         uint16_t        delay = ~0;
160         uint16_t        now;
161
162         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
163         for (;;) {
164                 PRINTD("Drag monitor count %d delay %d\n", ao_lco_drag_beep_count, delay);
165                 if (delay == (uint16_t) ~0)
166                         ao_sleep(&ao_lco_drag_beep_count);
167                 else
168                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
169
170                 delay = ~0;
171                 now = ao_time();
172                 delay = ao_lco_drag_warn_check(now, delay);
173                 delay = ao_lco_drag_beep_check(now, delay);
174         }
175 }
176
177 static void
178 ao_lco_input(void)
179 {
180         static struct ao_event  event;
181
182         for (;;) {
183                 ao_event_get(&event);
184                 PRINTD("event type %d unit %d value %d\n",
185                        event.type, event.unit, event.value);
186                 switch (event.type) {
187                 case AO_EVENT_QUADRATURE:
188                         switch (event.unit) {
189                         case AO_QUADRATURE_SELECT:
190                                 if (!ao_lco_armed) {
191                                         switch (ao_lco_select_mode) {
192                                         case AO_LCO_SELECT_PAD:
193                                                 ao_lco_step_pad((int8_t) event.value);
194                                                 break;
195                                         case AO_LCO_SELECT_BOX:
196                                                 ao_lco_step_box((int8_t) event.value);
197                                                 break;
198                                         default:
199                                                 break;
200                                         }
201                                 }
202                                 break;
203                         }
204                         break;
205                 case AO_EVENT_BUTTON:
206                         switch (event.unit) {
207                         case AO_BUTTON_ARM:
208                                 ao_lco_set_armed(event.value);
209                                 ao_lco_set_select();
210                                 break;
211                         case AO_BUTTON_FIRE:
212                                 if (ao_lco_armed)
213                                         ao_lco_set_firing(event.value);
214                                 break;
215                         case AO_BUTTON_DRAG_SELECT:
216                                 if (event.value && ao_lco_drag_race) {
217                                         if (ao_lco_pad != 0) {
218                                                 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
219                                                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
220                                                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
221                                                 ao_lco_drag_add_beeps(ao_lco_pad);
222                                         }
223                                 }
224                                 break;
225                         case AO_BUTTON_DRAG_MODE:
226                                 if (event.value)
227                                         ao_lco_drag_enable();
228                                 else
229                                         ao_lco_drag_disable();
230                                 break;
231                         case AO_BUTTON_ENCODER_SELECT:
232                                 if (event.value) {
233                                         if (!ao_lco_armed) {
234                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
235                                                 ao_lco_set_select();
236                                         }
237                                 }
238                                 break;
239                         }
240                         break;
241                 }
242         }
243 }
244
245 /*
246  * Light up everything for a second at power on to let the user
247  * visually inspect the system for correct operation
248  */
249 static void
250 ao_lco_display_test()
251 {
252         ao_mutex_get(&ao_lco_display_mutex);
253         ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
254         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
255         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
256         ao_mutex_put(&ao_lco_display_mutex);
257         ao_led_on(LEDS_AVAILABLE);
258         ao_delay(AO_MS_TO_TICKS(1000));
259         ao_led_off(LEDS_AVAILABLE);
260 }
261
262 static void
263 ao_lco_batt_voltage(void)
264 {
265         struct ao_adc   packet;
266         int16_t         decivolt;
267
268         ao_adc_single_get(&packet);
269         decivolt = ao_battery_decivolt(packet.v_batt);
270         ao_lco_set_voltage(decivolt);
271         ao_delay(AO_MS_TO_TICKS(1000));
272 }
273
274 static struct ao_task ao_lco_input_task;
275 static struct ao_task ao_lco_monitor_task;
276 static struct ao_task ao_lco_arm_warn_task;
277 static struct ao_task ao_lco_igniter_status_task;
278
279 static void
280 ao_lco_main(void)
281 {
282         ao_lco_display_test();
283         ao_lco_batt_voltage();
284         ao_lco_search();
285         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
286         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
287         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
288         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
289         ao_lco_monitor();
290 }
291
292 #if DEBUG
293 void
294 ao_lco_set_debug(void)
295 {
296         ao_cmd_decimal();
297         if (ao_cmd_status == ao_cmd_success)
298                 ao_lco_debug = ao_cmd_lex_i != 0;
299 }
300
301 __code struct ao_cmds ao_lco_cmds[] = {
302         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
303         { ao_lco_search,        "s\0Search for pad boxes" },
304         { 0, NULL }
305 };
306 #endif
307
308 void
309 ao_lco_init(void)
310 {
311         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
312 #if DEBUG
313         ao_cmd_register(&ao_lco_cmds[0]);
314 #endif
315 }