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