]> git.tdb.fi Git - ext/subsurface.git/blobdiff - profile.c
Cleanup Makefile and make it work for cross compiling with MinGW
[ext/subsurface.git] / profile.c
index 7832dccafd36242c7835f45ae1ac9e115a3d13e4..56716f5f767c11646d0edaade51d2b9b4d1b427f 100644 (file)
--- a/profile.c
+++ b/profile.c
@@ -166,11 +166,62 @@ static void plot_text(struct graphics_context *gc, const text_render_options_t *
        cairo_show_text(cr, buffer);
 }
 
+struct ev_select {
+       char *ev_name;
+       gboolean plot_ev;
+};
+static struct ev_select *ev_namelist;
+static int evn_allocated;
+static int evn_used;
+
+void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
+{
+       int i;
+
+       for (i = 0; i < evn_used; i++) {
+               callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
+       }
+}
+
+void remember_event(const char *eventname)
+{
+       int i=0, len;
+
+       if (!eventname || (len = strlen(eventname)) == 0)
+               return;
+       while (i < evn_used) {
+               if (!strncmp(eventname,ev_namelist[i].ev_name,len))
+                       return;
+               i++;
+       }
+       if (evn_used == evn_allocated) {
+               evn_allocated += 10;
+               ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
+               if (! ev_namelist)
+                       /* we are screwed, but let's just bail out */
+                       return;
+       }
+       ev_namelist[evn_used].ev_name = strdup(eventname);
+       ev_namelist[evn_used].plot_ev = TRUE;
+       evn_used++;
+}
+
 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
 {
        int i, depth = 0;
        int x,y;
 
+       /* is plotting this event disabled? */
+       if (event->name) {
+               for (i = 0; i < evn_used; i++) {
+                       if (! strcmp(event->name, ev_namelist[i].ev_name)) {
+                               if (ev_namelist[i].plot_ev)
+                                       break;
+                               else
+                                       return;
+                       }
+               }
+       }
        for (i = 0; i < pi->nr; i++) {
                struct plot_data *data = pi->entry + i;
                if (event->time.seconds < data->sec)
@@ -299,23 +350,43 @@ static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *p
 
 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
 {
-       int i;
+       int i, incr;
        cairo_t *cr = gc->cr;
        int sec, depth;
        struct plot_data *entry;
        int maxtime, maxdepth, marker;
+       int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
 
        /* Get plot scaling limits */
        maxtime = get_maxtime(pi);
        maxdepth = get_maxdepth(pi);
 
-       /* Time markers: every 5 min */
+       /* Time markers: at most every 5 min, but no more than 12 markers
+        * and for convenience we do 5, 10, 15 or 30 min intervals.
+        * This allows for 6h dives - enough (I hope) for even the craziest
+        * divers - but just in case, for those 8h depth-record-breaking dives,
+        * we double the interval if this still doesn't get us to 12 or fewer
+        * time markers */
+       i = 0;
+       while (maxtime / increments[i] > 12 && i < 4)
+               i++;
+       incr = increments[i];
+       while (maxtime / incr > 12)
+               incr *= 2;
+
        gc->leftx = 0; gc->rightx = maxtime;
        gc->topy = 0; gc->bottomy = 1.0;
-       for (i = 5*60; i < maxtime; i += 5*60) {
+       set_source_rgba(gc, 1, 1, 1, 0.5);
+       for (i = incr; i < maxtime; i += incr) {
                move_to(gc, i, 0);
                line_to(gc, i, 1);
        }
+       cairo_stroke(cr);
+
+       /* now the text on every second time marker */
+       text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
+       for (i = incr; i < maxtime; i += 2 * incr)
+               plot_text(gc, &tro, i, 1, "%d", i/60);
 
        /* Depth markers: every 30 ft or 10 m*/
        gc->leftx = 0; gc->rightx = 1.0;
@@ -860,6 +931,68 @@ static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
        }
 }
 
+static int get_cylinder_index(struct dive *dive, struct event *ev)
+{
+       int i;
+
+       /*
+        * Try to find a cylinder that matches the O2 percentage
+        * in the gas change event 'value' field.
+        *
+        * Crazy suunto gas change events. We really should do
+        * this in libdivecomputer or something.
+        */
+       for (i = 0; i < MAX_CYLINDERS; i++) {
+               cylinder_t *cyl = dive->cylinder+i;
+               int o2 = (cyl->gasmix.o2.permille + 5) / 10;
+               if (o2 == ev->value)
+                       return i;
+       }
+
+       return 0;
+}
+
+static struct event *get_next_gaschange(struct event *event)
+{
+       while (event) {
+               if (!strcmp(event->name, "gaschange"))
+                       return event;
+               event = event->next;
+       }
+       return event;
+}
+
+static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
+{
+       while (i < pi->nr) {
+               struct plot_data *entry = pi->entry+i;
+               if (entry->sec > end)
+                       break;
+               if (entry->cylinderindex != cylinderindex) {
+                       entry->cylinderindex = cylinderindex;
+                       entry->pressure[0] = 0;
+               }
+               i++;
+       }
+       return i;
+}
+
+static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
+{
+       int i = 0, cylinderindex = 0;
+       struct event *ev = get_next_gaschange(dive->events);
+
+       if (!ev)
+               return;
+
+       do {
+               i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
+               cylinderindex = get_cylinder_index(dive, ev);
+               ev = get_next_gaschange(ev->next);
+       } while (ev);
+       set_cylinder_index(pi, i, cylinderindex, ~0u);
+}
+
 /*
  * Create a plot-info with smoothing and ranged min/max
  *
@@ -887,9 +1020,6 @@ static struct plot_info *create_plot_info(struct dive *dive)
        sec = 0;
        lastindex = 0;
        lastdepth = -1;
-       for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
-               track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
-       current = track_pr[dive->sample[0].cylinderindex];
        for (i = 0; i < dive->samples; i++) {
                int depth;
                struct sample *sample = dive->sample+i;
@@ -897,9 +1027,29 @@ static struct plot_info *create_plot_info(struct dive *dive)
                entry = pi->entry + i + 2;
                sec = entry->sec = sample->time.seconds;
                depth = entry->depth = sample->depth.mm;
-               entry->same_cylinder = sample->cylinderindex == cylinderindex;
-               entry->cylinderindex = cylinderindex = sample->cylinderindex;
+               entry->cylinderindex = sample->cylinderindex;
                SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
+               entry->temperature = sample->temperature.mkelvin;
+
+               if (depth || lastdepth)
+                       lastindex = i+2;
+
+               lastdepth = depth;
+               if (depth > pi->maxdepth)
+                       pi->maxdepth = depth;
+       }
+
+       check_gas_change_events(dive, pi);
+
+       for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
+               track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
+       current = track_pr[dive->sample[0].cylinderindex];
+       for (i = 0; i < dive->samples; i++) {
+               entry = pi->entry + i + 2;
+
+               entry->same_cylinder = entry->cylinderindex == cylinderindex;
+               cylinderindex = entry->cylinderindex;
+
                /* track the segments per cylinder and their pressure/time integral */
                if (!entry->same_cylinder) {
                        current->end = SENSOR_PRESSURE(entry-1);
@@ -921,15 +1071,8 @@ static struct plot_info *create_plot_info(struct dive *dive)
                current->pressure_time += (entry->sec - (entry-1)->sec) *
                                                (1 + entry->depth / 10000.0);
                missing_pr |= !SENSOR_PRESSURE(entry);
-               entry->temperature = sample->temperature.mkelvin;
-
-               if (depth || lastdepth)
-                       lastindex = i+2;
-
-               lastdepth = depth;
-               if (depth > pi->maxdepth)
-                       pi->maxdepth = depth;
        }
+
        current->t_end = entry->sec;
        for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
                int pr = dive->cylinder[cyl].end.mbar;