Silence unused-variable warning (#2872)
[ExpressLRS.git] / src / lib / THERMAL / devThermal.cpp
blobd5834cd6245928d6373b1c10d62d2a3763d3a974
1 #include "targets.h"
2 #include "devThermal.h"
4 #if defined(HAS_THERMAL) || defined(HAS_FAN)
6 #include "config.h"
7 #include "logging.h"
9 #if defined(TARGET_RX)
10 extern RxConfig config;
11 #else
12 extern TxConfig config;
13 #endif
14 #define THERMAL_DURATION 1000
16 #if defined(HAS_THERMAL)
17 #include "common.h"
18 #include "thermal.h"
20 Thermal thermal;
22 #if defined(HAS_SMART_FAN)
23 bool is_smart_fan_control = false;
24 bool is_smart_fan_working = false;
25 #endif
27 #ifdef HAS_THERMAL_LM75A
28 #ifndef OPT_HAS_THERMAL_LM75A
29 #define OPT_HAS_THERMAL_LM75A true
30 #endif
31 #else
32 #define OPT_HAS_THERMAL_LM75A true
33 #endif
35 #endif // HAS_THERMAL
37 #include "POWERMGNT.h"
39 #if defined(GPIO_PIN_FAN_PWM)
40 constexpr uint8_t fanChannel = 0;
41 #endif
43 #if !defined(FAN_MIN_RUNTIME)
44 #define FAN_MIN_RUNTIME 30U // intervals (seconds)
45 #endif
47 #define FAN_MIN_CHANGETIME 10U // intervals (seconds)
49 #if !defined(TACHO_PULSES_PER_REV)
50 #define TACHO_PULSES_PER_REV 4
51 #endif
53 static uint16_t currentRPM = 0;
55 void init_rpm_counter(int pin);
56 uint32_t get_rpm();
58 static void initialize()
60 #if defined(HAS_THERMAL)
61 #if defined(PLATFORM_ESP32_S3)
62 thermal.init();
63 #else
64 if (OPT_HAS_THERMAL_LM75A && GPIO_PIN_SCL != UNDEF_PIN && GPIO_PIN_SDA != UNDEF_PIN)
66 thermal.init();
68 #endif
69 #endif
70 if (GPIO_PIN_FAN_EN != UNDEF_PIN)
72 pinMode(GPIO_PIN_FAN_EN, OUTPUT);
76 static void timeoutThermal()
78 #if defined(HAS_THERMAL)
79 #if !defined(PLATFORM_ESP32_S3)
80 if(OPT_HAS_THERMAL_LM75A)
81 #endif
83 thermal.handle();
84 #ifdef HAS_SMART_FAN
85 if(is_smart_fan_control & !is_smart_fan_working){
86 is_smart_fan_working = true;
87 thermal.update_threshold(USER_SMARTFAN_OFF);
89 if(!is_smart_fan_control & is_smart_fan_working){
90 is_smart_fan_working = false;
91 #endif
92 thermal.update_threshold(config.GetFanMode());
93 #ifdef HAS_SMART_FAN
95 #endif
97 #endif
100 #if defined(PLATFORM_ESP32)
101 #ifndef TARGET_RX
102 static void setFanSpeed()
104 const uint8_t defaultFanSpeeds[] = {
105 31, // 10mW
106 47, // 25mW
107 63, // 50mW
108 95, // 100mW
109 127, // 250mW
110 191, // 500mW
111 255, // 1000mW
112 255 // 2000mW
115 uint32_t speed = GPIO_PIN_FAN_SPEEDS == nullptr ? defaultFanSpeeds[POWERMGNT::currPower()] : GPIO_PIN_FAN_SPEEDS[POWERMGNT::currPower()-POWERMGNT::getMinPower()];
116 ledcWrite(fanChannel, speed);
117 DBGLN("Fan speed: %d (power) -> %u (pwm)", POWERMGNT::currPower(), speed);
119 #endif
120 #endif
123 * For enable-only fans:
124 * Checks the PowerFanThreshold vs CurrPower and enables the fan if at or above the threshold
125 * using a hysteresis. To turn on it must be at/above the threshold for a small time
126 * and then to turn off it must be below the threshold for FAN_MIN_RUNTIME intervals.
127 * For PWM fans:
128 * all of the above applies, but rather than just turning the fan on, the speed of the fan
129 * is set according to the power output level.
131 static void timeoutFan()
133 #if defined(HAS_FAN)
134 static uint8_t fanStateDuration;
135 static bool fanIsOn;
136 #if defined(TARGET_RX)
137 bool fanShouldBeOn = true;
138 #else
139 bool fanShouldBeOn = POWERMGNT::currPower() >= (PowerLevels_e)config.GetPowerFanThreshold();
140 #endif
141 if (fanIsOn)
143 if (fanShouldBeOn)
145 #if defined(PLATFORM_ESP32)
146 #ifndef TARGET_RX
147 if (GPIO_PIN_FAN_PWM != UNDEF_PIN)
149 static PowerLevels_e lastPower = MinPower;
150 if (POWERMGNT::currPower() < lastPower && fanStateDuration < FAN_MIN_CHANGETIME)
152 ++fanStateDuration;
154 if (POWERMGNT::currPower() > lastPower || fanStateDuration >= FAN_MIN_CHANGETIME)
156 setFanSpeed();
157 lastPower = POWERMGNT::currPower();
158 fanStateDuration = 0; // reset the timeout
161 else
162 #endif
163 #endif
165 fanStateDuration = 0; // reset the timeout
168 else if (fanStateDuration < firmwareOptions.fan_min_runtime)
170 ++fanStateDuration; // counting to turn off
172 else
174 // turn off expired
175 if (GPIO_PIN_FAN_EN != UNDEF_PIN)
177 digitalWrite(GPIO_PIN_FAN_EN, LOW);
179 #if defined(PLATFORM_ESP32)
180 else if (GPIO_PIN_FAN_PWM != UNDEF_PIN)
182 ledcWrite(fanChannel, 0);
184 #endif
185 fanStateDuration = 0;
186 fanIsOn = false;
189 // vv else fan is off currently vv
190 else if (fanShouldBeOn)
192 // Delay turning the fan on for 4 cycles to be sure it really should be on
193 if (fanStateDuration < 3)
195 ++fanStateDuration; // counting to turn on
197 else
199 if (GPIO_PIN_FAN_EN != UNDEF_PIN)
201 digitalWrite(GPIO_PIN_FAN_EN, HIGH);
202 fanStateDuration = 0;
204 #if defined(PLATFORM_ESP32)
205 else if (GPIO_PIN_FAN_PWM != UNDEF_PIN)
207 // bump the fan to full power for one cycle in case
208 // the PWM level is not sufficient to get it moving
209 ledcWrite(fanChannel, 192);
210 fanStateDuration = FAN_MIN_CHANGETIME;
212 #endif
213 fanIsOn = true;
216 #endif
219 uint16_t getCurrentRPM()
221 return currentRPM;
224 static void timeoutTacho()
226 #if defined(PLATFORM_ESP32)
227 if (GPIO_PIN_FAN_TACHO != UNDEF_PIN)
229 currentRPM = get_rpm();
230 DBGVLN("RPM %d", currentRPM);
232 #endif
235 static int start()
237 #if defined(PLATFORM_ESP32)
238 if (GPIO_PIN_FAN_PWM != UNDEF_PIN)
240 ledcSetup(fanChannel, 25000, 8);
241 ledcAttachPin(GPIO_PIN_FAN_PWM, fanChannel);
242 ledcWrite(fanChannel, 0);
244 if (GPIO_PIN_FAN_TACHO != UNDEF_PIN)
246 init_rpm_counter(GPIO_PIN_FAN_TACHO);
248 #endif
249 return DURATION_IMMEDIATELY;
252 static int event()
254 #if defined(HAS_THERMAL)
255 if (OPT_HAS_THERMAL_LM75A && GPIO_PIN_SCL != UNDEF_PIN && GPIO_PIN_SDA != UNDEF_PIN)
257 #ifdef HAS_SMART_FAN
258 if(!is_smart_fan_control)
260 #endif
261 thermal.update_threshold(config.GetFanMode());
262 #ifdef HAS_SMART_FAN
264 #endif
266 #endif
267 return DURATION_IGNORE;
270 static int timeout()
272 timeoutThermal();
273 timeoutFan();
274 timeoutTacho();
275 return THERMAL_DURATION;
278 device_t Thermal_device = {
279 .initialize = initialize,
280 .start = start,
281 .event = event,
282 .timeout = timeout
285 #endif // HAS_THERMAL || HAS_FAN