private Weight ReadWeight() { _readLock.Wait(); try { int raw = ReadRaw(); double unitValue = (raw - TareValue) / _settings.CalibrationValue; IWeightSystem baseWeight = null; if (_settings.CalibrationWeightSystem == WeightSystem.Metric) { baseWeight = new MetricWeight(unitValue, _settings.CalibrationMetricUnit); } else if (_settings.CalibrationWeightSystem == WeightSystem.Imperial) { baseWeight = new ImperialWeight(unitValue, _settings.CalibrationImperialUnit); } else { throw new Exception("HX711 was uncalibrated or not calibrated properly and can't convert to a weight!"); } return(new Weight(baseWeight)); } finally { _readLock.Release(); } }
public Weight(IWeightSystem weight) { if (weight is MetricWeight mw) { Metric = mw; } else if (weight is ImperialWeight iw) { Imperial = iw; } else { throw new UnknownConversionException($"Internal IoT Library Error! Unknown conversion to 'Weight' from '{weight.GetType()}'"); } }
/// <summary> /// New Imperial Weight measurement from existing weight measurement /// </summary> /// <param name="weightSystem"></param> public ImperialWeight(IWeightSystem weightSystem) { _pounds = _ounces = _grains = _stones = _tons = 0; if (weightSystem is MetricWeight mw) { BaseValue = mw.Grams * GRAMS_CONVERSION_FACTOR; } else if (weightSystem is ImperialWeight iw) { _pounds = iw.Pounds; _ounces = iw.Ounces; _grains = iw.Grains; _stones = iw.Stones; _tons = iw.Tons; } else { throw new UnknownConversionException($"Unknown conversion from '{weightSystem.GetType().ToString()}' to 'Imperial'"); } }
/// <summary> /// New Metric Weight measurement from existing weight measurement /// </summary> /// <param name="weightSystem"></param> public MetricWeight(IWeightSystem weightSystem) { _picograms = _nanograms = _micrograms = _milligrams = _grams = _kilograms = _megagrams = 0; if (weightSystem is ImperialWeight iw) { BaseValue = iw.Pounds / POUNDS_CONVERSION_FACTOR; } else if (weightSystem is MetricWeight mw) { _picograms = mw.Picograms; _nanograms = mw.Nanograms; _micrograms = mw.Micrograms; _milligrams = mw.Milligrams; _grams = mw.Grams; _kilograms = mw.Kilograms; _megagrams = mw.Megagrams; } else { throw new UnknownConversionException($"Unknown conversion from '{weightSystem.GetType().ToString()}' to 'Imperial'"); } }