Merge pull request #1269 from pkendall64/crsf-max-output
[ExpressLRS.git] / src / lib / THERMAL / devThermal.cpp
bloba1a286e53c0667f3622741c751cf561b68c3e3e7
1 #include "devThermal.h"
3 #if defined(HAS_THERMAL) || defined(HAS_FAN)
5 #if defined(TARGET_RX)
6 #error "devThermal not supported on RX"
7 #endif
9 #include "config.h"
10 extern TxConfig config;
12 #define THERMAL_DURATION 1000
14 #if defined(HAS_THERMAL)
15 #include "common.h"
16 #include "thermal.h"
17 extern bool IsArmed();
19 Thermal thermal;
21 #if defined(HAS_SMART_FAN)
22 bool is_smart_fan_control = false;
23 bool is_smart_fan_working = false;
24 #endif
26 #endif // HAS_THERMAL
28 #if defined(HAS_FAN)
29 #include "POWERMGNT.h"
31 #if !defined(FAN_MIN_RUNTIME)
32 #define FAN_MIN_RUNTIME 30U // seconds
33 #endif
35 #endif // HAS_FAN
37 static void initialize()
39 #if defined(HAS_THERMAL)
40 thermal.init();
41 #endif
42 #if defined(HAS_FAN)
43 pinMode(GPIO_PIN_FAN_EN, OUTPUT);
44 #endif
47 static void timeoutThermal()
49 #if defined(HAS_THERMAL)
50 if(!IsArmed() && connectionState != wifiUpdate)
52 thermal.handle();
53 #ifdef HAS_SMART_FAN
54 if(is_smart_fan_control & !is_smart_fan_working){
55 is_smart_fan_working = true;
56 thermal.update_threshold(USER_SMARTFAN_OFF);
58 if(!is_smart_fan_control & is_smart_fan_working){
59 is_smart_fan_working = false;
60 #endif
61 thermal.update_threshold(config.GetFanMode());
62 #ifdef HAS_SMART_FAN
64 #endif
66 #endif // HAS_THERMAL
69 /***
70 * Checks the PowerFanThreshold vs CurrPower and enables the fan if at or above the threshold
71 * using a hysteresis. To turn on it must be at/above the threshold for a small time
72 * and then to turn off it must be below the threshold for FAN_MIN_RUNTIME intervals
73 ***/
74 static void timeoutFan()
76 #if defined(HAS_FAN)
77 static uint8_t fanStateDuration;
78 static bool fanIsOn;
79 bool fanShouldBeOn = POWERMGNT::currPower() >= (PowerLevels_e)config.GetPowerFanThreshold();
81 if (fanIsOn)
83 if (fanShouldBeOn)
85 fanStateDuration = 0; // reset the timeout
87 else if (fanStateDuration < FAN_MIN_RUNTIME)
89 ++fanStateDuration; // counting to turn off
91 else
93 // turn off expired
94 digitalWrite(GPIO_PIN_FAN_EN, LOW);
95 fanStateDuration = 0;
96 fanIsOn = false;
99 // vv else fan is off currently vv
100 else if (fanShouldBeOn)
102 // Delay turning the fan on for 4 cycles to be sure it really should be on
103 if (fanStateDuration < 3)
105 ++fanStateDuration; // counting to turn on
107 else
109 digitalWrite(GPIO_PIN_FAN_EN, HIGH);
110 fanStateDuration = 0;
111 fanIsOn = true;
114 #endif // HAS_FAN
117 static int event()
119 #if defined(HAS_THERMAL)
120 #ifdef HAS_SMART_FAN
121 if(!is_smart_fan_control)
123 #endif
124 thermal.update_threshold(config.GetFanMode());
125 #ifdef HAS_SMART_FAN
127 #endif
128 #endif // HAS_THERMAL
130 return THERMAL_DURATION;
133 static int timeout()
135 timeoutThermal();
136 timeoutFan();
138 return THERMAL_DURATION;
141 device_t Thermal_device = {
142 .initialize = initialize,
143 .start = NULL,
144 .event = event,
145 .timeout = timeout
148 #endif // HAS_THERMAL || HAS_FAN