summaryrefslogtreecommitdiff
path: root/src/ao_monitor.c
blob: 4ba3da6d848949b27e31495bbde9af68fb56c9eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "ao.h"

__xdata uint8_t ao_monitoring;
__pdata uint8_t ao_monitor_led;

void
ao_monitor(void)
{
	__xdata struct ao_telemetry_recv recv;
	__xdata char callsign[AO_MAX_CALLSIGN+1];
	uint8_t state;
	int16_t rssi;

	for (;;) {
		__critical while (!ao_monitoring)
			ao_sleep(&ao_monitoring);
		if (!ao_radio_recv(&recv, sizeof (recv)))
			continue;
		state = recv.telemetry.flight_state;

		/* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
		rssi = (int16_t) (recv.rssi >> 1) - 74;
		memcpy(callsign, recv.telemetry.callsign, AO_MAX_CALLSIGN);
		if (state > ao_flight_invalid)
			state = ao_flight_invalid;
		if (recv.status & PKT_APPEND_STATUS_1_CRC_OK) {
			printf("VERSION %d CALL %s SERIAL %d FLIGHT %5u RSSI %4d STATUS %02x STATE %7s ",
			       AO_TELEMETRY_VERSION,
			       callsign,
			       recv.telemetry.serial,
			       recv.telemetry.flight,
			       rssi, recv.status,
			       ao_state_names[state]);
			printf("%5u a: %5d p: %5d t: %5d v: %5d d: %5d m: %5d "
			       "fa: %5d ga: %d fv: %7ld fp: %5d gp: %5d a+: %5d a-: %5d ",
			       recv.telemetry.adc.tick,
			       recv.telemetry.adc.accel,
			       recv.telemetry.adc.pres,
			       recv.telemetry.adc.temp,
			       recv.telemetry.adc.v_batt,
			       recv.telemetry.adc.sense_d,
			       recv.telemetry.adc.sense_m,
			       recv.telemetry.flight_accel,
			       recv.telemetry.ground_accel,
			       recv.telemetry.flight_vel,
			       recv.telemetry.flight_pres,
			       recv.telemetry.ground_pres,
			       recv.telemetry.accel_plus_g,
			       recv.telemetry.accel_minus_g);
			ao_gps_print(&recv.telemetry.gps);
			putchar(' ');
			ao_gps_tracking_print(&recv.telemetry.gps_tracking);
			putchar('\n');
			ao_rssi_set(rssi);
		} else {
			printf("CRC INVALID RSSI %3d\n", rssi);
		}
		ao_usb_flush();
		ao_led_toggle(ao_monitor_led);
	}
}

__xdata struct ao_task ao_monitor_task;

void
ao_set_monitor(uint8_t monitoring)
{
	ao_monitoring = monitoring;
	ao_wakeup(&ao_monitoring);
	if (!ao_monitoring)
		ao_radio_recv_abort();
}

static void
set_monitor(void)
{
	ao_cmd_hex();
	ao_set_monitor(ao_cmd_lex_i != 0);
}

__code struct ao_cmds ao_monitor_cmds[] = {
	{ 'm',	set_monitor,	"m <0 off, 1 on>                    Enable/disable radio monitoring" },
	{ 0,	set_monitor,	NULL },
};

void
ao_monitor_init(uint8_t monitor_led, uint8_t monitoring) __reentrant
{
	ao_monitor_led = monitor_led;
	ao_monitoring = monitoring;
	ao_cmd_register(&ao_monitor_cmds[0]);
	ao_add_task(&ao_monitor_task, ao_monitor, "monitor");
}