Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using ArduinoBluetoothAPI;
- using System;
- using UnityEngine.Events;
- using System.Text;
- using TMPro;
- //public enum Tier {1,2,3 }
- [Serializable]
- public class HeartRateEvent : UnityEvent<string>
- {
- }
- public class HeartData : MonoBehaviour
- {
- BluetoothHelper bluetoothHelper;
- public float baseLine, currentTier;
- private float tier0, tier1, tier2, tier3,baseTier, adjustingTier, difference, increment, median, joystickX,counter;
- public Vector2 joystickPos;
- private List<float> MedianHeartRateCol = new List<float>();
- private List<float> previousTiers = new List<float>();
- bool pressed, running;
- public AudioSource src;
- public GameObject btn1,btn2;
- //only for testing
- // public GameObject elevator;
- [HideInInspector]
- public HeartRateEvent getHeartData;
- public BaseLineCalculator bls;
- public TMP_Text avgHR;
- private void Start()
- {
- bls = new BaseLineCalculator();
- joystickX = 0f;
- //baseline represents an initial Beats pr. 300 iteration
- baseTier = 0.5f;
- tier0 = 0;
- tier1 = 3f;
- tier2 = 2;
- tier3 = 1;
- //tier2 = 0.5f;
- //tier3 = 0.25f;
- // currentTier = baseTier;
- currentTier = tier0;
- if(getHeartData == null)
- {
- getHeartData = new HeartRateEvent();
- }
- getHeartData.AddListener(ReceiveData);
- counter = 0;
- }
- private void FixedUpdate()
- {
- //testing purposes
- // elevator.transform.Translate((Vector3.up * currentTier) * 0.5f);
- }
- void EvaluateTier(float heartMedian)
- {
- float previousTier = currentTier;
- switch (heartMedian)
- {
- case float i when i > baseLine * 1.3f:
- //intermediateRate = tier3;
- SetTier(tier3);
- print("tier3");
- break;
- case float i when i > baseLine * 1.2f:
- //intermediateRate = tier2;
- SetTier(tier2);
- print("tier2");
- break;
- case float i when i > baseLine * 1.1f:
- //intermediateRate = tier1;
- SetTier(tier1);
- print("tier1");
- break;
- default:
- //intermediateRate = tier0;
- SetTier(baseTier);
- print("basetier");
- break;
- }
- //if the current tier is an increase by 2, set the tier to 0 -- moved to elevator ride
- // if(currentTier >= previousTier + 2f)
- // {
- // currentTier = tier0;
- // }
- print(currentTier + " current tier");
- }
- //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
- // because this should prove that the users heartrate has jumped a tier within the last 10 seconds - having a bad time!
- // bool evaluatePreviousTier(float currentTier)
- // {
- // //do nothing if there is only the one entry or none
- // if (previousTiers.Count <= 1)
- // {
- // return false;
- // }
- // else
- // //return if the current tier estimation is greater than the 2nd to last entry,
- // return currentTier >= previousTiers[previousTiers.Count - 2];
- // }
- public void SetTier(float tier)
- {
- currentTier = tier;
- }
- public float GetCurrentTier()
- {
- return currentTier;
- }
- public void ReceiveData(string androidMessage)
- {
- counter++;
- float heartRateString = float.Parse(androidMessage);
- if(counter > 10)
- {
- EvaluateTier(heartRateString);
- if(!running)
- {
- running = true;
- baseLine = bls.returnAverageBpm();
- //show start button
- btn1.SetActive(true);
- btn2.SetActive(true);
- }
- } else
- {
- bls.addValueToBaseLine(heartRateString);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement