2055701b7eb48f7c1a5b27b45d67a6909e7195c3
[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 #include <ao_adc_single.h>
26 #include <ao_pwm.h>
27
28 #define WIDTH   AO_ST7565_WIDTH
29 #define HEIGHT  AO_ST7565_HEIGHT
30 #define STRIDE  AO_BITMAP_STRIDE(WIDTH)
31
32 static uint32_t image[STRIDE * HEIGHT];
33
34 static struct ao_bitmap fb = {
35         .base = image,
36         .stride = STRIDE,
37         .width = WIDTH,
38         .height = HEIGHT,
39         .damage = AO_BOX_INIT,
40 };
41
42 static const struct ao_transform logo_transform = {
43         .x_scale = 48, .x_off = 2,
44         .y_scale = 48, .y_off = 0,
45 };
46
47 static const struct ao_transform show_transform = {
48         .x_scale = 36, .x_off = 100,
49         .y_scale = 36, .y_off = 0,
50 };
51
52 #define BIG_FONT BitstreamVeraSans_Roman_58_font
53 #define VOLT_FONT BitstreamVeraSans_Roman_58_font
54 #define SMALL_FONT BitstreamVeraSans_Roman_12_font
55 #define TINY_FONT BitstreamVeraSans_Roman_10_font
56 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
57
58 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
59 #define VALUE_Y         (int16_t) (LABEL_Y + BIG_FONT.ascent + 5)
60 #define BOX_X           2
61 #define PAD_X           90
62 #define BOX_LABEL_X     30
63 #define VOLT_LABEL_X    25
64 #define RSSI_LABEL_X    15
65 #define PAD_LABEL_X     95
66 #define SEP_X           (PAD_X - 8)
67 #define SCAN_X          (WIDTH - 100) / 2
68 #define SCAN_Y          50
69 #define SCAN_HEIGHT     3
70 #define FOUND_Y         63
71 #define FOUND_X         6
72 #define FOUND_WIDTH     (WIDTH - 6)
73 #define CONTRAST_LABEL_X        37
74 #define CONTRAST_WIDTH  100
75 #define CONTRAST_X      (WIDTH - CONTRAST_WIDTH) / 2
76 #define CONTRAST_Y      20
77 #define CONTRAST_HEIGHT 20
78 #define BACKLIGHT_LABEL_X       37
79 #define BACKLIGHT_WIDTH 100
80 #define BACKLIGHT_X     (WIDTH - BACKLIGHT_WIDTH) / 2
81 #define BACKLIGHT_Y     20
82 #define BACKLIGHT_HEIGHT        20
83 #define INFO_START_Y    ((int16_t) (SMALL_FONT.ascent + 2))
84 #define INFO_STEP_Y     ((int16_t) (SMALL_FONT.ascent + 3))
85
86 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
87 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
88
89 /* UI values */
90 static uint8_t  ao_lco_select_mode;
91 static uint8_t  ao_lco_event_debug;
92
93 #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)
94 #define AO_LCO_SELECT_PAD       0
95 #define AO_LCO_SELECT_BOX       1
96
97 static uint8_t  ao_lco_display_mutex;
98
99 static void
100 _ao_lco_show_pad(uint8_t pad)
101 {
102         char    str[5];
103
104         snprintf(str, sizeof(str), "%d", pad);
105         ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, AO_BLACK, AO_COPY);
106         ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", AO_BLACK, AO_COPY);
107 }
108
109 static void
110 _ao_lco_show_box(int16_t box)
111 {
112         char    str[7];
113
114         snprintf(str, sizeof(str), "%2d", box);
115         ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
116         ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", AO_BLACK, AO_COPY);
117 }
118
119 static void
120 _ao_lco_show_voltage(uint16_t decivolts, const char *label)
121 {
122         char    str[7];
123
124         PRINTD("voltage %d\n", decivolts);
125         snprintf(str, sizeof(str), "%2d.%d", decivolts / 10, decivolts % 10);
126         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
127         ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, label, AO_BLACK, AO_COPY);
128 }
129
130 static void
131 _ao_lco_batt_voltage(void)
132 {
133         struct ao_adc   packet;
134         int16_t         decivolt;
135
136         ao_adc_single_get(&packet);
137         decivolt = ao_battery_decivolt(packet.v_batt);
138         _ao_lco_show_voltage((uint16_t) decivolt, "LCO battery");
139         ao_st7565_update(&fb);
140 }
141
142 static void
143 _ao_lco_show_contrast(void)
144 {
145         uint8_t brightness = ao_st7565_get_brightness();
146         int16_t contrast = (int16_t) (brightness * CONTRAST_WIDTH / AO_LCO_MAX_CONTRAST);
147
148         ao_text(&fb, &SMALL_FONT, CONTRAST_LABEL_X, LABEL_Y, "Contrast", AO_BLACK, AO_COPY);
149         ao_rect(&fb, CONTRAST_X, CONTRAST_Y, contrast, CONTRAST_HEIGHT, AO_BLACK, AO_COPY);
150 }
151
152 static void
153 _ao_lco_show_backlight(void)
154 {
155         int32_t backlight = ao_lco_get_backlight();
156         int16_t value = (int16_t) (backlight * BACKLIGHT_WIDTH / AO_LCO_MAX_BACKLIGHT);
157
158         ao_text(&fb, &SMALL_FONT, BACKLIGHT_LABEL_X, LABEL_Y, "Backlight", AO_BLACK, AO_COPY);
159         ao_rect(&fb, BACKLIGHT_X, BACKLIGHT_Y, value, BACKLIGHT_HEIGHT, AO_BLACK, AO_COPY);
160 }
161
162 static int16_t info_y;
163
164 static void
165 _ao_lco_info(const char *format, ...)
166 {
167         va_list a;
168         char    buf[20];
169         va_start(a, format);
170         vsnprintf(buf, sizeof(buf), format, a);
171         va_end(a);
172         ao_text(&fb, &SMALL_FONT, 0, info_y, buf, AO_BLACK, AO_COPY);
173         info_y += INFO_STEP_Y;
174 }
175
176 static void
177 _ao_lco_show_info(void)
178 {
179         info_y = INFO_START_Y;
180         ao_logo_poly(&fb, &show_transform, AO_BLACK, AO_COPY);
181         _ao_lco_info("%s", ao_product);
182         _ao_lco_info("Version: %s", ao_version);
183         _ao_lco_info("Serial: %d", ao_serial_number);
184         _ao_lco_info("Callsign: %s", ao_config.callsign);
185         _ao_lco_info("Frequency: %ld.%03d",
186                      ao_config.frequency / 1000,
187                      (int) (ao_config.frequency % 1000));
188 }
189
190 void
191 ao_lco_show(void)
192 {
193         ao_mutex_get(&ao_lco_display_mutex);
194         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
195         switch (ao_lco_box) {
196         case AO_LCO_LCO_VOLTAGE:
197                 _ao_lco_batt_voltage();
198                 break;
199         case AO_LCO_CONTRAST:
200                 _ao_lco_show_contrast();
201                 break;
202         case AO_LCO_BACKLIGHT:
203                 _ao_lco_show_backlight();
204                 break;
205         case AO_LCO_INFO:
206                 _ao_lco_show_info();
207                 break;
208         default:
209                 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
210                         _ao_lco_show_voltage(ao_pad_query.battery, "Pad battery");
211                 } else {
212                         _ao_lco_show_pad(ao_lco_pad);
213                         _ao_lco_show_box(ao_lco_box);
214                         ao_rect(&fb, SEP_X, 0, 2, HEIGHT, AO_BLACK, AO_COPY);
215                 }
216                 break;
217         }
218         ao_st7565_update(&fb);
219         ao_mutex_put(&ao_lco_display_mutex);
220 }
221
222 static void
223 ao_lco_set_select(void)
224 {
225         if (ao_lco_armed) {
226                 ao_led_off(AO_LED_PAD);
227                 ao_led_off(AO_LED_BOX);
228         } else {
229                 switch (ao_lco_select_mode) {
230                 case AO_LCO_SELECT_PAD:
231                         ao_led_off(AO_LED_BOX);
232                         ao_led_on(AO_LED_PAD);
233                         break;
234                 case AO_LCO_SELECT_BOX:
235                         ao_led_off(AO_LED_PAD);
236                         ao_led_on(AO_LED_BOX);
237                         break;
238                 default:
239                         break;
240                 }
241         }
242 }
243
244
245 void
246 ao_lco_set_contrast(int32_t contrast)
247 {
248         ao_st7565_set_brightness((uint8_t) contrast);
249 }
250
251 int32_t
252 ao_lco_get_contrast(void)
253 {
254         return (int32_t) ao_st7565_get_brightness();
255 }
256
257 static uint16_t ao_backlight;
258
259 void
260 ao_lco_set_backlight(int32_t backlight)
261 {
262         ao_backlight = (uint16_t) backlight;
263         ao_pwm_set(AO_LCD_BL_PWM_CHAN, ao_backlight);
264 }
265
266 int32_t
267 ao_lco_get_backlight(void)
268 {
269         return (int32_t) ao_backlight;
270 }
271
272 static struct ao_task   ao_lco_drag_task;
273
274 static void
275 ao_lco_drag_monitor(void)
276 {
277         AO_TICK_TYPE    delay = ~0UL;
278         AO_TICK_TYPE    now;
279
280         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
281         for (;;) {
282                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
283                 if (delay == (AO_TICK_TYPE) ~0)
284                         ao_sleep(&ao_lco_drag_beep_count);
285                 else
286                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
287
288                 delay = ~0UL;
289                 now = ao_time();
290                 delay = ao_lco_drag_warn_check(now, delay);
291                 delay = ao_lco_drag_beep_check(now, delay);
292         }
293 }
294
295 static void
296 ao_lco_input(void)
297 {
298         static struct ao_event  event;
299
300         for (;;) {
301                 ao_event_get(&event);
302                 PRINTE("event type %d unit %d value %ld\n",
303                        event.type, event.unit, (long) event.value);
304                 switch (event.type) {
305                 case AO_EVENT_QUADRATURE:
306                         switch (event.unit) {
307                         case AO_QUADRATURE_SELECT:
308                                 if (!ao_lco_armed) {
309                                         switch (ao_lco_select_mode) {
310                                         case AO_LCO_SELECT_PAD:
311                                                 ao_lco_step_pad((int8_t) event.value);
312                                                 break;
313                                         case AO_LCO_SELECT_BOX:
314                                                 ao_lco_step_box((int8_t) event.value);
315                                                 break;
316                                         default:
317                                                 break;
318                                         }
319                                 }
320                                 break;
321                         }
322                         break;
323                 case AO_EVENT_BUTTON:
324                         switch (event.unit) {
325                         case AO_BUTTON_ARM:
326                                 ao_lco_set_armed((uint8_t) event.value);
327                                 ao_lco_set_select();
328                                 break;
329                         case AO_BUTTON_FIRE:
330                                 if (ao_lco_armed)
331                                         ao_lco_set_firing((uint8_t) event.value);
332                                 break;
333                         case AO_BUTTON_DRAG_SELECT:
334                                 if (event.value)
335                                         ao_lco_toggle_drag();
336                                 break;
337                         case AO_BUTTON_DRAG_MODE:
338                                 if (event.value)
339                                         ao_lco_drag_enable();
340                                 else
341                                         ao_lco_drag_disable();
342                                 break;
343                         case AO_BUTTON_ENCODER_SELECT:
344                                 if (event.value) {
345                                         if (!ao_lco_armed) {
346                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
347                                                 ao_lco_set_select();
348                                         }
349                                 }
350                                 break;
351                         }
352                         break;
353                 }
354         }
355 }
356
357 /*
358  * Light up everything for a second at power on to let the user
359  * visually inspect the system for correct operation
360  */
361 static void
362 ao_lco_display_test(void)
363 {
364         ao_led_on(AO_LEDS_AVAILABLE);
365         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_BLACK, AO_COPY);
366         ao_st7565_update(&fb);
367         ao_delay(AO_MS_TO_TICKS(250));
368         ao_led_off(AO_LEDS_AVAILABLE);
369 }
370
371 static struct ao_task ao_lco_input_task;
372 static struct ao_task ao_lco_monitor_task;
373 static struct ao_task ao_lco_arm_warn_task;
374 static struct ao_task ao_lco_igniter_status_task;
375
376 static int16_t  found_x;
377
378 void
379 ao_lco_search_start(void)
380 {
381         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
382         ao_logo(&fb, &logo_transform, &LOGO_FONT, AO_BLACK, AO_COPY);
383         found_x = FOUND_X;
384 }
385
386 void
387 ao_lco_search_box_check(int16_t box)
388 {
389         if (box > 0)
390                 ao_rect(&fb, SCAN_X, SCAN_Y, box, SCAN_HEIGHT, AO_BLACK, AO_COPY);
391         ao_st7565_update(&fb);
392 }
393
394 void
395 ao_lco_search_box_present(int16_t box)
396 {
397         char    str[8];
398         if (found_x < FOUND_WIDTH)
399         {
400                 snprintf(str, sizeof(str), "%s%02u", found_x ? ", " : "", box);
401                 found_x = ao_text(&fb, &TINY_FONT, found_x, FOUND_Y, str, AO_BLACK, AO_COPY);
402         }
403 }
404
405 void
406 ao_lco_search_done(void)
407 {
408         ao_st7565_update(&fb);
409 }
410
411 static void
412 ao_lco_main(void)
413 {
414         ao_lco_display_test();
415         ao_lco_search();
416         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
417         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
418         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
419         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
420         ao_lco_monitor();
421 }
422
423 #if DEBUG
424 static void
425 ao_lco_set_debug(void)
426 {
427         uint32_t r = ao_cmd_decimal();
428         if (ao_cmd_status == ao_cmd_success){
429                 ao_lco_debug = r & 1;
430                 ao_lco_event_debug = (r & 2) >> 1;
431         }
432 }
433
434 const struct ao_cmds ao_lco_cmds[] = {
435         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
436         { ao_lco_search,        "s\0Search for pad boxes" },
437         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
438         { 0, NULL }
439 };
440 #endif
441
442 void
443 ao_lco_init(void)
444 {
445         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
446 #if DEBUG
447         ao_cmd_register(&ao_lco_cmds[0]);
448 #endif
449 }