Double MSP (TLM and MAVLink) throughput for Gemini hardware (#3037)
[ExpressLRS.git] / src / lib / THERMAL / thermal.cpp
blob9b9a8fbbd55718a98a46389f37f72c70e4c5821a
1 #include "thermal.h"
2 #include "logging.h"
4 #if defined(PLATFORM_ESP32)
5 #include "lm75a.h"
6 LM75A lm75a;
7 #if defined(PLATFORM_ESP32_S3) || defined(PLATFORM_ESP32_C3)
8 #include "driver/temp_sensor.h"
9 #endif
11 static const uint8_t thermal_threshold_data[] = {
12 THERMAL_FAN_DEFAULT_HIGH_THRESHOLD,
13 THERMAL_FAN_DEFAULT_LOW_THRESHOLD,
14 THERMAL_FAN_ALWAYS_ON_HIGH_THRESHOLD,
15 THERMAL_FAN_ALWAYS_ON_LOW_THRESHOLD,
16 THERMAL_FAN_OFF_HIGH_THRESHOLD,
17 THERMAL_FAN_OFF_LOW_THRESHOLD
20 static Thermal_Status_t thermal_status = THERMAL_STATUS_FAIL;
22 void Thermal::init()
24 int status = -1;
25 if (OPT_HAS_THERMAL_LM75A)
27 status = lm75a.init();
29 #if defined(PLATFORM_ESP32_S3) || defined(PLATFORM_ESP32_C3)
30 if (status == -1)
32 temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
33 temp_sensor.dac_offset = TSENS_DAC_L2; //TSENS_DAC_L2 is default L4(-40℃ ~ 20℃), L2(-10℃ ~ 80℃) L1(20℃ ~ 100℃) L0(50℃ ~ 125℃)
34 temp_sensor_set_config(temp_sensor);
35 temp_sensor_start();
37 #else
38 if (status == -1)
40 ERRLN("Thermal failed!");
41 return;
43 #endif
44 DBGLN("Thermal OK!");
45 temp_value = 0;
46 thermal_status = THERMAL_STATUS_NORMAL;
47 update_threshold(0);
50 void Thermal::handle()
52 temp_value = read_temp();
55 uint8_t Thermal::read_temp()
57 if(thermal_status != THERMAL_STATUS_NORMAL)
59 ERRLN("thermal not ready!");
60 return 0;
62 if (OPT_HAS_THERMAL_LM75A)
64 return lm75a.read_lm75a();
67 #if defined(PLATFORM_ESP32_S3) || defined(PLATFORM_ESP32_C3)
68 float result = 0;
69 temp_sensor_read_celsius(&result);
70 return static_cast<int>(result);
71 #else
72 return 0;
73 #endif
76 void Thermal::update_threshold(int index)
78 static int prevIndex = -1;
79 if (index == prevIndex)
81 return;
83 prevIndex = index;
84 if(thermal_status != THERMAL_STATUS_NORMAL)
86 ERRLN("thermal not ready!");
87 return;
89 constexpr int size = sizeof(thermal_threshold_data)/sizeof(thermal_threshold_data[0]);
90 if(index > size/2)
92 ERRLN("thermal index out of range!");
93 return;
95 if (OPT_HAS_THERMAL_LM75A)
97 const uint8_t high = thermal_threshold_data[2*index];
98 const uint8_t low = thermal_threshold_data[2*index+1];
99 lm75a.update_lm75a_threshold(high, low);
102 #endif