altos: Add initial TeleLCO-v3 bits
[fw/altos] / src / telelco-v3.0 / ao_lco_v3.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_quadrature.h>
23 #include <ao_radio_cmac.h>
24
25 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
26 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
27
28 /* UI values */
29 static uint8_t  ao_lco_select_mode;
30 static uint8_t  ao_lco_event_debug;
31
32 #define PRINTE(...) do { if (!ao_lco_debug && !ao_lco_event_debug) break; printf ("\r%5lu %s: ", (unsigned long) ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
33 #define AO_LCO_SELECT_PAD       0
34 #define AO_LCO_SELECT_BOX       1
35
36 static uint8_t  ao_lco_display_mutex;
37
38 void
39 ao_lco_show_pad(uint8_t pad)
40 {
41         ao_mutex_get(&ao_lco_display_mutex);
42         (void) pad;
43 //      ao_seven_segment_set(AO_LCO_PAD_DIGIT, (uint8_t) (pad | (ao_lco_drag_race << 4)));
44         ao_mutex_put(&ao_lco_display_mutex);
45 }
46 void
47 ao_lco_show_box(uint16_t box)
48 {
49         ao_mutex_get(&ao_lco_display_mutex);
50         (void) box;
51 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, (uint8_t) (box % 10 | (ao_lco_drag_race << 4)));
52 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, (uint8_t) (box / 10 | (ao_lco_drag_race << 4)));
53         ao_mutex_put(&ao_lco_display_mutex);
54 }
55
56 static void
57 ao_lco_show_voltage(uint16_t decivolts)
58 {
59         uint8_t tens, ones, tenths;
60
61         PRINTD("voltage %d\n", decivolts);
62         tenths = (uint8_t) (decivolts % 10);
63         ones = (uint8_t) ((decivolts / 10) % 10);
64         tens = (uint8_t) ((decivolts / 100) % 10);
65         ao_mutex_get(&ao_lco_display_mutex);
66         (void) tenths;
67         (void) ones;
68         (void) tens;
69 //      ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
70 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
71 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
72         ao_mutex_put(&ao_lco_display_mutex);
73 }
74
75 void
76 ao_lco_show(void)
77 {
78         if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
79                 ao_lco_show_voltage(ao_pad_query.battery);
80         } else {
81                 ao_lco_show_pad(ao_lco_pad);
82                 ao_lco_show_box(ao_lco_box);
83         }
84 }
85
86 uint8_t
87 ao_lco_box_present(uint16_t box)
88 {
89         if (box >= AO_PAD_MAX_BOXES)
90                 return 0;
91         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
92 }
93
94 static void
95 ao_lco_set_select(void)
96 {
97         if (ao_lco_armed) {
98                 ao_led_off(AO_LED_PAD);
99                 ao_led_off(AO_LED_BOX);
100         } else {
101                 switch (ao_lco_select_mode) {
102                 case AO_LCO_SELECT_PAD:
103                         ao_led_off(AO_LED_BOX);
104                         ao_led_on(AO_LED_PAD);
105                         break;
106                 case AO_LCO_SELECT_BOX:
107                         ao_led_off(AO_LED_PAD);
108                         ao_led_on(AO_LED_BOX);
109                         break;
110                 default:
111                         break;
112                 }
113         }
114 }
115
116 static void
117 ao_lco_step_box(int8_t dir)
118 {
119         int32_t new_box = (int32_t) ao_lco_box;
120
121         do {
122                 new_box += dir;
123                 if (new_box > ao_lco_max_box)
124                         new_box = ao_lco_min_box;
125                 else if (new_box < ao_lco_min_box)
126                         new_box = ao_lco_max_box;
127                 if (new_box == ao_lco_box)
128                         break;
129         } while (!ao_lco_box_present((uint16_t) new_box));
130         ao_lco_set_box((uint16_t) new_box);
131 }
132
133 static struct ao_task   ao_lco_drag_task;
134
135 static void
136 ao_lco_drag_monitor(void)
137 {
138         AO_TICK_TYPE    delay = ~0UL;
139         AO_TICK_TYPE    now;
140
141         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
142         for (;;) {
143                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
144                 if (delay == (AO_TICK_TYPE) ~0)
145                         ao_sleep(&ao_lco_drag_beep_count);
146                 else
147                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
148
149                 delay = ~0UL;
150                 now = ao_time();
151                 delay = ao_lco_drag_warn_check(now, delay);
152                 delay = ao_lco_drag_beep_check(now, delay);
153         }
154 }
155
156 static void
157 ao_lco_input(void)
158 {
159         static struct ao_event  event;
160
161         for (;;) {
162                 ao_event_get(&event);
163                 PRINTE("event type %d unit %d value %ld\n",
164                        event.type, event.unit, (long) event.value);
165                 switch (event.type) {
166                 case AO_EVENT_QUADRATURE:
167                         switch (event.unit) {
168                         case AO_QUADRATURE_SELECT:
169                                 if (!ao_lco_armed) {
170                                         switch (ao_lco_select_mode) {
171                                         case AO_LCO_SELECT_PAD:
172                                                 ao_lco_step_pad((int8_t) event.value);
173                                                 break;
174                                         case AO_LCO_SELECT_BOX:
175                                                 ao_lco_step_box((int8_t) event.value);
176                                                 break;
177                                         default:
178                                                 break;
179                                         }
180                                 }
181                                 break;
182                         }
183                         break;
184                 case AO_EVENT_BUTTON:
185                         switch (event.unit) {
186                         case AO_BUTTON_ARM:
187                                 ao_lco_set_armed((uint8_t) event.value);
188                                 ao_lco_set_select();
189                                 break;
190                         case AO_BUTTON_FIRE:
191                                 if (ao_lco_armed)
192                                         ao_lco_set_firing((uint8_t) event.value);
193                                 break;
194                         case AO_BUTTON_DRAG_SELECT:
195                                 if (event.value)
196                                         ao_lco_toggle_drag();
197                                 break;
198                         case AO_BUTTON_DRAG_MODE:
199                                 if (event.value)
200                                         ao_lco_drag_enable();
201                                 else
202                                         ao_lco_drag_disable();
203                                 break;
204                         case AO_BUTTON_ENCODER_SELECT:
205                                 if (event.value) {
206                                         if (!ao_lco_armed) {
207                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
208                                                 ao_lco_set_select();
209                                         }
210                                 }
211                                 break;
212                         }
213                         break;
214                 }
215         }
216 }
217
218 /*
219  * Light up everything for a second at power on to let the user
220  * visually inspect the system for correct operation
221  */
222 static void
223 ao_lco_display_test(void)
224 {
225         ao_mutex_get(&ao_lco_display_mutex);
226 //      ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
227 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
228 //      ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
229         ao_mutex_put(&ao_lco_display_mutex);
230         ao_led_on(AO_LEDS_AVAILABLE);
231         ao_delay(AO_MS_TO_TICKS(1000));
232         ao_led_off(AO_LEDS_AVAILABLE);
233 }
234
235 static void
236 ao_lco_batt_voltage(void)
237 {
238         struct ao_adc   packet;
239         int16_t         decivolt;
240
241 //      ao_adc_single_get(&packet);
242         packet.v_batt = 0;
243         decivolt = ao_battery_decivolt(packet.v_batt);
244         ao_lco_show_voltage((uint16_t) decivolt);
245         ao_delay(AO_MS_TO_TICKS(1000));
246 }
247
248 static struct ao_task ao_lco_input_task;
249 static struct ao_task ao_lco_monitor_task;
250 static struct ao_task ao_lco_arm_warn_task;
251 static struct ao_task ao_lco_igniter_status_task;
252
253 static void
254 ao_lco_main(void)
255 {
256         ao_lco_display_test();
257         ao_lco_batt_voltage();
258         ao_lco_search();
259         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
260         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
261         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
262         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
263         ao_lco_monitor();
264 }
265
266 #if DEBUG
267 static void
268 ao_lco_set_debug(void)
269 {
270         uint32_t r = ao_cmd_decimal();
271         if (ao_cmd_status == ao_cmd_success){
272                 ao_lco_debug = r & 1;
273                 ao_lco_event_debug = (r & 2) >> 1;
274         }
275 }
276
277 const struct ao_cmds ao_lco_cmds[] = {
278         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
279         { ao_lco_search,        "s\0Search for pad boxes" },
280         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
281         { 0, NULL }
282 };
283 #endif
284
285 void
286 ao_lco_init(void)
287 {
288         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
289 #if DEBUG
290         ao_cmd_register(&ao_lco_cmds[0]);
291 #endif
292 }