2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 #include <ao_quadrature.h>
23 #include <ao_radio_cmac.h>
24 #include <ao_st7565.h>
25 #include <ao_adc_single.h>
28 #define WIDTH AO_ST7565_WIDTH
29 #define HEIGHT AO_ST7565_HEIGHT
30 #define STRIDE AO_BITMAP_STRIDE(WIDTH)
32 static uint32_t image[STRIDE * HEIGHT];
34 static struct ao_bitmap fb = {
39 .damage = AO_BOX_INIT,
42 static const struct ao_transform logo_transform = {
43 .x_scale = 48, .x_off = 2,
44 .y_scale = 48, .y_off = 0,
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
53 #define LABEL_Y (int16_t) (SMALL_FONT.ascent)
54 #define VALUE_Y (int16_t) (LABEL_Y + BIG_FONT.ascent + 5)
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
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
72 #define CONTRAST_HEIGHT 20
73 #define BACKLIGHT_LABEL_X 37
74 #define BACKLIGHT_WIDTH 100
75 #define BACKLIGHT_X (WIDTH - BACKLIGHT_WIDTH) / 2
76 #define BACKLIGHT_Y 20
77 #define BACKLIGHT_HEIGHT 20
78 #define INFO_START_Y ((int16_t) (SMALL_FONT.ascent + 2))
79 #define INFO_STEP_Y ((int16_t) (SMALL_FONT.ascent + 3))
81 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
82 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
85 static uint8_t ao_lco_select_mode;
86 static uint8_t ao_lco_event_debug;
88 #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)
89 #define AO_LCO_SELECT_PAD 0
90 #define AO_LCO_SELECT_BOX 1
92 static uint8_t ao_lco_display_mutex;
95 _ao_lco_show_pad(uint8_t pad)
99 snprintf(str, sizeof(str), "%d", pad);
100 ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, AO_BLACK, AO_COPY);
101 ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", AO_BLACK, AO_COPY);
105 _ao_lco_show_box(int16_t box)
109 snprintf(str, sizeof(str), "%2d", box);
110 ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
111 ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", AO_BLACK, AO_COPY);
115 _ao_lco_show_voltage(uint16_t decivolts, const char *label)
119 PRINTD("voltage %d\n", decivolts);
120 snprintf(str, sizeof(str), "%2d.%d", decivolts / 10, decivolts % 10);
121 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
122 ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, label, AO_BLACK, AO_COPY);
126 _ao_lco_batt_voltage(void)
128 struct ao_adc packet;
131 ao_adc_single_get(&packet);
132 decivolt = ao_battery_decivolt(packet.v_batt);
133 _ao_lco_show_voltage((uint16_t) decivolt, "LCO battery");
134 ao_st7565_update(&fb);
138 _ao_lco_show_contrast(void)
140 uint8_t brightness = ao_st7565_get_brightness();
141 int16_t contrast = (int16_t) (brightness * CONTRAST_WIDTH / AO_LCO_MAX_CONTRAST);
143 ao_text(&fb, &SMALL_FONT, CONTRAST_LABEL_X, LABEL_Y, "Contrast", AO_BLACK, AO_COPY);
144 ao_rect(&fb, CONTRAST_X, CONTRAST_Y, contrast, CONTRAST_HEIGHT, AO_BLACK, AO_COPY);
148 _ao_lco_show_backlight(void)
150 int32_t backlight = ao_lco_get_backlight();
151 int16_t value = (int16_t) (backlight * BACKLIGHT_WIDTH / AO_LCO_MAX_BACKLIGHT);
153 ao_text(&fb, &SMALL_FONT, BACKLIGHT_LABEL_X, LABEL_Y, "Backlight", AO_BLACK, AO_COPY);
154 ao_rect(&fb, BACKLIGHT_X, BACKLIGHT_Y, value, BACKLIGHT_HEIGHT, AO_BLACK, AO_COPY);
157 static int16_t info_y;
160 _ao_lco_info(const char *format, ...)
165 vsnprintf(buf, sizeof(buf), format, a);
167 ao_text(&fb, &SMALL_FONT, 0, info_y, buf, AO_BLACK, AO_COPY);
168 info_y += INFO_STEP_Y;
172 _ao_lco_show_info(void)
174 info_y = INFO_START_Y;
175 _ao_lco_info("%s", ao_product);
176 _ao_lco_info("Version: %s", ao_version);
177 _ao_lco_info("Serial: %d", ao_serial_number);
178 _ao_lco_info("Callsign: %s", ao_config.callsign);
179 _ao_lco_info("Frequency: %ld.%03d",
180 ao_config.frequency / 1000,
181 (int) (ao_config.frequency % 1000));
187 ao_mutex_get(&ao_lco_display_mutex);
188 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
189 switch (ao_lco_box) {
190 case AO_LCO_LCO_VOLTAGE:
191 _ao_lco_batt_voltage();
193 case AO_LCO_CONTRAST:
194 _ao_lco_show_contrast();
196 case AO_LCO_BACKLIGHT:
197 _ao_lco_show_backlight();
203 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
204 _ao_lco_show_voltage(ao_pad_query.battery, "Pad battery");
206 _ao_lco_show_pad(ao_lco_pad);
207 _ao_lco_show_box(ao_lco_box);
208 ao_rect(&fb, SEP_X, 0, 2, HEIGHT, AO_BLACK, AO_COPY);
212 ao_st7565_update(&fb);
213 ao_mutex_put(&ao_lco_display_mutex);
217 ao_lco_set_select(void)
220 ao_led_off(AO_LED_PAD);
221 ao_led_off(AO_LED_BOX);
223 switch (ao_lco_select_mode) {
224 case AO_LCO_SELECT_PAD:
225 ao_led_off(AO_LED_BOX);
226 ao_led_on(AO_LED_PAD);
228 case AO_LCO_SELECT_BOX:
229 ao_led_off(AO_LED_PAD);
230 ao_led_on(AO_LED_BOX);
240 ao_lco_set_contrast(int32_t contrast)
242 ao_st7565_set_brightness((uint8_t) contrast);
246 ao_lco_get_contrast(void)
248 return (int32_t) ao_st7565_get_brightness();
251 static uint16_t ao_backlight;
254 ao_lco_set_backlight(int32_t backlight)
256 ao_backlight = (uint16_t) backlight;
257 ao_pwm_set(AO_LCD_BL_PWM_CHAN, ao_backlight);
261 ao_lco_get_backlight(void)
263 return (int32_t) ao_backlight;
266 static struct ao_task ao_lco_drag_task;
269 ao_lco_drag_monitor(void)
271 AO_TICK_TYPE delay = ~0UL;
274 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
276 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
277 if (delay == (AO_TICK_TYPE) ~0)
278 ao_sleep(&ao_lco_drag_beep_count);
280 ao_sleep_for(&ao_lco_drag_beep_count, delay);
284 delay = ao_lco_drag_warn_check(now, delay);
285 delay = ao_lco_drag_beep_check(now, delay);
292 static struct ao_event event;
295 ao_event_get(&event);
296 PRINTE("event type %d unit %d value %ld\n",
297 event.type, event.unit, (long) event.value);
298 switch (event.type) {
299 case AO_EVENT_QUADRATURE:
300 switch (event.unit) {
301 case AO_QUADRATURE_SELECT:
303 switch (ao_lco_select_mode) {
304 case AO_LCO_SELECT_PAD:
305 ao_lco_step_pad((int8_t) event.value);
307 case AO_LCO_SELECT_BOX:
308 ao_lco_step_box((int8_t) event.value);
317 case AO_EVENT_BUTTON:
318 switch (event.unit) {
320 ao_lco_set_armed((uint8_t) event.value);
325 ao_lco_set_firing((uint8_t) event.value);
327 case AO_BUTTON_DRAG_SELECT:
329 ao_lco_toggle_drag();
331 case AO_BUTTON_DRAG_MODE:
333 ao_lco_drag_enable();
335 ao_lco_drag_disable();
337 case AO_BUTTON_ENCODER_SELECT:
340 ao_lco_select_mode = 1 - ao_lco_select_mode;
352 * Light up everything for a second at power on to let the user
353 * visually inspect the system for correct operation
356 ao_lco_display_test(void)
358 ao_led_on(AO_LEDS_AVAILABLE);
359 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_BLACK, AO_COPY);
360 ao_st7565_update(&fb);
361 ao_delay(AO_MS_TO_TICKS(250));
362 ao_led_off(AO_LEDS_AVAILABLE);
365 static struct ao_task ao_lco_input_task;
366 static struct ao_task ao_lco_monitor_task;
367 static struct ao_task ao_lco_arm_warn_task;
368 static struct ao_task ao_lco_igniter_status_task;
370 static int16_t found_x;
373 ao_lco_search_start(void)
375 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
376 ao_logo(&fb, &logo_transform, &LOGO_FONT, AO_BLACK, AO_COPY);
381 ao_lco_search_box_check(int16_t box)
384 ao_rect(&fb, SCAN_X, SCAN_Y, box, SCAN_HEIGHT, AO_BLACK, AO_COPY);
385 ao_st7565_update(&fb);
389 ao_lco_search_box_present(int16_t box)
392 if (found_x < FOUND_WIDTH)
394 snprintf(str, sizeof(str), "%s%02u", found_x ? ", " : "", box);
395 found_x = ao_text(&fb, &TINY_FONT, found_x, FOUND_Y, str, AO_BLACK, AO_COPY);
400 ao_lco_search_done(void)
402 ao_st7565_update(&fb);
408 ao_lco_display_test();
410 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
411 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
412 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
413 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
419 ao_lco_set_debug(void)
421 uint32_t r = ao_cmd_decimal();
422 if (ao_cmd_status == ao_cmd_success){
423 ao_lco_debug = r & 1;
424 ao_lco_event_debug = (r & 2) >> 1;
428 const struct ao_cmds ao_lco_cmds[] = {
429 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
430 { ao_lco_search, "s\0Search for pad boxes" },
431 { ao_lco_pretend, "p\0Pretend there are lots of pad boxes" },
439 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
441 ao_cmd_register(&ao_lco_cmds[0]);