2
0

DeskFanController.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Threading.Tasks;
  3. using Controller.Bicycle;
  4. using UnityEngine;
  5. namespace SicknessReduction.Haptic
  6. {
  7. public class DeskFanController : EspController
  8. {
  9. private const int MAX_FAN_VALUE = 95; //in %. Sometimes 100% doesn't work correctly
  10. private const int MIN_FAN_VALUE = 25; //everything below 35 will not be enough to let the fan spin; a bit of buffer for not starting to high
  11. private const string TOPIC = "DeskFan/Control";
  12. public float maxValueAtSpeed = 6.94f; //25km/h
  13. public float speedUntilNoFan = 1.94f; //2kmh
  14. public RbBicycleController bicycleController;
  15. private int previousFanValue;
  16. protected override async void Update()
  17. {
  18. base.Update();
  19. if (!DoUpdate || PreviousUpdateActive) return;
  20. PreviousUpdateActive = true;
  21. var cycle = FanCycleForSpeed();
  22. await SendFanValue(cycle);
  23. PreviousUpdateActive = false;
  24. }
  25. private async Task SendFanValue(int value)
  26. {
  27. if (previousFanValue != value)
  28. {
  29. previousFanValue = value;
  30. await Broker.Publish(TOPIC, $"{value}");
  31. }
  32. }
  33. private int FanCycleForSpeed()
  34. {
  35. var speed = bicycleController.CurrentSpeed;
  36. if (speed <= speedUntilNoFan) return 0;
  37. return (int) Mathf.Lerp(MIN_FAN_VALUE, MAX_FAN_VALUE, speed / maxValueAtSpeed);
  38. }
  39. }
  40. }