altos/telelco-v3.0: Merge info into one screen
[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 = 40, .x_off = 8,
44         .y_scale = 40, .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_24_font
57
58 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
59 #define VALUE_Y         (int16_t) (LABEL_Y + 5 + BIG_FONT.ascent)
60
61 #define SEP_X           82
62 #define SEP_WIDTH       2
63
64 #define BOX_X           (SEP_X / 2)
65 #define PAD_X           ((WIDTH + SEP_X + SEP_WIDTH) / 2)
66
67 #define VALUE_LABEL_X   64
68 #define RSSI_LABEL_X    15
69
70 #define SCAN_X          (WIDTH - 100) / 2
71 #define SCAN_Y          50
72 #define SCAN_HEIGHT     3
73 #define SCANNING_X      (WIDTH / 2)
74 #define SCANNING_Y      (SCAN_Y - 2)
75 #define FOUND_Y         63
76 #define FOUND_X         3
77 #define FOUND_WIDTH     (WIDTH - 6)
78 #define CONTRAST_LABEL_X        37
79 #define CONTRAST_WIDTH  100
80 #define CONTRAST_X      (WIDTH - CONTRAST_WIDTH) / 2
81 #define CONTRAST_Y      20
82 #define CONTRAST_HEIGHT 20
83 #define CONTRAST_VALUE_X        64
84 #define CONTRAST_VALUE_Y        (CONTRAST_Y + CONTRAST_HEIGHT + SMALL_FONT.ascent + 3)
85 #define BACKLIGHT_LABEL_X       37
86 #define BACKLIGHT_WIDTH 100
87 #define BACKLIGHT_X     (WIDTH - BACKLIGHT_WIDTH) / 2
88 #define BACKLIGHT_Y     20
89 #define BACKLIGHT_HEIGHT        20
90 #define BACKLIGHT_VALUE_X       64
91 #define BACKLIGHT_VALUE_Y       (BACKLIGHT_Y + BACKLIGHT_HEIGHT + SMALL_FONT.ascent + 3)
92 #define INFO_FONT       TINY_FONT
93 #define INFO_START_Y    ((int16_t) (INFO_FONT.ascent + 2))
94 #define INFO_STEP_Y     ((int16_t) (INFO_FONT.ascent + 2))
95
96 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
97 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
98
99 /* UI values */
100 static uint8_t  ao_lco_select_mode;
101 static uint8_t  ao_lco_event_debug;
102
103 #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)
104 #define AO_LCO_SELECT_BOX       0
105 #define AO_LCO_SELECT_PAD       1
106
107 static uint8_t  ao_lco_display_mutex;
108
109 static void
110 _ao_center_text(int16_t x, int16_t y, const struct ao_font *font, const char *str)
111 {
112         int16_t width = ao_text_width(font, str);
113         ao_text(&fb, font, x - width/2, y, str, AO_BLACK, AO_COPY);
114 }
115
116 static void
117 _ao_lco_show_pad(int8_t pad)
118 {
119         char    str[5];
120
121         _ao_center_text(PAD_X, LABEL_Y, &SMALL_FONT, "Pad");
122         snprintf(str, sizeof(str), "%d", pad);
123         _ao_center_text(PAD_X, VALUE_Y, &BIG_FONT, str);
124 }
125
126 static void
127 _ao_lco_show_box(int16_t box)
128 {
129         char    str[7];
130
131         _ao_center_text(BOX_X, LABEL_Y, &SMALL_FONT, "Bank");
132         snprintf(str, sizeof(str), "%d", box);
133         _ao_center_text(BOX_X, VALUE_Y, &BIG_FONT, str);
134 }
135
136 static void
137 _ao_format_voltage(char *str, size_t size, uint16_t decivolts)
138 {
139         snprintf(str, size, "%d.%d", decivolts / 10, decivolts % 10);
140 }
141
142 #if AO_LCO_HAS_CONTRAST
143 static void
144 _ao_lco_show_contrast(void)
145 {
146         char buf[8];
147         uint8_t brightness = ao_st7565_get_brightness();
148         int16_t contrast = (int16_t) (brightness * CONTRAST_WIDTH / AO_LCO_MAX_CONTRAST);
149
150         _ao_center_text(WIDTH/2, LABEL_Y, &SMALL_FONT, "Contrast");
151         ao_rect(&fb, CONTRAST_X, CONTRAST_Y, contrast, CONTRAST_HEIGHT, AO_BLACK, AO_COPY);
152         snprintf(buf, sizeof(buf), "%d %%", brightness * 100 / AO_LCO_MAX_CONTRAST);
153         _ao_center_text(WIDTH/2, CONTRAST_VALUE_Y, &SMALL_FONT, buf);
154 }
155 #endif
156
157 #if AO_LCO_HAS_BACKLIGHT
158 static void
159 _ao_lco_show_backlight(void)
160 {
161         char buf[8];
162         int32_t backlight = ao_lco_get_backlight();
163         int16_t value = (int16_t) (backlight * BACKLIGHT_WIDTH / AO_LCO_MAX_BACKLIGHT);
164
165         _ao_center_text(WIDTH/2, LABEL_Y, &SMALL_FONT, "Backlight");
166         ao_rect(&fb, BACKLIGHT_X, BACKLIGHT_Y, value, BACKLIGHT_HEIGHT, AO_BLACK, AO_COPY);
167         snprintf(buf, sizeof(buf), "%ld %%", backlight * 100 / AO_LCO_MAX_BACKLIGHT);
168         _ao_center_text(WIDTH/2, BACKLIGHT_VALUE_Y, &SMALL_FONT, buf);
169 }
170 #endif
171
172 static int16_t info_y;
173
174 static void
175 _ao_lco_info(const char *format, ...)
176 {
177         va_list a;
178         char    buf[20];
179         va_start(a, format);
180         vsnprintf(buf, sizeof(buf), format, a);
181         va_end(a);
182         ao_text(&fb, &INFO_FONT, 0, info_y, buf, AO_BLACK, AO_COPY);
183         info_y += INFO_STEP_Y;
184 }
185
186 static void
187 _ao_lco_show_lco_info(void)
188 {
189         char            battery[7];
190         struct ao_adc   packet;
191         int16_t         decivolt;
192
193         ao_logo_poly(&fb, &show_transform, AO_BLACK, AO_COPY);
194
195         ao_adc_single_get(&packet);
196         decivolt = ao_battery_decivolt(packet.v_batt);
197         _ao_format_voltage(battery, sizeof(battery), (uint16_t) decivolt);
198
199         info_y = INFO_START_Y;
200         _ao_lco_info("%s", ao_product);
201         _ao_lco_info("Serial: %d", ao_serial_number);
202         _ao_lco_info("Battery: %sV", battery);
203         _ao_lco_info("Version: %s", ao_version);
204         _ao_lco_info("Callsign: %s", ao_config.callsign);
205         _ao_lco_info("Frequency: %ld.%03d",
206                      ao_config.frequency / 1000,
207                      (int) (ao_config.frequency % 1000));
208 }
209
210 static uint8_t
211 popcount(uint32_t value)
212 {
213         uint8_t count = 0;
214         while(value != 0) {
215                 count += value & 1;
216                 value >>= 1;
217         }
218         return count;
219 }
220
221 static void
222 _ao_lco_show_pad_info(void)
223 {
224         char    pad_battery[7];
225
226         ao_logo_poly(&fb, &show_transform, AO_BLACK, AO_COPY);
227         info_y = INFO_START_Y;
228         _ao_lco_info("Bank: %d", ao_lco_box);
229         if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
230                 _ao_lco_info("Contact lost");
231                 _ao_lco_info("Last RSSI: %ddBm", ao_radio_cmac_last_rssi);
232         } else {
233                 _ao_lco_info("Total pads: %d", popcount(ao_pad_query.channels));
234                 _ao_lco_info("RSSI: %ddBm", ao_radio_cmac_rssi);
235                 _ao_format_voltage(pad_battery, sizeof(pad_battery), ao_pad_query.battery);
236                 _ao_lco_info("Battery: %sV", pad_battery);
237                 _ao_lco_info("Arming switch: %s", ao_pad_query.arm_status ? "On" : "Off");
238         }
239 }
240
241 void
242 ao_lco_show(void)
243 {
244         ao_mutex_get(&ao_lco_display_mutex);
245         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
246         switch (ao_lco_box) {
247 #if AO_LCO_HAS_CONTRAST
248         case AO_LCO_CONTRAST:
249                 _ao_lco_show_contrast();
250                 break;
251 #endif
252 #if AO_LCO_HAS_BACKLIGHT
253         case AO_LCO_BACKLIGHT:
254                 _ao_lco_show_backlight();
255                 break;
256 #endif
257         case AO_LCO_LCO_INFO:
258                 _ao_lco_show_lco_info();
259                 break;
260         default:
261                 switch (ao_lco_pad) {
262                 case AO_LCO_PAD_INFO:
263                         _ao_lco_show_pad_info();
264                         break;
265                 default:
266                         _ao_lco_show_pad(ao_lco_pad);
267                         _ao_lco_show_box(ao_lco_box);
268                         ao_rect(&fb, SEP_X, 0, SEP_WIDTH, HEIGHT, AO_BLACK, AO_COPY);
269                 }
270                 break;
271         }
272         ao_st7565_update(&fb);
273         ao_mutex_put(&ao_lco_display_mutex);
274 }
275
276 static void
277 ao_lco_set_select(void)
278 {
279         if (ao_lco_armed) {
280                 ao_led_off(AO_LED_PAD);
281                 ao_led_off(AO_LED_BOX);
282         } else {
283                 switch (ao_lco_select_mode) {
284                 case AO_LCO_SELECT_PAD:
285                         ao_led_off(AO_LED_BOX);
286                         ao_led_on(AO_LED_PAD);
287                         break;
288                 case AO_LCO_SELECT_BOX:
289                         ao_led_off(AO_LED_PAD);
290                         ao_led_on(AO_LED_BOX);
291                         break;
292                 default:
293                         break;
294                 }
295         }
296 }
297
298
299 #if AO_LCO_HAS_CONTRAST
300 void
301 ao_lco_set_contrast(int32_t contrast)
302 {
303         ao_st7565_set_brightness((uint8_t) contrast);
304 }
305
306 int32_t
307 ao_lco_get_contrast(void)
308 {
309         return (int32_t) ao_st7565_get_brightness();
310 }
311 #endif
312
313 #if AO_LCO_HAS_BACKLIGHT
314 static uint16_t ao_backlight;
315
316 void
317 ao_lco_set_backlight(int32_t backlight)
318 {
319         ao_backlight = (uint16_t) backlight;
320         ao_pwm_set(AO_LCD_BL_PWM_CHAN, ao_backlight);
321 }
322
323 int32_t
324 ao_lco_get_backlight(void)
325 {
326         return (int32_t) ao_backlight;
327 }
328 #endif
329
330 static struct ao_task   ao_lco_drag_task;
331
332 static void
333 ao_lco_drag_monitor(void)
334 {
335         AO_TICK_TYPE    delay = ~0UL;
336         AO_TICK_TYPE    now;
337
338         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
339         for (;;) {
340                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
341                 if (delay == (AO_TICK_TYPE) ~0)
342                         ao_sleep(&ao_lco_drag_beep_count);
343                 else
344                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
345
346                 delay = ~0UL;
347                 now = ao_time();
348                 delay = ao_lco_drag_warn_check(now, delay);
349                 delay = ao_lco_drag_beep_check(now, delay);
350         }
351 }
352
353 static void
354 ao_lco_input(void)
355 {
356         static struct ao_event  event;
357
358         for (;;) {
359                 ao_event_get(&event);
360                 PRINTE("event type %d unit %d value %ld\n",
361                        event.type, event.unit, (long) event.value);
362                 switch (event.type) {
363                 case AO_EVENT_QUADRATURE:
364                         switch (event.unit) {
365                         case AO_QUADRATURE_SELECT:
366                                 if (!ao_lco_armed) {
367                                         switch (ao_lco_select_mode) {
368                                         case AO_LCO_SELECT_PAD:
369                                                 ao_lco_step_pad((int8_t) event.value);
370                                                 break;
371                                         case AO_LCO_SELECT_BOX:
372                                                 ao_lco_step_box((int8_t) event.value);
373                                                 break;
374                                         default:
375                                                 break;
376                                         }
377                                 }
378                                 break;
379                         }
380                         break;
381                 case AO_EVENT_BUTTON:
382                         switch (event.unit) {
383                         case AO_BUTTON_ARM:
384                                 ao_lco_set_armed((uint8_t) event.value);
385                                 ao_lco_set_select();
386                                 break;
387                         case AO_BUTTON_FIRE:
388                                 if (ao_lco_armed)
389                                         ao_lco_set_firing((uint8_t) event.value);
390                                 break;
391                         case AO_BUTTON_DRAG_SELECT:
392                                 if (event.value)
393                                         ao_lco_toggle_drag();
394                                 break;
395                         case AO_BUTTON_DRAG_MODE:
396                                 if (event.value)
397                                         ao_lco_drag_enable();
398                                 else
399                                         ao_lco_drag_disable();
400                                 break;
401                         case AO_BUTTON_ENCODER_SELECT:
402                                 if (event.value) {
403                                         if (!ao_lco_armed) {
404                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
405                                                 ao_lco_set_select();
406                                         }
407                                 }
408                                 break;
409                         }
410                         break;
411                 }
412         }
413 }
414
415 /*
416  * Light up everything for a second at power on to let the user
417  * visually inspect the system for correct operation
418  */
419 static void
420 ao_lco_display_test(void)
421 {
422         ao_led_on(AO_LEDS_AVAILABLE);
423         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_BLACK, AO_COPY);
424         ao_st7565_update(&fb);
425         ao_delay(AO_MS_TO_TICKS(1000));
426         ao_led_off(AO_LEDS_AVAILABLE);
427 }
428
429 static struct ao_task ao_lco_input_task;
430 static struct ao_task ao_lco_monitor_task;
431 static struct ao_task ao_lco_arm_warn_task;
432 static struct ao_task ao_lco_igniter_status_task;
433
434 static int16_t  found_width;
435 #define MAX_FOUND       32
436 static int16_t  found_boxes[MAX_FOUND];
437 static uint8_t  nfound;
438
439 void
440 ao_lco_search_start(void)
441 {
442         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
443         ao_logo(&fb, &logo_transform, &LOGO_FONT, AO_BLACK, AO_COPY);
444         _ao_center_text(SCANNING_X, SCANNING_Y, &TINY_FONT, "Scanning...");
445         found_width = 0;
446         nfound = 0;
447 }
448
449 void
450 ao_lco_search_box_check(int16_t box)
451 {
452         if (box > 0)
453                 ao_rect(&fb, SCAN_X, SCAN_Y, box, SCAN_HEIGHT, AO_BLACK, AO_COPY);
454         ao_st7565_update(&fb);
455 }
456
457 void
458 ao_lco_search_box_present(int16_t box)
459 {
460         char    str[8];
461         int16_t width;
462         int16_t box_top = FOUND_Y - TINY_FONT.ascent;
463         int16_t x;
464         uint8_t n;
465
466         snprintf(str, sizeof(str), "%s%u", nfound ? ", " : "", box);
467         width = ao_text_width(&TINY_FONT, str);
468         while (found_width + width > FOUND_WIDTH || nfound == MAX_FOUND)
469         {
470                 snprintf(str, sizeof(str), "%u, ", found_boxes[0]);
471                 found_width -= ao_text_width(&TINY_FONT, str);
472                 memmove(&found_boxes[0], &found_boxes[1], (nfound - 1) * sizeof (int16_t));
473                 nfound--;
474         }
475         found_boxes[nfound++] = box;
476
477         ao_rect(&fb, FOUND_X, FOUND_Y - TINY_FONT.ascent, FOUND_WIDTH, HEIGHT - box_top, AO_WHITE, AO_COPY);
478         x = FOUND_X;
479         for (n = 0; n < nfound; n++) {
480                 snprintf(str, sizeof(str), "%s%u", n ? ", " : "", found_boxes[n]);
481                 int16_t next_x = ao_text(&fb, &TINY_FONT, x, FOUND_Y, str, AO_BLACK, AO_COPY);
482                 x = next_x;
483         }
484         found_width = x - FOUND_X;
485 }
486
487 void
488 ao_lco_search_done(void)
489 {
490         ao_st7565_update(&fb);
491 }
492
493 static void
494 ao_lco_main(void)
495 {
496         ao_lco_display_test();
497         ao_lco_search();
498         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
499         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
500         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
501         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
502         ao_lco_monitor();
503 }
504
505 #if DEBUG
506 static void
507 ao_lco_set_debug(void)
508 {
509         uint32_t r = ao_cmd_decimal();
510         if (ao_cmd_status == ao_cmd_success){
511                 ao_lco_debug = r & 1;
512                 ao_lco_event_debug = (r & 2) >> 1;
513         }
514 }
515
516 const struct ao_cmds ao_lco_cmds[] = {
517         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
518         { ao_lco_search,        "s\0Search for pad boxes" },
519         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
520         { 0, NULL }
521 };
522 #endif
523
524 void
525 ao_lco_init(void)
526 {
527         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
528 #if DEBUG
529         ao_cmd_register(&ao_lco_cmds[0]);
530 #endif
531 }