void add_sample_data(struct sample *sample, enum csv_format type, double val) { switch (type) { case CSV_DEPTH: sample->depth.mm = feet_to_mm(val); break; case CSV_TEMP: sample->temperature.mkelvin = F_to_mkelvin(val); break; case CSV_PRESSURE: sample->pressure[0].mbar = psi_to_mbar(val * 4); break; case POSEIDON_DEPTH: sample->depth.mm = lrint(val * 0.5 * 1000); break; case POSEIDON_TEMP: sample->temperature.mkelvin = C_to_mkelvin(val * 0.2); break; case POSEIDON_SETPOINT: sample->setpoint.mbar = lrint(val * 10); break; case POSEIDON_SENSOR1: sample->o2sensor[0].mbar = lrint(val * 10); break; case POSEIDON_SENSOR2: sample->o2sensor[1].mbar = lrint(val * 10); break; case POSEIDON_NDL: sample->ndl.seconds = lrint(val * 60); break; case POSEIDON_CEILING: sample->stopdepth.mm = lrint(val * 1000); break; } }
void DivePlannerPointsModel::setReserveGas(int reserve) { if (prefs.units.pressure == units::BAR) prefs.reserve_gas = reserve * 1000; else prefs.reserve_gas = psi_to_mbar(reserve); emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1)); }
void DivePlannerPointsModel::setReserveGas(int reserve) { auto planner = SettingsObjectWrapper::instance()->planner_settings; if (prefs.units.pressure == units::BAR) planner->setReserveGas(reserve * 1000); else planner->setReserveGas(psi_to_mbar(reserve)); emitDataChanged(); }
static void add_sample_data(struct sample *sample, enum csv_format type, double val) { switch (type) { case CSV_DEPTH: sample->depth.mm = feet_to_mm(val); break; case CSV_TEMP: sample->temperature.mkelvin = F_to_mkelvin(val); break; case CSV_PRESSURE: sample->cylinderpressure.mbar = psi_to_mbar(val*4); break; } }
/* * Did I mention how bat-shit crazy divinglog is? The sample * pressures are in PSI. But the tank working pressure is in * bar. WTF^2? * * Crazy stuff like this is why subsurface has everything in * these inconvenient typed structures, and you have to say * "pressure->mbar" to get the actual value. Exactly so that * you can never have unit confusion. * * It gets worse: sometimes apparently the pressures are in * bar, sometimes in psi. Dirk suspects that this may be a * DivingLog Uemis importer bug, and that they are always * supposed to be in bar, but that the importer got the * sample importing wrong. * * Sadly, there's no way to really tell. So I think we just * have to have some arbitrary cut-off point where we assume * that smaller values mean bar.. Not good. */ static void psi_or_bar(char *buffer, void *_pressure) { pressure_t *pressure = _pressure; union int_or_float val; switch (integer_or_float(buffer, &val)) { case FLOAT: if (val.fp > 400) pressure->mbar = psi_to_mbar(val.fp); else pressure->mbar = val.fp * 1000 + 0.5; break; default: fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer); } }
/* if a default cylinder is set, use that */ void fill_default_cylinder(cylinder_t *cyl) { const char *cyl_name = prefs.default_cylinder; struct tank_info_t *ti = tank_info; pressure_t pO2 = {.mbar = 1600}; if (!cyl_name) return; while (ti->name != NULL) { if (strcmp(ti->name, cyl_name) == 0) break; ti++; } if (ti->name == NULL) /* didn't find it */ return; cyl->type.description = strdup(ti->name); if (ti->ml) { cyl->type.size.mliter = ti->ml; cyl->type.workingpressure.mbar = ti->bar * 1000; } else { cyl->type.workingpressure.mbar = psi_to_mbar(ti->psi); if (ti->psi) cyl->type.size.mliter = cuft_to_l(ti->cuft) * 1000 / bar_to_atm(psi_to_bar(ti->psi)); } // MOD of air cyl->depth = gas_mod(&cyl->gasmix, pO2, 1); } /* make sure that the gas we are switching to is represented in our * list of cylinders */ static int verify_gas_exists(struct gasmix mix_in) { int i; cylinder_t *cyl; for (i = 0; i < MAX_CYLINDERS; i++) { cyl = displayed_dive.cylinder + i; if (cylinder_nodata(cyl)) continue; if (gasmix_distance(&cyl->gasmix, &mix_in) < 200) return i; } fprintf(stderr, "this gas %s should have been on the cylinder list\nThings will fail now\n", gasname(&mix_in)); return -1; }
void TestUnitConversion::testUnitConversions() { QCOMPARE(IS_FP_SAME(grams_to_lbs(1000), 2.204586), true); QCOMPARE(lbs_to_grams(1), 454); QCOMPARE(IS_FP_SAME(ml_to_cuft(1000), 0.0353147), true); QCOMPARE(IS_FP_SAME(cuft_to_l(1), 28.316847), true); QCOMPARE(IS_FP_SAME(mm_to_feet(1000), 3.280840), true); QCOMPARE(feet_to_mm(1), (long unsigned int) 305); QCOMPARE(to_feet((depth_t){ 1000 }), 3); QCOMPARE(IS_FP_SAME(mkelvin_to_C(647000), 373.85), true); QCOMPARE(IS_FP_SAME(mkelvin_to_F(647000), 704.93), true); QCOMPARE(F_to_mkelvin(704.93), (unsigned long)647000); QCOMPARE(C_to_mkelvin(373.85), (unsigned long)647000); QCOMPARE(IS_FP_SAME(psi_to_bar(14.6959488), 1.01325), true); QCOMPARE(psi_to_mbar(14.6959488), (long)1013); QCOMPARE(to_PSI((pressure_t){ 1013 }), (int)15); QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1.0), true); QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1.0), true); QCOMPARE(mbar_to_PSI(1013), (int)15); get_units(); }
//TODO: Move to C. pressure_t string_to_pressure(const char *str) { const char *end; double value = strtod_flags(str, &end, 0); QString rest = QString(end).trimmed(); QString local_psi = QObject::tr("psi"); QString local_bar = QObject::tr("bar"); pressure_t pressure; if (rest.startsWith("bar") || rest.startsWith(local_bar)) goto bar; if (rest.startsWith("psi") || rest.startsWith(local_psi)) goto psi; if (prefs.units.pressure == prefs.units.PSI) goto psi; bar: pressure.mbar = rint(value * 1000); return pressure; psi: pressure.mbar = psi_to_mbar(value); return pressure; }
bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, int role) { bool addDiveMode = DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING; if (addDiveMode) DivePlannerPointsModel::instance()->rememberTanks(); cylinder_t *cyl = cylinderAt(index); switch(index.column()) { case TYPE: if (!value.isNull()) { QByteArray ba = value.toByteArray(); const char *text = ba.constData(); if (!cyl->type.description || strcmp(cyl->type.description, text)) { cyl->type.description = strdup(text); changed = true; } } break; case SIZE: if (CHANGED(toDouble, "cuft", "l")) { // if units are CUFT then this value is meaningless until we have working pressure if (value.toDouble() != 0.0) { TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description); int mbar = cyl->type.workingpressure.mbar; int mliter; if (mbar && prefs.units.volume == prefs.units.CUFT) { double liters = cuft_to_l(value.toDouble()); liters /= bar_to_atm(mbar / 1000.0); mliter = rint(liters * 1000); } else { mliter = rint(value.toDouble() * 1000); } if (cyl->type.size.mliter != mliter) { mark_divelist_changed(TRUE); cyl->type.size.mliter = mliter; if (!matches.isEmpty()) tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl->type.size.mliter); } changed = true; } } break; case WORKINGPRESS: if (CHANGED(toDouble, "psi", "bar")) { QString vString = value.toString(); vString.remove("psi").remove("bar"); if (vString.toDouble() != 0.0) { TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description); if (prefs.units.pressure == prefs.units.PSI) cyl->type.workingpressure.mbar = psi_to_mbar(vString.toDouble()); else cyl->type.workingpressure.mbar = vString.toDouble() * 1000; if (!matches.isEmpty()) tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0); changed = true; } } break; case START: if (CHANGED(toDouble, "psi", "bar")) { if (value.toDouble() != 0.0) { if (prefs.units.pressure == prefs.units.PSI) cyl->start.mbar = psi_to_mbar(value.toDouble()); else cyl->start.mbar = value.toDouble() * 1000; changed = true; } } break; case END: if (CHANGED(toDouble, "psi", "bar")) { if (value.toDouble() != 0.0) { if (prefs.units.pressure == prefs.units.PSI) cyl->end.mbar = psi_to_mbar(value.toDouble()); else cyl->end.mbar = value.toDouble() * 1000; changed = true; } } break; case O2: if (CHANGED(toDouble, "%", "%")) { int o2 = value.toString().remove('%').toDouble() * 10 + 0.5; if (cyl->gasmix.he.permille + o2 <= 1000) { cyl->gasmix.o2.permille = o2; changed = true; } } break; case HE: if (CHANGED(toDouble, "%", "%")) { int he = value.toString().remove('%').toDouble() * 10 + 0.5; if (cyl->gasmix.o2.permille + he <= 1000) { cyl->gasmix.he.permille = he; changed = true; } } break; case DEPTH: if (CHANGED(toDouble, "ft", "m")) { if (value.toInt() != 0) { if (prefs.units.length == prefs.units.FEET) cyl->depth.mm = feet_to_mm(value.toString().remove("ft").remove("m").toInt()); else cyl->depth.mm = value.toString().remove("ft").remove("m").toInt() * 1000; } } } dataChanged(index, index); if (addDiveMode) DivePlannerPointsModel::instance()->tanksUpdated(); return true; }
bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, int role) { cylinder_t *cyl = ¤t->cylinder[index.row()]; switch(index.column()) { case TYPE: if (!value.isNull()) { QByteArray ba = value.toByteArray(); const char *text = ba.constData(); if (!cyl->type.description || strcmp(cyl->type.description, text)) { cyl->type.description = strdup(text); mark_divelist_changed(TRUE); } } break; case SIZE: if (CHANGED(toDouble, "cuft", "l")) { // if units are CUFT then this value is meaningless until we have working pressure if (value.toDouble() != 0.0) { TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description); int mbar = cyl->type.workingpressure.mbar; int mliter; if (mbar && prefs.units.volume == prefs.units.CUFT) { double liters = cuft_to_l(value.toDouble()); liters /= bar_to_atm(mbar / 1000.0); mliter = rint(liters * 1000); } else { mliter = rint(value.toDouble() * 1000); } if (cyl->type.size.mliter != mliter) { mark_divelist_changed(TRUE); cyl->type.size.mliter = mliter; if (!matches.isEmpty()) tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl->type.size.mliter); } } } break; case WORKINGPRESS: if (CHANGED(toDouble, "psi", "bar")) { if (value.toDouble() != 0.0) { TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description); if (prefs.units.pressure == prefs.units.PSI) cyl->type.workingpressure.mbar = psi_to_mbar(value.toDouble()); else cyl->type.workingpressure.mbar = value.toDouble() * 1000; if (!matches.isEmpty()) tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0); mark_divelist_changed(TRUE); } } break; case START: if (CHANGED(toDouble, "psi", "bar")) { if (value.toDouble() != 0.0) { if (prefs.units.pressure == prefs.units.PSI) cyl->start.mbar = psi_to_mbar(value.toDouble()); else cyl->start.mbar = value.toDouble() * 1000; mark_divelist_changed(TRUE); } } break; case END: if (CHANGED(toDouble, "psi", "bar")) { if (value.toDouble() != 0.0) { if (prefs.units.pressure == prefs.units.PSI) cyl->end.mbar = psi_to_mbar(value.toDouble()); else cyl->end.mbar = value.toDouble() * 1000; } } break; case O2: if (CHANGED(toDouble, "%", "%")) { cyl->gasmix.o2.permille = value.toDouble() * 10 + 0.5; mark_divelist_changed(TRUE); } break; case HE: if (CHANGED(toDouble, "%", "%")) { cyl->gasmix.he.permille = value.toDouble() * 10 + 0.5; mark_divelist_changed(TRUE); } break; } return QAbstractItemModel::setData(index, value, role); }