altos/telelco-v3.0: Add some real drawing stuff
[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 #include <ao_st7565.h>
25
26 #define WIDTH   AO_ST7565_WIDTH
27 #define HEIGHT  AO_ST7565_HEIGHT
28 #define STRIDE  AO_BITMAP_STRIDE(WIDTH)
29
30 static uint32_t image[STRIDE * HEIGHT];
31
32 static struct ao_bitmap fb = {
33         .base = image,
34         .stride = STRIDE,
35         .width = WIDTH,
36         .height = HEIGHT,
37         .damage = AO_BOX_INIT,
38 };
39
40 static const struct ao_transform logo_transform = {
41         .x_scale = 48, .x_off = 2,
42         .y_scale = 48, .y_off = 0,
43 };
44
45 #define BIG_FONT BitstreamVeraSans_Roman_58_font
46 #define VOLT_FONT BitstreamVeraSans_Roman_58_font
47 #define SMALL_FONT BitstreamVeraSans_Roman_12_font
48 #define TINY_FONT BitstreamVeraSans_Roman_10_font
49 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
50
51 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
52 #define VALUE_Y         (int16_t) (LABEL_Y + BIG_FONT.ascent + 5)
53 #define BOX_X           2
54 #define PAD_X           90
55 #define BOX_LABEL_X     30
56 #define VOLT_LABEL_X    25
57 #define RSSI_LABEL_X    15
58 #define PAD_LABEL_X     95
59 #define SEP_X           (PAD_X - 8)
60 #define SCAN_X          (WIDTH - 100) / 2
61 #define SCAN_Y          50
62 #define SCAN_HEIGHT     3
63 #define FOUND_Y         63
64 #define FOUND_X         6
65 #define FOUND_WIDTH     17
66 #define MAX_VALID       (WIDTH / FOUND_WIDTH)
67
68 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
69 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
70
71 /* UI values */
72 static uint8_t  ao_lco_select_mode;
73 static uint8_t  ao_lco_event_debug;
74
75 #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)
76 #define AO_LCO_SELECT_PAD       0
77 #define AO_LCO_SELECT_BOX       1
78
79 static uint8_t  ao_lco_display_mutex;
80
81 void
82 ao_lco_show_pad(uint8_t pad)
83 {
84         char    str[5];
85
86         ao_mutex_get(&ao_lco_display_mutex);
87         snprintf(str, sizeof(str), "%d", pad);
88         ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, AO_BLACK, AO_COPY);
89         ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", AO_BLACK, AO_COPY);
90         ao_rect(&fb, SEP_X, 0, 2, HEIGHT, AO_BLACK, AO_COPY);
91         ao_mutex_put(&ao_lco_display_mutex);
92 }
93 void
94 ao_lco_show_box(uint16_t box)
95 {
96         char    str[7];
97
98         ao_mutex_get(&ao_lco_display_mutex);
99         snprintf(str, sizeof(str), "%2d", box);
100         ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
101         ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", AO_BLACK, AO_COPY);
102         ao_mutex_put(&ao_lco_display_mutex);
103 }
104
105 static void
106 ao_lco_show_voltage(uint16_t decivolts, const char *label)
107 {
108         char    str[7];
109
110         PRINTD("voltage %d\n", decivolts);
111         snprintf(str, sizeof(str), "%2d.%d", decivolts / 10, decivolts % 10);
112         ao_mutex_get(&ao_lco_display_mutex);
113         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
114         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
115         ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, label, AO_BLACK, AO_COPY);
116         ao_mutex_put(&ao_lco_display_mutex);
117 }
118
119 void
120 ao_lco_show(void)
121 {
122         if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
123                 ao_lco_show_voltage(ao_pad_query.battery, "Pad battery");
124         } else {
125                 ao_lco_show_pad(ao_lco_pad);
126                 ao_lco_show_box(ao_lco_box);
127         }
128 }
129
130 uint8_t
131 ao_lco_box_present(uint16_t box)
132 {
133         if (box >= AO_PAD_MAX_BOXES)
134                 return 0;
135         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
136 }
137
138 static void
139 ao_lco_set_select(void)
140 {
141         if (ao_lco_armed) {
142                 ao_led_off(AO_LED_PAD);
143                 ao_led_off(AO_LED_BOX);
144         } else {
145                 switch (ao_lco_select_mode) {
146                 case AO_LCO_SELECT_PAD:
147                         ao_led_off(AO_LED_BOX);
148                         ao_led_on(AO_LED_PAD);
149                         break;
150                 case AO_LCO_SELECT_BOX:
151                         ao_led_off(AO_LED_PAD);
152                         ao_led_on(AO_LED_BOX);
153                         break;
154                 default:
155                         break;
156                 }
157         }
158 }
159
160 static void
161 ao_lco_step_box(int8_t dir)
162 {
163         int32_t new_box = (int32_t) ao_lco_box;
164
165         do {
166                 new_box += dir;
167                 if (new_box > ao_lco_max_box)
168                         new_box = ao_lco_min_box;
169                 else if (new_box < ao_lco_min_box)
170                         new_box = ao_lco_max_box;
171                 if (new_box == ao_lco_box)
172                         break;
173         } while (!ao_lco_box_present((uint16_t) new_box));
174         ao_lco_set_box((uint16_t) new_box);
175 }
176
177 static struct ao_task   ao_lco_drag_task;
178
179 static void
180 ao_lco_drag_monitor(void)
181 {
182         AO_TICK_TYPE    delay = ~0UL;
183         AO_TICK_TYPE    now;
184
185         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
186         for (;;) {
187                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
188                 if (delay == (AO_TICK_TYPE) ~0)
189                         ao_sleep(&ao_lco_drag_beep_count);
190                 else
191                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
192
193                 delay = ~0UL;
194                 now = ao_time();
195                 delay = ao_lco_drag_warn_check(now, delay);
196                 delay = ao_lco_drag_beep_check(now, delay);
197         }
198 }
199
200 static void
201 ao_lco_input(void)
202 {
203         static struct ao_event  event;
204
205         for (;;) {
206                 ao_event_get(&event);
207                 PRINTE("event type %d unit %d value %ld\n",
208                        event.type, event.unit, (long) event.value);
209                 switch (event.type) {
210                 case AO_EVENT_QUADRATURE:
211                         switch (event.unit) {
212                         case AO_QUADRATURE_SELECT:
213                                 if (!ao_lco_armed) {
214                                         switch (ao_lco_select_mode) {
215                                         case AO_LCO_SELECT_PAD:
216                                                 ao_lco_step_pad((int8_t) event.value);
217                                                 break;
218                                         case AO_LCO_SELECT_BOX:
219                                                 ao_lco_step_box((int8_t) event.value);
220                                                 break;
221                                         default:
222                                                 break;
223                                         }
224                                 }
225                                 break;
226                         }
227                         break;
228                 case AO_EVENT_BUTTON:
229                         switch (event.unit) {
230                         case AO_BUTTON_ARM:
231                                 ao_lco_set_armed((uint8_t) event.value);
232                                 ao_lco_set_select();
233                                 break;
234                         case AO_BUTTON_FIRE:
235                                 if (ao_lco_armed)
236                                         ao_lco_set_firing((uint8_t) event.value);
237                                 break;
238                         case AO_BUTTON_DRAG_SELECT:
239                                 if (event.value)
240                                         ao_lco_toggle_drag();
241                                 break;
242                         case AO_BUTTON_DRAG_MODE:
243                                 if (event.value)
244                                         ao_lco_drag_enable();
245                                 else
246                                         ao_lco_drag_disable();
247                                 break;
248                         case AO_BUTTON_ENCODER_SELECT:
249                                 if (event.value) {
250                                         if (!ao_lco_armed) {
251                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
252                                                 ao_lco_set_select();
253                                         }
254                                 }
255                                 break;
256                         }
257                         break;
258                 }
259         }
260 }
261
262 /*
263  * Light up everything for a second at power on to let the user
264  * visually inspect the system for correct operation
265  */
266 static void
267 ao_lco_display_test(void)
268 {
269         ao_mutex_get(&ao_lco_display_mutex);
270         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
271         ao_logo(&fb, &logo_transform, &LOGO_FONT, AO_BLACK, AO_COPY);
272         ao_mutex_put(&ao_lco_display_mutex);
273         ao_led_on(AO_LEDS_AVAILABLE);
274         ao_delay(AO_MS_TO_TICKS(1000));
275         ao_led_off(AO_LEDS_AVAILABLE);
276 }
277
278 static void
279 ao_lco_batt_voltage(void)
280 {
281         struct ao_adc   packet;
282         int16_t         decivolt;
283
284 //      ao_adc_single_get(&packet);
285         packet.v_batt = 0;
286         decivolt = ao_battery_decivolt(packet.v_batt);
287         ao_lco_show_voltage((uint16_t) decivolt, "LCO battery");
288         ao_delay(AO_MS_TO_TICKS(1000));
289 }
290
291 static struct ao_task ao_lco_input_task;
292 static struct ao_task ao_lco_monitor_task;
293 static struct ao_task ao_lco_arm_warn_task;
294 static struct ao_task ao_lco_igniter_status_task;
295
296 static void
297 ao_lco_main(void)
298 {
299         ao_lco_display_test();
300         ao_lco_batt_voltage();
301         ao_lco_search();
302         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
303         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
304         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
305         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
306         ao_lco_monitor();
307 }
308
309 #if DEBUG
310 static void
311 ao_lco_set_debug(void)
312 {
313         uint32_t r = ao_cmd_decimal();
314         if (ao_cmd_status == ao_cmd_success){
315                 ao_lco_debug = r & 1;
316                 ao_lco_event_debug = (r & 2) >> 1;
317         }
318 }
319
320 const struct ao_cmds ao_lco_cmds[] = {
321         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
322         { ao_lco_search,        "s\0Search for pad boxes" },
323         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
324         { 0, NULL }
325 };
326 #endif
327
328 void
329 ao_lco_init(void)
330 {
331         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
332 #if DEBUG
333         ao_cmd_register(&ao_lco_cmds[0]);
334 #endif
335 }