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