]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Add placeholder for cylinder type description
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 #include <stdlib.h>
5 #include <time.h>
6
7 /*
8  * Some silly typedefs to make our units very explicit.
9  *
10  * Also, the units are chosen so that values can be expressible as
11  * integers, so that we never have FP rounding issues. And they
12  * are small enough that converting to/from imperial units doesn't
13  * really matter.
14  *
15  * We also strive to make '0' a meaningless number saying "not
16  * initialized", since many values are things that may not have
17  * been reported (eg cylinder pressure or temperature from dive
18  * computers that don't support them). But sometimes -1 is an even
19  * more explicit way of saying "not there".
20  *
21  * Thus "millibar" for pressure, for example, or "millikelvin" for
22  * temperatures. Doing temperatures in celsius or fahrenheit would
23  * make for loss of precision when converting from one to the other,
24  * and using millikelvin is SI-like but also means that a temperature
25  * of '0' is clearly just a missing temperature or cylinder pressure.
26  *
27  * Also strive to use units that can not possibly be mistaken for a
28  * valid value in a "normal" system without conversion. If the max
29  * depth of a dive is '20000', you probably didn't convert from mm on
30  * output, or if the max depth gets reported as "0.2ft" it was either
31  * a really boring dive, or there was some missing input conversion,
32  * and a 60-ft dive got recorded as 60mm.
33  *
34  * Doing these as "structs containing value" means that we always
35  * have to explicitly write out those units in order to get at the
36  * actual value. So there is hopefully little fear of using a value
37  * in millikelvin as Fahrenheit by mistake.
38  *
39  * We don't actually use these all yet, so maybe they'll change, but
40  * I made a number of types as guidelines.
41  */
42 typedef struct {
43         int seconds;
44 } duration_t;
45
46 typedef struct {
47         int mm;
48 } depth_t;
49
50 typedef struct {
51         int mbar;
52 } pressure_t;
53
54 typedef struct {
55         int mkelvin;
56 } temperature_t;
57
58 typedef struct {
59         int mliter;
60 } volume_t;
61
62 typedef struct {
63         int permille;
64 } fraction_t;
65
66 typedef struct {
67         int grams;
68 } weight_t;
69
70 typedef struct {
71         fraction_t o2;
72         fraction_t he;
73 } gasmix_t;
74
75 typedef struct {
76         volume_t size;
77         pressure_t workingpressure;
78         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
79 } cylinder_type_t;
80
81 typedef struct {
82         cylinder_type_t type;
83         gasmix_t gasmix;
84 } cylinder_t;
85
86 static inline int to_feet(depth_t depth)
87 {
88         return depth.mm * 0.00328084 + 0.5;
89 }
90
91 static inline int to_C(temperature_t temp)
92 {
93         if (!temp.mkelvin)
94                 return 0;
95         return (temp.mkelvin - 273150) / 1000;
96 }
97
98 static inline int to_PSI(pressure_t pressure)
99 {
100         return pressure.mbar * 0.0145037738 + 0.5;
101 }
102
103 struct sample {
104         duration_t time;
105         depth_t depth;
106         temperature_t temperature;
107         pressure_t cylinderpressure;
108         int cylinderindex;
109 };
110
111 #define MAX_CYLINDERS (4)
112
113 struct dive {
114         const char *name;
115         time_t when;
116         char *location;
117         char *notes;
118         depth_t maxdepth, meandepth;
119         duration_t duration, surfacetime;
120         depth_t visibility;
121         temperature_t airtemp, watertemp;
122         pressure_t beginning_pressure, end_pressure;
123         cylinder_t cylinder[MAX_CYLINDERS];
124         int samples;
125         struct sample sample[];
126 };
127
128 extern int verbose;
129
130 struct dive_table {
131         int nr, allocated;
132         struct dive **dives;
133 };
134
135 extern struct dive_table dive_table;
136
137 static inline struct dive *get_dive(unsigned int nr)
138 {
139         if (nr >= dive_table.nr)
140                 return NULL;
141         return dive_table.dives[nr];
142 }
143
144 extern void parse_xml_init(void);
145 extern void parse_xml_file(const char *filename);
146
147 extern void flush_dive_info_changes(void);
148 extern void save_dives(const char *filename);
149
150 static inline unsigned int dive_size(int samples)
151 {
152         return sizeof(struct dive) + samples*sizeof(struct sample);
153 }
154
155 extern struct dive *fixup_dive(struct dive *dive);
156 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
157
158 #endif /* DIVE_H */