2 * Copyright © 2011 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_flight.h>
22 static __xdata struct ao_telemetry_sensor ao_tel_sensor;
23 static __xdata struct ao_telemetry_location ao_tel_location;
24 static __xdata struct ao_telemetry_configuration ao_tel_config;
25 static __xdata int16_t ao_tel_max_speed;
26 static __xdata int16_t ao_tel_max_height;
27 static int8_t ao_tel_rssi;
29 static __xdata char ao_lcd_line[17];
30 static __xdata char ao_state_name[] = "SIPBFCDMLI";
33 ao_terraui_line(uint8_t addr)
36 ao_lcd_putstring(ao_lcd_line);
39 #define ao_terraui_state() (ao_state_name[ao_tel_sensor.state])
42 ao_terraui_igniter(int16_t sense)
44 if (sense < AO_IGNITER_OPEN)
46 if (sense > AO_IGNITER_CLOSED)
52 ao_terraui_battery(void)
54 if (ao_tel_sensor.v_batt > 25558)
62 if (ao_tel_location.flags & (1 << 4)) {
63 if ((ao_tel_location.flags & 0xf) >= 4)
70 ao_terraui_local_gps(void)
72 if (ao_gps_data.flags & (1 << 4)) {
73 if ((ao_gps_data.flags & 0xf) >= 4)
80 ao_terraui_logging(void)
82 if (ao_tel_config.flight != 0)
87 static __code char ao_progress[4] = { '\011', '\012', '\014', '\013' };
89 static uint8_t ao_telem_progress;
90 static uint8_t ao_gps_progress;
93 ao_terraui_startup(void)
95 sprintf(ao_lcd_line, "%-16.16s", ao_product);
96 ao_terraui_line(AO_LCD_ADDR(0,0));
97 sprintf(ao_lcd_line, "%-8.8s %5u ", ao_version, ao_serial_number);
98 ao_terraui_line(AO_LCD_ADDR(1,0));
102 ao_terraui_info_firstline(void)
104 sprintf(ao_lcd_line, "S %4d %7.7s %c",
105 ao_tel_sensor.serial,
106 ao_tel_config.callsign,
108 ao_terraui_line(AO_LCD_ADDR(0,0));
112 ao_terraui_info(void)
114 ao_terraui_info_firstline();
115 sprintf(ao_lcd_line, "F %4d RSSI%4d%c",
116 ao_tel_config.flight,
118 ao_progress[ao_telem_progress]);
119 ao_terraui_line(AO_LCD_ADDR(1,0));
125 sprintf(ao_lcd_line, "B%c A%c M%c L%c G%c %c",
126 ao_terraui_battery(),
127 ao_terraui_igniter(ao_tel_sensor.sense_d),
128 ao_terraui_igniter(ao_tel_sensor.sense_m),
129 ao_terraui_logging(),
132 ao_terraui_line(AO_LCD_ADDR(0,0));
133 sprintf(ao_lcd_line, "SAT %2d RSSI%4d%c",
134 ao_tel_location.flags & 0xf,
136 ao_progress[ao_telem_progress]);
137 ao_terraui_line(AO_LCD_ADDR(1,0));
141 ao_terraui_ascent(void)
143 sprintf(ao_lcd_line, "S %5d S\011%5d%c",
144 ao_tel_sensor.speed >> 4,
145 ao_tel_max_speed >> 4,
147 ao_terraui_line(AO_LCD_ADDR(0,0));
148 sprintf(ao_lcd_line, "H %5d H\011%5d%c",
149 ao_tel_sensor.height >> 4,
150 ao_tel_max_height >> 4,
151 ao_progress[ao_telem_progress]);
152 ao_terraui_line(AO_LCD_ADDR(1,0));
155 static int16_t mag(int32_t d)
176 while (d >= (2147483647 / 111198)) {
180 d = (d * 111198) / m;
186 static __code uint8_t cos_table[] = {
244 static __code uint8_t tan_table[] = {
292 int16_t ao_atan2(int32_t dy, int32_t dx) __reentrant
322 t = dy; dy = dx; dx = t;
328 dy = (dy + (dx >> 1)) / dx;
330 for (t = 0; t < 44; t++)
331 if (tan_table[t] >= r)
336 static __pdata int32_t lon_dist, lat_dist;
337 static __pdata uint32_t ground_dist, range;
338 static __pdata uint8_t dist_in_km;
339 static __pdata int16_t bearing, elevation;
342 ao_terraui_lat_dist(void)
344 lat_dist = dist (ao_tel_location.latitude - ao_gps_data.latitude);
348 ao_terraui_lon_dist(void) __reentrant
350 uint8_t c = cos_table[ao_gps_data.latitude >> 24];
351 lon_dist = ao_tel_location.longitude;
353 /* check if it's shorter to go the other way around */
354 if ((int16_t) (lon_dist >> 24) < (int16_t) (ao_gps_data.longitude >> 24) - (int16_t) (1800000000 >> 24))
355 lon_dist += 3600000000ul;
356 lon_dist = dist(lon_dist - ao_gps_data.longitude);
358 if (lon_dist & 0x7f800000)
359 lon_dist = (lon_dist >> 8) * c;
361 lon_dist = (lon_dist * (int16_t) c) >> 8;
365 static int32_t sqr(int32_t x) { return x * x; }
368 ao_terraui_compute(void)
370 uint16_t h = ao_tel_sensor.height;
372 ao_terraui_lat_dist();
373 ao_terraui_lon_dist();
374 if (lat_dist > 32767 || lon_dist > 32767) {
376 ground_dist = sqr(lat_dist/1000) + sqr(lon_dist/1000);
380 ground_dist = sqr(lat_dist) + sqr(lon_dist);
382 ground_dist = ao_sqrt (ground_dist);
383 range = ao_sqrt(ground_dist * ground_dist + ao_tel_sensor.height * ao_tel_sensor.height);
384 bearing = ao_atan2(lon_dist, lat_dist);
387 elevation = ao_atan2(ao_tel_sensor.height, ground_dist);
391 ao_terraui_descent(void)
393 ao_terraui_compute();
394 sprintf(ao_lcd_line, "\007 %4d \005 %3d %c",
397 ao_terraui_line(AO_LCD_ADDR(0,0));
398 sprintf(ao_lcd_line, "H:%5d S:%5d%c",
399 ao_tel_sensor.height, ao_tel_sensor.speed >> 4,
400 ao_progress[ao_telem_progress]);
401 ao_terraui_line(AO_LCD_ADDR(1,0));
405 ao_terraui_recover(void)
407 ao_terraui_compute();
408 sprintf(ao_lcd_line, "\007 %4d \005 %3d %c",
411 ao_terraui_line(AO_LCD_ADDR(0,0));
412 sprintf(ao_lcd_line, "R%5ld%cRSSI%4d%c",
413 ground_dist, dist_in_km ? 'k' : ' ',
415 ao_progress[ao_telem_progress]);
416 ao_terraui_line(AO_LCD_ADDR(1,0));
420 ao_terraui_coord(int32_t c, char plus, char minus, char extra) __reentrant
435 f = (c + 500) / 1000;
436 sprintf(ao_lcd_line, "%c %3d\362 %2d.%04d\"%c",
437 plus, d, m, f, extra);
441 ao_terraui_remote(void)
443 ao_terraui_coord(ao_tel_location.latitude, 'N', 'S', ao_terraui_state());
444 ao_terraui_line(AO_LCD_ADDR(0,0));
445 ao_terraui_coord(ao_tel_location.longitude, 'E', 'W', ao_progress[ao_telem_progress]);
446 ao_terraui_line(AO_LCD_ADDR(1,0));
450 ao_terraui_local(void) __reentrant
452 ao_terraui_coord(ao_gps_data.latitude, 'n', 's',
453 ao_terraui_local_gps());
454 ao_terraui_line(AO_LCD_ADDR(0,0));
455 ao_terraui_coord(ao_gps_data.longitude, 'e', 'w', ao_progress[ao_gps_progress]);
456 ao_terraui_line(AO_LCD_ADDR(1,0));
459 static __pdata uint8_t ao_set_freq;
460 static __pdata uint32_t ao_set_freq_orig;
463 ao_terraui_freq(void) __reentrant
468 MHz = ao_config.frequency / 1000;
469 frac = ao_config.frequency % 1000;
470 ao_terraui_info_firstline();
471 sprintf(ao_lcd_line, "Freq: %3d.%03d MHz", MHz, frac);
472 ao_terraui_line(AO_LCD_ADDR(1,0));
473 ao_lcd_goto(AO_LCD_ADDR(1,11));
477 ao_terraui_freq_start(void)
480 ao_set_freq_orig = ao_config.frequency;
485 ao_terraui_freq_button(char b) {
491 if (ao_config.frequency > 430000)
492 ao_config.frequency -= 100;
497 if (ao_set_freq_orig != ao_config.frequency)
501 if (ao_config.frequency < 438000)
502 ao_config.frequency += 100;
506 ao_config_set_radio();
507 ao_radio_recv_abort();
510 static __code void (*__code ao_terraui_page[])(void) = {
521 #define NUM_PAGE (sizeof (ao_terraui_page)/sizeof (ao_terraui_page[0]))
523 static __pdata uint8_t ao_current_page = 0;
524 static __pdata uint8_t ao_shown_about = 3;
531 ao_delay(AO_MS_TO_TICKS(100));
540 ao_terraui_page[ao_current_page]();
542 ao_alarm(AO_SEC_TO_TICKS(1));
547 ao_beep_for(AO_BEEP_HIGH, AO_MS_TO_TICKS(10));
552 ao_terraui_freq_button(b);
557 if (ao_shown_about) {
558 if (--ao_shown_about == 0)
564 if (ao_current_page >= NUM_PAGE)
568 ao_terraui_freq_start();
571 if (ao_current_page == 0)
572 ao_current_page = NUM_PAGE;
579 __xdata static struct ao_task ao_terraui_task;
582 ao_terramonitor(void)
586 monitor = ao_monitor_head;
587 ao_monitor_set(sizeof (struct ao_telemetry_generic));
588 for (monitor = ao_monitor_head;;
589 monitor = ao_monitor_ring_next(monitor))
591 while (monitor == ao_monitor_head)
592 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
593 if (ao_monitoring != sizeof (union ao_telemetry_all))
595 if (!(ao_monitor_ring[monitor].all.status & PKT_APPEND_STATUS_1_CRC_OK))
597 ao_tel_rssi = AO_RSSI_FROM_RADIO(ao_monitor_ring[monitor].all.rssi);
598 switch (ao_monitor_ring[monitor].all.telemetry.generic.type) {
599 case AO_TELEMETRY_SENSOR_TELEMETRUM:
600 case AO_TELEMETRY_SENSOR_TELEMINI:
601 case AO_TELEMETRY_SENSOR_TELENANO:
602 ao_xmemcpy(&ao_tel_sensor, &ao_monitor_ring[monitor], sizeof (ao_tel_sensor));
603 if (ao_tel_sensor.state < ao_flight_boost) {
604 ao_tel_max_speed = 0;
605 ao_tel_max_height = 0;
607 if (ao_tel_sensor.speed > ao_tel_max_speed)
608 ao_tel_max_speed = ao_tel_sensor.speed;
609 if (ao_tel_sensor.height > ao_tel_max_height)
610 ao_tel_max_height = ao_tel_sensor.height;
612 ao_telem_progress = (ao_telem_progress + 1) & 0x3;
614 case AO_TELEMETRY_LOCATION:
615 ao_xmemcpy(&ao_tel_location, &ao_monitor_ring[monitor], sizeof (ao_tel_location));
617 case AO_TELEMETRY_CONFIGURATION:
618 ao_xmemcpy(&ao_tel_config, &ao_monitor_ring[monitor], sizeof (ao_tel_config));
623 __xdata static struct ao_task ao_terramonitor_task;
628 uint16_t gps_tick = ao_gps_progress;
631 while (ao_gps_tick == gps_tick)
632 ao_sleep(&ao_gps_new);
633 gps_tick = ao_gps_tick;
634 ao_gps_progress = (ao_gps_progress + 1) & 3;
638 __xdata static struct ao_task ao_terragps_task;
641 ao_terraui_init(void)
643 ao_add_task(&ao_terraui_task, ao_terraui, "ui");
644 ao_add_task(&ao_terramonitor_task, ao_terramonitor, "monitor");
645 ao_add_task(&ao_terragps_task, ao_terragps, "gps");