]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
More consistency improvements
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <glib.h>
8
9 /*
10  * Some silly typedefs to make our units very explicit.
11  *
12  * Also, the units are chosen so that values can be expressible as
13  * integers, so that we never have FP rounding issues. And they
14  * are small enough that converting to/from imperial units doesn't
15  * really matter.
16  *
17  * We also strive to make '0' a meaningless number saying "not
18  * initialized", since many values are things that may not have
19  * been reported (eg cylinder pressure or temperature from dive
20  * computers that don't support them). But sometimes -1 is an even
21  * more explicit way of saying "not there".
22  *
23  * Thus "millibar" for pressure, for example, or "millikelvin" for
24  * temperatures. Doing temperatures in celsius or fahrenheit would
25  * make for loss of precision when converting from one to the other,
26  * and using millikelvin is SI-like but also means that a temperature
27  * of '0' is clearly just a missing temperature or cylinder pressure.
28  *
29  * Also strive to use units that can not possibly be mistaken for a
30  * valid value in a "normal" system without conversion. If the max
31  * depth of a dive is '20000', you probably didn't convert from mm on
32  * output, or if the max depth gets reported as "0.2ft" it was either
33  * a really boring dive, or there was some missing input conversion,
34  * and a 60-ft dive got recorded as 60mm.
35  *
36  * Doing these as "structs containing value" means that we always
37  * have to explicitly write out those units in order to get at the
38  * actual value. So there is hopefully little fear of using a value
39  * in millikelvin as Fahrenheit by mistake.
40  *
41  * We don't actually use these all yet, so maybe they'll change, but
42  * I made a number of types as guidelines.
43  */
44 typedef struct {
45         int seconds;
46 } duration_t;
47
48 typedef struct {
49         int mm;
50 } depth_t;
51
52 typedef struct {
53         int mbar;
54 } pressure_t;
55
56 typedef struct {
57         int mkelvin;
58 } temperature_t;
59
60 typedef struct {
61         int mliter;
62 } volume_t;
63
64 typedef struct {
65         int permille;
66 } fraction_t;
67
68 typedef struct {
69         int grams;
70 } weight_t;
71
72 struct gasmix {
73         fraction_t o2;
74         fraction_t he;
75 };
76
77 typedef struct {
78         volume_t size;
79         pressure_t workingpressure;
80         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
81 } cylinder_type_t;
82
83 typedef struct {
84         cylinder_type_t type;
85         struct gasmix gasmix;
86         pressure_t start, end;
87 } cylinder_t;
88
89 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
90 extern double get_volume_units(unsigned int mm, int *frac, const char **units);
91 extern double get_temp_units(unsigned int mm, const char **units);
92
93 static inline double ml_to_cuft(int ml)
94 {
95         return ml / 28317.0;
96 }
97
98 static inline double mm_to_feet(int mm)
99 {
100         return mm * 0.00328084;
101 }
102
103 static inline int to_feet(depth_t depth)
104 {
105         return mm_to_feet(depth.mm) + 0.5;
106 }
107
108 static double mkelvin_to_C(int mkelvin)
109 {
110         return (mkelvin - 273150) / 1000.0;
111 }
112
113 static double mkelvin_to_F(int mkelvin)
114 {
115         return mkelvin * 9 / 5000.0 - 459.670;
116 }
117
118 static inline int to_C(temperature_t temp)
119 {
120         if (!temp.mkelvin)
121                 return 0;
122         return mkelvin_to_C(temp.mkelvin) + 0.5;
123 }
124
125 static inline int to_F(temperature_t temp)
126 {
127         if (!temp.mkelvin)
128                 return 0;
129         return mkelvin_to_F(temp.mkelvin) + 0.5;
130 }
131
132 static inline int to_K(temperature_t temp)
133 {
134         if (!temp.mkelvin)
135                 return 0;
136         return (temp.mkelvin + 499)/1000;
137 }
138
139 static inline int to_PSI(pressure_t pressure)
140 {
141         return pressure.mbar * 0.0145037738 + 0.5;
142 }
143
144 static inline double to_ATM(pressure_t pressure)
145 {
146         return pressure.mbar / 1013.25;
147 }
148
149 struct sample {
150         duration_t time;
151         depth_t depth;
152         temperature_t temperature;
153         pressure_t cylinderpressure;
154         int cylinderindex;
155 };
156
157 /*
158  * Events are currently pretty meaningless. This is
159  * just based on the random data that libdivecomputer
160  * gives us. I'm not sure what a real "architected"
161  * event model would actually look like, but right
162  * now you can associate a list of events with a dive,
163  * and we'll do something about it.
164  */
165 struct event {
166         struct event *next;
167         duration_t time;
168         int type, flags, value;
169         char name[];
170 };
171
172 #define MAX_CYLINDERS (8)
173
174 struct dive {
175         int number;
176         time_t when;
177         char *location;
178         char *notes;
179         char *divemaster, *buddy;
180         double latitude, longitude;
181         depth_t maxdepth, meandepth;
182         duration_t duration, surfacetime;
183         depth_t visibility;
184         temperature_t airtemp, watertemp;
185         cylinder_t cylinder[MAX_CYLINDERS];
186         int sac, otu;
187         struct event *events;
188         int samples, alloc_samples;
189         struct sample sample[];
190 };
191
192 /*
193  * We keep our internal data in well-specified units, but
194  * the input and output may come in some random format. This
195  * keeps track of those units.
196  */
197 /* turns out in Win32 PASCAL is defined as a calling convention */
198 #ifdef WIN32
199 #undef PASCAL
200 #endif
201 struct units {
202         enum { METERS, FEET } length;
203         enum { LITER, CUFT } volume;
204         enum { BAR, PSI, PASCAL } pressure;
205         enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
206         enum { KG, LBS } weight;
207 };
208
209 extern const struct units SI_units, IMPERIAL_units;
210 extern struct units input_units, output_units;
211
212 extern int verbose;
213
214 struct dive_table {
215         int nr, allocated, preexisting;
216         struct dive **dives;
217 };
218
219 extern struct dive_table dive_table;
220
221 extern int selected_dive;
222 #define current_dive (get_dive(selected_dive))
223
224 static inline struct dive *get_dive(unsigned int nr)
225 {
226         if (nr >= dive_table.nr)
227                 return NULL;
228         return dive_table.dives[nr];
229 }
230
231 extern void parse_xml_init(void);
232 extern void parse_xml_file(const char *filename, GError **error);
233 extern void set_filename(const char *filename);
234
235 extern void show_dive_info(struct dive *);
236 extern void flush_dive_info_changes(struct dive *);
237
238 extern void show_dive_equipment(struct dive *);
239 extern void flush_dive_equipment_changes(struct dive *);
240
241 extern void update_dive(struct dive *new_dive);
242 extern void save_dives(const char *filename);
243
244 static inline unsigned int dive_size(int samples)
245 {
246         return sizeof(struct dive) + samples*sizeof(struct sample);
247 }
248
249 extern time_t utc_mktime(struct tm *tm);
250
251 extern struct dive *alloc_dive(void);
252 extern void record_dive(struct dive *dive);
253
254 extern struct sample *prepare_sample(struct dive **divep);
255 extern void finish_sample(struct dive *dive, struct sample *sample);
256
257 extern void report_dives(gboolean imported);
258 extern struct dive *fixup_dive(struct dive *dive);
259 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
260
261 extern void renumber_dives(int nr);
262
263 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
264
265 /* UI related protopypes */
266
267 extern void init_ui(int argc, char **argv);
268
269 extern void run_ui(void);
270
271 extern void report_error(GError* error);
272
273 extern void add_cylinder_description(cylinder_type_t *);
274 extern void add_people(const char *string);
275 extern void add_location(const char *string);
276 extern void remember_event(const char *eventname);
277 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
278
279 extern void dive_list_update_dives(void);
280 extern void flush_divelist(struct dive *dive);
281
282 #define DIVE_ERROR_PARSE 1
283
284 const char *weekday(int wday);
285 const char *monthname(int mon);
286
287 #define UTF8_DEGREE "\xc2\xb0"
288 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
289
290 #endif /* DIVE_H */