1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using System.Threading.Tasks;
- using Controller.Bicycle;
- using UnityEngine;
- namespace SicknessReduction.Haptic
- {
- public class DeskFanController : EspController
- {
- private const int MAX_FAN_VALUE = 95; //in %. Sometimes 100% doesn't work correctly
- 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
- private const string TOPIC = "DeskFan/Control";
- public float maxValueAtSpeed = 6.94f; //25km/h
- public float speedUntilNoFan = 1.94f; //2kmh
- public RbBicycleController bicycleController;
- private int previousFanValue;
- protected override async void Update()
- {
- base.Update();
- if (!DoUpdate || PreviousUpdateActive) return;
- PreviousUpdateActive = true;
- var cycle = FanCycleForSpeed();
- await SendFanValue(cycle);
- PreviousUpdateActive = false;
- }
- private async Task SendFanValue(int value)
- {
- if (previousFanValue != value)
- {
- previousFanValue = value;
- await Broker.Publish(TOPIC, $"{value}");
- }
- }
- private int FanCycleForSpeed()
- {
- var speed = bicycleController.CurrentSpeed;
- if (speed <= speedUntilNoFan) return 0;
- return (int) Mathf.Lerp(MIN_FAN_VALUE, MAX_FAN_VALUE, speed / maxValueAtSpeed);
- }
- }
- }
|