2493675849624d359db390ddb442253ed5df065e
[fw/altos] / ao-tools / altosui / AltosCSV.java
1 /*
2  * Copyright © 2010 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.io.*;
22 import altosui.AltosRecord;
23
24 public class AltosCSV {
25         File            name;
26         PrintStream     out;
27         boolean         header_written;
28
29         static final int ALTOS_CSV_VERSION = 1;
30
31         /* Version 1 format:
32          *
33          * General info
34          *      version number
35          *      serial number
36          *      flight number
37          *      callsign
38          *      time (seconds since boost)
39          *
40          * Flight status
41          *      state
42          *      state name
43          *
44          * Basic sensors
45          *      acceleration (m/s²)
46          *      pressure (mBar)
47          *      altitude (m)
48          *      accelerometer speed (m/s)
49          *      barometer speed (m/s)
50          *      temp (°C)
51          *      battery (V)
52          *      drogue (V)
53          *      main (V)
54          *
55          * GPS data
56          *      connected (1/0)
57          *      locked (1/0)
58          *      nsat (used for solution)
59          *      latitude (°)
60          *      longitude (°)
61          *      altitude (m)
62          *      year (e.g. 2010)
63          *      month (1-12)
64          *      day (1-31)
65          *      hour (0-23)
66          *      minute (0-59)
67          *      second (0-59)
68          *
69          * GPS Sat data
70          *      hdop
71          *      C/N0 data for all 32 valid SDIDs
72          */
73
74         void write_general_header() {
75                 out.printf("version serial flight call time");
76         }
77
78         void write_general(AltosRecord record) {
79                 out.printf("%s,%d,%d,%s,%d",
80                            record.version, record.serial, record.flight, record.callsign, record.tick);
81         }
82
83         void write_flight_header() {
84                 out.printf("state state_name");
85         }
86
87         void write_flight(AltosRecord record) {
88                 out.printf("%d,%s", record.state, record.state());
89         }
90
91         void write_header() {
92                 out.printf("# "); write_general_header();
93                 out.printf(" "); write_flight_header();
94                 out.printf ("\n");
95         }
96
97         public void write(AltosRecord record) {
98                 if (!header_written) {
99                         write_header();
100                         header_written = true;
101                 }
102                 write_general(record); out.printf(",");
103                 write_flight(record);
104                 out.printf ("\n");
105         }
106
107         public PrintStream out() {
108                 return out;
109         }
110
111         public AltosCSV(File in_name) throws FileNotFoundException {
112                 name = in_name;
113                 out = new PrintStream(name);
114         }
115 }