apieceoffruit

EmteqHeartRateService

Oct 30th, 2020
572
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public class EmteqHeartRateService : IHeartRateService
  2.     {
  3.         public const float CALIBRATION_DURATION = 2;
  4.        
  5.         public double Baseline { get; protected set; }
  6.         public double HeartRate { get; protected set; }
  7.         public bool Ready { get; protected set; }
  8.  
  9.         public float IncreasePercent { get; protected set; }
  10.        
  11.         public EmteqHeartRateService(MonoBehaviour owner)
  12.         {
  13.             _owner = owner;
  14.             _heartrate = new EmteqHeartrate();
  15.             _heartrate.BaselineSet += OnBaselineSet;
  16.             _heartrate.HeartRate += OnHeartrate;
  17.             _heartrate.CalibrationStarted += Calibrate;
  18.         }
  19.  
  20.         float PercentDiff(float a,float b) => (b - a) / Math.Abs(a) * 100;
  21.         void Calibrate() => _owner.StartCoroutine(Co_CalibrateFor(CALIBRATION_DURATION));
  22.  
  23.         IEnumerator Co_CalibrateFor(float seconds)
  24.         {
  25.             yield return new WaitForSeconds(seconds);
  26.             _heartrate.EndCalibration();
  27.             Ready = true;
  28.         }
  29.  
  30.  
  31.         void OnBaselineSet(double val) => Baseline = val;
  32.         void OnHeartrate(double obj)
  33.         {
  34.             HeartRate = obj;
  35.             IncreasePercent = PercentDiff((float)Baseline, (float)HeartRate);
  36.         }
  37.  
  38.         public void Dispose()
  39.         {
  40.             _heartrate.BaselineSet -= OnBaselineSet;
  41.             _heartrate.HeartRate -= OnHeartrate;
  42.             _heartrate.CalibrationStarted -= Calibrate;
  43.             _heartrate?.Dispose();
  44.         }
  45.        
  46.         readonly EmteqHeartrate _heartrate;
  47.         readonly MonoBehaviour _owner;
  48.     }
RAW Paste Data