Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using ArduinoBluetoothAPI;
  6. using System;
  7. using UnityEngine.Events;
  8. using System.Text;
  9. using TMPro;
  10.  
  11.  
  12. //public enum Tier {1,2,3 }
  13.  
  14. [Serializable]
  15. public class HeartRateEvent : UnityEvent<string>
  16. {
  17.  
  18. }
  19. public class HeartData : MonoBehaviour
  20. {
  21.  
  22. BluetoothHelper bluetoothHelper;
  23.  
  24. public float baseLine, currentTier;
  25. private float tier0, tier1, tier2, tier3,baseTier, adjustingTier, difference, increment, median, joystickX,counter;
  26. public Vector2 joystickPos;
  27. private List<float> MedianHeartRateCol = new List<float>();
  28. private List<float> previousTiers = new List<float>();
  29. bool pressed, running;
  30. public AudioSource src;
  31.  
  32. public GameObject btn1,btn2;
  33.  
  34. //only for testing
  35. // public GameObject elevator;
  36.  
  37. [HideInInspector]
  38. public HeartRateEvent getHeartData;
  39.  
  40. public BaseLineCalculator bls;
  41.  
  42. public TMP_Text avgHR;
  43.  
  44. private void Start()
  45. {
  46. bls = new BaseLineCalculator();
  47. joystickX = 0f;
  48. //baseline represents an initial Beats pr. 300 iteration
  49. baseTier = 0.5f;
  50. tier0 = 0;
  51. tier1 = 3f;
  52. tier2 = 2;
  53. tier3 = 1;
  54. //tier2 = 0.5f;
  55. //tier3 = 0.25f;
  56.  
  57.  
  58. // currentTier = baseTier;
  59. currentTier = tier0;
  60.  
  61.  
  62. if(getHeartData == null)
  63. {
  64. getHeartData = new HeartRateEvent();
  65. }
  66. getHeartData.AddListener(ReceiveData);
  67. counter = 0;
  68. }
  69.  
  70. private void FixedUpdate()
  71. {
  72. //testing purposes
  73. // elevator.transform.Translate((Vector3.up * currentTier) * 0.5f);
  74. }
  75.  
  76. void EvaluateTier(float heartMedian)
  77. {
  78. float previousTier = currentTier;
  79.  
  80. switch (heartMedian)
  81. {
  82. case float i when i > baseLine * 1.3f:
  83. //intermediateRate = tier3;
  84. SetTier(tier3);
  85. print("tier3");
  86.  
  87. break;
  88. case float i when i > baseLine * 1.2f:
  89. //intermediateRate = tier2;
  90. SetTier(tier2);
  91. print("tier2");
  92.  
  93. break;
  94. case float i when i > baseLine * 1.1f:
  95. //intermediateRate = tier1;
  96. SetTier(tier1);
  97. print("tier1");
  98.  
  99. break;
  100. default:
  101. //intermediateRate = tier0;
  102. SetTier(baseTier);
  103. print("basetier");
  104.  
  105. break;
  106. }
  107.  
  108.  
  109. //if the current tier is an increase by 2, set the tier to 0 -- moved to elevator ride
  110. // if(currentTier >= previousTier + 2f)
  111. // {
  112. // currentTier = tier0;
  113. // }
  114.  
  115. print(currentTier + " current tier");
  116. }
  117.  
  118. //evaluates if the current tier is equal or larger than the 2nd to last of the list - if true evaluate tier should set current tier to stopTier
  119. // because this should prove that the users heartrate has jumped a tier within the last 10 seconds - having a bad time!
  120. // bool evaluatePreviousTier(float currentTier)
  121. // {
  122. // //do nothing if there is only the one entry or none
  123. // if (previousTiers.Count <= 1)
  124. // {
  125. // return false;
  126.  
  127. // }
  128. // else
  129. // //return if the current tier estimation is greater than the 2nd to last entry,
  130. // return currentTier >= previousTiers[previousTiers.Count - 2];
  131. // }
  132.  
  133. public void SetTier(float tier)
  134. {
  135. currentTier = tier;
  136. }
  137.  
  138. public float GetCurrentTier()
  139. {
  140. return currentTier;
  141. }
  142.  
  143. public void ReceiveData(string androidMessage)
  144. {
  145. counter++;
  146. float heartRateString = float.Parse(androidMessage);
  147.  
  148. if(counter > 10)
  149. {
  150. EvaluateTier(heartRateString);
  151.  
  152. if(!running)
  153. {
  154. running = true;
  155. baseLine = bls.returnAverageBpm();
  156.  
  157. //show start button
  158. btn1.SetActive(true);
  159. btn2.SetActive(true);
  160.  
  161.  
  162. }
  163.  
  164. } else
  165. {
  166. bls.addValueToBaseLine(heartRateString);
  167. }
  168.  
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement