+}
+
+/* gets both the actual start and end pressure as well as the scaling factors */
+static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
+{
+ gc->leftx = 0;
+ gc->rightx = get_maxtime(pi);
+
+ gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
+ return pi->maxpressure != 0;
+}
+
+/* set the color for the pressure plot according to temporary sac rate
+ * as compared to avg_sac; the calculation simply maps the delta between
+ * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
+ * more than 6000 ml/min below avg_sac mapped to 0 */
+
+static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
+{
+ int sac_index = 0;
+ int delta = sac - avg_sac + 7000;
+
+ if (!gc->printer) {
+ sac_index = delta / 2000;
+ if (sac_index < 0)
+ sac_index = 0;
+ if (sac_index > SAC_COLORS - 1)
+ sac_index = SAC_COLORS - 1;
+ set_source_rgba(gc, SAC_COLORS_START_IDX + sac_index);
+ } else {
+ set_source_rgba(gc, SAC_DEFAULT);
+ }
+}
+
+/* calculate the current SAC in ml/min and convert to int */
+#define GET_LOCAL_SAC(_entry1, _entry2, _dive) (int) \
+ ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) * \
+ (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter / \
+ (((_entry2)->sec - (_entry1)->sec) / 60.0) / \
+ (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) / \
+ 1000.0)
+
+#define SAC_WINDOW 45 /* sliding window in seconds for current SAC calculation */
+
+static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
+ struct dive *dive)
+{
+ int i;
+ int last = -1;
+ int lift_pen = FALSE;
+ int first_plot = TRUE;
+ int sac = 0;
+ struct plot_data *last_entry = NULL;
+
+ if (!get_cylinder_pressure_range(gc, pi))
+ return;
+
+ cairo_set_line_width(gc->cr, 2);
+
+ for (i = 0; i < pi->nr; i++) {
+ int mbar;
+ struct plot_data *entry = pi->entry + i;
+
+ mbar = GET_PRESSURE(entry);
+ if (!entry->same_cylinder) {
+ lift_pen = TRUE;
+ last_entry = NULL;
+ }
+ if (!mbar) {
+ lift_pen = TRUE;
+ continue;
+ }
+ if (!last_entry) {
+ last = i;
+ last_entry = entry;
+ sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
+ } else {
+ int j;
+ sac = 0;
+ for (j = last; j < i; j++)
+ sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
+ sac /= (i - last);
+ if (entry->sec - last_entry->sec >= SAC_WINDOW) {
+ last++;
+ last_entry = pi->entry + last;
+ }
+ }
+ set_sac_color(gc, sac, dive->sac);
+ if (lift_pen) {
+ if (!first_plot && entry->same_cylinder) {
+ /* if we have a previous event from the same tank,
+ * draw at least a short line */
+ int prev_pr;
+ prev_pr = GET_PRESSURE(entry - 1);
+ move_to(gc, (entry-1)->sec, prev_pr);
+ line_to(gc, entry->sec, mbar);
+ } else {
+ first_plot = FALSE;
+ move_to(gc, entry->sec, mbar);
+ }
+ lift_pen = FALSE;
+ } else {
+ line_to(gc, entry->sec, mbar);
+ }
+ cairo_stroke(gc->cr);
+ move_to(gc, entry->sec, mbar);
+ }
+}
+
+static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
+ int xalign, int yalign)
+{
+ int pressure;
+ const char *unit;
+
+ pressure = get_pressure_units(mbar, &unit);
+ text_render_options_t tro = {10, PRESSURE_TEXT, xalign, yalign};
+ plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
+}
+
+static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
+{
+ int i;
+ int mbar, cyl;
+ int seen_cyl[MAX_CYLINDERS] = { FALSE, };
+ int last_pressure[MAX_CYLINDERS] = { 0, };
+ int last_time[MAX_CYLINDERS] = { 0, };
+ struct plot_data *entry;
+
+ if (!get_cylinder_pressure_range(gc, pi))
+ return;
+
+ /* only loop over the actual events from the dive computer
+ * plus the second synthetic event at the start (to make sure
+ * we get "time=0" right)
+ * sadly with a recent change that first entry may no longer
+ * have any pressure reading - in that case just grab the
+ * pressure from the second entry */
+ if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
+ INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+
+ if (!entry->same_cylinder) {
+ cyl = entry->cylinderindex;
+ if (!seen_cyl[cyl]) {
+ mbar = GET_PRESSURE(entry);
+ plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
+ seen_cyl[cyl] = TRUE;
+ }
+ if (i > 2) {
+ /* remember the last pressure and time of
+ * the previous cylinder */
+ cyl = (entry - 1)->cylinderindex;
+ last_pressure[cyl] = GET_PRESSURE(entry - 1);
+ last_time[cyl] = (entry - 1)->sec;
+ }
+ }
+ }
+ cyl = entry->cylinderindex;
+ if (GET_PRESSURE(entry))
+ last_pressure[cyl] = GET_PRESSURE(entry);
+ last_time[cyl] = entry->sec;
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ if (last_time[cyl]) {
+ plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
+ }
+ }
+}
+
+static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
+{
+ struct plot_data *p = entry;
+ int time = entry->sec;
+ int seconds = 90*(index+1);
+ struct plot_data *min, *max;
+ int avg, nr;
+
+ /* Go back 'seconds' in time */
+ while (p > first) {
+ if (p[-1].sec < time - seconds)
+ break;
+ p--;
+ }
+
+ /* Then go forward until we hit an entry past the time */
+ min = max = p;
+ avg = p->depth;
+ nr = 1;
+ while (++p < last) {
+ int depth = p->depth;
+ if (p->sec > time + seconds)
+ break;
+ avg += depth;
+ nr ++;
+ if (depth < min->depth)
+ min = p;
+ if (depth > max->depth)
+ max = p;
+ }
+ entry->min[index] = min;
+ entry->max[index] = max;
+ entry->avg[index] = (avg + nr/2) / nr;
+}
+
+static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
+{
+ analyze_plot_info_minmax_minute(entry, first, last, 0);
+ analyze_plot_info_minmax_minute(entry, first, last, 1);
+ analyze_plot_info_minmax_minute(entry, first, last, 2);
+}
+
+static velocity_t velocity(int speed)
+{
+ velocity_t v;
+
+ if (speed < -304) /* ascent faster than -60ft/min */
+ v = CRAZY;
+ else if (speed < -152) /* above -30ft/min */
+ v = FAST;
+ else if (speed < -76) /* -15ft/min */
+ v = MODERATE;
+ else if (speed < -25) /* -5ft/min */
+ v = SLOW;
+ else if (speed < 25) /* very hard to find data, but it appears that the recommendations
+ for descent are usually about 2x ascent rate; still, we want
+ stable to mean stable */
+ v = STABLE;
+ else if (speed < 152) /* between 5 and 30ft/min is considered slow */
+ v = SLOW;
+ else if (speed < 304) /* up to 60ft/min is moderate */
+ v = MODERATE;
+ else if (speed < 507) /* up to 100ft/min is fast */
+ v = FAST;
+ else /* more than that is just crazy - you'll blow your ears out */
+ v = CRAZY;
+
+ return v;
+}
+static struct plot_info *analyze_plot_info(struct plot_info *pi)
+{
+ int i;
+ int nr = pi->nr;
+
+ /* Do pressure min/max based on the non-surface data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int pressure = GET_PRESSURE(entry);
+ int temperature = entry->temperature;
+
+ if (pressure) {
+ if (pressure > pi->maxpressure)
+ pi->maxpressure = pressure;
+ }
+
+ if (temperature) {
+ if (!pi->mintemp || temperature < pi->mintemp)
+ pi->mintemp = temperature;
+ if (temperature > pi->maxtemp)
+ pi->maxtemp = temperature;
+ }
+ }
+
+ /* Smoothing function: 5-point triangular smooth */
+ for (i = 2; i < nr; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int depth;
+
+ if (i < nr-2) {
+ depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
+ entry->smoothed = (depth+4) / 9;
+ }
+ /* vertical velocity in mm/sec */
+ /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
+ if (entry[0].sec - entry[-1].sec) {
+ entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
+ /* if our samples are short and we aren't too FAST*/
+ if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
+ int past = -2;
+ while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
+ past--;
+ entry->velocity = velocity((entry[0].depth - entry[past].depth) /
+ (entry[0].sec - entry[past].sec));
+ }
+ } else
+ entry->velocity = STABLE;
+ }
+
+ /* One-, two- and three-minute minmax data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry +i;
+ analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
+ }
+
+ return pi;
+}
+
+/*
+ * simple structure to track the beginning and end tank pressure as
+ * well as the integral of depth over time spent while we have no
+ * pressure reading from the tank */
+typedef struct pr_track_struct pr_track_t;
+struct pr_track_struct {
+ int start;
+ int end;
+ int t_start;
+ int t_end;
+ double pressure_time;
+ pr_track_t *next;
+};
+
+static pr_track_t *pr_track_alloc(int start, int t_start) {
+ pr_track_t *pt = malloc(sizeof(pr_track_t));
+ pt->start = start;
+ pt->t_start = t_start;
+ pt->end = 0;
+ pt->t_end = 0;
+ pt->pressure_time = 0.0;
+ pt->next = NULL;
+ return pt;
+}
+
+/* poor man's linked list */
+static pr_track_t *list_last(pr_track_t *list)
+{
+ pr_track_t *tail = list;
+ if (!tail)
+ return NULL;
+ while (tail->next) {
+ tail = tail->next;
+ }
+ return tail;
+}
+
+static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
+{
+ pr_track_t *tail = list_last(list);
+ if (!tail)
+ return element;
+ tail->next = element;
+ return list;
+}
+
+static void list_free(pr_track_t *list)
+{
+ if (!list)
+ return;
+ list_free(list->next);
+ free(list);
+}
+
+static void dump_pr_track(pr_track_t **track_pr)
+{
+ int cyl;
+ pr_track_t *list;
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ list = track_pr[cyl];
+ while (list) {
+ printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
+ list->start, list->end, list->t_start, list->t_end, list->pressure_time);
+ list = list->next;
+ }
+ }
+}
+
+static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
+ pr_track_t **track_pr)
+{
+ pr_track_t *list = NULL;
+ pr_track_t *nlist = NULL;
+ double pt, magic;
+ int cyl, i;
+ struct plot_data *entry;
+ int cur_pr[MAX_CYLINDERS];
+
+ if (0) {
+ /* another great debugging tool */
+ dump_pr_track(track_pr);
+ }
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ cur_pr[cyl] = track_pr[cyl]->start;
+ }
+
+ /* The first two are "fillers", but in case we don't have a sample
+ * at time 0 we need to process the second of them here */
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+ if (SENSOR_PRESSURE(entry)) {
+ cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
+ } else {
+ if(!list || list->t_end < entry->sec) {
+ nlist = track_pr[entry->cylinderindex];
+ list = NULL;
+ while (nlist && nlist->t_start <= entry->sec) {
+ list = nlist;
+ nlist = list->next;
+ }
+ /* there may be multiple segments - so
+ * let's assemble the length */
+ nlist = list;
+ pt = list->pressure_time;
+ while (!nlist->end) {
+ nlist = nlist->next;
+ if (!nlist) {
+ /* oops - we have no end pressure,
+ * so this means this is a tank without
+ * gas consumption information */
+ break;
+ }
+ pt += nlist->pressure_time;
+ }
+ if (!nlist) {
+ /* just continue without calculating
+ * interpolated values */
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ list = NULL;
+ continue;
+ }
+ magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
+ }
+ if (pt != 0.0) {
+ double cur_pt = (entry->sec - (entry-1)->sec) *
+ (1 + (entry->depth + (entry-1)->depth) / 20000.0);
+ INTERPOLATED_PRESSURE(entry) =
+ cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
+ cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
+ } else
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ }
+ }
+}
+
+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;
+}